FREEDM DGI
CClockSynchronizer.hpp
Go to the documentation of this file.
1 
23 #ifndef FREEDM_CLOCK_HPP
24 #define FREEDM_CLOCK_HPP
25 
26 #include "IDGIModule.hpp"
27 
28 #include <list>
29 #include <map>
30 
31 #include <boost/asio.hpp>
32 #include <boost/asio/deadline_timer.hpp>
33 #include <boost/noncopyable.hpp>
34 
35 namespace freedm {
36  namespace broker {
37 
38 class CBroker;
39 class CPeerNode;
40 
42  : public IDGIModule
43 {
44 public:
46  explicit CClockSynchronizer(boost::asio::io_service& ios);
48  boost::posix_time::ptime GetSynchronizedTime() const;
50  void Run();
52  void Stop();
54  void HandleIncomingMessage(boost::shared_ptr<const ModuleMessage> msg, CPeerNode peer);
55 
56 private:
58  typedef std::pair<std::string,std::string> MapIndex;
60  typedef std::map< MapIndex, boost::posix_time::time_duration > OffsetMap;
62  typedef std::pair<unsigned int, boost::posix_time::ptime> QueryRecord;
64  typedef std::map< MapIndex, QueryRecord > QueryMap;
66  typedef std::pair<boost::posix_time::ptime, boost::posix_time::ptime> TimeTuple;
68  typedef std::list< TimeTuple > ResponseList;
70  typedef std::map< MapIndex, ResponseList > ResponseMap;
72  typedef std::map< MapIndex, double > SkewMap;
74  typedef std::pair< double, boost::posix_time::ptime > DecayingWeight;
76  typedef std::map< MapIndex, DecayingWeight > WeightMap;
78  typedef std::map< MapIndex, unsigned int > LastResponseMap;
79 
81  void HandleExchangeResponse(const ExchangeResponseMessage& msg, CPeerNode peer);
83  void HandleExchange(const ExchangeMessage& msg, CPeerNode peer);
85  void Exchange(const boost::system::error_code& err );
86 
88  ModuleMessage CreateExchangeMessage(unsigned int k);
90  ModuleMessage CreateExchangeResponse(unsigned int k);
92  static ModuleMessage PrepareForSending(const ClockSynchronizerMessage& message);
93 
95  OffsetMap m_offsets;
97  SkewMap m_skews;
99  WeightMap m_weights;
101  QueryMap m_queries;
103  ResponseMap m_responses;
105  boost::posix_time::ptime m_lastinteraction;
107  unsigned int m_kcounter;
109  LastResponseMap m_lastresponse;
110 
112  boost::posix_time::time_duration m_myoffset;
114  double m_myskew;
115 
117  boost::asio::deadline_timer m_exchangetimer;
118 
120  double GetWeight(MapIndex i) const;
121 
123  void SetWeight(MapIndex i, double w);
124 
126  static double TDToDouble(boost::posix_time::time_duration td);
127 
129  static boost::posix_time::time_duration DoubleToTD(double td);
130 };
131 
132 
133 }
134 }
135 #endif
unsigned int m_kcounter
The current k for identifying the freshness.
Definition: CClockSynchronizer.hpp:107
std::pair< boost::posix_time::ptime, boost::posix_time::ptime > TimeTuple
Tuples of challenge query/response stuff.
Definition: CClockSynchronizer.hpp:66
std::map< MapIndex, ResponseList > ResponseMap
Stores the challenge responses.
Definition: CClockSynchronizer.hpp:70
void HandleExchangeResponse(const ExchangeResponseMessage &msg, CPeerNode peer)
Handler for clock exchange responses.
Definition: CClockSynchronizer.cpp:165
std::map< MapIndex, DecayingWeight > WeightMap
Type used by the weights.
Definition: CClockSynchronizer.hpp:76
Base interface for agents/broker modules.
Definition: CPeerNode.hpp:37
double GetWeight(MapIndex i) const
Gets the weight with a decay.
Definition: CClockSynchronizer.cpp:446
void Run()
Starts the synchronization algorithm.
Definition: CClockSynchronizer.cpp:84
boost::posix_time::time_duration m_myoffset
My offset.
Definition: CClockSynchronizer.hpp:112
boost::posix_time::ptime m_lastinteraction
Time between interactions.
Definition: CClockSynchronizer.hpp:105
void HandleExchange(const ExchangeMessage &msg, CPeerNode peer)
Receiver for clock exchange requests.
Definition: CClockSynchronizer.cpp:147
void Exchange(const boost::system::error_code &err)
Sends clock exchange requests to other processes.
Definition: CClockSynchronizer.cpp:301
CClockSynchronizer(boost::asio::io_service &ios)
Initialize module.
Definition: CClockSynchronizer.cpp:61
std::pair< unsigned int, boost::posix_time::ptime > QueryRecord
Query Tuple.
Definition: CClockSynchronizer.hpp:62
std::pair< std::string, std::string > MapIndex
Does the i,j referencing.
Definition: CClockSynchronizer.hpp:58
static double TDToDouble(boost::posix_time::time_duration td)
Turn a time duration into a double.
Definition: CClockSynchronizer.cpp:484
void Stop()
Stops the syncrhonization algorithm.
Definition: CClockSynchronizer.cpp:99
boost::asio::deadline_timer m_exchangetimer
Time for the exchange.
Definition: CClockSynchronizer.hpp:117
LastResponseMap m_lastresponse
The last time a node responded.
Definition: CClockSynchronizer.hpp:109
Definition: CClockSynchronizer.hpp:41
SkewMap m_skews
Relative skews.
Definition: CClockSynchronizer.hpp:97
ResponseMap m_responses
Old responses.
Definition: CClockSynchronizer.hpp:103
OffsetMap m_offsets
Relative offsets.
Definition: CClockSynchronizer.hpp:95
static boost::posix_time::time_duration DoubleToTD(double td)
Turn a double into a time duration.
Definition: CClockSynchronizer.cpp:500
An interface for an object which can handle recieving incoming messages.
Definition: IDGIModule.hpp:36
std::map< MapIndex, double > SkewMap
Type used by skews.
Definition: CClockSynchronizer.hpp:72
std::map< MapIndex, unsigned int > LastResponseMap
Last responses type.
Definition: CClockSynchronizer.hpp:78
QueryMap m_queries
Outstanding Clock Queries.
Definition: CClockSynchronizer.hpp:101
std::pair< double, boost::posix_time::ptime > DecayingWeight
Container for decaying weights.
Definition: CClockSynchronizer.hpp:74
General FREEDM Namespace.
Definition: CBroker.cpp:53
double m_myskew
My skew.
Definition: CClockSynchronizer.hpp:114
void HandleIncomingMessage(boost::shared_ptr< const ModuleMessage > msg, CPeerNode peer)
Processes incoming messages from other modules.
Definition: CClockSynchronizer.cpp:112
void SetWeight(MapIndex i, double w)
Sets the weight for a process.
Definition: CClockSynchronizer.cpp:467
std::map< MapIndex, QueryRecord > QueryMap
Stores the outstanding clock queries.
Definition: CClockSynchronizer.hpp:64
static ModuleMessage PrepareForSending(const ClockSynchronizerMessage &message)
Wraps a clock synchronizer message in a ModuleMessage.
Definition: CClockSynchronizer.cpp:519
std::map< MapIndex, boost::posix_time::time_duration > OffsetMap
Stores the relative offsets.
Definition: CClockSynchronizer.hpp:60
ModuleMessage CreateExchangeResponse(unsigned int k)
Generate the exchange response message.
Definition: CClockSynchronizer.cpp:403
ModuleMessage CreateExchangeMessage(unsigned int k)
Generate the exchange message.
Definition: CClockSynchronizer.cpp:382
boost::posix_time::ptime GetSynchronizedTime() const
Returns the synchronized time.
Definition: CClockSynchronizer.cpp:431
WeightMap m_weights
Relative weights.
Definition: CClockSynchronizer.hpp:99
std::list< TimeTuple > ResponseList
Stores the response pairs.
Definition: CClockSynchronizer.hpp:68