reSIProcate/DialogUsageManager  9680
ClientPagerMessage.cxx
Go to the documentation of this file.
00001 
00002 #if defined(HAVE_CONFIG_H)
00003   #include "config.h"
00004 #endif
00005 
00006 #include "resip/stack/SipMessage.hxx"
00007 #include "resip/stack/MethodTypes.hxx"
00008 #include "resip/stack/TransactionUser.hxx"
00009 #include "resip/dum/PagerMessageCreator.hxx"
00010 #include "resip/dum/DialogUsageManager.hxx"
00011 #include "resip/dum/ClientPagerMessage.hxx"
00012 #include "resip/dum/PagerMessageHandler.hxx"
00013 #include "resip/dum/Dialog.hxx"
00014 #include "resip/dum/UsageUseException.hxx"
00015 #include "resip/dum/DumHelper.hxx"
00016 #include "rutil/Logger.hxx"
00017 #include "resip/stack/Helper.hxx"
00018 
00019 #ifdef USE_SSL
00020 #include "resip/stack/ssl/Security.hxx"
00021 #endif
00022 
00023 using namespace resip;
00024 
00025 #if(0)
00026 //  for appcliation behaves like ICQ
00027 app::onSendButtonClicked(im)
00028 {
00029     disableSendButton();
00030     mPageManager.page(im);
00031 }
00032 
00033 app::onSuccess(...)
00034 {
00035     enableSendButton();
00036 }
00037 
00038 app::onFailure(...)
00039 {
00040     reportFailueToUI();
00041     enableSendButton();
00042 }
00043 
00044 //  for application behaves like MSN
00045 app::onSendButtonClicked(im)
00046 {
00047     mPageManager.page(im);
00048 }
00049 
00050 app::onSuccess(...)
00051 {
00052     // most likely no-op
00053 }
00054 
00055 app::onFailure(...)
00056 {
00057     reportFailueToUI();
00058 }
00059 
00060 //  for application behaves like MSN but wants to limit number of pages queued.
00061 app::onSendButtonClicked(im)
00062 {
00063     if(mPageManager.msgQueued() > 5 - 1)
00064     {
00065         disableSendButton();
00066     }
00067     mPageManager.page(im);
00068 }
00069 
00070 app::onSuccess(...)
00071 {
00072     if(mPageManager.msgQueued() < 5)
00073     {
00074         enableSendButton();
00075     }
00076 }
00077 
00078 app::onFailure(...)
00079 {
00080     reportFailueToUI();
00081     if(mPageManager.msgQueued() < 5)
00082     {
00083         enableSendButton();
00084     }
00085 }
00086 #endif
00087 
00088 #define RESIPROCATE_SUBSYSTEM Subsystem::DUM
00089 
00090 ClientPagerMessageHandle
00091 ClientPagerMessage::getHandle()
00092 {
00093    return ClientPagerMessageHandle(mDum, getBaseHandle().getId());
00094 }
00095 
00096 ClientPagerMessage::ClientPagerMessage(DialogUsageManager& dum, DialogSet& dialogSet)
00097    : NonDialogUsage(dum, dialogSet),
00098      mRequest(dialogSet.getCreator()->getLastRequest()),
00099      mEnded(false)
00100 {
00101 }
00102 
00103 ClientPagerMessage::~ClientPagerMessage()
00104 {
00105    this->clearMsgQueued();
00106    mDialogSet.mClientPagerMessage = 0;
00107 }
00108 
00109 SipMessage&
00110 ClientPagerMessage::getMessageRequest()
00111 {
00112    return *mRequest;
00113 }
00114 
00115 void
00116 ClientPagerMessage::page(std::auto_ptr<Contents> contents,
00117                          DialogUsageManager::EncryptionLevel level)
00118 {
00119     assert(contents.get() != 0);
00120     bool do_page = mMsgQueue.empty();
00121     Item item;
00122     item.contents = contents.release();
00123     item.encryptionLevel = level;
00124     mMsgQueue.push_back(item);
00125     if(do_page)
00126     {
00127        this->pageFirstMsgQueued();
00128     }
00129 }
00130 
00131 class ClientPagerMessagePageCommand : public DumCommandAdapter
00132 {
00133 public:
00134    ClientPagerMessagePageCommand(ClientPagerMessage& clientPagerMessage, 
00135       std::auto_ptr<Contents> contents,
00136       DialogUsageManager::EncryptionLevel level)
00137       : mClientPagerMessage(clientPagerMessage),
00138         mContents(contents),
00139         mLevel(level)
00140    {
00141 
00142    }
00143 
00144    virtual void executeCommand()
00145    {
00146       mClientPagerMessage.page(mContents, mLevel);
00147    }
00148 
00149    virtual EncodeStream& encodeBrief(EncodeStream& strm) const
00150    {
00151       return strm << "ClientPagerMessagePageCommand";
00152    }
00153 private:
00154    ClientPagerMessage& mClientPagerMessage;
00155    std::auto_ptr<Contents> mContents;
00156    DialogUsageManager::EncryptionLevel mLevel;
00157 };
00158 
00159 void
00160 ClientPagerMessage::pageCommand(std::auto_ptr<Contents> contents,
00161                                 DialogUsageManager::EncryptionLevel level)
00162 {
00163    mDum.post(new ClientPagerMessagePageCommand(*this, contents, level));
00164 }
00165 
00166 void
00167 ClientPagerMessage::dispatch(const SipMessage& msg)
00168 {
00169    assert(msg.isResponse());
00170 
00171    ClientPagerMessageHandler* handler = mDum.mClientPagerMessageHandler;
00172    assert(handler);
00173 
00174    int code = msg.header(h_StatusLine).statusCode();
00175 
00176    DebugLog ( << "ClientPagerMessageReq::dispatch(msg)" << msg.brief() );
00177    {
00178       assert(mMsgQueue.empty() == false);
00179       if (code < 200)
00180       {
00181          DebugLog ( << "ClientPagerMessageReq::dispatch - encountered provisional response" << msg.brief() );
00182       }
00183       else if (code < 300)
00184       {
00185          if(mMsgQueue.empty() == false)
00186          {
00187             delete mMsgQueue.front().contents;
00188             mMsgQueue.pop_front();
00189             if(mMsgQueue.empty() == false)
00190             {
00191                this->pageFirstMsgQueued();
00192             }
00193 
00194             handler->onSuccess(getHandle(), msg);
00195          }
00196       }
00197       else
00198       {
00199          SipMessage errResponse;
00200          MsgQueue::iterator contents;
00201          for(contents = mMsgQueue.begin(); contents != mMsgQueue.end(); ++contents)
00202          {
00203             Contents* p = contents->contents;
00204             WarningLog ( << "Paging failed " << *p );
00205             Helper::makeResponse(errResponse, *mRequest, code);
00206             handler->onFailure(getHandle(), errResponse, std::auto_ptr<Contents>(p));
00207             contents->contents = 0;
00208          }
00209          mMsgQueue.clear();
00210       }
00211    }
00212 }
00213 
00214 void
00215 ClientPagerMessage::dispatch(const DumTimeout& timer)
00216 {
00217 }
00218 
00219 void
00220 ClientPagerMessage::end()
00221 {
00222    if(!mEnded)
00223    {
00224       mEnded = true;
00225       mDum.destroy(this);
00226    }
00227 }
00228 
00229 class ClientPagerMessageEndCommand : public DumCommandAdapter
00230 {
00231 public:
00232    ClientPagerMessageEndCommand(ClientPagerMessage& clientPagerMessage)
00233       : mClientPagerMessage(clientPagerMessage)
00234    {
00235    }
00236 
00237    virtual void executeCommand()
00238    {
00239       mClientPagerMessage.end();
00240    }
00241 
00242    virtual EncodeStream& encodeBrief(EncodeStream& strm) const
00243    {
00244       return strm << "ClientPagerMessageEndCommand";
00245    }
00246 private:
00247    ClientPagerMessage& mClientPagerMessage;
00248 };
00249 
00250 void
00251 ClientPagerMessage::endCommand()
00252 {
00253    mDum.post(new ClientPagerMessageEndCommand(*this));
00254 }
00255 
00256 size_t
00257 ClientPagerMessage::msgQueued () const
00258 {
00259    return  mMsgQueue.size();
00260 }
00261 
00262 void
00263 ClientPagerMessage::pageFirstMsgQueued ()
00264 {
00265    assert(mMsgQueue.empty() == false);
00266    mRequest->header(h_CSeq).sequence()++;
00267    mRequest->setContents(mMsgQueue.front().contents);
00268    DumHelper::setOutgoingEncryptionLevel(*mRequest, mMsgQueue.front().encryptionLevel);
00269    DebugLog(<< "ClientPagerMessage::pageFirstMsgQueued: " << *mRequest);
00270    mDum.send(mRequest);
00271 }
00272 
00273 void
00274 ClientPagerMessage::clearMsgQueued ()
00275 {
00276    MsgQueue::iterator   contents;
00277    for(contents = mMsgQueue.begin(); contents != mMsgQueue.end(); ++contents)
00278    {
00279       Contents* p = contents->contents;
00280       delete p;
00281    }
00282    mMsgQueue.clear();
00283 }
00284 
00285 EncodeStream& 
00286 ClientPagerMessage::dump(EncodeStream& strm) const
00287 {
00288    strm << "ClientPagerMessage queued: " << mMsgQueue.size();
00289    return strm;
00290 }
00291 
00292 
00293 
00294 /* ====================================================================
00295  * The Vovida Software License, Version 1.0
00296  *
00297  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00298  *
00299  * Redistribution and use in source and binary forms, with or without
00300  * modification, are permitted provided that the following conditions
00301  * are met:
00302  *
00303  * 1. Redistributions of source code must retain the above copyright
00304  *    notice, this list of conditions and the following disclaimer.
00305  *
00306  * 2. Redistributions in binary form must reproduce the above copyright
00307  *    notice, this list of conditions and the following disclaimer in
00308  *    the documentation and/or other materials provided with the
00309 
00310  *    distribution.
00311  *
00312  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00313  *    and "Vovida Open Communication Application Library (VOCAL)" must
00314  *    not be used to endorse or promote products derived from this
00315  *    software without prior written permission. For written
00316  *    permission, please contact vocal@vovida.org.
00317  *
00318  * 4. Products derived from this software may not be called "VOCAL", nor
00319  *    may "VOCAL" appear in their name, without prior written
00320  *    permission of Vovida Networks, Inc.
00321  *
00322  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00323  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00324  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00325  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00326  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00327  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00328  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00329  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00330  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00331  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00332  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00333  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00334  * DAMAGE.
00335  *
00336  * ====================================================================
00337  *
00338  * This software consists of voluntary contributions made by Vovida
00339  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00340  * Inc.  For more information on Vovida Networks, Inc., please see
00341  * <http://www.vovida.org/>.
00342  *
00343  */