reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
/home/runner/work/resiprocate/resiprocate/rutil/TimeLimitFifo.hxx

A templated, threadsafe message-queue class that enforces constraints on time-depth.

A templated, threadsafe message-queue class that enforces constraints on time-depth.

TimeLimitFifo<Foo> tlf(5, 10); // 5 seconds or 10 count limit
//
// where Foo is defined as:
// class Foo
// {
// public:
// Foo(const Data& val) : mVal(val) {}
// Data mVal;
// };
//
resip_assert(tlf.empty());
resip_assert(tlf.size() == 0);
resip_assert(tlf.timeDepth() == 0);
c = tlf.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);
// no longer empty
resip_assert(!tlf.empty());
resip_assert(tlf.size() == 1);
resip_assert(tlf.timeDepth() == 0);
sleepMs(2000);
// still not empty
resip_assert(!tlf.empty());
resip_assert(tlf.size() == 1);
resip_assert(tlf.timeDepth() > 1);
if (tlf.messageAvailable())
delete tlf.getNext();
// Foo* fp = tlf.getNext();
// resip_assert (fp->mVal == "first")
// delete fp ;
// now its empty
resip_assert(tlf.empty());
resip_assert(tlf.size() == 0);
resip_assert(tlf.timeDepth() == 0);
#define resip_assert(x)
This file needs to work for both C and C++ code.
#define sleepMs(t)
Definition Server.hxx:16
void list()
Definition userAdmin.cxx:66
#ifndef RESIP_TimeLimitFifo_hxx
#define RESIP_TimeLimitFifo_hxx
#include <memory>
#include <iostream>
#if defined( WIN32 )
#include <time.h>
#endif
// efficiency note: use a circular buffer to avoid list node allocation
// what happens to timers that can't be queued?
namespace resip
{
template<typename Payload>
class Timestamped
{
public:
Timestamped(const Payload& msg, time_t n)
: mMsg(msg),
mTime(n)
{}
inline const Payload& getMsg() const { return mMsg;}
inline void setMsg(const Payload& pMsg) { mMsg = pMsg;}
inline const time_t& getTime() const { return mTime;}
private:
Payload mMsg;
time_t mTime;
};
template <class Msg>
class TimeLimitFifo : public AbstractFifo< Timestamped<Msg*> >
{
public:
TimeLimitFifo(unsigned int maxDurationSecs,
unsigned int maxSize);
virtual ~TimeLimitFifo();
using AbstractFifo< Timestamped<Msg*> >::mFifo;
using AbstractFifo< Timestamped<Msg*> >::mMutex;
using AbstractFifo< Timestamped<Msg*> >::mCondition;
using AbstractFifo< Timestamped<Msg*> >::empty;
using AbstractFifo< Timestamped<Msg*> >::size;
using AbstractFifo< Timestamped<Msg*> >::onMessagePushed;
bool add(Msg* msg, DepthUsage usage);
Msg* getNext();
Msg* getNext(int ms);
virtual time_t timeDepth() const;
void clear();
virtual size_t getCountDepth() const;
virtual time_t getTimeDepth() const;
virtual size_t getCountDepthTolerance() const;
virtual time_t getTimeDepthTolerance() const;
virtual void setCountDepthTolerance(unsigned int max);
virtual void setTimeDepthTolerance(unsigned int maxSecs);
private:
time_t timeDepthInternal() const;
inline bool wouldAcceptInteral(DepthUsage usage) const;
TimeLimitFifo(const TimeLimitFifo& rhs);
TimeLimitFifo& operator=(const TimeLimitFifo& rhs);
time_t mMaxDurationSecs;
unsigned int mMaxSize;
unsigned int mUnreservedMaxSize;
};
template <class Msg>
TimeLimitFifo<Msg>::TimeLimitFifo(unsigned int maxDurationSecs,
unsigned int maxSize)
: AbstractFifo< Timestamped<Msg*> >(),
mMaxDurationSecs(maxDurationSecs),
mMaxSize(maxSize),
mUnreservedMaxSize((int)((maxSize*8)/10)) // !dlb! random guess
{}
template <class Msg>
{
clear();
}
template <class Msg>
bool
DepthUsage usage)
{
Lock lock(mMutex); (void)lock;
if (wouldAcceptInteral(usage))
{
time_t n = time(0);
mFifo.push_back(Timestamped<Msg*>(msg, n));
onMessagePushed(1);
mCondition.notify_one();
return true;
}
else
{
return false;
}
}
template <class Msg>
bool
{
Lock lock(mMutex); (void)lock;
return wouldAcceptInteral(usage);
}
template <class Msg>
Msg*
{
Timestamped<Msg*> tm(AbstractFifo< Timestamped<Msg*> >::getNext());
return tm.getMsg();
}
template <class Msg>
Msg*
{
Timestamped<Msg*> tm(0,0);
if(AbstractFifo< Timestamped<Msg*> >::getNext(ms, tm))
{
return tm.getMsg();
}
return 0;
}
template <class Msg>
time_t
TimeLimitFifo<Msg>::timeDepthInternal() const
{
if(mFifo.empty())
{
return 0;
}
return time(0) - mFifo.front().getTime();
}
template <class Msg>
bool
TimeLimitFifo<Msg>::wouldAcceptInteral(DepthUsage usage) const
{
if ((mMaxSize != 0 &&
mFifo.size() >= mMaxSize))
{
return false;
}
if (usage == InternalElement)
{
return true;
}
if (mUnreservedMaxSize != 0 &&
mFifo.size() >= mUnreservedMaxSize)
{
return false;
}
if (usage == IgnoreTimeDepth)
{
return true;
}
resip_assert(usage == EnforceTimeDepth);
if (mFifo.size() == 0 ||
mMaxDurationSecs == 0 ||
timeDepthInternal() < mMaxDurationSecs)
{
return true;
}
return false;
}
template <class Msg>
time_t
{
Lock lock(mMutex); (void)lock;
return timeDepthInternal();
}
template <class Msg>
void
{
Lock lock(mMutex); (void)lock;
while (!mFifo.empty())
{
delete mFifo.front().getMsg();
mFifo.pop_front();
}
}
template <class Msg>
size_t
{
return size();
}
template <class Msg>
size_t
{
return mUnreservedMaxSize;
}
template <class Msg>
time_t
{
return timeDepth();
}
template <class Msg>
time_t
{
return mMaxDurationSecs;
}
template <class Msg>
void
{
Lock lock(mMutex); (void)lock;
mUnreservedMaxSize=int(maxCount * 0.8);
}
template <class Msg>
void
{
Lock lock(mMutex); (void)lock;
mMaxDurationSecs=maxSecs;
}
} // namespace resip
#endif
/* ====================================================================
* The Vovida Software License, Version 1.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The names "VOCAL", "Vovida Open Communication Application Library",
* and "Vovida Open Communication Application Library (VOCAL)" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact vocal@vovida.org.
*
* 4. Products derived from this software may not be called "VOCAL", nor
* may "VOCAL" appear in their name, without prior written
* permission of Vovida Networks, Inc.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
* NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
* NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
* IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* ====================================================================
*
* This software consists of voluntary contributions made by Vovida
* Networks, Inc. and many individuals on behalf of Vovida Networks,
* Inc. For more information on Vovida Networks, Inc., please see
* <http://www.vovida.org/>.
*
*/
#define max(x, y)
static Mimes empty
static void usage(void)
Definition adig.c:755
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.
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.
const Payload & getMsg() const
void setMsg(const Payload &pMsg)
const time_t & getTime() const
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