FREEDM DGI
CBroker.hpp
Go to the documentation of this file.
1 
33 #ifndef FREEDM_BROKER_HPP
34 #define FREEDM_BROKER_HPP
35 
36 #include "CClockSynchronizer.hpp"
37 
38 #include <list>
39 #include <string>
40 
41 #include <boost/asio.hpp>
42 #include <boost/asio/deadline_timer.hpp>
43 #include <boost/function.hpp>
44 #include <boost/noncopyable.hpp>
45 #include <boost/thread/shared_mutex.hpp>
46 
47 namespace freedm {
48  namespace broker {
49 
50 class CConnectionManager;
51 class CDispatcher;
52 
54 const unsigned int ALIGNMENT_DURATION = 250;
55 
57 class CBroker : private boost::noncopyable
58 {
59 public:
60  typedef boost::function<void (boost::system::error_code)> Scheduleable;
61  typedef boost::function<void ()> BoundScheduleable;
62  typedef std::string ModuleIdent;
63  typedef std::pair<ModuleIdent, boost::posix_time::time_duration> PhaseTuple;
64  typedef std::vector< PhaseTuple > ModuleVector;
65  typedef unsigned int PhaseMarker;
66  typedef unsigned int TimerHandle;
67  typedef std::map<TimerHandle, ModuleIdent> TimerAlloc;
68  typedef std::map<TimerHandle, boost::asio::deadline_timer* > TimersMap;
69  typedef std::map<TimerHandle, bool > NextTimeMap;
70  typedef std::map<ModuleIdent, std::list< BoundScheduleable > > ReadyMap;
71 
73  static CBroker& Instance();
74 
76  ~CBroker();
77 
79  void Run();
80 
82  boost::asio::io_service& GetIOService();
83 
85  void Stop(unsigned int signum = 0);
86 
88  void HandleSignal(const boost::system::error_code& error, int parameter);
89 
91  void HandleStop(unsigned int signum = 0);
92 
94  int Schedule(TimerHandle h, boost::posix_time::time_duration wait, Scheduleable x);
95 
97  int Schedule(ModuleIdent m, BoundScheduleable x, bool start_worker=true);
98 
100  TimerHandle AllocateTimer(ModuleIdent module);
101 
103  void RegisterModule(ModuleIdent m, boost::posix_time::time_duration phase);
104 
106  bool IsModuleRegistered(ModuleIdent m);
107 
109  boost::posix_time::time_duration TimeRemaining();
110 
113 
114 private:
116  CBroker();
117 
119  boost::asio::io_service m_ioService;
120 
122  void ChangePhase(const boost::system::error_code &err);
123 
125  void ScheduledTask(Scheduleable x, TimerHandle handle, const boost::system::error_code &err);
126 
128  void Worker();
129 
131  bool m_busy;
132 
134  boost::posix_time::ptime m_last_alignment;
135 
137  ModuleVector m_modules;
138 
140  PhaseMarker m_phase;
141 
143  boost::posix_time::ptime m_phaseends;
144 
146  boost::asio::deadline_timer m_phasetimer;
147 
149  TimerHandle m_handlercounter;
150 
152  TimerAlloc m_allocs;
153 
155  TimersMap m_timers;
156 
158  NextTimeMap m_nexttime;
159 
161  NextTimeMap m_ntexpired;
162 
164  ReadyMap m_ready;
165 
167  boost::mutex m_schmutex;
168 
170  boost::shared_ptr<CClockSynchronizer> m_synchronizer;
171 
173  boost::asio::signal_set m_signals;
174 
177 
179  boost::mutex m_stoppingMutex;
180 };
181 
182  } // namespace broker
183 } // namespace freedm
184 
185 #endif // FREEDM_BROKER_HPP
186 
boost::posix_time::ptime m_last_alignment
The last time the phases were aligned.
Definition: CBroker.hpp:134
ReadyMap m_ready
A map of jobs that are ready to run as soon as their phase comes up.
Definition: CBroker.hpp:164
static CBroker & Instance()
Get the singleton instance of this class.
Definition: CBroker.cpp:67
CClockSynchronizer & GetClockSynchronizer()
Returns the synchronizer.
Definition: CBroker.cpp:622
bool m_busy
True while the worker is actively running tasks.
Definition: CBroker.hpp:131
std::string ModuleIdent
Definition: CBroker.hpp:62
unsigned int PhaseMarker
Definition: CBroker.hpp:65
std::map< TimerHandle, ModuleIdent > TimerAlloc
Definition: CBroker.hpp:67
void HandleSignal(const boost::system::error_code &error, int parameter)
Handle signals from the operating system (ie, Control-C)
Definition: CBroker.cpp:189
void RegisterModule(ModuleIdent m, boost::posix_time::time_duration phase)
Registers a module for the scheduler.
Definition: CBroker.cpp:256
void HandleStop(unsigned int signum=0)
Handles the stop signal from the operating System.
Definition: CBroker.cpp:216
boost::posix_time::ptime m_phaseends
Computed ptime for when the current phase ends.
Definition: CBroker.hpp:143
int Schedule(TimerHandle h, boost::posix_time::time_duration wait, Scheduleable x)
Schedules a task that will run after a timer expires.
Definition: CBroker.cpp:341
boost::mutex m_schmutex
Lock for the scheduler.
Definition: CBroker.hpp:167
TimersMap m_timers
A relation between the timer handles and the actual timer objects.
Definition: CBroker.hpp:155
std::map< ModuleIdent, std::list< BoundScheduleable > > ReadyMap
Definition: CBroker.hpp:70
~CBroker()
De-allocates the timers when the CBroker is destroyed.
Definition: CBroker.cpp:95
boost::function< void(boost::system::error_code)> Scheduleable
Definition: CBroker.hpp:60
PhaseMarker m_phase
The active module in the scheduler.
Definition: CBroker.hpp:140
void Worker()
Executes tasks from the active module&#39;s task queue.
Definition: CBroker.cpp:582
NextTimeMap m_ntexpired
Maps if a specific timer has been cancelled or triggered by end of round.
Definition: CBroker.hpp:161
void ScheduledTask(Scheduleable x, TimerHandle handle, const boost::system::error_code &err)
Adds a task scheduled by a module to the task queue when the timer expires.
Definition: CBroker.cpp:545
void ChangePhase(const boost::system::error_code &err)
An task that will advance the Broker&#39;s active module to the next module.
Definition: CBroker.cpp:423
Definition: CClockSynchronizer.hpp:41
void Run()
Starts the DGI Broker scheduler.
Definition: CBroker.cpp:112
boost::mutex m_stoppingMutex
Lock for m_stopping.
Definition: CBroker.hpp:179
unsigned int TimerHandle
Definition: CBroker.hpp:66
const unsigned int ALIGNMENT_DURATION
How often the scheduler should verify the schedule is being followed.
Definition: CBroker.hpp:54
boost::asio::signal_set m_signals
The registered signal handlers.
Definition: CBroker.hpp:173
std::vector< PhaseTuple > ModuleVector
Definition: CBroker.hpp:64
std::map< TimerHandle, bool > NextTimeMap
Definition: CBroker.hpp:69
bool IsModuleRegistered(ModuleIdent m)
Checks to see if a module is registered with the scheduler.
Definition: CBroker.cpp:281
TimerHandle m_handlercounter
The current counter for the time handlers.
Definition: CBroker.hpp:149
boost::asio::deadline_timer m_phasetimer
Timer for the phases.
Definition: CBroker.hpp:146
TimerAlloc m_allocs
Timer allocations for modules.
Definition: CBroker.hpp:152
void Stop(unsigned int signum=0)
Requests that the Broker stops execution to exit the DGI.
Definition: CBroker.cpp:167
boost::asio::io_service & GetIOService()
Return a reference to the boost::ioservice.
Definition: CBroker.cpp:151
General FREEDM Namespace.
Definition: CBroker.cpp:53
boost::asio::io_service m_ioService
The io_service used to perform asynchronous operations.
Definition: CBroker.hpp:119
std::pair< ModuleIdent, boost::posix_time::time_duration > PhaseTuple
Definition: CBroker.hpp:63
CBroker()
Private constructor for the singleton instance.
Definition: CBroker.cpp:76
NextTimeMap m_nexttime
Maps handle to bool: if a timer handle is set to expire for the next round.
Definition: CBroker.hpp:158
ModuleVector m_modules
List of modules for the scheduler.
Definition: CBroker.hpp:137
boost::shared_ptr< CClockSynchronizer > m_synchronizer
The clock synchronizer which aligns clocks between DGIs.
Definition: CBroker.hpp:170
boost::function< void()> BoundScheduleable
Definition: CBroker.hpp:61
std::map< TimerHandle, boost::asio::deadline_timer * > TimersMap
Definition: CBroker.hpp:68
Scheduler for the DGI modules.
Definition: CBroker.hpp:57
bool m_stopping
Flag to prevent modules from scheduling, set when the DGI is stopping.
Definition: CBroker.hpp:176
TimerHandle AllocateTimer(ModuleIdent module)
Allocate a timer to a specified module.
Definition: CBroker.cpp:304
boost::posix_time::time_duration TimeRemaining()
Returns how much time the current module has left in its phase.
Definition: CBroker.cpp:533