IBR-DTNSuite  0.10
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 #include <ibrcommon/Exceptions.h>
29 #include <list>
30 #include <set>
31 #include <map>
32 
33 #ifndef NATIVEDAEMON_H_
34 #define NATIVEDAEMON_H_
35 
36 namespace dtn
37 {
38  namespace daemon
39  {
48  };
49 
51  public:
52 
53  virtual ~NativeDaemonCallback() = 0;
54 
55  virtual void levelChanged(DaemonRunLevel level) throw () = 0;
56  };
57 
59  public:
60  virtual ~NativeEventCallback() = 0;
61 
62  virtual void eventRaised(const std::string &event, const std::string &action, const std::vector<std::string> &data) throw () = 0;
63  };
64 
65  class NativeNode {
66  public:
67  enum Type {
73  };
74 
75  NativeNode(const std::string &e)
76  : eid(e), type(NODE_STATIC) { };
77 
79 
80  std::string eid;
82  };
83 
84  class NativeStats {
85  public:
87  : uptime(0), timestamp(0), neighbors(0), storage_size(0),
92  { };
93 
94  ~NativeStats() { };
95 
96  size_t uptime;
97  size_t timestamp;
98  size_t neighbors;
99  size_t storage_size;
100 
101  double time_offset;
102  double time_rating;
104 
113 
114  const std::vector<std::string>& getTags() {
115  return _tags;
116  }
117 
118  size_t getData(int index) {
119  return _data[index];
120  }
121 
122  void addData(const std::string &tag, size_t data) {
123  _tags.push_back(tag);
124  _data.push_back(data);
125  }
126 
127  private:
128  std::vector<std::string> _tags;
129  std::vector<size_t> _data;
130  };
131 
133  {
134  public:
135  NativeDaemonException(std::string what = "An error happened.") throw() : ibrcommon::Exception(what)
136  {
137  };
138  };
139 
140  class NativeEventLoop;
141 
143  public:
144  static const std::string TAG;
145 
149  NativeDaemon(NativeDaemonCallback *statecb = NULL, NativeEventCallback *eventcb = NULL);
150 
154  virtual ~NativeDaemon();
155 
159  DaemonRunLevel getRunLevel() const throw ();
160 
164  void init(DaemonRunLevel rl) throw (NativeDaemonException);
165 
169  void wait(DaemonRunLevel rl) throw ();
170 
174  void reload() throw ();
175 
179  void setDebug(int level) throw ();
180 
184  void setLogging(const std::string &defaultTag, int logLevel) const throw ();
185 
189  void setLogFile(const std::string &path, int logLevel) const throw ();
190 
194  void setConfigFile(const std::string &config_file);
195 
201  std::string getLocalUri() const throw ();
202 
206  std::vector<std::string> getNeighbors() const throw ();
207 
211  NativeNode getInfo(const std::string &neighbor_eid) const throw (NativeDaemonException);
212 
216  NativeStats getStats() throw ();
217 
221  void addConnection(std::string eid, std::string protocol, std::string address, std::string service, bool local = false) const throw ();
222 
226  void removeConnection(std::string eid, std::string protocol, std::string address, std::string service, bool local = false) const throw ();
227 
231  void initiateConnection(std::string eid);
232 
236  virtual void raiseEvent(const Event *evt) throw ();
237 
241  std::vector<std::string> getVersion() const throw ();
242 
246  void clearStorage() const throw ();
247 
248  private:
249  void init_up(DaemonRunLevel rl) throw (NativeDaemonException);
250  void init_down(DaemonRunLevel rl) throw (NativeDaemonException);
251 
252  void addEventData(const dtn::data::Bundle &b, std::vector<std::string> &data) const;
253  void addEventData(const dtn::data::MetaBundle &b, std::vector<std::string> &data) const;
254  void addEventData(const dtn::data::BundleID &b, std::vector<std::string> &data) const;
255 
256  void bindEvents();
257  void unbindEvents();
258 
262  void init_core() throw (NativeDaemonException);
263  void shutdown_core() throw (NativeDaemonException);
264 
268  void init_storage() throw (NativeDaemonException);
269  void shutdown_storage() const throw (NativeDaemonException);
270 
274  void init_routing() throw (NativeDaemonException);
275  void shutdown_routing() const throw (NativeDaemonException);
276 
281  void init_api() throw (NativeDaemonException);
282  void shutdown_api() throw (NativeDaemonException);
283 
287  void init_network() throw (NativeDaemonException);
288  void shutdown_network() throw (NativeDaemonException);
289 
293  void init_routing_extensions() throw (NativeDaemonException);
294  void shutdown_routing_extensions() const throw (NativeDaemonException);
295 
296  // conditional
297  ibrcommon::Conditional _runlevel_cond;
298  DaemonRunLevel _runlevel;
299 
300  NativeDaemonCallback *_statecb;
301  NativeEventCallback *_eventcb;
302 
303  // list of components
304  typedef std::list< dtn::daemon::Component* > component_list;
305  typedef std::map<DaemonRunLevel, component_list > component_map;
306  component_map _components;
307 
308  // list of embedded apps
309  typedef std::list< dtn::core::AbstractWorker* > app_list;
310  app_list _apps;
311 
312  NativeEventLoop *_event_loop;
313 
314  ibrcommon::File _config_file;
315  };
316 
317  class NativeEventLoop : public ibrcommon::JoinableThread {
318  public:
319  NativeEventLoop(NativeDaemon &daemon);
320  ~NativeEventLoop();
321  virtual void run(void) throw ();
322  virtual void __cancellation() throw ();
323 
324  private:
325  NativeDaemon &_daemon;
326  };
327  } /* namespace daemon */
328 } /* namespace dtn */
329 #endif /* NATIVEDAEMON_H_ */