summaryrefslogtreecommitdiff
path: root/src/libzrtpcpp/TimeoutProvider.h
blob: 9f0edf7a3d20df98430c6cb4f65733efbf94033f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
  Copyright (C) 2006, 2005, 2004 Erik Eliasson, Johan Bilien, Werner Dittmann

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#ifndef TIMEOUTPROVIDER_H
#define TIMEOUTPROVIDER_H

/**
 * Provides a way to request timeouts after a number of milli seconds.
 *
 * A command is associated to each timeout.
 *
 * Modified to use the common c++ library functions and the STL
 * list by Werner Dittmann.
 *
 * @author Erik Eliasson, eliasson@it.kth.se, 2003
 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
 */

#include <list>
#include <sys/time.h>

#include <commoncpp/config.h>
#include <commoncpp/thread.h>

/**
 * Represents a request of a "timeout" (delivery of a command to a
 * "timeout receiver" after at least a specified time period).
 *
 * Slightly modified to use gettimeofday directly.
 *
 * NOTE: This class is only used internaly.
 * @author Erik Eliasson
 * @author Werner Dittmann
 */
template <class TOCommand, class TOSubscriber>
class TPRequest
{

public:

    TPRequest( TOSubscriber tsi, int timeoutMs, const TOCommand &command):
    subscriber(tsi)
    {
        struct timeval tv;
        gettimeofday(&tv, NULL );

        when_ms = ((uint64)tv.tv_sec) * (uint64)1000 + ((uint64)tv.tv_usec) / (uint64)1000;
    when_ms += timeoutMs;
    this->command = command;
    }

    /**
     * @param t  ms since Epoch
     */
    bool happensBefore(uint64 t)
    {
    if (when_ms < t) {
        return true;
    }
    if (when_ms > t) {
        return false;
    }
    return false; // if equal it does not "happens_before"

    }

    bool happensBefore(const TPRequest *req){
    return happensBefore(req->when_ms);
    }

    /**
     * Number of milli seconds until timeout from when this method is
     * called
     */
    int getMsToTimeout ()
    {
        struct timeval tv;
        gettimeofday(&tv, NULL );

        uint64 now = ((uint64)tv.tv_sec) * (uint64)1000 + ((uint64)tv.tv_usec) / (uint64)1000;

    if (happensBefore(now)) {
        return 0;
    }
    else {
        return (int)(when_ms - now);
    }
    }

    TOCommand getCommand()
    {
    return command;
    }

    TOSubscriber getSubscriber()
    {
    return subscriber;
    }

    /**
     * Two timeout requests are considered equeal if they have
     * the same subscriber AND command AND time when they
     * occur. If one of the time is zero then this is a
     * wildcard and matches always.
     */
    bool operator==(const TPRequest<TOCommand, TOSubscriber> &req)
    {
    if (req.subscriber == subscriber &&
               req.command == command &&
               req.when_ms == when_ms) {
            return true;
        }
        return false;
    }

private:
    TOSubscriber subscriber;
    uint64 when_ms;     // Time since Epoch in ms when the timeout
                // will happen

    TOCommand command;      // Command that will be delivered to the
                // receiver (subscriber) of the timeout.
};

/**
 * Class to generate objects giving timeout functionality.
 *
 * @author Erik Eliasson
 * @author Werner Dittmann
 */
template<class TOCommand, class TOSubscriber>
        class TimeoutProvider : public ost::Thread, ost::Event {

public:

    /**
     * Timeout Provide Constructor
     */
    TimeoutProvider(): requests(), synchLock(), stop(false)  { }

    /**
     * Destructor also terminates the Timeout thread.
     */
    ~TimeoutProvider() {
    terminate();
    }

    /**
     * Terminates the Timeout provider thread.
     */
    void stopThread(){
    stop = true;
    signal();       // signal event to waiting thread
    }

    /**
     * Request a timeout trigger.
     *
     * @param time_ms   Number of milli-seconds until the timeout is
     *          wanted. Note that a small additional period of time is
     *          added that depends on execution speed.
     * @param subscriber The receiver of the callback when the command has timed
     *          out. This argument must not be NULL.
     * @param command   Specifies the String command to be passed back in the
     *          callback.
     */
    void requestTimeout(int32_t time_ms, TOSubscriber subscriber, const TOCommand &command)
    {
        TPRequest<TOCommand, TOSubscriber>* request =
                new TPRequest<TOCommand, TOSubscriber>(subscriber, time_ms, command);

        synchLock.enter();

    if (requests.size()==0) {
        requests.push_front(request);
        signal();
        synchLock.leave();
        return;
    }
        if (request->happensBefore(requests.front())) {
        requests.push_front(request);
        signal();
            synchLock.leave();
        return;
    }
        if (requests.back()->happensBefore(request)){
        requests.push_back(request);
        signal();
            synchLock.leave();
        return;
    }

        typename std::list<TPRequest<TOCommand, TOSubscriber>* >::iterator i;
        for(i = requests.begin(); i != requests.end(); i++ ) {
            if( request->happensBefore(*i)) {
                requests.insert(i, request);
                break;
            }
        }
    signal();
    synchLock.leave();
    }

    /**
     * Removes timeout requests that belong to a subscriber and command.
     *
     * @see requestTimeout
     */
    void cancelRequest(TOSubscriber subscriber, const TOCommand &command)
    {
        synchLock.enter();
        typename std::list<TPRequest<TOCommand, TOSubscriber>* >::iterator i;
        for(i = requests.begin(); i != requests.end(); ) {
            if( (*i)->getCommand() == command &&
                (*i)->getSubscriber() == subscriber) {
                i = requests.erase(i);
                continue;
            }
            i++;
        }
    synchLock.leave();
    }

protected:

    void run()
    {
    do {
        synchLock.enter();
        int32_t time = 3600000;
        int32_t size = 0;
        if ((size = requests.size()) > 0) {
                time = requests.front()->getMsToTimeout();
            }
        if (time == 0 && size > 0) {
        if (stop){  // This must be checked so that we will
                // stop even if we have timeouts to deliver.
                    synchLock.leave();
            return;
        }
                TPRequest<TOCommand, TOSubscriber>* req = requests.front();
        TOSubscriber subs = req->getSubscriber();
        TOCommand command = req->getCommand();

                requests.pop_front();

        synchLock.leave(); // call the command with free Mutex
        subs->handleTimeout(command);
        continue;
        }
        synchLock.leave();
        if (stop) {     // If we were told to stop while delivering
                                // a timeout we will exit here
        return;
        }
        reset();        // ready to receive triggers again
            wait(time);
        if (stop) {     // If we are told to exit while waiting we
                                // will exit
        return;
        }
    } while(true);
    }

private:

    // The timeouts are ordered in the order of which they
    // will expire. Nearest in future is first in list.
    std::list<TPRequest<TOCommand, TOSubscriber> *> requests;

    ost::Mutex synchLock;   // Protects the internal data structures

    bool stop;      // Flag to tell the worker thread
                        // to terminate. Set to true and
                        // wake the worker thread to
                        // terminate it.
};

#endif

/** EMACS **
 * Local variables:
 * mode: c++
 * c-default-style: ellemtel
 * c-basic-offset: 4
 * End:
 */