IBR-DTNSuite  0.10
TCPConnection.h
Go to the documentation of this file.
1 /*
2  * TCPConnection.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef TCPCONNECTION_H_
23 #define TCPCONNECTION_H_
24 
25 #include "core/NodeEvent.h"
26 
27 #include <ibrdtn/data/Bundle.h>
28 #include <ibrdtn/data/EID.h>
29 #include <ibrdtn/data/MetaBundle.h>
30 #include <ibrdtn/data/Serializer.h>
31 
34 
35 #include <ibrcommon/net/socket.h>
37 #include <ibrcommon/thread/Queue.h>
39 
40 #include <memory>
41 
42 namespace dtn
43 {
44  namespace net
45  {
46  class TCPConvergenceLayer;
47 
49  {
50  const static std::string TAG;
51  public:
60  TCPConnection(TCPConvergenceLayer &tcpsrv, const dtn::core::Node &node, ibrcommon::clientsocket *sock, const size_t timeout = 10);
61 
66  virtual ~TCPConnection();
67 
71  virtual void initialize() throw ();
72 
76  void shutdown() throw ();
77 
83 
88  const dtn::core::Node& getNode() const;
89 
94  virtual void eventTimeout() throw ();
95  virtual void eventError() throw ();
96  virtual void eventConnectionUp(const dtn::streams::StreamContactHeader &header) throw ();
97  virtual void eventConnectionDown() throw ();
98 
99  virtual void eventBundleRefused() throw ();
100  virtual void eventBundleForwarded() throw ();
101  virtual void eventBundleAck(const dtn::data::Length &ack) throw ();
102 
104 
109  void queue(const dtn::net::BundleTransfer &job);
110 
111  bool match(const dtn::core::Node &n) const;
112  bool match(const dtn::data::EID &destination) const;
113  bool match(const dtn::core::NodeEvent &evt) const;
114 
115 #ifdef WITH_TLS
116 
119  void enableTLS();
120 #endif
121 
122  size_t getTrafficStats(int index);
123  void resetTrafficStats();
124 
125  protected:
126  void rejectTransmission();
127 
128  void setup() throw ();
129  void connect();
130  void run() throw ();
131  void finally() throw ();
132  void __cancellation() throw ();
133 
134  void clearQueue();
135 
136  void keepalive();
137  bool good() const;
138 
140 
141  private:
142  class KeepaliveSender : public ibrcommon::JoinableThread
143  {
144  public:
145  KeepaliveSender(TCPConnection &connection, size_t &keepalive_timeout);
146  ~KeepaliveSender();
147 
151  void run() throw ();
152 
156  void __cancellation() throw ();
157 
158  private:
160  TCPConnection &_connection;
161  size_t &_keepalive_timeout;
162  };
163 
164  class Sender : public ibrcommon::JoinableThread, public ibrcommon::Queue<dtn::net::BundleTransfer>
165  {
166  public:
167  Sender(TCPConnection &connection);
168  virtual ~Sender();
169 
170  protected:
171  void run() throw ();
172  void finally() throw ();
173  void __cancellation() throw ();
174 
175  private:
176  TCPConnection &_connection;
177  };
178 
179  void __setup_socket(ibrcommon::clientsocket *sock, bool server);
180 
181  // lock object for the procotol stream
183 
188  safe_streamconnection getProtocolStream() throw (ibrcommon::Exception);
189 
191  dtn::core::Node _node;
192 
193  ibrcommon::clientsocket *_socket;
194 
195  ibrcommon::socketstream *_socket_stream;
196 
197  // optional security layer between socketstream and bundle protocol layer
198  std::iostream *_sec_stream;
199 
200  // mutex for the protocol stream
201  ibrcommon::RWMutex _protocol_stream_mutex;
202 
203  // this connection stream implements the bundle protocol
204  dtn::streams::StreamConnection *_protocol_stream;
205 
206  // This thread gets awaiting bundles of the queue
207  // and transmit them to the peer.
208  Sender _sender;
209 
210  // Keepalive sender
211  KeepaliveSender _keepalive_sender;
212 
213  // handshake variables
214  size_t _timeout;
215 
217  dtn::data::Length _lastack;
218  size_t _keepalive_timeout;
219 
220  TCPConvergenceLayer &_callback;
221 
222  /* flags to be used in this nodes StreamContactHeader */
224 
225  /* with this boolean the connection is marked as aborted */
226  bool _aborted;
227  };
228  }
229 }
230 
231 #endif /* TCPCONNECTION_H_ */