1 /*
2 Copyright (C) 2006, 2005, 2004 Erik Eliasson, Johan Bilien, Werner Dittmann
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 #ifndef TIMEOUTPROVIDER_H
21 #define TIMEOUTPROVIDER_H
22
35 #include <list>
36 #include <sys/time.h>
37
38 #include <commoncpp/config.h>
39 #include <commoncpp/thread.h>
40
51 template <class TOCommand, class TOSubscriber>
53 {
54
55 public:
56
59 {
60 struct timeval tv;
61 gettimeofday(&tv, NULL );
62
63 when_ms = ((uint64)tv.tv_sec) * (uint64)1000 + ((uint64)tv.tv_usec) / (uint64)1000;
66 }
67
72 {
74 return true;
75 }
77 return false;
78 }
79 return false; // if equal it does not "happens_before"
80
81 }
82
85 }
86
92 {
93 struct timeval tv;
94 gettimeofday(&tv, NULL );
95
96 uint64 now = ((uint64)tv.tv_sec) * (uint64)1000 + ((uint64)tv.tv_usec) / (uint64)1000;
97
99 return 0;
100 }
101 else {
103 }
104 }
105
107 {
109 }
110
112 {
114 }
115
123 {
127 return true;
128 }
129 return false;
130 }
131
132 private:
134 uint64
when_ms;
// Time since Epoch in ms when the timeout
135 // will happen
136
137 TOCommand
command;
// Command that will be delivered to the
138 // receiver (subscriber) of the timeout.
139 };
140
147 template<class TOCommand, class TOSubscriber>
149
150 public:
151
156
161 terminate();
162 }
163
169 signal(); // signal event to waiting thread
170 }
171
183 void requestTimeout(int32_t time_ms, TOSubscriber subscriber,
const TOCommand &command)
184 {
187
189
192 signal();
194 return;
195 }
198 signal();
200 return;
201 }
202 if (
requests.back()->happensBefore(request)){
204 signal();
206 return;
207 }
208
209 typename std::list<TPRequest<TOCommand, TOSubscriber>* >::iterator i;
213 break;
214 }
215 }
216 signal();
218 }
219
226 {
228 typename std::list<TPRequest<TOCommand, TOSubscriber>* >::iterator i;
230 if( (*i)->getCommand() == command &&
231 (*i)->getSubscriber() == subscriber) {
233 continue;
234 }
235 i++;
236 }
238 }
239
240 protected:
241
243 {
244 do {
246 int32_t time = 3600000;
247 int32_t size = 0;
249 time =
requests.front()->getMsToTimeout();
250 }
251 if (time == 0 && size > 0) {
252 if (
stop){
// This must be checked so that we will
253 // stop even if we have timeouts to deliver.
255 return;
256 }
260
262
263 synchLock.leave();
// call the command with free Mutex
264 subs->handleTimeout(command);
265 continue;
266 }
268 if (
stop) {
// If we were told to stop while delivering
269 // a timeout we will exit here
270 return;
271 }
272 reset(); // ready to receive triggers again
274 if (
stop) {
// If we are told to exit while waiting we
275 // will exit
276 return;
277 }
278 } while(true);
279 }
280
281 private:
282
283 // The timeouts are ordered in the order of which they
284 // will expire. Nearest in future is first in list.
285 std::list<TPRequest<TOCommand, TOSubscriber> *>
requests;
286
287 ost::Mutex
synchLock;
// Protects the internal data structures
288
289 bool stop;
// Flag to tell the worker thread
290 // to terminate. Set to true and
291 // wake the worker thread to
292 // terminate it.
293 };
294
295 #endif
296
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.