IBR-DTN  1.0.0
NativeDaemon.h
Go to the documentation of this file.
1 /*
2  * NativeDaemon.h
3  *
4  * Copyright (C) 2013 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 #include "Component.h"
23 #include "core/AbstractWorker.h"
24 #include "net/IPNDAgent.h"
25 #include "storage/BundleSeeker.h"
26 #include "storage/BundleIndex.h"
27 #include "core/EventReceiver.h"
28 
29 #include "core/NodeEvent.h"
30 #include "core/GlobalEvent.h"
31 #include "core/CustodyEvent.h"
35 #include "net/ConnectionEvent.h"
37 
38 #include <ibrdtn/ibrdtn.h>
39 #ifdef IBRDTN_SUPPORT_BSP
41 #endif
42 
43 #include <ibrcommon/Exceptions.h>
44 #include <list>
45 #include <set>
46 #include <map>
47 
48 #ifndef NATIVEDAEMON_H_
49 #define NATIVEDAEMON_H_
50 
51 namespace dtn
52 {
53  namespace daemon
54  {
63  };
64 
66  public:
67 
68  virtual ~NativeDaemonCallback() = 0;
69 
70  virtual void levelChanged(DaemonRunLevel level) throw () = 0;
71  };
72 
74  public:
75  virtual ~NativeEventCallback() = 0;
76 
77  virtual void eventRaised(const std::string &event, const std::string &action, const std::vector<std::string> &data) throw () = 0;
78  };
79 
80  class NativeNode {
81  public:
82  enum Type {
88  };
89 
90  NativeNode(const std::string &e)
91  : eid(e), type(NODE_STATIC) { };
92 
94 
95  std::string eid;
97  };
98 
99  class NativeKeyInfo {
100  public:
101  std::string endpoint;
102  std::string fingerprint;
103  std::string data;
105  unsigned int flags;
106  };
107 
108  class NativeStats {
109  public:
111  : uptime(0), timestamp(0), neighbors(0), storage_size(0),
116  { };
117 
119 
120  size_t uptime;
121  size_t timestamp;
122  size_t neighbors;
123  size_t storage_size;
124 
125  double time_offset;
126  double time_rating;
128 
136 
137  const std::vector<std::string>& getTags() {
138  return _tags;
139  }
140 
141  const std::string& getData(int index) {
142  return _data[index];
143  }
144 
145  void addData(const std::string &tag, const std::string &data) {
146  _tags.push_back(tag);
147  _data.push_back(data);
148  }
149 
150  private:
151  std::vector<std::string> _tags;
152  std::vector<std::string> _data;
153  };
154 
155  class NativeDaemonException : public ibrcommon::Exception
156  {
157  public:
158  NativeDaemonException(std::string what = "An error happened.") throw() : ibrcommon::Exception(what)
159  {
160  };
161  };
162 
163  class NativeEventLoop;
164 
165  class NativeDaemon :
166  public dtn::core::EventReceiver<dtn::core::NodeEvent>,
167  public dtn::core::EventReceiver<dtn::core::GlobalEvent>,
168  public dtn::core::EventReceiver<dtn::core::CustodyEvent>,
169  public dtn::core::EventReceiver<dtn::net::BundleReceivedEvent>,
170  public dtn::core::EventReceiver<dtn::net::TransferAbortedEvent>,
171  public dtn::core::EventReceiver<dtn::net::TransferCompletedEvent>,
172  public dtn::core::EventReceiver<dtn::net::ConnectionEvent>,
173  public dtn::core::EventReceiver<dtn::routing::QueueBundleEvent>
174 #ifdef IBRDTN_SUPPORT_BSP
175  , public dtn::core::EventReceiver<dtn::security::KeyExchangeEvent>
176 #endif
177  {
178  static const std::string TAG;
179 
180  public:
184  NativeDaemon(NativeDaemonCallback *statecb = NULL, NativeEventCallback *eventcb = NULL);
185 
189  virtual ~NativeDaemon();
190 
194  DaemonRunLevel getRunLevel() const throw ();
195 
199  void init(DaemonRunLevel rl) throw (NativeDaemonException);
200 
204  void wait(DaemonRunLevel rl) throw ();
205 
209  void reload() throw ();
210 
214  void setDebug(int level) throw ();
215 
219  void setLogging(const std::string &defaultTag, int logLevel) const throw ();
220 
224  void setLogFile(const std::string &path, int logLevel) const throw ();
225 
229  void setConfigFile(const std::string &config_file);
230 
236  std::string getLocalUri() const throw ();
237 
241  std::vector<std::string> getNeighbors() const throw ();
242 
246  NativeNode getInfo(const std::string &neighbor_eid) const throw (NativeDaemonException);
247 
251  NativeStats getStats() throw ();
252 
256  void addConnection(std::string eid, std::string protocol, std::string address, std::string service, bool local = false) const throw ();
257 
261  void removeConnection(std::string eid, std::string protocol, std::string address, std::string service, bool local = false) const throw ();
262 
266  void initiateConnection(std::string eid) const;
267 
271  void setGloballyConnected(bool connected) const;
272 
276  void onKeyExchangeBegin(std::string eid, int protocol, std::string password) const;
277 
281  void onKeyExchangeResponse(std::string eid, int protocol, int session, int step, std::string data) const;
282 
286  NativeKeyInfo getKeyInfo(std::string eid) const throw (NativeDaemonException);
287 
291  void removeKey(std::string eid) const throw (NativeDaemonException);
292 
296  virtual void raiseEvent(const dtn::core::NodeEvent &evt) throw ();
297  virtual void raiseEvent(const dtn::core::GlobalEvent &evt) throw ();
298  virtual void raiseEvent(const dtn::core::CustodyEvent &evt) throw ();
299  virtual void raiseEvent(const dtn::net::BundleReceivedEvent &evt) throw ();
300  virtual void raiseEvent(const dtn::net::TransferAbortedEvent &evt) throw ();
301  virtual void raiseEvent(const dtn::net::TransferCompletedEvent &evt) throw ();
302  virtual void raiseEvent(const dtn::net::ConnectionEvent &evt) throw ();
303  virtual void raiseEvent(const dtn::routing::QueueBundleEvent &evt) throw ();
304 
305 #ifdef IBRDTN_SUPPORT_BSP
306  virtual void raiseEvent(const dtn::security::KeyExchangeEvent &evt) throw ();
307 #endif
308 
312  std::vector<std::string> getVersion() const throw ();
313 
317  void clearStorage() const throw ();
318 
322  void startDiscovery() const throw ();
323 
327  void stopDiscovery() const throw ();
328 
332  void setLeMode(bool low_energy) const throw ();
333 
334  private:
335  void init_up(DaemonRunLevel rl) throw (NativeDaemonException);
336  void init_down(DaemonRunLevel rl) throw (NativeDaemonException);
337 
338  void addEventData(const dtn::data::Bundle &b, std::vector<std::string> &data) const;
339  void addEventData(const dtn::data::MetaBundle &b, std::vector<std::string> &data) const;
340  void addEventData(const dtn::data::BundleID &b, std::vector<std::string> &data) const;
341 
342  void bindEvents();
343  void unbindEvents();
344 
348  void init_core() throw (NativeDaemonException);
349  void shutdown_core() throw (NativeDaemonException);
350 
354  void init_storage() throw (NativeDaemonException);
355  void shutdown_storage() const throw (NativeDaemonException);
356 
360  void init_routing() throw (NativeDaemonException);
361  void shutdown_routing() const throw (NativeDaemonException);
362 
367  void init_api() throw (NativeDaemonException);
368  void shutdown_api() throw (NativeDaemonException);
369 
373  void init_network() throw (NativeDaemonException);
374  void shutdown_network() throw (NativeDaemonException);
375 
379  void init_routing_extensions() throw (NativeDaemonException);
380  void shutdown_routing_extensions() const throw (NativeDaemonException);
381 
382  // conditional
383  ibrcommon::Conditional _runlevel_cond;
384  DaemonRunLevel _runlevel;
385 
386  NativeDaemonCallback *_statecb;
387  NativeEventCallback *_eventcb;
388 
389  // list of components
390  typedef std::list< dtn::daemon::Component* > component_list;
391  typedef std::map<DaemonRunLevel, component_list > component_map;
392  component_map _components;
393 
394  // list of embedded apps
395  typedef std::list< dtn::core::AbstractWorker* > app_list;
396  app_list _apps;
397 
398  NativeEventLoop *_event_loop;
399 
400  ibrcommon::File _config_file;
401  };
402 
403  class NativeEventLoop : public ibrcommon::JoinableThread {
404  public:
405  NativeEventLoop(NativeDaemon &daemon);
407  virtual void run(void) throw ();
408  virtual void __cancellation() throw ();
409 
410  private:
411  NativeDaemon &_daemon;
412  };
413  } /* namespace daemon */
414 } /* namespace dtn */
415 #endif /* NATIVEDAEMON_H_ */
void initiateConnection(std::string eid) const
NativeEventLoop(NativeDaemon &daemon)
void onKeyExchangeBegin(std::string eid, int protocol, std::string password) const
NativeNode(const std::string &e)
Definition: NativeDaemon.h:90
void wait(DaemonRunLevel rl)
void setLogging(const std::string &defaultTag, int logLevel) const
std::vector< std::string > getVersion() const
void setConfigFile(const std::string &config_file)
const std::string & getData(int index)
Definition: NativeDaemon.h:141
void removeConnection(std::string eid, std::string protocol, std::string address, std::string service, bool local=false) const
void addData(const std::string &tag, const std::string &data)
Definition: NativeDaemon.h:145
void addConnection(std::string eid, std::string protocol, std::string address, std::string service, bool local=false) const
DaemonRunLevel getRunLevel() const
void removeKey(std::string eid) const
NativeNode getInfo(const std::string &neighbor_eid) const
virtual void raiseEvent(const dtn::core::NodeEvent &evt)
virtual void levelChanged(DaemonRunLevel level)=0
NativeDaemon(NativeDaemonCallback *statecb=NULL, NativeEventCallback *eventcb=NULL)
std::string getLocalUri() const
void onKeyExchangeResponse(std::string eid, int protocol, int session, int step, std::string data) const
void setLogFile(const std::string &path, int logLevel) const
void setGloballyConnected(bool connected) const
virtual void eventRaised(const std::string &event, const std::string &action, const std::vector< std::string > &data)=0
NativeDaemonException(std::string what="An error happened.")
Definition: NativeDaemon.h:158
const std::vector< std::string > & getTags()
Definition: NativeDaemon.h:137
void init(DaemonRunLevel rl)
void setLeMode(bool low_energy) const
std::vector< std::string > getNeighbors() const
NativeKeyInfo getKeyInfo(std::string eid) const