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