reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
TimeLimitFifo.hxx
Go to the documentation of this file.
1#ifndef RESIP_TimeLimitFifo_hxx
2#define RESIP_TimeLimitFifo_hxx
3
4#include "rutil/ResipAssert.h"
5#include <memory>
7#include <iostream>
8#if defined( WIN32 )
9#include <time.h>
10#endif
11
12// efficiency note: use a circular buffer to avoid list node allocation
13
14// what happens to timers that can't be queued?
15
16namespace resip
17{
18
23template<typename Payload>
25{
26 public:
27 Timestamped(const Payload& msg, time_t n)
28 : mMsg(msg),
29 mTime(n)
30 {}
31
32 inline const Payload& getMsg() const { return mMsg;}
33 inline void setMsg(const Payload& pMsg) { mMsg = pMsg;}
34 inline const time_t& getTime() const { return mTime;}
35
36 private:
37 Payload mMsg;
38 time_t mTime;
39};
40
90template <class Msg>
91class TimeLimitFifo : public AbstractFifo< Timestamped<Msg*> >
92{
93 public:
95
97 TimeLimitFifo(unsigned int maxDurationSecs,
98 unsigned int maxSize);
99
100 virtual ~TimeLimitFifo();
101
108
128 bool add(Msg* msg, DepthUsage usage);
129
140 Msg* getNext();
151 Msg* getNext(int ms);
152
157 virtual time_t timeDepth() const;
158
159
167
171 void clear();
172
177 virtual size_t getCountDepth() const;
183 virtual time_t getTimeDepth() const;
188 virtual size_t getCountDepthTolerance() const;
193 virtual time_t getTimeDepthTolerance() const;
201 virtual void setCountDepthTolerance(unsigned int max);
207 virtual void setTimeDepthTolerance(unsigned int maxSecs);
208
209 private:
210 time_t timeDepthInternal() const;
211 inline bool wouldAcceptInteral(DepthUsage usage) const;
212 TimeLimitFifo(const TimeLimitFifo& rhs);
213 TimeLimitFifo& operator=(const TimeLimitFifo& rhs);
214
215 time_t mMaxDurationSecs;
216 unsigned int mMaxSize;
217 unsigned int mUnreservedMaxSize;
218};
219
220template <class Msg>
221TimeLimitFifo<Msg>::TimeLimitFifo(unsigned int maxDurationSecs,
222 unsigned int maxSize)
223 : AbstractFifo< Timestamped<Msg*> >(),
224 mMaxDurationSecs(maxDurationSecs),
225 mMaxSize(maxSize),
226 mUnreservedMaxSize((int)((maxSize*8)/10)) // !dlb! random guess
227{}
228
229template <class Msg>
235
236template <class Msg>
237bool
240{
241 Lock lock(mMutex); (void)lock;
242
243 if (wouldAcceptInteral(usage))
244 {
245 time_t n = time(0);
246 mFifo.push_back(Timestamped<Msg*>(msg, n));
247 onMessagePushed(1);
248 mCondition.notify_one();
249 return true;
250 }
251 else
252 {
253 return false;
254 }
255}
256
257template <class Msg>
258bool
260{
261 Lock lock(mMutex); (void)lock;
262
263 return wouldAcceptInteral(usage);
264}
265
266template <class Msg>
267Msg*
269{
271 return tm.getMsg();
272}
273
274template <class Msg>
275Msg*
277{
278 Timestamped<Msg*> tm(0,0);
279 if(AbstractFifo< Timestamped<Msg*> >::getNext(ms, tm))
280 {
281 return tm.getMsg();
282 }
283 return 0;
284}
285
286template <class Msg>
287time_t
289{
290 if(mFifo.empty())
291 {
292 return 0;
293 }
294
295 return time(0) - mFifo.front().getTime();
296}
297
298template <class Msg>
299bool
300TimeLimitFifo<Msg>::wouldAcceptInteral(DepthUsage usage) const
301{
302 if ((mMaxSize != 0 &&
303 mFifo.size() >= mMaxSize))
304 {
305 return false;
306 }
307
308 if (usage == InternalElement)
309 {
310 return true;
311 }
312
313 if (mUnreservedMaxSize != 0 &&
314 mFifo.size() >= mUnreservedMaxSize)
315 {
316 return false;
317 }
318
319 if (usage == IgnoreTimeDepth)
320 {
321 return true;
322 }
323
324 resip_assert(usage == EnforceTimeDepth);
325
326 if (mFifo.size() == 0 ||
327 mMaxDurationSecs == 0 ||
328 timeDepthInternal() < mMaxDurationSecs)
329 {
330 return true;
331 }
332
333 return false;
334}
335
336template <class Msg>
337time_t
339{
340 Lock lock(mMutex); (void)lock;
341 return timeDepthInternal();
342}
343
344template <class Msg>
345void
347{
348 Lock lock(mMutex); (void)lock;
349
350 while (!mFifo.empty())
351 {
352 delete mFifo.front().getMsg();
353 mFifo.pop_front();
354 }
355}
356
357template <class Msg>
358size_t
360{
361 return size();
362}
363
364template <class Msg>
365size_t
367{
368 return mUnreservedMaxSize;
369}
370
371template <class Msg>
372time_t
374{
375 return timeDepth();
376}
377
378template <class Msg>
379time_t
381{
382 return mMaxDurationSecs;
383}
384
385template <class Msg>
386void
388{
389 Lock lock(mMutex); (void)lock;
390 mUnreservedMaxSize=int(maxCount * 0.8);
391}
392
393template <class Msg>
394void
396{
397 Lock lock(mMutex); (void)lock;
398
399 mMaxDurationSecs=maxSecs;
400}
401
402
403} // namespace resip
404
405#endif
406
407/* ====================================================================
408 * The Vovida Software License, Version 1.0
409 *
410 * Redistribution and use in source and binary forms, with or without
411 * modification, are permitted provided that the following conditions
412 * are met:
413 *
414 * 1. Redistributions of source code must retain the above copyright
415 * notice, this list of conditions and the following disclaimer.
416 *
417 * 2. Redistributions in binary form must reproduce the above copyright
418 * notice, this list of conditions and the following disclaimer in
419 * the documentation and/or other materials provided with the
420 * distribution.
421 *
422 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
423 * and "Vovida Open Communication Application Library (VOCAL)" must
424 * not be used to endorse or promote products derived from this
425 * software without prior written permission. For written
426 * permission, please contact vocal@vovida.org.
427 *
428 * 4. Products derived from this software may not be called "VOCAL", nor
429 * may "VOCAL" appear in their name, without prior written
430 * permission of Vovida Networks, Inc.
431 *
432 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
433 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
434 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
435 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
436 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
437 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
438 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
439 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
440 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
441 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
442 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
443 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
444 * DAMAGE.
445 *
446 * ====================================================================
447 *
448 * This software consists of voluntary contributions made by Vovida
449 * Networks, Inc. and many individuals on behalf of Vovida Networks,
450 * Inc. For more information on Vovida Networks, Inc., please see
451 * <http://www.vovida.org/>.
452 *
453 */
#define max(x, y)
#define resip_assert(x)
This file needs to work for both C and C++ code.
static Mimes empty
static void usage(void)
Definition adig.c:755
The base class from which various templated Fifo classes are derived.
virtual unsigned int size() const
get the current size of the fifo.
Condition mCondition
condition for waiting on new queue items
bool empty() const
is the queue empty?
Messages mFifo
container for FIFO items
Mutex mMutex
access serialization lock
virtual void setCountDepthTolerance(unsigned int max)
set the count depth tolerance of the FIFO
bool wouldAccept(DepthUsage usage) const
would an add called now work?
virtual size_t getCountDepth() const
gets the depth of the FIFO
Msg * getNext()
Returns the first message available.
virtual void setTimeDepthTolerance(unsigned int maxSecs)
set the time depth tolerance of the FIFO
virtual time_t getTimeDepth() const
Return the time depth of the queue.
TimeLimitFifo(unsigned int maxDurationSecs, unsigned int maxSize)
After it runs out of the lesser of these limits it will start to refuse messages.
Msg * getNext(int ms)
Returns the first message available within a time limit.
void clear()
removes all items from the FIFO
virtual size_t getCountDepthTolerance() const
returns the FIFO count depth tolerance
virtual time_t timeDepth() const
Return the time depth of the queue.
virtual time_t getTimeDepthTolerance() const
returns the FIFO time depth tolerance
bool add(Msg *msg, DepthUsage usage)
Add a message to the fifo.
wraps an object with a timestamp
const Payload & getMsg() const
void setMsg(const Payload &pMsg)
const time_t & getTime() const
Timestamped(const Payload &msg, time_t n)
dcm! – add UnusedChecking(_enum) below;
std::unique_lock< Mutex > Lock
A convenience class to lock a Lockable (such as a Mutex) on object creation, and unlock on destructio...
Definition Lock.hxx:28