reSIProcate/stack  9694
TupleMarkManager.cxx
Go to the documentation of this file.
00001 #include "resip/stack/TupleMarkManager.hxx"
00002 
00003 #include "resip/stack/MarkListener.hxx"
00004 #include "rutil/Lock.hxx"
00005 #include "rutil/Timer.hxx"
00006 
00007 namespace resip
00008 {
00009 
00010 TupleMarkManager::MarkType 
00011 TupleMarkManager::getMarkType(const Tuple& tuple)
00012 {
00013    ListEntry entry(tuple,0);
00014    resip::Lock g(mListMutex);
00015    TupleList::iterator i=mList.find(entry);
00016    
00017    if(i!=mList.end())
00018    {
00019       UInt64 now=Timer::getTimeMs();
00020       if(i->first.mExpiry > now)
00021       {
00022          return i->second;
00023       }
00024       else
00025       {
00026          mList.erase(i);
00027          // ?bwc? Should we do this?
00028          UInt64 expiry = 0;
00029          MarkType mark = OK;
00030          notifyListeners(tuple,expiry,mark);
00031       }
00032    }
00033    
00034    return OK;
00035 }
00036 
00037 void TupleMarkManager::mark(const Tuple& tuple,UInt64 expiry,MarkType mark)
00038 {
00039    // .amr. Notify listeners first so they can change the entry if they want
00040    notifyListeners(tuple,expiry,mark);
00041    ListEntry entry(tuple,expiry);
00042    resip::Lock g(mListMutex);
00043    mList[entry]=mark;
00044 }
00045 
00046 void TupleMarkManager::registerMarkListener(MarkListener* listener)
00047 {
00048    mListeners.insert(listener);
00049 }
00050 
00051 void TupleMarkManager::unregisterMarkListener(MarkListener* listener)
00052 {
00053    mListeners.erase(listener);
00054 }
00055 
00056 void
00057 TupleMarkManager::notifyListeners(const resip::Tuple& tuple, UInt64& expiry, MarkType& mark)
00058 {
00059    for(Listeners::iterator i = mListeners.begin(); i!=mListeners.end(); ++i)
00060    {
00061       (*i)->onMark(tuple,expiry,mark);
00062    }
00063 }
00064 
00065 TupleMarkManager::ListEntry::ListEntry(const Tuple& tuple, UInt64 expiry)
00066    : mTuple(tuple),
00067    mExpiry(expiry)
00068 {}
00069 
00070 TupleMarkManager::ListEntry::ListEntry(const TupleMarkManager::ListEntry& orig)
00071    : mTuple(orig.mTuple),
00072    mExpiry(orig.mExpiry)
00073 {}
00074 
00075 TupleMarkManager::ListEntry::~ListEntry()
00076 {}
00077 
00078 bool 
00079 TupleMarkManager::ListEntry::operator<(const TupleMarkManager::ListEntry& rhs) const
00080 {
00081    if(mTuple < rhs.mTuple)
00082    {
00083       return true;
00084    }
00085    else if(rhs.mTuple < mTuple)
00086    {
00087       return false;
00088    }
00089    
00090    return mTuple.getTargetDomain() < rhs.mTuple.getTargetDomain();
00091 }
00092 
00093 bool 
00094 TupleMarkManager::ListEntry::operator>(const TupleMarkManager::ListEntry& rhs) const
00095 {
00096    if(rhs.mTuple < mTuple)
00097    {
00098       return true;
00099    }
00100    else if(mTuple < rhs.mTuple)
00101    {
00102       return false;
00103    }
00104    
00105    return mTuple.getTargetDomain() > rhs.mTuple.getTargetDomain();
00106 }
00107 
00108 bool 
00109 TupleMarkManager::ListEntry::operator==(const TupleMarkManager::ListEntry& rhs) const
00110 {
00111    return (mTuple==rhs.mTuple && mTuple.getTargetDomain()==rhs.mTuple.getTargetDomain());
00112 }
00113 
00114 
00115 }
00116 
00117 /* ====================================================================
00118  * The Vovida Software License, Version 1.0 
00119  * 
00120  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00121  * 
00122  * Redistribution and use in source and binary forms, with or without
00123  * modification, are permitted provided that the following conditions
00124  * are met:
00125  * 
00126  * 1. Redistributions of source code must retain the above copyright
00127  *    notice, this list of conditions and the following disclaimer.
00128  * 
00129  * 2. Redistributions in binary form must reproduce the above copyright
00130  *    notice, this list of conditions and the following disclaimer in
00131  *    the documentation and/or other materials provided with the
00132  *    distribution.
00133  * 
00134  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00135  *    and "Vovida Open Communication Application Library (VOCAL)" must
00136  *    not be used to endorse or promote products derived from this
00137  *    software without prior written permission. For written
00138  *    permission, please contact vocal@vovida.org.
00139  *
00140  * 4. Products derived from this software may not be called "VOCAL", nor
00141  *    may "VOCAL" appear in their name, without prior written
00142  *    permission of Vovida Networks, Inc.
00143  * 
00144  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00145  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00146  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00147  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00148  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00149  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00150  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00151  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00152  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00153  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00154  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00155  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00156  * DAMAGE.
00157  * 
00158  * ====================================================================
00159  * 
00160  * This software consists of voluntary contributions made by Vovida
00161  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00162  * Inc.  For more information on Vovida Networks, Inc., please see
00163  * <http://www.vovida.org/>.
00164  *
00165  */