/[resiprocate]/branches/b-directory-reorg/sip/resiprocate/TimerQueue.cxx
ViewVC logotype

Contents of /branches/b-directory-reorg/sip/resiprocate/TimerQueue.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5271 - (show annotations) (download)
Thu Aug 18 23:43:07 2005 UTC (14 years, 3 months ago) by jason
File size: 7016 byte(s)
new directory reorg proposal
1
2 #if defined(HAVE_CONFIG_H)
3 #include "resiprocate/config.hxx"
4 #endif
5
6 #include <cassert>
7 #include <limits.h>
8
9 #include "resiprocate/TimerQueue.hxx"
10 #include "resiprocate/TimerMessage.hxx"
11 #include "resiprocate/TransactionMessage.hxx"
12 #include "resiprocate/TuSelector.hxx"
13 #include "resiprocate/os/Logger.hxx"
14 #include "resiprocate/os/Inserter.hxx"
15 #include "resiprocate/os/WinLeakCheck.hxx"
16
17 using namespace resip;
18 using namespace std;
19
20 #define RESIPROCATE_SUBSYSTEM Subsystem::TRANSACTION
21
22 TimerQueue::TimerQueue(Fifo<TransactionMessage>& fifo)
23 : mFifo(fifo)
24 {
25 }
26
27 #ifdef USE_DTLS
28
29 DtlsTimerQueue::DtlsTimerQueue( Fifo<DtlsMessage>& fifo )
30 : mFifo( fifo )
31 {
32 }
33
34 #endif
35
36 BaseTimerQueue::~BaseTimerQueue()
37 {
38 //xkd-2004-11-4
39 // delete the message associated with the timer
40 for (std::multiset<Timer>::iterator i = mTimers.begin(); i != mTimers.end(); ++i)
41 {
42 if (i->getMessage())
43 {
44 delete i->getMessage();
45 }
46 }
47 }
48
49 unsigned int
50 BaseTimerQueue::msTillNextTimer()
51 {
52 if (!mTimers.empty())
53 {
54 UInt64 next = mTimers.begin()->mWhen;
55 UInt64 now = Timer::getTimeMs();
56 if (now > next)
57 {
58 return 0;
59 }
60 else
61 {
62 UInt64 ret64 = next - now;
63 if ( ret64 > UInt64(INT_MAX) )
64 {
65 return INT_MAX;
66 }
67 else
68 {
69 int ret = int(ret64);
70 return ret;
71 }
72 }
73 }
74 else
75 {
76 return INT_MAX;
77 }
78 }
79
80 Timer::Id
81 TimerQueue::add(Timer::Type type, const Data& transactionId, unsigned long msOffset)
82 {
83 Timer t(msOffset, type, transactionId);
84 mTimers.insert(t);
85 DebugLog (<< "Adding timer: " << Timer::toData(type) << " tid=" << transactionId << " ms=" << msOffset);
86
87 return t.getId();
88 }
89
90 #ifdef USE_DTLS
91
92 void
93 DtlsTimerQueue::add( SSL *ssl, unsigned long msOffset )
94 {
95 Timer t( msOffset, new DtlsMessage( ssl ) ) ;
96 mTimers.insert( t ) ;
97 }
98
99 #endif
100
101 void
102 BaseTimeLimitTimerQueue::add(const Timer& timer)
103 {
104 assert(timer.getMessage());
105 DebugLog(<< "Adding application timer: " << timer.getMessage()->brief());
106 mTimers.insert(timer);
107 }
108
109 int
110 BaseTimerQueue::size() const
111 {
112 return mTimers.size();
113 }
114
115 bool
116 BaseTimerQueue::empty() const
117 {
118 return mTimers.empty();
119 }
120
121 void
122 BaseTimeLimitTimerQueue::process()
123 {
124 // get the set of timers that have fired and insert TimerMsg into the state
125 // machine fifo and application messages into the TU fifo
126
127 if (!mTimers.empty() && msTillNextTimer() == 0)
128 {
129 Timer now(0);
130 std::multiset<Timer>::iterator end = mTimers.upper_bound(now);
131 for (std::multiset<Timer>::iterator i = mTimers.begin(); i != end; ++i)
132 {
133 assert(i->getMessage());
134 addToFifo(i->getMessage(), TimeLimitFifo<Message>::InternalElement);
135 }
136 mTimers.erase(mTimers.begin(), end);
137 }
138 }
139
140 void
141 TimerQueue::process()
142 {
143 // get the set of timers that have fired and insert TimerMsg into the state
144 // machine fifo and application messages into the TU fifo
145
146 if (!mTimers.empty() && msTillNextTimer() == 0)
147 {
148 Timer now(0);
149 std::multiset<Timer>::iterator end = mTimers.upper_bound(now);
150 for (std::multiset<Timer>::iterator i = mTimers.begin(); i != end; ++i)
151 {
152 mFifo.add(new TimerMessage(i->mTransactionId, i->mType, i->mDuration));
153 }
154 mTimers.erase(mTimers.begin(), end);
155 }
156 }
157
158 TimeLimitTimerQueue::TimeLimitTimerQueue(TimeLimitFifo<Message>& fifo) : mFifo(fifo)
159 {}
160
161 void
162 TimeLimitTimerQueue::addToFifo(Message*msg, TimeLimitFifo<Message>::DepthUsage d)
163 {
164 mFifo.add(msg, d);
165 }
166
167 TuSelectorTimerQueue::TuSelectorTimerQueue(TuSelector& sel) : mFifoSelector(sel)
168 {}
169
170 void
171 TuSelectorTimerQueue::addToFifo(Message*msg, TimeLimitFifo<Message>::DepthUsage d)
172 {
173 mFifoSelector.add(msg, d);
174 }
175
176
177 #ifdef USE_DTLS
178
179 void
180 DtlsTimerQueue::process()
181 {
182 // get the set of timers that have fired and insert TimerMsg into the state
183 // machine fifo and application messages into the TU fifo
184
185 if (!mTimers.empty() && msTillNextTimer() == 0)
186 {
187 Timer now(0);
188 std::multiset<Timer>::iterator end = mTimers.upper_bound(now);
189 for (std::multiset<Timer>::iterator i = mTimers.begin(); i != end; ++i)
190 {
191 mFifo.add( (DtlsMessage *)i->getMessage() ) ;
192 }
193 mTimers.erase( mTimers.begin(), end );
194 }
195 }
196
197 #endif
198
199 std::ostream&
200 resip::operator<<(std::ostream& str, const BaseTimerQueue& tq)
201 {
202 str << "TimerQueue[" ;
203
204 for (std::multiset<Timer>::const_iterator i = tq.mTimers.begin();
205 i != tq.mTimers.end(); ++i)
206 {
207 str << *i << " " ;
208 }
209
210 str << "]" << endl;
211 return str;
212 }
213
214
215 /* ====================================================================
216 * The Vovida Software License, Version 1.0
217 *
218 * Copyright (c) 2004 Vovida Networks, Inc. All rights reserved.
219 *
220 * Redistribution and use in source and binary forms, with or without
221 * modification, are permitted provided that the following conditions
222 * are met:
223 *
224 * 1. Redistributions of source code must retain the above copyright
225 * notice, this list of conditions and the following disclaimer.
226 *
227 * 2. Redistributions in binary form must reproduce the above copyright
228 * notice, this list of conditions and the following disclaimer in
229 * the documentation and/or other materials provided with the
230 * distribution.
231 *
232 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
233 * and "Vovida Open Communication Application Library (VOCAL)" must
234 * not be used to endorse or promote products derived from this
235 * software without prior written permission. For written
236 * permission, please contact vocal@vovida.org.
237 *
238 * 4. Products derived from this software may not be called "VOCAL", nor
239 * may "VOCAL" appear in their name, without prior written
240 * permission of Vovida Networks, Inc.
241 *
242 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
243 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
244 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
245 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
246 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
247 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
248 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
249 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
250 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
251 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
253 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
254 * DAMAGE.
255 *
256 * ====================================================================
257 *
258 * This software consists of voluntary contributions made by Vovida
259 * Networks, Inc. and many individuals on behalf of Vovida Networks,
260 * Inc. For more information on Vovida Networks, Inc., please see
261 * <http://www.vovida.org/>.
262 *
263 */

Properties

Name Value
svn:eol-style LF

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27