Home2L - C/C++ API v1.4-0-g38cc (2024-05-25)
Smart Tools for a Private Home
|
Service keeper: Helper class to keep an unreliable service running.
This class takes care of keeping a service running by scheduling re-open attempts according to some given strategy.
The class can be used in two ways:
#include "base.H"
Public Member Functions | |
Initialization and setup ... | |
CServiceKeeper () | |
CServiceKeeper (TTicks _tDShort, TTicks _tDLong=NEVER, TTicks _tShortToLong=NEVER) | |
~CServiceKeeper () | |
void | Setup (TTicks _tDShort, TTicks _tDLong=NEVER, TTicks _tShortToLong=NEVER) |
Setup the retry policy. More... | |
Using the service ... | |
void | Open () |
Request the service to open. More... | |
void | Close () |
Request the service to close. More... | |
void | Refresh () |
Force retry on next Iterate() and return to short retry intervals. | |
void | Iterate () |
Iterate (callback use); perform all open/close actions as adequate. Not needed for polling use. | |
bool | IsOpen () |
check if the service is actually open. | |
bool | ShouldBeOpen () |
check if the service has been requested to be open. | |
Callbacks (for callback use) ... | |
These callbacks are always called from Iterate(). | |
virtual void | DoOpen () |
Attempt to open the service in callback use. If this method is overloaded it must call ReportOpenAttempt() to report that the attempt has occurred. | |
virtual void | DoClose () |
Actually close the service. If this method is overloaded it must call ReportClosed() to report that the closing has succeeded. Otherwise, this method will be called again on next call of Iterate(). | |
Polling methods (for polling use) ... | |
bool | OpenAttemptNow () |
Query if the connection should be (re-)opened now. After the attempt, ReportOpenAttempt() must be called to report whether the attempt was successful or not. | |
bool | CloseNow () |
Query if the connection should be closed now. Closing must not fail. | |
Reporting about the service (for callback and polling use) ... | |
void | ReportLost () |
Report that the service is no longer running. | |
void | ReportOpenAttempt (bool success) |
Report an open attempt (either successful or not). This must be called from DoOpen() or after OpenAttemptNow() has returned 'true' to confirm that an open attempt has taken placed. | |
void | ReportClosed () |
Report that the service was actually closed. This must be calld from DoClose() or after CloseNow() has returned 'true' to confirm that the service is actually closed. | |
|
inline |
Setup the retry policy.
_tDShort | is the retry interval after the service got lost (e.g. disconnected / closed). |
_tDLong | is the retry interval after the service was lost for a longer time. |
_tShortToLong | is the time after which the retry interval is changed from _tDShort to _tDLong. |
|
inline |
|
inline |
Request the service to close.
Actual opening will be deferred until the next call to Iterate(). To properly close the service, it must be closed explicitely by the caller. In callback use, this can be accomplished by calling Iterate() until IsOpen() returns 'false'.