IBR-DTN  1.0.0
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"
27 #include "core/TimeEvent.h"
29 #include "Configuration.h"
30 #include <time.h>
31 
32 namespace dtn
33 {
34  namespace daemon
35  {
37  {
38  public:
42  DTNTPWorker();
43 
47  virtual ~DTNTPWorker();
48 
53  void callbackBundleReceived(const Bundle &b);
54 
59  void raiseEvent(const dtn::core::TimeEvent &evt) throw ();
60 
67  void onUpdateBeacon(const ibrcommon::vinterface &iface, DiscoveryBeacon &announcement)
69 
77  {
78  public:
79  enum MSG_TYPE
80  {
83  };
84 
87 
89 
91  double origin_rating;
92 
93  timeval peer_timestamp;
94  double peer_rating;
95 
96  friend std::ostream &operator<<(std::ostream &stream, const DTNTPWorker::TimeSyncMessage &obj);
97  friend std::istream &operator>>(std::istream &stream, DTNTPWorker::TimeSyncMessage &obj);
98  };
99 
101  public:
102  TimeSyncState();
103  virtual ~TimeSyncState();
104 
105  // sync threshold
107 
108  // the base rating used to determine the current clock rating
109  double base_rating;
110 
111  // the local rating is at least decremented by this value between each synchronization
112  double psi;
113 
114  // current value for sigma
115  double sigma;
116 
117  // timestamp of the last synchronization with another (better) clock
118  struct timespec last_sync_time;
119 
120  // defines if the last_sync_time is set
122 
126  static double toDouble(const timespec &val);
127  };
128 
132  static const TimeSyncState& getState();
133 
134  private:
135  class SyncPeer {
136  public:
137  SyncPeer();
138  virtual ~SyncPeer();
139 
140  enum State {
141  STATE_IDLE = 0,
142  STATE_PREPARE = 1,
143  STATE_REQUEST = 2,
144  STATE_SYNC = 3
145  };
146 
150  void touch();
151 
155  bool isExpired() const;
156 
157  State state;
158  struct timespec request_monotonic_time;
159  timeval request_timestamp;
160 
161  private:
162  dtn::data::Timestamp _touched;
163  };
164 
165  static const unsigned int PROTO_VERSION;
166  static const std::string TAG;
167 
172  float getPeerRating(const dtn::core::Node &node) const;
173 
178  void syncWith(const dtn::data::EID &peer);
179 
184  bool hasReference() const;
185 
193  void decode(const dtn::core::Node::Attribute &attr, unsigned int &version, dtn::data::Timestamp &timestamp, float &quality) const;
194 
200  void sync(const TimeSyncMessage &msg, const struct timeval &tv, const struct timeval &local, const struct timeval &remote);
201 
205  static TimeSyncState _sync_state;
206 
207  // send discovery announcements with the local clock rating
208  bool _announce_rating;
209 
210  // synchronize with other nodes
211  bool _sync;
212 
213  // Mutex to lock the synchronization process
214  ibrcommon::Mutex _sync_lock;
215 
216  // manage a list of recently sync'd nodes
217  ibrcommon::Mutex _peer_lock;
218  typedef std::map<EID, SyncPeer> peer_map;
219  peer_map _peers;
220  };
221  }
222 }
223 
224 #endif /* DTNTPWORKER_H_ */
void onUpdateBeacon(const ibrcommon::vinterface &iface, DiscoveryBeacon &announcement)
friend std::ostream & operator<<(std::ostream &stream, const DTNTPWorker::TimeSyncMessage &obj)
friend std::istream & operator>>(std::istream &stream, DTNTPWorker::TimeSyncMessage &obj)
static double toDouble(const timespec &val)
Definition: DTNTPWorker.cpp:62
void callbackBundleReceived(const Bundle &b)
const std::string TAG
Definition: dtnoutbox.cpp:62
static const TimeSyncState & getState()
void raiseEvent(const dtn::core::TimeEvent &evt)