IBR-DTNSuite  0.12
DTNTPWorker.h
Go to the documentation of this file.
1 /*
2  * DTNTPWorker.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 DTNTPWORKER_H_
23 #define DTNTPWORKER_H_
24 
25 #include "core/AbstractWorker.h"
26 #include "core/EventReceiver.h"
28 #include "Configuration.h"
29 #include <time.h>
30 
31 namespace dtn
32 {
33  namespace daemon
34  {
36  {
37  public:
41  DTNTPWorker();
42 
46  virtual ~DTNTPWorker();
47 
52  void callbackBundleReceived(const Bundle &b);
53 
58  void raiseEvent(const dtn::core::Event *evt) throw ();
59 
66  void onUpdateBeacon(const ibrcommon::vinterface &iface, DiscoveryBeacon &announcement)
68 
76  {
77  public:
78  enum MSG_TYPE
79  {
82  };
83 
86 
88 
90  double origin_rating;
91 
92  timeval peer_timestamp;
93  double peer_rating;
94 
95  friend std::ostream &operator<<(std::ostream &stream, const DTNTPWorker::TimeSyncMessage &obj);
96  friend std::istream &operator>>(std::istream &stream, DTNTPWorker::TimeSyncMessage &obj);
97  };
98 
99  class TimeSyncState {
100  public:
101  TimeSyncState();
102  virtual ~TimeSyncState();
103 
104  // sync threshold
106 
107  // the base rating used to determine the current clock rating
108  double base_rating;
109 
110  // the local rating is at least decremented by this value between each synchronization
111  double psi;
112 
113  // current value for sigma
114  double sigma;
115 
116  // timestamp of the last synchronization with another (better) clock
117  struct timespec last_sync_time;
118 
119  // defines if the last_sync_time is set
121 
125  static double toDouble(const timespec &val);
126  };
127 
131  static const TimeSyncState& getState();
132 
133  private:
134  class SyncPeer {
135  public:
136  SyncPeer();
137  virtual ~SyncPeer();
138 
139  enum State {
140  STATE_IDLE = 0,
141  STATE_PREPARE = 1,
142  STATE_REQUEST = 2,
143  STATE_SYNC = 3
144  };
145 
149  void touch();
150 
154  bool isExpired() const;
155 
156  State state;
157  struct timespec request_monotonic_time;
158  timeval request_timestamp;
159 
160  private:
161  dtn::data::Timestamp _touched;
162  };
163 
164  static const unsigned int PROTO_VERSION;
165  static const std::string TAG;
166 
171  bool shouldSyncWith(const dtn::core::Node &node) const;
172 
177  void syncWith(const dtn::core::Node &node);
178 
183  bool hasReference() const;
184 
192  void decode(const dtn::core::Node::Attribute &attr, unsigned int &version, dtn::data::Timestamp &timestamp, float &quality) const;
193 
199  void sync(const TimeSyncMessage &msg, const struct timeval &tv, const struct timeval &local, const struct timeval &remote);
200 
204  static TimeSyncState _sync_state;
205 
206  // send discovery announcements with the local clock rating
207  bool _announce_rating;
208 
209  // synchronize with other nodes
210  bool _sync;
211 
212  // Mutex to lock the synchronization process
213  ibrcommon::Mutex _sync_lock;
214 
215  // manage a list of recently sync'd nodes
216  ibrcommon::Mutex _peer_lock;
217  typedef std::map<EID, SyncPeer> peer_map;
218  peer_map _peers;
219  };
220  }
221 }
222 
223 #endif /* DTNTPWORKER_H_ */