reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
TimerQueue.hxx
Go to the documentation of this file.
1#if !defined(RESIP_TIMERQUEUE_HXX)
2#define RESIP_TIMERQUEUE_HXX
3
4#if defined(HAVE_CONFIG_H)
5 #include "config.h"
6#endif
7
8#include <functional>
9#include <queue>
10#include <set>
11#include <iosfwd>
14#include "rutil/Fifo.hxx"
16#include "rutil/Timer.hxx"
17
18// .dlb.
19// to do: timer wheel for transaction-bound timers and a heap for
20// everything longer.
21
22namespace resip
23{
24
25class Message;
26class TransactionMessage;
27class TuSelector;
28
38template <class T>
40{
41 public:
42 // This is the logic that runs when a timer goes off. This is the only
43 // thing subclasses must implement.
44 virtual void processTimer(const T& timer)=0;
45
47 virtual ~TimerQueue()
48 {
49 //xkd-2004-11-4
50 // delete the message associated with the timer
51 while (!mTimers.empty())
52 {
53 mTimers.pop();
54 }
55 }
56
62 unsigned int msTillNextTimer()
63 {
64 if (!mTimers.empty())
65 {
66 uint64_t next = mTimers.top().getWhen();
68 if (now > next)
69 {
70 return 0;
71 }
72 else
73 {
74 uint64_t ret64 = next - now;
75 if ( ret64 > uint64_t(INT_MAX) )
76 {
77 return INT_MAX;
78 }
79 else
80 {
81 int ret = int(ret64);
82 return ret;
83 }
84 }
85 }
86 else
87 {
88 return INT_MAX;
89 }
90 }
91
94 virtual uint64_t process()
95 {
96 if (!mTimers.empty())
97 {
99 while (!mTimers.empty() && !(mTimers.top().getWhen() > now))
100 {
101 processTimer(mTimers.top());
102 mTimers.pop();
103 }
104
105 if(!mTimers.empty())
106 {
107 return mTimers.top().getWhen();
108 }
109 }
110 return 0;
111 }
112
113 int size() const
114 {
115 return (int)mTimers.size();
116 }
117
118 bool empty() const
119 {
120 return mTimers.empty();
121 }
122
123 std::ostream& encode(std::ostream& str) const
124 {
125 if(mTimers.size() > 0)
126 {
127 return str << "TimerQueue[ size =" << mTimers.size()
128 << " top=" << mTimers.top() << "]" ;
129 }
130 else
131 {
132 return str << "TimerQueue[ size = 0 ]";
133 }
134 }
135
136#ifndef RESIP_USE_STL_STREAMS
138 {
139 if(mTimers.size() > 0)
140 {
141 return str << "TimerQueue[ size =" << mTimers.size()
142 << " top=" << mTimers.top() << "]" ;
143 }
144 else
145 {
146 return str << "TimerQueue[ size = 0 ]";
147 }
148 }
149#endif
150
151 protected:
152 typedef std::vector<T, std::allocator<T> > TimerVector;
153 std::priority_queue<T, TimerVector, std::greater<T> > mTimers;
154};
155
159class BaseTimeLimitTimerQueue : public TimerQueue<TimerWithPayload>
160{
161 public:
163 uint64_t add(unsigned int timeMs,Message* payload);
164 virtual void processTimer(const TimerWithPayload& timer);
165 protected:
167};
168
169
174{
175 public:
177 protected:
179 private:
181};
182
183
187class TuSelectorTimerQueue : public TimerQueue<TimerWithPayload>
188{
189 public:
192 uint64_t add(unsigned int timeMs,Message* payload);
193 virtual void processTimer(const TimerWithPayload& timer);
194 private:
195 TuSelector& mFifoSelector;
196};
197
198
202class TransactionTimerQueue : public TimerQueue<TransactionTimer>
203{
204 public:
206 uint64_t add(Timer::Type type, const Data& transactionId, unsigned long msOffset);
207 virtual void processTimer(const TransactionTimer& timer);
208 private:
209 Fifo<TimerMessage>& mFifo;
210};
211
212#ifdef USE_DTLS
213
214#include <openssl/ssl.h>
215
219class DtlsTimerQueue : public TimerQueue<TimerWithPayload>
220{
221 public:
224 uint64_t add(SSL *, unsigned long msOffset);
225 virtual void processTimer(const TimerWithPayload& timer) ;
226
227 private:
228 Fifo<DtlsMessage>& mFifo ;
229};
230
231#endif
232
233template <class T>
234std::ostream& operator<<(std::ostream& str, const TimerQueue<T>& tq)
235{
236 return tq.encode(str);
237}
238
239#ifndef RESIP_USE_STL_STREAMS
240template <class T>
242{
243 return tq.encode(str);
244}
245#endif
246
247
248}
249
250#endif
251
252/* ====================================================================
253 * The Vovida Software License, Version 1.0
254 *
255 * Copyright (c) 2004 Vovida Networks, Inc. All rights reserved.
256 *
257 * Redistribution and use in source and binary forms, with or without
258 * modification, are permitted provided that the following conditions
259 * are met:
260 *
261 * 1. Redistributions of source code must retain the above copyright
262 * notice, this list of conditions and the following disclaimer.
263 *
264 * 2. Redistributions in binary form must reproduce the above copyright
265 * notice, this list of conditions and the following disclaimer in
266 * the documentation and/or other materials provided with the
267 * distribution.
268 *
269 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
270 * and "Vovida Open Communication Application Library (VOCAL)" must
271 * not be used to endorse or promote products derived from this
272 * software without prior written permission. For written
273 * permission, please contact vocal@vovida.org.
274 *
275 * 4. Products derived from this software may not be called "VOCAL", nor
276 * may "VOCAL" appear in their name, without prior written
277 * permission of Vovida Networks, Inc.
278 *
279 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
280 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
281 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
282 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
283 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
284 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
285 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
286 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
287 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
288 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
289 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
290 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
291 * DAMAGE.
292 *
293 * ====================================================================
294 *
295 * This software consists of voluntary contributions made by Vovida
296 * Networks, Inc. and many individuals on behalf of Vovida Networks,
297 * Inc. For more information on Vovida Networks, Inc., please see
298 * <http://www.vovida.org/>.
299 *
300 */
struct ssl_st SSL
uint64_t add(unsigned int timeMs, Message *payload)
virtual void addToFifo(Message *, TimeLimitFifo< Message >::DepthUsage)=0
virtual void processTimer(const TimerWithPayload &timer)
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
uint64_t add(SSL *, unsigned long msOffset)
virtual void processTimer(const TimerWithPayload &timer)
A templated, threadsafe message-queue class.
Definition Fifo.hxx:16
The base-class used for message-passing.
Definition Message.hxx:21
virtual void addToFifo(Message *, TimeLimitFifo< Message >::DepthUsage)
std::vector< T, std::allocator< T > > TimerVector
unsigned int msTillNextTimer()
provides the time in milliseconds before the next timer will fire
std::priority_queue< T, TimerVector, std::greater< T > > mTimers
bool empty() const
std::ostream & encode(std::ostream &str) const
EncodeStream & encode(EncodeStream &str) const
virtual uint64_t process()
gets the set of timers that have fired, and inserts TimerMsg into the state machine fifo and applicat...
virtual void processTimer(const T &timer)=0
virtual ~TimerQueue()
deletes the message associated with the timer as well.
static uint64_t getTimeMs()
Returns the current clock time in milliseconds.
Definition Timer.hxx:72
uint64_t add(Timer::Type type, const Data &transactionId, unsigned long msOffset)
virtual void processTimer(const TransactionTimer &timer)
virtual void processTimer(const TimerWithPayload &timer)
uint64_t add(unsigned int timeMs, Message *payload)
dcm! – add UnusedChecking(_enum) below;
unsigned long long uint64_t
Definition Stun.hxx:39
EncodeStream & operator<<(EncodeStream &strm, const BaseException &e)
#define EncodeStream