reSIProcate/DialogUsageManager  9694
ServerPublication.cxx
Go to the documentation of this file.
00001 #include "resip/dum/DialogUsageManager.hxx"
00002 #include "resip/dum/ServerPublication.hxx"
00003 #include "resip/dum/PublicationHandler.hxx"
00004 #include "resip/dum/ServerSubscription.hxx"
00005 #include "resip/dum/SubscriptionHandler.hxx"
00006 #include "resip/stack/Helper.hxx"
00007 #include "resip/stack/SecurityAttributes.hxx"
00008 #include "rutil/WinLeakCheck.hxx"
00009 
00010 using namespace resip;
00011 
00012 ServerPublication::ServerPublication(DialogUsageManager& dum,  
00013                                      const Data& etag,
00014                                      const SipMessage& msg)
00015    : BaseUsage(dum),
00016      mLastResponse(new SipMessage), 
00017      mEtag(etag),
00018      mEventType(msg.header(h_Event).value()),
00019      mTimerSeq(0)
00020 {
00021 }
00022 
00023 ServerPublication::~ServerPublication()
00024 {
00025    mDum.mServerPublications.erase(getEtag());
00026 }
00027 
00028 ServerPublicationHandle 
00029 ServerPublication::getHandle()
00030 {
00031    return ServerPublicationHandle(mDum, getBaseHandle().getId());
00032 }
00033 
00034 const Data&
00035 ServerPublication::getEtag() const
00036 {
00037    return mEtag;
00038 }
00039 
00040 const Data&
00041 ServerPublication::getDocumentKey() const
00042 {
00043    return mDocumentKey;
00044 }
00045 
00046 Data
00047 ServerPublication::getPublisher() const
00048 {
00049    return mLastRequest.header(h_From).uri().getAor();
00050 }
00051 
00052 void
00053 ServerPublication::updateMatchingSubscriptions()
00054 {
00055    Data key = mEventType + mLastRequest.header(h_RequestLine).uri().getAor();
00056    std::pair<DialogUsageManager::ServerSubscriptions::iterator,DialogUsageManager::ServerSubscriptions::iterator> subs;
00057    subs = mDum.mServerSubscriptions.equal_range(key);
00058    
00059    ServerSubscriptionHandler* handler = mDum.getServerSubscriptionHandler(mEventType);
00060    for (DialogUsageManager::ServerSubscriptions::iterator i=subs.first; i!=subs.second; ++i)
00061    {
00062       handler->onPublished(i->second->getHandle(), 
00063                            getHandle(), 
00064                            mLastBody.mContents.get(), 
00065                            mLastBody.mAttributes.get());
00066    }
00067    mLastBody.mContents.reset();
00068    mLastBody.mAttributes.reset();
00069 }
00070 
00071 
00072 SharedPtr<SipMessage>
00073 ServerPublication::accept(int statusCode)
00074 {
00075    Helper::makeResponse(*mLastResponse, mLastRequest, statusCode);
00076    mLastResponse->header(h_Expires).value() = mExpires;
00077 
00078    updateMatchingSubscriptions();
00079 
00080    return mLastResponse;   
00081 }
00082 
00083 SharedPtr<SipMessage>
00084 ServerPublication::reject(int statusCode)
00085 {
00086    Helper::makeResponse(*mLastResponse, mLastRequest, statusCode);
00087    mLastResponse->header(h_Expires).value() = mExpires;
00088    return mLastResponse;  
00089 }
00090 
00091 void 
00092 ServerPublication::end()
00093 {
00094    delete this;
00095 }
00096 
00097 void 
00098 ServerPublication::dispatch(const SipMessage& msg)
00099 {
00100    assert(msg.isRequest());
00101    ServerPublicationHandler* handler = mDum.getServerPublicationHandler(mEventType);
00102    mLastRequest = msg;
00103    mExpires = 3600; //bad
00104    if (msg.exists(h_Expires))
00105    {
00106       mExpires = msg.header(h_Expires).value();
00107    }
00108    if (msg.exists(h_SIPIfMatch))
00109    {      
00110       if (mExpires == 0)
00111       {
00112          handler->onRemoved(getHandle(), mEtag, msg, mExpires);
00113          Helper::makeResponse(*mLastResponse, mLastRequest, 200);
00114          mLastResponse->header(h_Expires).value() = mExpires;
00115          mDum.send(mLastResponse);
00116          delete this;
00117          return;
00118       }
00119       mLastBody = Helper::extractFromPkcs7(msg, *mDum.getSecurity());
00120       if (msg.getContents())
00121       {
00122          handler->onUpdate(getHandle(), mEtag, msg, 
00123                            mLastBody.mContents.get(), 
00124                            mLastBody.mAttributes.get(), 
00125                            mExpires);
00126       }
00127       else
00128       {
00129          handler->onRefresh(getHandle(), mEtag, msg, 
00130                             mLastBody.mContents.get(), 
00131                             mLastBody.mAttributes.get(), 
00132                             mExpires);
00133       }
00134    }
00135    else
00136    {
00137       mLastBody = Helper::extractFromPkcs7(msg, *mDum.getSecurity());
00138       handler->onInitial(getHandle(), mEtag, msg, 
00139                          mLastBody.mContents.get(), 
00140                          mLastBody.mAttributes.get(), 
00141                          mExpires);
00142    }
00143 }
00144 
00145 void
00146 ServerPublication::dispatch(const DumTimeout& msg)
00147 {
00148    if (msg.seq() == mTimerSeq)
00149    {
00150       ServerPublicationHandler* handler = mDum.getServerPublicationHandler(mEventType);
00151       handler->onExpired(getHandle(), mEtag);
00152       delete this;
00153    }
00154 }
00155 
00156 void 
00157 ServerPublication::send(SharedPtr<SipMessage> response)
00158 {
00159    assert(response->isResponse());
00160    response->header(h_SIPETag).value() = mEtag;
00161    mDum.send(response);
00162    if (response->header(h_StatusLine).statusCode() >= 300)
00163    {
00164       delete this;
00165    }
00166    else
00167    {
00168       mDum.addTimer(DumTimeout::Publication, response->header(h_Expires).value(), getBaseHandle(), ++mTimerSeq);
00169    }
00170 }
00171 
00172 EncodeStream& 
00173 ServerPublication::dump(EncodeStream& strm) const
00174 {
00175    strm << "ServerPublication " << mDocumentKey << " " << mEventType;
00176    return strm;
00177 }
00178    
00179 
00180 /* ====================================================================
00181  * The Vovida Software License, Version 1.0 
00182  * 
00183  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00184  * 
00185  * Redistribution and use in source and binary forms, with or without
00186  * modification, are permitted provided that the following conditions
00187  * are met:
00188  * 
00189  * 1. Redistributions of source code must retain the above copyright
00190  *    notice, this list of conditions and the following disclaimer.
00191  * 
00192  * 2. Redistributions in binary form must reproduce the above copyright
00193  *    notice, this list of conditions and the following disclaimer in
00194  *    the documentation and/or other materials provided with the
00195 
00196  *    distribution.
00197  * 
00198  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00199  *    and "Vovida Open Communication Application Library (VOCAL)" must
00200  *    not be used to endorse or promote products derived from this
00201  *    software without prior written permission. For written
00202  *    permission, please contact vocal@vovida.org.
00203  *
00204  * 4. Products derived from this software may not be called "VOCAL", nor
00205  *    may "VOCAL" appear in their name, without prior written
00206  *    permission of Vovida Networks, Inc.
00207  * 
00208  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00209  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00210  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00211  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00212  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00213  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00214  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00215  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00216  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00217  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00218  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00219  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00220  * DAMAGE.
00221  * 
00222  * ====================================================================
00223  * 
00224  * This software consists of voluntary contributions made by Vovida
00225  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00226  * Inc.  For more information on Vovida Networks, Inc., please see
00227  * <http://www.vovida.org/>.
00228  *
00229  */