IBR-DTNSuite  0.12
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  const std::string& getData(int index) {
119  return _data[index];
120  }
121 
122  void addData(const std::string &tag, const std::string &data) {
123  _tags.push_back(tag);
124  _data.push_back(data);
125  }
126 
127  private:
128  std::vector<std::string> _tags;
129  std::vector<std::string> _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  {
144  static const std::string TAG;
145 
146  public:
150  NativeDaemon(NativeDaemonCallback *statecb = NULL, NativeEventCallback *eventcb = NULL);
151 
155  virtual ~NativeDaemon();
156 
160  DaemonRunLevel getRunLevel() const throw ();
161 
165  void init(DaemonRunLevel rl) throw (NativeDaemonException);
166 
170  void wait(DaemonRunLevel rl) throw ();
171 
175  void reload() throw ();
176 
180  void setDebug(int level) throw ();
181 
185  void setLogging(const std::string &defaultTag, int logLevel) const throw ();
186 
190  void setLogFile(const std::string &path, int logLevel) const throw ();
191 
195  void setConfigFile(const std::string &config_file);
196 
202  std::string getLocalUri() const throw ();
203 
207  std::vector<std::string> getNeighbors() const throw ();
208 
212  NativeNode getInfo(const std::string &neighbor_eid) const throw (NativeDaemonException);
213 
217  NativeStats getStats() throw ();
218 
222  void addConnection(std::string eid, std::string protocol, std::string address, std::string service, bool local = false) const throw ();
223 
227  void removeConnection(std::string eid, std::string protocol, std::string address, std::string service, bool local = false) const throw ();
228 
232  void initiateConnection(std::string eid);
233 
237  virtual void raiseEvent(const dtn::core::Event *evt) throw ();
238 
242  std::vector<std::string> getVersion() const throw ();
243 
247  void clearStorage() const throw ();
248 
252  void startDiscovery() const throw ();
253 
257  void stopDiscovery() const throw ();
258 
259  private:
260  void init_up(DaemonRunLevel rl) throw (NativeDaemonException);
261  void init_down(DaemonRunLevel rl) throw (NativeDaemonException);
262 
263  void addEventData(const dtn::data::Bundle &b, std::vector<std::string> &data) const;
264  void addEventData(const dtn::data::MetaBundle &b, std::vector<std::string> &data) const;
265  void addEventData(const dtn::data::BundleID &b, std::vector<std::string> &data) const;
266 
267  void bindEvents();
268  void unbindEvents();
269 
273  void init_core() throw (NativeDaemonException);
274  void shutdown_core() throw (NativeDaemonException);
275 
279  void init_storage() throw (NativeDaemonException);
280  void shutdown_storage() const throw (NativeDaemonException);
281 
285  void init_routing() throw (NativeDaemonException);
286  void shutdown_routing() const throw (NativeDaemonException);
287 
292  void init_api() throw (NativeDaemonException);
293  void shutdown_api() throw (NativeDaemonException);
294 
298  void init_network() throw (NativeDaemonException);
299  void shutdown_network() throw (NativeDaemonException);
300 
304  void init_routing_extensions() throw (NativeDaemonException);
305  void shutdown_routing_extensions() const throw (NativeDaemonException);
306 
307  // conditional
308  ibrcommon::Conditional _runlevel_cond;
309  DaemonRunLevel _runlevel;
310 
311  NativeDaemonCallback *_statecb;
312  NativeEventCallback *_eventcb;
313 
314  // list of components
315  typedef std::list< dtn::daemon::Component* > component_list;
316  typedef std::map<DaemonRunLevel, component_list > component_map;
317  component_map _components;
318 
319  // list of embedded apps
320  typedef std::list< dtn::core::AbstractWorker* > app_list;
321  app_list _apps;
322 
323  NativeEventLoop *_event_loop;
324 
325  ibrcommon::File _config_file;
326  };
327 
329  public:
330  NativeEventLoop(NativeDaemon &daemon);
332  virtual void run(void) throw ();
333  virtual void __cancellation() throw ();
334 
335  private:
336  NativeDaemon &_daemon;
337  };
338  } /* namespace daemon */
339 } /* namespace dtn */
340 #endif /* NATIVEDAEMON_H_ */