#include <WatchedControllers.hpp>
Public Member Functions | |
WatchedControllers (const std::vector< ControllerInfo > &controllersPrm, int64_t initTime, int controllerTimeout, ControllerStateChangedCallback *cbPtr) | |
Creates a new list of watched controllers with the given set of controllers and an initial lastTimeSeen value. | |
~WatchedControllers () | |
Deletes the callback. | |
void | addController (const ControllerInfo &ctrl, int64_t lastTimeSeen) |
Adds a controller to the list and associates the given lastTimeSeenValue to it. | |
void | removeController (const ControllerInfo &ctrl) |
Removes the selected controller and its associated lastTimeSeen. | |
void | setControllerResponsed (const SocketAddress &ctrlAddr, int64_t newTime) |
Associates given lastTimeSeen value to specified controller and update its state: if the controller was considered as down, calls the ControllerStateChangedCallback#onControllerUp(const ControllerInfo&). | |
void | lookForDeadControllers (int64_t currentTime) |
Updates all controllers state according to the given time. | |
void | setControllerDown (const SocketAddress &ctrlAddr) |
Forces the given controller to be considered as down. | |
std::vector< SocketAddress > | getControllerAddresses () const |
Returns a 'safe' read-only iterator on controllers' ControllerInfo. | |
Classes | |
class | ControllerAndState |
Holds a controller, its lastTimeSeen variable and its state (up or down). |
To each controller is associated a lastTimeSeen value corresponding to the last time the controller responded to a ping. Their state is updated each time the lastTimeSeen value is accessed (read or written). At init time, all controllers are considered as up (responding to pings). Controllers can also be forced as 'no more responding to pings' (ie. down)
CarobNS::WatchedControllers::WatchedControllers | ( | const std::vector< ControllerInfo > & | controllersPrm, | |
int64_t | initTime, | |||
int | controllerTimeout, | |||
ControllerStateChangedCallback * | cbPtr | |||
) |
Creates a new list of watched controllers with the given set of controllers and an initial lastTimeSeen value.
controllersPrm | set of controllers identified by their ControllerInfo | |
initTime | initial lastTimeSeen value, typically clock() | |
controllerTimeout | delay after which a controller will be considered as failing if it did not respond to pings | |
cbPtr | Callback implementation to call when a controller state changes |
void CarobNS::WatchedControllers::addController | ( | const ControllerInfo & | ctrl, | |
int64_t | lastTimeSeen | |||
) |
Adds a controller to the list and associates the given lastTimeSeenValue to it.
Note that this operation is thread-safe, thus can slow-down other concurrent operations like removeController(const ControllerInfo&), setControllerResponsed(const SocketAddress&, int64_t)
ctrl | controller to be added | |
lastTimeSeen | initial time to associate to this controller. Typically the result of gettimeofday() |
std::vector<SocketAddress> CarobNS::WatchedControllers::getControllerAddresses | ( | ) | const |
Returns a 'safe' read-only iterator on controllers' ControllerInfo.
Creates a copy of the controller hashmap keys and returns a iterator on it. This way, the iterator will not be affected by hashmap operations
void CarobNS::WatchedControllers::lookForDeadControllers | ( | int64_t | currentTime | ) |
Updates all controllers state according to the given time.
Iterates through the controller list and (if the controller was not already considered as down) if the lastTimeSeen value is older than the given time minus controller_timeout, the controller will the be considered as failing, and ControllerStateChangedCallback#onControllerDown(const ControllerInfo&) will be called.
currentTime | reference time to compare to last time seen values. Typically the result of gettimeofday() |
void CarobNS::WatchedControllers::setControllerDown | ( | const SocketAddress & | ctrlAddr | ) |
Forces the given controller to be considered as down.
If the given controller was already down, does nothing. Otherwise, marks it as dead and calls ControllerStateChangedCallback#onControllerDown(const ControllerInfo&)
ctrlAddr | controller socket address to consider as failing |
void CarobNS::WatchedControllers::setControllerResponsed | ( | const SocketAddress & | ctrlAddr, | |
int64_t | newTime | |||
) |
Associates given lastTimeSeen value to specified controller and update its state: if the controller was considered as down, calls the ControllerStateChangedCallback#onControllerUp(const ControllerInfo&).
ctrlAddr | controller socket address to be updated | |
newTime | time to associate to this controller. Typically the result of gettimeofday() |