• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

daemon/src/net/TCPConvergenceLayer.h

Go to the documentation of this file.
00001 /*
00002  * TCPConvergenceLayer.h
00003  *
00004  *  Created on: 05.08.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef TCPCONVERGENCELAYER_H_
00009 #define TCPCONVERGENCELAYER_H_
00010 
00011 #include "Component.h"
00012 
00013 #include "core/EventReceiver.h"
00014 #include "core/NodeEvent.h"
00015 #include "core/Node.h"
00016 
00017 #include "net/ConvergenceLayer.h"
00018 #include "net/DiscoveryService.h"
00019 #include "net/DiscoveryServiceProvider.h"
00020 
00021 #include <ibrdtn/data/Bundle.h>
00022 #include <ibrdtn/data/EID.h>
00023 #include <ibrdtn/data/MetaBundle.h>
00024 #include <ibrdtn/data/Serializer.h>
00025 #include <ibrdtn/streams/StreamConnection.h>
00026 #include <ibrdtn/streams/StreamContactHeader.h>
00027 
00028 #include <ibrcommon/net/tcpserver.h>
00029 #include <ibrcommon/net/tcpstream.h>
00030 #include <ibrcommon/net/vinterface.h>
00031 
00032 #include <memory>
00033 #include <ibrcommon/thread/Queue.h>
00034 
00035 using namespace dtn::streams;
00036 
00037 namespace dtn
00038 {
00039         namespace net
00040         {
00041                 class TCPConvergenceLayer;
00042 
00043                 class TCPConnection : public StreamConnection::Callback, public ibrcommon::DetachedThread
00044                 {
00045                 public:
00054                         TCPConnection(TCPConvergenceLayer &tcpsrv, ibrcommon::tcpstream *stream, const dtn::data::EID &name, const size_t timeout = 10);
00055 
00064                         TCPConnection(TCPConvergenceLayer &tcpsrv, const dtn::core::Node &node, const dtn::data::EID &name, const size_t timeout = 10);
00065 
00070                         virtual ~TCPConnection();
00071 
00075                         virtual void initialize();
00076 
00080                         void shutdown();
00081 
00086                         const StreamContactHeader& getHeader() const;
00087 
00092                         const dtn::core::Node& getNode() const;
00093 
00097                         virtual void eventShutdown(StreamConnection::ConnectionShutdownCases csc);
00098                         virtual void eventTimeout();
00099                         virtual void eventError();
00100                         virtual void eventConnectionUp(const StreamContactHeader &header);
00101                         virtual void eventConnectionDown();
00102 
00103                         virtual void eventBundleRefused();
00104                         virtual void eventBundleForwarded();
00105                         virtual void eventBundleAck(size_t ack);
00106 
00107                         dtn::core::Node::Protocol getDiscoveryProtocol() const;
00108 
00113                         void queue(const dtn::data::BundleID &bundle);
00114 
00115                         bool match(const dtn::core::Node &n) const;
00116                         bool match(const dtn::data::EID &destination) const;
00117                         bool match(const dtn::core::NodeEvent &evt) const;
00118 
00119                         friend TCPConnection& operator>>(TCPConnection &conn, dtn::data::Bundle &bundle);
00120                         friend TCPConnection& operator<<(TCPConnection &conn, const dtn::data::Bundle &bundle);
00121 
00122                 protected:
00123                         void rejectTransmission();
00124 
00125                         void setup();
00126                         void run();
00127                         void finally();
00128 
00129                         void clearQueue();
00130 
00131                         void keepalive();
00132 
00133                 private:
00134                         class Sender : public ibrcommon::JoinableThread, public ibrcommon::Queue<dtn::data::BundleID>
00135                         {
00136                         public:
00137                                 Sender(TCPConnection &connection, size_t &keepalive_timeout);
00138                                 virtual ~Sender();
00139 
00140                         protected:
00141                                 void run();
00142                                 void finally();
00143                                 bool __cancellation();
00144 
00145                         private:
00146                                 TCPConnection &_connection;
00147                                 size_t &_keepalive_timeout;
00148                                 dtn::data::BundleID _current_transfer;
00149                         };
00150 
00151                         StreamContactHeader _peer;
00152                         dtn::core::Node _node;
00153 
00154                         std::auto_ptr<ibrcommon::tcpstream> _tcpstream;
00155                         StreamConnection _stream;
00156 
00157                         // This thread gets awaiting bundles of the queue
00158                         // and transmit them to the peer.
00159                         Sender _sender;
00160 
00161                         // handshake variables
00162                         dtn::data::EID _name;
00163                         size_t _timeout;
00164 
00165                         ibrcommon::Queue<dtn::data::MetaBundle> _sentqueue;
00166                         size_t _lastack;
00167                         size_t _keepalive_timeout;
00168 
00169                         TCPConvergenceLayer &_callback;
00170                 };
00171 
00176                 class TCPConvergenceLayer : public dtn::daemon::IndependentComponent, public ConvergenceLayer, public DiscoveryServiceProvider, public dtn::core::EventReceiver
00177                 {
00178                         friend class TCPConnection;
00179                 public:
00185                         TCPConvergenceLayer();
00186 
00190                         virtual ~TCPConvergenceLayer();
00191 
00197                         void bind(const ibrcommon::vinterface &net, int port);
00198 
00202                         void raiseEvent(const dtn::core::Event *evt);
00203 
00208                         void queue(const dtn::core::Node &n, const ConvergenceLayer::Job &job);
00209 
00215                         void open(const dtn::core::Node &n);
00216 
00220                         virtual const std::string getName() const;
00221 
00226                         dtn::core::Node::Protocol getDiscoveryProtocol() const;
00227 
00231                         void update(const ibrcommon::vinterface &iface, std::string &name, std::string &data) throw(dtn::net::DiscoveryServiceProvider::NoServiceHereException);
00232 
00233                 protected:
00234                         bool __cancellation();
00235 
00236                         void componentUp();
00237                         void componentRun();
00238                         void componentDown();
00239 
00240                 private:
00244                         void closeAll();
00245 
00250                         void connectionUp(TCPConnection *conn);
00251 
00257                         void connectionDown(TCPConnection *conn);
00258 
00259                         static const int DEFAULT_PORT;
00260                         bool _running;
00261 
00262                         ibrcommon::tcpserver _tcpsrv;
00263 
00264                         ibrcommon::Conditional _connections_cond;
00265                         std::list<TCPConnection*> _connections;
00266                         std::list<ibrcommon::vinterface> _interfaces;
00267                         std::map<ibrcommon::vinterface, unsigned int> _portmap;
00268                 };
00269         }
00270 }
00271 
00272 #endif /* TCPCONVERGENCELAYER_H_ */

Generated on Wed Mar 30 2011 11:11:49 for IBR-DTNSuite by  doxygen 1.7.1