IBR-DTNSuite  0.8
daemon/src/net/DatagramConvergenceLayer.h
Go to the documentation of this file.
00001 /*
00002  * DatagramConvergenceLayer.h
00003  *
00004  *  Created on: 21.11.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef DATAGRAMCONVERGENCELAYER_H_
00009 #define DATAGRAMCONVERGENCELAYER_H_
00010 
00011 #include "Component.h"
00012 #include "net/DatagramConnection.h"
00013 #include "net/ConvergenceLayer.h"
00014 #include "net/DiscoveryAgent.h"
00015 #include "net/DiscoveryServiceProvider.h"
00016 #include "net/DatagramConnectionParameter.h"
00017 #include "core/Node.h"
00018 #include <ibrcommon/net/vinterface.h>
00019 
00020 #include <list>
00021 
00022 namespace dtn
00023 {
00024         namespace net
00025         {
00026                 class DatagramService
00027                 {
00028                 public:
00029                         enum FLOWCONTROL
00030                         {
00031                                 FLOW_NONE = 0,
00032                                 FLOW_STOPNWAIT = 1
00033                         };
00034 
00035                         virtual ~DatagramService() {};
00036 
00041                         virtual void bind() throw (DatagramException) = 0;
00042 
00046                         virtual void shutdown() = 0;
00047 
00055                         virtual void send(const char &type, const char &flags, const unsigned int &seqno, const std::string &address, const char *buf, size_t length) throw (DatagramException) = 0;
00056 
00063                         virtual void send(const char &type, const char &flags, const unsigned int &seqno, const char *buf, size_t length) throw (DatagramException) = 0;
00064 
00073                         virtual size_t recvfrom(char *buf, size_t length, char &type, char &flags, unsigned int &seqno, std::string &address) throw (DatagramException) = 0;
00074 
00079                         virtual const std::string getServiceTag() const = 0;
00080 
00086                         virtual const std::string getServiceDescription() const = 0;
00087 
00092                         virtual const ibrcommon::vinterface& getInterface() const = 0;
00093 
00098                         virtual dtn::core::Node::Protocol getProtocol() const = 0;
00099 
00104                         virtual const DatagramConnectionParameter& getParameter() const = 0;
00105                 };
00106 
00107                 class DatagramConvergenceLayer : public ConvergenceLayer, public dtn::daemon::IndependentComponent,
00108                         public EventReceiver, public DatagramConnectionCallback
00109                 {
00110                 public:
00111                         enum HEADER_FLAGS
00112                         {
00113                                 HEADER_UNKOWN = 0,
00114                                 HEADER_BROADCAST = 1,
00115                                 HEADER_SEGMENT = 2,
00116                                 HEADER_ACK = 4
00117                         };
00118 
00119                         DatagramConvergenceLayer(DatagramService *ds);
00120                         virtual ~DatagramConvergenceLayer();
00121 
00126                         dtn::core::Node::Protocol getDiscoveryProtocol() const;
00127 
00134                         void queue(const dtn::core::Node &n, const ConvergenceLayer::Job &job);
00135 
00139                         virtual const std::string getName() const;
00140 
00145                         virtual void raiseEvent(const dtn::core::Event *evt);
00146 
00147                 protected:
00148                         virtual void componentUp();
00149                         virtual void componentRun();
00150                         virtual void componentDown();
00151                         void __cancellation();
00152 
00153                         void sendAnnoucement();
00154 
00162                         void callback_send(DatagramConnection &connection, const char &flags, const unsigned int &seqno, const std::string &destination, const char *buf, int len) throw (DatagramException);
00163 
00164                         void callback_ack(DatagramConnection &connection, const unsigned int &seqno, const std::string &destination) throw (DatagramException);
00165 
00166                         void connectionUp(const DatagramConnection *conn);
00167                         void connectionDown(const DatagramConnection *conn);
00168 
00169                 private:
00170                         DatagramConnection& getConnection(const std::string &identifier);
00171 
00172                         DatagramService *_service;
00173 
00174                         ibrcommon::Mutex _send_lock;
00175 
00176                         ibrcommon::Conditional _connection_cond;
00177                         std::list<DatagramConnection*> _connections;
00178 
00179                         bool _running;
00180 
00181                         u_int16_t _discovery_sn;
00182                 };
00183         } /* namespace data */
00184 } /* namespace dtn */
00185 #endif /* DATAGRAMCONVERGENCELAYER_H_ */