33 #ifndef FREEDM_BROKER_HPP 34 #define FREEDM_BROKER_HPP 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> 50 class CConnectionManager;
57 class CBroker :
private boost::noncopyable
60 typedef boost::function<void (boost::system::error_code)>
Scheduleable;
63 typedef std::pair<ModuleIdent, boost::posix_time::time_duration>
PhaseTuple;
68 typedef std::map<TimerHandle, boost::asio::deadline_timer* >
TimersMap;
70 typedef std::map<ModuleIdent, std::list< BoundScheduleable > >
ReadyMap;
85 void Stop(
unsigned int signum = 0);
88 void HandleSignal(
const boost::system::error_code& error,
int parameter);
94 int Schedule(TimerHandle h, boost::posix_time::time_duration wait, Scheduleable x);
97 int Schedule(ModuleIdent m, BoundScheduleable x,
bool start_worker=
true);
103 void RegisterModule(ModuleIdent m, boost::posix_time::time_duration phase);
122 void ChangePhase(
const boost::system::error_code &err);
125 void ScheduledTask(Scheduleable x, TimerHandle handle,
const boost::system::error_code &err);
185 #endif // FREEDM_BROKER_HPP 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'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'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