reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
CancelableTimerQueue.hxx
Go to the documentation of this file.
1#ifndef resip_CancelableTimerQueue_hxx
2#define resip_CancelableTimerQueue_hxx
3
4#include <map>
5#include <limits.h>
6#include <iosfwd>
7
8#include "rutil/Timer.hxx"
9#include "rutil/HashMap.hxx"
10#include "rutil/Inserter.hxx"
11
12using namespace std;
13
14namespace resip
15{
16
17template<class T>
19{
20 public:
21 CancelableTimerQueue() : mNextId(0) {};
23
24 typedef long Id;
25 private:
26 typedef std::multimap< uint64_t, std::pair<T, Id> > TimerMap;
27 typedef HashMap<Id, uint64_t> IdToTimer;
28
29 public:
31 unsigned int offset)
32 {
33 return addTimer(msg, resip::Timer::getTimeMs() + offset);
34 }
35
36 //returns true if the id existed
37 bool cancel(Id id)
38 {
39 typename IdToTimer::iterator j = mIdToTimer.find(id);
40 if (j == mIdToTimer.end())
41 {
42 return false;
43 }
44
45 for (typename TimerMap::iterator i = mTimerMap.lower_bound(j->second); i != mTimerMap.upper_bound(j->second); i++)
46 {
47 if (i->second.second == id)
48 {
49 mTimerMap.erase(i);
50 mIdToTimer.erase(j);
51 return true;
52 }
53 }
54 //cerr << "mIdToTimer: " << Inserter(mIdToTimer) << endl;
55 //cerr << "Searching for ID: " << id << endl;
56 //cerr << "TimerMap state: " << Inserter(mTimerMap) << endl;
57 resip_assert(mIdToTimer.size() == mTimerMap.size());
58 resip_assert(0);
59 return false;
60 }
61
62 //get the number of milliseconds until the next event, returns -1 if no
63 //event is available
64 int getTimeout() const
65 {
66 if (mTimerMap.empty())
67 {
68 return -1;
69 }
70 else
71 {
72 int timeout = (int)(mTimerMap.begin()->first - resip::Timer::getTimeMs());
73 if (timeout < 0)
74 {
75 return 0;
76 }
77 else
78 {
79 return timeout;
80 }
81 }
82 }
83
84 bool available() const
85 {
86 return (!mTimerMap.empty() &&
87 mTimerMap.begin()->first <= resip::Timer::getTimeMs());
88 }
89
91 {
92 resip_assert(mIdToTimer.size() == mTimerMap.size());
93
95
96 typename TimerMap::iterator it = mTimerMap.begin();
97 mIdToTimer.erase(it->second.second);
98
99 T msg = it->second.first;
100 mTimerMap.erase(it);
101
102 resip_assert(mIdToTimer.size() == mTimerMap.size());
103
104 return msg;
105 }
106
107 void clear()
108 {
109 mTimerMap.clear();
110 mIdToTimer.clear();
111 }
112
113 bool empty() const
114 {
115 return mTimerMap.empty();
116 }
117
118 size_t size() const
119 {
120 return mTimerMap.size();
121 }
122
123 private:
124
125 Id addTimer(T msg, uint64_t expiry)
126 {
127 Id id = getNextId();
128 mTimerMap.insert(std::make_pair(expiry, std::make_pair(msg, id)));
129 mIdToTimer[id] = expiry;
130 resip_assert(mIdToTimer.size() == mTimerMap.size());
131 return id;
132 }
133
134 Id getNextId()
135 {
136 mNextId++;
137 if (mNextId == INT_MAX)
138 {
139 mNextId = 1;
140 }
141 return mNextId;
142 }
143
144 Id mNextId;
145 IdToTimer mIdToTimer;
146 TimerMap mTimerMap;
147};
148
149}
150
151#endif
152/* ====================================================================
153 * The Vovida Software License, Version 1.0
154 *
155 * Copyright (c) 2004 PurpleComm, Inc. All rights reserved.
156 *
157 * Redistribution and use in source and binary forms, with or without
158 * modification, are permitted provided that the following conditions
159 * are met:
160 *
161 * 1. Redistributions of source code must retain the above copyright
162 * notice, this list of conditions and the following disclaimer.
163 *
164 * 2. Redistributions in binary form must reproduce the above copyright
165 * notice, this list of conditions and the following disclaimer in
166 * the documentation and/or other materials provided with the
167 * distribution.
168 *
169 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
170 * and "Vovida Open Communication Application Library (VOCAL)" must
171 * not be used to endorse or promote products derived from this
172 * software without prior written permission. For written
173 * permission, please contact vocal@vovida.org.
174 *
175 * 4. Products derived from this software may not be called "VOCAL", nor
176 * may "VOCAL" appear in their name, without prior written
177 * permission of Vovida Networks, Inc.
178 *
179 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
180 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
182 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
183 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
184 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
185 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
186 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
187 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
188 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
189 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
190 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
191 * DAMAGE.
192 *
193 */
Wraps your platform's implementation of hash_map, hash_multimap, and hash_set.
#define resip_assert(x)
This file needs to work for both C and C++ code.
Id addRelative(T msg, unsigned int offset)
static uint64_t getTimeMs()
Returns the current clock time in milliseconds.
Definition Timer.hxx:72
dcm! – add UnusedChecking(_enum) below;
unsigned long long uint64_t
Definition Stun.hxx:39