reSIProcate/DialogUsageManager  9694
RegEventClient.cxx
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include "rutil/Logger.hxx"
00006 #include "resip/stack/SipMessage.hxx"
00007 #include "resip/stack/OctetContents.hxx"
00008 #include "resip/dum/MasterProfile.hxx"
00009 #include "resip/dum/ClientAuthManager.hxx"
00010 #include "resip/dum/ClientSubscription.hxx"
00011 
00012 #ifdef USE_SSL
00013 #include "resip/stack/ssl/Security.hxx"
00014 #endif
00015 
00016 #include "RegEventClient.hxx"
00017 
00018 using namespace resip;
00019 
00020 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00021 
00022 static Token regEvent("reg");
00023 
00024 AddAor::AddAor(RegEventClient& client, const resip::Uri& aor) : 
00025    mClient(client),
00026    mAor(aor)
00027 {
00028 }
00029 
00030 void
00031 AddAor::executeCommand()
00032 {
00033    //InfoLog (<< "Execute: " << *this);
00034    SharedPtr<SipMessage> sub = mClient.mDum.makeSubscription(resip::NameAddr(mAor), regEvent.value());
00035    mClient.mDum.send(sub);
00036 }
00037 
00038 resip::Message* 
00039 AddAor::clone() const
00040 {
00041    return new AddAor(mClient, mAor);
00042 }
00043 
00044 #ifdef RESIP_USE_STL_STREAMS
00045 std::ostream& 
00046 AddAor::encode(std::ostream& strm) const
00047 #else
00048 resip::ResipFastOStream&
00049 AddAor::encode(resip::ResipFastOStream& strm) const
00050 #endif
00051 {
00052    strm << "Add RegEvent watcher " << mAor;
00053    return strm;
00054 }
00055 
00056 #ifdef RESIP_USE_STL_STREAMS
00057 std::ostream& 
00058 AddAor::encodeBrief(std::ostream& strm) const
00059 #else
00060 resip::ResipFastOStream&
00061 AddAor::encodeBrief(resip::ResipFastOStream& strm) const
00062 #endif
00063 {
00064    return encode(strm);
00065 }
00066 
00067 
00068 RegEventClient::RegEventClient(SharedPtr<MasterProfile> profile) :
00069    mSecurity(0),
00070    mStack(mSecurity),
00071    mStackThread(mStack),
00072    mDum(mStack),
00073    mDumThread(mDum),
00074    mProfile(profile)
00075 {
00076    mDum.addTransport(UDP, 5060);
00077    mDum.addTransport(TCP, 5060);
00078 
00079    mProfile->addSupportedMethod(NOTIFY);
00080    mProfile->addAllowedEvent(regEvent);
00081    mProfile->validateAcceptEnabled() = false;
00082    mProfile->validateContentEnabled() = false;
00083    mProfile->setUserAgent("RFC3680-testUA");
00084    mDum.setMasterProfile(mProfile);
00085    
00086    std::auto_ptr<resip::ClientAuthManager> clam(new resip::ClientAuthManager());
00087    mDum.setClientAuthManager(clam);
00088    mDum.setClientRegistrationHandler(this);
00089    
00090    mDum.addClientSubscriptionHandler(regEvent.value(), this);
00091 }
00092 
00093 RegEventClient::~RegEventClient()
00094 {
00095 #ifdef USE_SSL
00096    delete mSecurity;
00097    mSecurity = 0;
00098 #endif
00099 }
00100 
00101 void
00102 RegEventClient::run()
00103 {
00104    mStackThread.run();
00105    mDumThread.run();
00106 }
00107 
00108 void
00109 RegEventClient::watchAor(const resip::Uri& aor)
00110 {
00111    AddAor* add = new AddAor(*this, aor);
00112    mDum.post(add);
00113 }
00114  
00115 const OctetContents* 
00116 toGenericContents(const SipMessage& notify)
00117 {
00118    assert(notify.getContents());
00119    
00120    const OctetContents* generic = dynamic_cast<const OctetContents*>(notify.getContents());
00121    assert(generic);
00122    
00123    if (generic && 
00124        generic->getType().type() == "application" && 
00125        generic->getType().subType() == "reginfo+xml")
00126    {
00127 #if 0
00128       TiXmlDocument doc;
00129       doc.parse(generic->text().c_str());
00130       if (doc.Error())
00131       {
00132          WarningLog (<< "Error parsing doc: " << doc.Value() << " " << doc.ErrorDesc());
00133          onRegEventError(generic->text());
00134       }
00135       else
00136       {
00137          
00138       }
00139 #endif
00140 
00141       return generic;
00142    }
00143    else
00144    {
00145       return 0;
00146    }
00147 }
00148 
00149 // Client must call acceptUpdate or rejectUpdate for any onUpdateFoo
00150 
00151 void 
00152 RegEventClient::onNewSubscription(ClientSubscriptionHandle h, const SipMessage& notify)
00153 {
00154    InfoLog (<< "Got subscription " << notify.brief());
00155 }
00156 
00157 void 
00158 RegEventClient::onUpdatePending(ClientSubscriptionHandle h, const SipMessage& notify, bool outOfOrder)
00159 {
00160    h->acceptUpdate();
00161    const OctetContents* generic = toGenericContents(notify);
00162    if (generic)
00163    {
00164       onRegEvent(h->getDocumentKey(), generic->octets());
00165    }
00166 }
00167 
00168 void 
00169 RegEventClient::onUpdateActive(ClientSubscriptionHandle h, const SipMessage& notify, bool outOfOrder)
00170 {
00171    h->acceptUpdate();
00172    const OctetContents* generic = toGenericContents(notify);
00173    if (generic)
00174    {
00175       onRegEvent(h->getDocumentKey(), generic->octets());
00176    }
00177 }
00178 
00179 void 
00180 RegEventClient::onUpdateExtension(ClientSubscriptionHandle h, const SipMessage& notify, bool outOfOrder)
00181 {
00182    h->acceptUpdate();
00183 }
00184 
00185 int 
00186 RegEventClient::onRequestRetry(ClientSubscriptionHandle, int retrySeconds, const SipMessage& notify)
00187 {
00188    return -1;
00189 }
00190       
00191 void 
00192 RegEventClient::onTerminated(ClientSubscriptionHandle, const SipMessage* msg)
00193 {
00194    WarningLog (<< "Subscription terminated " << (msg ? "with message " : " with no message"));
00195    if(msg)
00196    {
00197       WarningLog(<< msg->brief());
00198    }
00199 }
00200 
00201