IBR-DTNSuite  0.12
DatagramConvergenceLayer.h
Go to the documentation of this file.
1 /*
2  * DatagramConvergenceLayer.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 DATAGRAMCONVERGENCELAYER_H_
23 #define DATAGRAMCONVERGENCELAYER_H_
24 
25 #include "Component.h"
27 #include "net/DatagramService.h"
28 #include "net/DatagramConnection.h"
29 #include "net/ConvergenceLayer.h"
30 #include "core/EventReceiver.h"
31 
32 #include <list>
33 
34 namespace dtn
35 {
36  namespace net
37  {
40  {
41  static const std::string TAG;
42 
43  public:
45  {
51  };
52 
54  virtual ~DatagramConvergenceLayer();
55 
59  void raiseEvent(const dtn::core::Event *evt) throw ();
60 
66 
73  void queue(const dtn::core::Node &n, const dtn::net::BundleTransfer &job);
74 
78  virtual const std::string getName() const;
79 
80  virtual void resetStats();
81 
82  virtual void getStats(ConvergenceLayer::stats_data &data) const;
83 
84  void onAdvertiseBeacon(const ibrcommon::vinterface &iface, const DiscoveryBeacon &beacon) throw ();
85 
86  protected:
87  virtual void componentUp() throw ();
88  virtual void componentRun() throw ();
89  virtual void componentDown() throw ();
90  void __cancellation() throw ();
98  void callback_send(DatagramConnection &connection, const char &flags, const unsigned int &seqno, const std::string &destination, const char *buf, const dtn::data::Length &len) throw (DatagramException);
99 
100  void callback_ack(DatagramConnection &connection, const unsigned int &seqno, const std::string &destination) throw (DatagramException);
101 
102  void callback_nack(DatagramConnection &connection, const unsigned int &seqno, const std::string &destination) throw (DatagramException);
103 
104  void connectionUp(const DatagramConnection *conn);
105  void connectionDown(const DatagramConnection *conn);
106 
107  void reportSuccess(size_t retries, double rtt);
108  void reportFailure();
109 
110  void receive() throw ();
111 
112  private:
113  class ConnectionNotAvailableException : public ibrcommon::Exception {
114  public:
115  ConnectionNotAvailableException(const std::string what = "connection not available")
116  : ibrcommon::Exception(what) {
117  }
118 
119  virtual ~ConnectionNotAvailableException() throw () {
120  }
121  };
122 
127  class Receiver : public ibrcommon::JoinableThread {
128  public:
129  Receiver(DatagramConvergenceLayer &cl);
130  virtual ~Receiver();
131 
132  void init() throw ();
133 
134  void run() throw ();
135  void __cancellation() throw ();
136 
137  private:
139  };
140 
141  class Action {
142  public:
143  Action() {};
144  virtual ~Action() {};
145  };
146 
147  class SegmentReceived : public Action {
148  public:
149  SegmentReceived(size_t maxlen) : seqno(0), flags(0), data(maxlen), len(0) {};
150  virtual ~SegmentReceived() {};
151 
152  std::string address;
153  unsigned int seqno;
154  char flags;
155  std::vector<char> data;
156  size_t len;
157  };
158 
159  class BeaconReceived : public Action {
160  public:
161  BeaconReceived() {};
162  virtual ~BeaconReceived() {};
163 
164  std::string address;
165  DiscoveryBeacon data;
166  };
167 
168  class AckReceived : public Action {
169  public:
170  AckReceived() : seqno(0) {};
171  virtual ~AckReceived() {};
172 
173  std::string address;
174  unsigned int seqno;
175  };
176 
177  class NackReceived : public Action {
178  public:
179  NackReceived() : seqno(0), temporary(false) {};
180  virtual ~NackReceived() {};
181 
182  std::string address;
183  unsigned int seqno;
184  bool temporary;
185  };
186 
187  class QueueBundle : public Action {
188  public:
189  QueueBundle(const BundleTransfer &bt) : job(bt) {};
190  virtual ~QueueBundle() {};
191 
192  BundleTransfer job;
193  std::string uri;
194  };
195 
196  class ConnectionDown : public Action {
197  public:
198  ConnectionDown() {};
199  virtual ~ConnectionDown() {};
200 
201  std::string id;
202  };
203 
204  class NodeGone : public Action {
205  public:
206  NodeGone() {};
207  virtual ~NodeGone() {};
208 
209  dtn::data::EID eid;
210  };
211 
212  class Shutdown : public Action {
213  public:
214  Shutdown() {};
215  virtual ~Shutdown() {};
216  };
217 
225  DatagramConnection& getConnection(const std::string &identifier, bool create) throw (ConnectionNotAvailableException);
226 
227  // associated datagram service
228  DatagramService *_service;
229 
230  // this thread receives data from the datagram service
231  // and generates actions to process
232  Receiver _receiver;
233 
234  // actions are queued here until they get processed
235  ibrcommon::Queue<Action*> _action_queue;
236 
237  // on any send operation this mutex should be locked
238  ibrcommon::Mutex _send_lock;
239 
240  // conditional to protect _active_conns
241  ibrcommon::Conditional _cond_connections;
242 
243  typedef std::list<DatagramConnection*> connection_list;
244  connection_list _connections;
245 
246  // false, if the main thread is cancelled
247  bool _running;
248 
249  // stats variables
250  size_t _stats_in;
251  size_t _stats_out;
252  double _stats_rtt;
253  size_t _stats_retries;
254  size_t _stats_failure;
255  };
256  } /* namespace data */
257 } /* namespace dtn */
258 #endif /* DATAGRAMCONVERGENCELAYER_H_ */