20 #ifndef TIMEOUTPROVIDER_H
21 #define TIMEOUTPROVIDER_H
38 #include <commoncpp/config.h>
39 #include <commoncpp/thread.h>
51 template <
class TOCommand,
class TOSubscriber>
61 gettimeofday(&tv, NULL );
63 when_ms = ((uint64)tv.tv_sec) * (uint64)1000 + ((uint64)tv.tv_usec) / (uint64)1000;
94 gettimeofday(&tv, NULL );
96 uint64 now = ((uint64)tv.tv_sec) * (uint64)1000 + ((uint64)tv.tv_usec) / (uint64)1000;
147 template<
class TOCommand,
class TOSubscriber>
183 void requestTimeout(int32_t time_ms, TOSubscriber subscriber,
const TOCommand &command)
202 if (
requests.back()->happensBefore(request)){
209 typename std::list<TPRequest<TOCommand, TOSubscriber>* >::iterator i;
228 typename std::list<TPRequest<TOCommand, TOSubscriber>* >::iterator i;
230 if( (*i)->getCommand() == command &&
231 (*i)->getSubscriber() == subscriber) {
246 int32_t time = 3600000;
249 time =
requests.front()->getMsToTimeout();
251 if (time == 0 && size > 0) {
264 subs->handleTimeout(command);
285 std::list<TPRequest<TOCommand, TOSubscriber> *>
requests;
TPRequest(TOSubscriber tsi, int timeoutMs, const TOCommand &command)
Provides a way to request timeouts after a number of milli seconds.
void requestTimeout(int32_t time_ms, TOSubscriber subscriber, const TOCommand &command)
Request a timeout trigger.
Class to generate objects giving timeout functionality.
bool happensBefore(const TPRequest *req)
TOSubscriber getSubscriber()
void stopThread()
Terminates the Timeout provider thread.
std::list< TPRequest< TOCommand, TOSubscriber > * > requests
TimeoutProvider()
Timeout Provide Constructor.
bool operator==(const TPRequest< TOCommand, TOSubscriber > &req)
Two timeout requests are considered equeal if they have the same subscriber AND command AND time when...
int getMsToTimeout()
Number of milli seconds until timeout from when this method is called.
bool happensBefore(uint64 t)
void cancelRequest(TOSubscriber subscriber, const TOCommand &command)
Removes timeout requests that belong to a subscriber and command.
~TimeoutProvider()
Destructor also terminates the Timeout thread.