[asterisk-scf-commits] asterisk-scf/integration/sip.git branch "queue-shutdown" updated.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Wed Jun 8 09:58:18 CDT 2011


branch "queue-shutdown" has been updated
       via  e8b826ae01d1de3261627a3f75ab1bca6b337ce4 (commit)
      from  c75f3b9b6d21058d881faf51736b4221522cc7d3 (commit)

Summary of changes:
 src/PJSipSessionModule.cpp      |    8 +-
 src/PJSipSessionModule.cpp.orig | 1355 ---------------------------------------
 src/SipSession.cpp              |    9 +-
 3 files changed, 6 insertions(+), 1366 deletions(-)
 delete mode 100644 src/PJSipSessionModule.cpp.orig


- Log -----------------------------------------------------------------
commit e8b826ae01d1de3261627a3f75ab1bca6b337ce4
Author: Mark Michelson <mmichelson at digium.com>
Date:   Wed Jun 8 09:57:33 2011 -0500

    Make adjustments based on review feedback.

diff --git a/src/PJSipSessionModule.cpp b/src/PJSipSessionModule.cpp
index aeb6d24..b4bdd69 100644
--- a/src/PJSipSessionModule.cpp
+++ b/src/PJSipSessionModule.cpp
@@ -1617,7 +1617,7 @@ void SessionWork::workAdded(const QueueBasePtr&, Ice::Long, bool wasEmpty)
         {
             mThreadPoolQueue->enqueueWork(this);
         }
-        catch (ShuttingDown)
+        catch (const ShuttingDown&)
         {
             lg(Warning) << "Queue has shut down. Not enqueueing task.";
         }
@@ -1630,7 +1630,7 @@ void SessionWork::workResumable(const QueueBasePtr&)
     {
         mThreadPoolQueue->enqueueWork(this);
     }
-    catch (ShuttingDown)
+    catch (const ShuttingDown&)
     {
         lg(Warning) << "Queue has shut down. Not enqueueing task.";
     }
@@ -1654,7 +1654,7 @@ void SessionWork::execute()
             // no-op
         }
     }
-    catch (ShuttingDown)
+    catch (const ShuttingDown&)
     {
         lg(Warning) << "Queue has shut down. Not enqueueing task.";
     }
@@ -1666,7 +1666,7 @@ void SessionWork::enqueueWork(const SuspendableWorkPtr& work)
     {
         mInternalQueue->enqueueWork(work);
     }
-    catch (ShuttingDown)
+    catch (const ShuttingDown&)
     {
         lg(Warning) << "Queue has shut down. Not enqueueing task.";
     }
diff --git a/src/PJSipSessionModule.cpp.orig b/src/PJSipSessionModule.cpp.orig
deleted file mode 100644
index 32ed923..0000000
--- a/src/PJSipSessionModule.cpp.orig
+++ /dev/null
@@ -1,1355 +0,0 @@
-/*
- * Asterisk SCF -- An open-source communications framework.
- *
- * Copyright (C) 2010, Digium, Inc.
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk SCF project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE.txt file
- * at the top of the source tree.
- */
-#include <IceUtil/UUID.h>
-
-#include <AsteriskSCF/Core/Endpoint/EndpointIf.h>
-#include <AsteriskSCF/Core/Routing/RoutingIf.h>
-#include <AsteriskSCF/SessionCommunications/SessionCommunicationsIf.h>
-#include <AsteriskSCF/Media/MediaIf.h>
-#include <AsteriskSCF/logger.h>
-#include <AsteriskSCF/WorkQueue.h>
-#include <AsteriskSCF/SuspendableWorkQueue.h>
-
-#include "PJSipSessionModule.h"
-#include "SipEndpoint.h"
-#include "SipEndpointFactory.h"
-#include "SipSession.h"
-#include "PJSipManager.h"
-#include "SipStateReplicator.h"
-
-using namespace AsteriskSCF::System::Logging;
-
-namespace
-{
-Logger lg = getLoggerFactory().getLogger("AsteriskSCF.SipSessionManager");
-//Constants used for AMI callback class construction when calling
-//SessionListenerPrx methods.
-const std::string RingingCallbackName("ringing");
-const std::string ProgressingCallbackName("progressing");
-const std::string ConnectedCallbackName("connected");
-}
-
-namespace AsteriskSCF
-{
-
-namespace SipSessionManager
-{
-
-using namespace AsteriskSCF::Core::Routing::V1;
-using namespace AsteriskSCF::Core::Endpoint::V1;
-using namespace AsteriskSCF::SessionCommunications::V1;
-using namespace AsteriskSCF::Media::V1;
-using namespace AsteriskSCF::SIP::V1;
-using namespace AsteriskSCF::System::ThreadPool::V1;
-using namespace AsteriskSCF::System::WorkQueue::V1;
-using namespace AsteriskSCF::WorkQueue;
-
-class RouteSessionCallback : public IceUtil::Shared
-{
-public:
-    RouteSessionCallback(pjsip_inv_session *inv_session, 
-                         pjsip_tx_data *tdata,
-                         const SipSessionPtr& session,
-                         const std::string& destination,
-                         const std::string& operationId)
-        : mInvSession(inv_session), 
-          mTData(tdata),
-          mSession(session),
-          mDestination(destination),
-          mOperationId(operationId)
-    { 
-    }
-
-    void callback(const Ice::AsyncResultPtr& r)
-    {
-        SessionRouterPrx router = SessionRouterPrx::uncheckedCast(r->getProxy());
-        try
-        {
-            router->end_routeSession(r);
-        }
-        catch (const DestinationNotFoundException &)
-        {
-            pjsip_inv_end_session(mInvSession, 404, NULL, &mTData);
-            pjsip_inv_send_msg(mInvSession, mTData);
-        }
-        catch (...)
-        {
-            pjsip_inv_end_session(mInvSession, 500, NULL, &mTData);
-            pjsip_inv_send_msg(mInvSession, mTData);
-        }
-    }
-private:
-    pjsip_inv_session *mInvSession;
-    pjsip_tx_data *mTData;
-    SipSessionPtr mSession;
-    std::string mDestination;
-    std::string mOperationId;
-};
-typedef IceUtil::Handle<RouteSessionCallback> RouteSessionCallbackPtr;
-
-class ConnectBridgedSessionsCallback : public IceUtil::Shared
-{
-public:
-    ConnectBridgedSessionsCallback(pjsip_inv_session *inv_session,
-                                   pjsip_tx_data *tdata, 
-                                   pjsip_transaction *tsx,
-                                   const SipSessionPtr& session,
-                                   const SipSessionPtr& otherSession,
-                                   const std::string& operationId)
-        : mInvSession(inv_session), 
-          mTData(tdata), 
-          mTsx(tsx),
-          mSession(session),
-          mOtherSession(otherSession),
-          mOperationId(operationId)
-
-    { 
-    }
-
-    void callback(const Ice::AsyncResultPtr &r)
-    {
-        SessionRouterPrx router = SessionRouterPrx::uncheckedCast(r->getProxy());
-        try
-        {
-            router->end_connectBridgedSessions(r);
-        }
-        catch (const std::exception &e)
-        {
-            lg(Debug) << "ConnectBridgedSessionsCallback sending 400 due to exception:  " << e.what();
-            pjsip_dlg_modify_response(mInvSession->dlg, mTData, 400, NULL);
-            pjsip_dlg_send_response(mInvSession->dlg, mTsx, mTData);
-            return;
-        }
-        pjsip_dlg_send_response(mInvSession->dlg, mTsx, mTData);
-        
-        Ice::Current current;
-        lg(Debug) << "ConnectBridgedSessionsCallback calling session->stop(). ";
-        mSession->stop(new ResponseCode(16), current);
-    }
-private:
-    pjsip_inv_session *mInvSession;
-    pjsip_tx_data *mTData;
-    pjsip_transaction *mTsx;
-    SipSessionPtr mSession;
-    SipSessionPtr mOtherSession;
-    std::string mOperationId;
-};
-
-typedef IceUtil::Handle<ConnectBridgedSessionsCallback> ConnectBridgedSessionsCallbackPtr;
-
-class ConnectBridgedSessionsWithDestinationCallback : public IceUtil::Shared
-{
-public:
-    ConnectBridgedSessionsWithDestinationCallback(pjsip_inv_session *inv_session, 
-                                                  pjsip_tx_data *tdata, 
-                                                  pjsip_transaction *tsx,
-                                                  SipSessionPtr session, 
-                                                  const std::string& target,
-                                                  const std::string& operationId)
-        : mInvSession(inv_session), 
-          mTData(tdata), 
-          mTsx(tsx),
-          mSession(session), 
-          mTarget(target),
-          mOperationId(operationId)
-    { 
-    }
-
-    void callback(const Ice::AsyncResultPtr &r)
-    {
-        SessionRouterPrx router = SessionRouterPrx::uncheckedCast(r->getProxy());
-        try
-        {
-            router->end_connectBridgedSessions(r);
-        }
-        catch (const AsteriskSCF::Core::Routing::V1::DestinationNotFoundException &)
-        {
-            lg(Debug) << "ConnectBridgedSessionsWithDestination sending 404 due to destination not found for target: "
-                      << mTarget;
-
-            pjsip_dlg_modify_response(mInvSession->dlg, mTData, 400, NULL);
-            pjsip_dlg_send_response(mInvSession->dlg, mTsx, mTData);
-            return;
-        }
-        catch (const std::exception &e)
-        {
-            lg(Debug) << "ConnectBridgedSessionsWithDestination sending 400 due to exception:  " << e.what();
-            pjsip_dlg_modify_response(mInvSession->dlg, mTData, 400, NULL);
-            pjsip_dlg_send_response(mInvSession->dlg, mTsx, mTData);
-            return;
-        }
-        pjsip_dlg_send_response(mInvSession->dlg, mTsx, mTData);
-        
-        Ice::Current current;
-        lg(Debug) << "ConnectBridgedSessionsWithDestination calling session->stop(). ";
-        mSession->stop(new ResponseCode(16), current);
-    }
-private:
-    pjsip_inv_session *mInvSession;
-    pjsip_tx_data *mTData;
-    pjsip_transaction *mTsx;
-    SipSessionPtr mSession;
-    std::string mTarget;
-    std::string mOperationId;
-};
-
-typedef IceUtil::Handle<ConnectBridgedSessionsWithDestinationCallback> ConnectBridgedSessionsWithDestinationCallbackPtr;
-
-class ListenerCallback : public IceUtil::Shared
-{
-public:
-    ListenerCallback(const std::string& name) : mCallbackName(name) {}
-    void failure(const Ice::Exception& ex)
-    {
-        lg(Error) << "Ice exception when attempting to relate " << mCallbackName << " state: " << ex.what();
-    }
-private:
-    const std::string mCallbackName;
-};
-
-typedef IceUtil::Handle<ListenerCallback> ListenerCallbackPtr;
-
-PJSipSessionModInfo::PJSipSessionModInfo(pjsip_inv_session *inv_session,
-    const SipSessionPtr& session) :
-    mSessionState(new SipSessionStateItem),
-    mInviteState(new SipInviteSessionStateItem),
-    mNeedsReplication(true),
-    mNeedsRemoval(false),
-    mSession(session)
-{
-    mSessionState->key = IceUtil::generateUUID();
-    mInviteState->key = IceUtil::generateUUID();
-    mSessionState->mSessionId = mSessionState->key;
-    mInviteState->mSessionId = mSessionState->key;
-    updateSessionState(inv_session);
-}
-
-PJSipSessionModInfo::~PJSipSessionModInfo()
-{
-    if (mSession)
-    {
-        mSession->destroy();
-        mSession = 0;
-    }
-}
-
-void PJSipSessionModInfo::updateSessionState(pjsip_inv_session *inv_session)
-{
-    boost::unique_lock<boost::shared_mutex> lock(mLock);
-    if (mSession)
-    {
-        mSessionState->mEndpointName = mSession->getEndpoint()->getName();
-        mSessionState->mSessionObjectId = mSession->getSessionProxy()->ice_getIdentity();
-        mSessionState->mMediaSessionObjectId = mSession->getMediaSessionProxy()->ice_getIdentity();
-        mSessionState->mSources = mSession->getSources();
-        mSessionState->mSinks = mSession->getSinks();
-        mSessionState->mMediaSession = mSession->getHiddenMediaSession();
-        mSessionState->mListeners = mSession->getListeners();
-        try
-        {
-            mSessionState->mBridge = mSession->getBridge();
-        }
-        catch (...)
-        {
-            mSessionState->mBridge = 0;
-        }
-    }
-
-    //Now we get stuff from the inv_session itself.
-    mInviteState->mCancelling = inv_session->cancelling == PJ_TRUE ? true : false;
-    mInviteState->mPendingCancel = inv_session->pending_cancel == PJ_TRUE ? true : false;
-    mInviteState->mCause = inv_session->cause;
-    mInviteState->mCauseText = std::string(pj_strbuf(&inv_session->cause_text), pj_strlen(&inv_session->cause_text));
-    mInviteState->mNotify = inv_session->notify == PJ_TRUE ? true : false;
-    mInviteState->mLastAckCseq = inv_session->last_ack_cseq;
-    mInviteState->mCurrentState = inviteStateTranslate(inv_session->state);
-    mNeedsReplication = true;
-}
-
-SipSessionPtr PJSipSessionModInfo::getSessionPtr()
-{
-    return mSession;
-}
-
-void PJSipSessionModInfo::setSessionPtr(const SipSessionPtr& sessionPtr)
-{
-    mSession = sessionPtr;
-}
-
-InviteSessionState PJSipSessionModInfo::inviteStateTranslate(pjsip_inv_state state)
-{
-    InviteSessionState retState;
-    switch (state)
-    {
-    case PJSIP_INV_STATE_NULL:
-        retState = InviteSessionStateNull;
-        break;
-    case PJSIP_INV_STATE_CALLING:
-        retState = InviteSessionStateCalling;
-        break;
-    case PJSIP_INV_STATE_INCOMING:
-        retState = InviteSessionStateIncoming;
-        break;
-    case PJSIP_INV_STATE_EARLY:
-        retState = InviteSessionStateEarly;
-        break;
-    case PJSIP_INV_STATE_CONNECTING:
-        retState = InviteSessionStateConnecting;
-        break;
-    case PJSIP_INV_STATE_CONFIRMED:
-        retState = InviteSessionStateConfirmed;
-        break;
-    case PJSIP_INV_STATE_DISCONNECTED:
-        retState = InviteSessionStateDisconnected;
-        break;
-    default:
-        lg(Warning) << "Unknwon PJSIP INVITE state encountered: " << state;
-        retState = InviteSessionStateNull;
-        break;
-    }
-    return retState;
-}
-
-void PJSipSessionModule::replicateState(PJSipDialogModInfo *dlgInfo, PJSipTransactionModInfo *tsxInfo,
-    PJSipSessionModInfo *sessionInfo)
-{
-    SipStateItemSeq setItems;
-    Ice::StringSeq removeItems;
-
-    lg(Debug) << "========== Begin State Replication Dump ==========";
-
-    if (dlgInfo)
-    {
-        lg(Debug) << "--- Begin Dialog " << dlgInfo->mDialogState->key;
-        lg(Debug) << "Callid: " << dlgInfo->mDialogState->mCallId;
-        lg(Debug) << "Is Dialog Established: " << dlgInfo->mDialogState->mIsDialogEstablished;
-        lg(Debug) << "Is Secure: " << dlgInfo->mDialogState->mIsSecure;
-        lg(Debug) << "Local CSeq: " << dlgInfo->mDialogState->mLocalCSeq;
-        lg(Debug) << "Local URI: " << dlgInfo->mDialogState->mLocalUri;
-        lg(Debug) << "Remote CSeq: " << dlgInfo->mDialogState->mRemoteCSeq;
-        lg(Debug) << "Remote URI: " << dlgInfo->mDialogState->mRemoteUri;
-        lg(Debug) << "Transport: " << dlgInfo->mDialogState->mTransport;
-        lg(Debug) << "UAC Has 2xx: " << dlgInfo->mDialogState->mUacHas2xx;
-        lg(Debug) << "Is Uac: " << dlgInfo->mDialogState->mIsUac;
-        if (dlgInfo->mNeedsRemoval == true)
-        {
-            lg(Debug) << "Removing dialog";
-            removeItems.push_back(dlgInfo->mDialogState->key);
-        }
-        else if (dlgInfo->mNeedsReplication == true)
-        {
-            lg(Debug) << "Replicating dialog";
-            setItems.push_back(dlgInfo->mDialogState);
-            dlgInfo->mNeedsReplication = false;
-        }
-        lg(Debug) << "--- End Dialog " << dlgInfo->mDialogState->key;
-    }
-    if (sessionInfo)
-    {
-        boost::shared_lock<boost::shared_mutex> lock(sessionInfo->mLock);
-        lg(Debug) << "--- Begin Session " << sessionInfo->mSessionState->key;
-        lg(Debug) << "Endpoint name: " << sessionInfo->mSessionState->mEndpointName;
-        lg(Debug) << "Session object identity: " << sessionInfo->mSessionState->mSessionObjectId.name;
-        lg(Debug) << "Media session object identity: " << sessionInfo->mSessionState->mMediaSessionObjectId.name;
-        lg(Debug) << "Media session: " << sessionInfo->mSessionState->mMediaSession;
-        lg(Debug) << "Bridge: " << sessionInfo->mSessionState->mBridge;
-        lg(Debug) << "--- Begin Invite Session " << sessionInfo->mInviteState->key;
-        lg(Debug) << "Current state: " << sessionInfo->mInviteState->mCurrentState;
-        lg(Debug) << "Cancelling: " << sessionInfo->mInviteState->mCancelling;
-        lg(Debug) << "Pending cancel: " << sessionInfo->mInviteState->mPendingCancel;
-        lg(Debug) << "Cause: " << sessionInfo->mInviteState->mCause;
-        lg(Debug) << "Cause text: " << sessionInfo->mInviteState->mCauseText;
-        lg(Debug) << "Notify: " << sessionInfo->mInviteState->mNotify;
-        lg(Debug) << "Last Ack CSeq: " << sessionInfo->mInviteState->mLastAckCseq;
-        if (sessionInfo->mNeedsRemoval == true)
-        {
-            removeItems.push_back(sessionInfo->mInviteState->key);
-            removeItems.push_back(sessionInfo->mSessionState->key);
-            lg(Debug) << "Removing session and invite session";
-        }
-        else if (sessionInfo->mNeedsReplication == true)
-        {
-            setItems.push_back(sessionInfo->mInviteState);
-            setItems.insert(setItems.begin(), sessionInfo->mSessionState);
-            sessionInfo->mNeedsReplication = false;
-            lg(Debug) << "Replicating session and invite session";
-        }
-        lg(Debug) << "--- End Session and Invite Session";
-    }
-    if (tsxInfo)
-    {
-        if (tsxInfo->mNeedsRemoval == true)
-        {
-            removeItems.push_back(tsxInfo->mTransactionState->key);
-        }
-        else if (tsxInfo->mNeedsReplication == true)
-        {
-            setItems.push_back(tsxInfo->mTransactionState);
-            tsxInfo->mNeedsReplication = false;
-        }
-    }
-    lg(Debug) << "========== End State Replication Dump ==========";
-    if (mReplica->isActive() == true)
-    {
-        if (setItems.size() != 0 && mStateReplicator)
-        {
-            Ice::ObjectPrx oneway;
-            try
-            {
-                oneway = mStateReplicator->ice_oneway();
-            }
-            catch (const Ice::NoEndpointException&)
-            {
-                lg(Error) << "No endpoint for oneway invocation of setState() for state replication.";
-            }
-
-            AsteriskSCF::SIP::V1::SipStateReplicatorPrx oneWayStateReplicator = 
-                AsteriskSCF::SIP::V1::SipStateReplicatorPrx::uncheckedCast(oneway);
-
-            try
-            {
-                oneWayStateReplicator->setState(setItems);
-            }
-            catch (const Ice::TwowayOnlyException&)
-            {
-                lg(Error) << "setState() is not oneway.";
-            }
-        }
-
-        if (removeItems.size() != 0 && mStateReplicator)
-        {
-            Ice::ObjectPrx oneway;
-            try
-            {
-                oneway = mStateReplicator->ice_oneway();
-            }
-            catch (const Ice::NoEndpointException&)
-            {
-                lg(Error) << "No endpoint for oneway invocation of removeState() for state replication.";
-            }
-
-            AsteriskSCF::SIP::V1::SipStateReplicatorPrx oneWayStateReplicator =
-                AsteriskSCF::SIP::V1::SipStateReplicatorPrx::uncheckedCast(oneway);
-
-            try
-            {
-                oneWayStateReplicator->removeState(removeItems);
-            }
-            catch (const Ice::TwowayOnlyException&)
-            {
-                lg(Error) << "removeState() is not oneway.";
-            }
-        }
-    }
-}
-
-pj_status_t PJSipSessionModule::load(pjsip_endpoint*)
-{
-    return PJ_SUCCESS;
-}
-
-pj_status_t PJSipSessionModule::start()
-{
-    return PJ_SUCCESS;
-}
-
-pj_status_t PJSipSessionModule::stop()
-{
-    return PJ_SUCCESS;
-}
-
-pj_status_t PJSipSessionModule::unload()
-{
-    return PJ_SUCCESS;
-}
-
-void PJSipSessionModule::handleNewInvite(pjsip_rx_data *rdata)
-{
-    //What do we do here?
-    //
-    //First we need to identify who this is coming from.
-    //If it's someone we recognize, then we can figure
-    //out whether we need to send a 401/407 request.
-    //For now, we don't have any sort of configured users,
-    //so we skip this step. Instead, we'll skip ahead to
-    //creating an endpoint, creating the invite session,
-    //sending a 100 trying, finding the remote endpoint
-    //to call, and placing a call to it.
-
-    //XXX Put caller identification code in here!
-
-    pjsip_tx_data *tdata = NULL;
-    unsigned options = PJSIP_INV_SUPPORT_100REL;
-
-    // Verify we can handle this invite request and respond accordingly if we can not
-    if (pjsip_inv_verify_request(rdata, &options, NULL, NULL, mEndpoint, &tdata) != PJ_SUCCESS)
-    {
-	if (tdata)
-	{
-	    pjsip_endpt_send_response2(mEndpoint, rdata, tdata, NULL, NULL);
-	}
-	else
-	{
-	    pjsip_endpt_respond_stateless(mEndpoint, rdata, 500, NULL, NULL, NULL);
-	}
-	return;
-    }
-
-    pjsip_dialog *dlg, *replaced_dlg;
-
-    // If this is an attended transfer and something is amuck... respond accordingly
-    if (pjsip_replaces_verify_request(rdata, &replaced_dlg, PJ_FALSE, &tdata) != PJ_SUCCESS)
-    {
-	if (tdata)
-	{
-	    pjsip_endpt_send_response2(mEndpoint, rdata, tdata, NULL, NULL);
-	}
-	else
-	{
-	    pjsip_endpt_respond_stateless(mEndpoint, rdata, 500, NULL, NULL, NULL);
-	}
-        return;
-    }
-
-    //XXX The NULL parameter should be replaced with
-    //An appropriate Contact header. Leaving it NULL makes
-    //PJSIP create the contact header for responses based
-    //on the To header of the incoming Invite.
-    if (pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, NULL, &dlg) != PJ_SUCCESS)
-    {
-        lg(Warning) << "Unable to create UAS dialog on incoming INVITE";
-        return;
-    }
-
-    PJSipDialogModInfo *dlg_mod_info = new PJSipDialogModInfo(dlg);
-
-    pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
-    PJSipTransactionModInfo *tsx_mod_info = new PJSipTransactionModInfo(tsx);
-    tsx->mod_data[mModule.id] = (void *)tsx_mod_info;
-
-    //XXX The sdp argument is NULL for now, but can be changed if we
-    //know what has been configured for this particular caller.
-    pjsip_inv_session *inv_session;
-    if (pjsip_inv_create_uas(dlg, rdata, NULL, 0, &inv_session) != PJ_SUCCESS)
-    {
-        lg(Warning) << "Unable to create INVITE session";
-        //Since the inv_session was not created, we need to access the base dialog
-        //directly instead. Other failure cases will use pjsip_inv_terminate instead.
-        pjsip_dlg_terminate(dlg);
-        return;
-    }
-
-    // Add our own module as a dialog usage
-    pjsip_dlg_add_usage(dlg, &mModule, NULL);
-
-    pjsip_timer_setting session_timer_settings;
-    pjsip_timer_setting_default(&session_timer_settings);
-    pjsip_timer_init_session(inv_session, &session_timer_settings);
-
-    if (pjsip_inv_initial_answer(inv_session, rdata, 100, NULL, NULL, &tdata) != PJ_SUCCESS)
-    {
-        lg(Warning) << "Failed to create 100 Trying response";
-        pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
-        return;
-    }
-
-    if (pjsip_inv_send_msg(inv_session, tdata) != PJ_SUCCESS)
-    {
-        lg(Warning) << "Failed to send 100 Trying response";
-        pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
-    }
-
-    pjsip_uri *from = rdata->msg_info.from->uri;
-    std::string callerName("");
-    if (PJSIP_URI_SCHEME_IS_SIP(from) || PJSIP_URI_SCHEME_IS_SIPS(from))
-    {
-        pjsip_sip_uri *sipFrom = (pjsip_sip_uri *)pjsip_uri_get_uri(from);
-        callerName = std::string(pj_strbuf(&sipFrom->user), pj_strlen(&sipFrom->user));
-        lg(Debug) << "Got caller name " << callerName;
-    }
-    SipEndpointPtr caller = mEndpointFactory->findByName(callerName);
-    if (caller == 0)
-    {
-        lg(Warning) << "Unknown calling endpoint " << callerName;
-        pjsip_inv_end_session(inv_session, 403, NULL, &tdata);
-        pjsip_inv_send_msg(inv_session, tdata);
-        return;
-    }
-    SipEndpointConfig &config = caller->getConfig();
-    if (config.sessionConfig.callDirection != BOTH && config.sessionConfig.callDirection != INBOUND)
-    {
-        lg(Warning) << "Caller " << callerName << " does not have permission to make inbound calls.";
-        pjsip_inv_end_session(inv_session, 403, NULL, &tdata);
-        pjsip_inv_send_msg(inv_session, tdata);
-        return;
-    }
-
-    //We've created our calling endpoint. Now we need to look up the destination.
-    pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
-    std::string destination("");
-    if (PJSIP_URI_SCHEME_IS_SIP(ruri) || PJSIP_URI_SCHEME_IS_SIPS(ruri))
-    {
-        pjsip_sip_uri *sipRuri = (pjsip_sip_uri *)pjsip_uri_get_uri(ruri);
-        //For now, we only know about destination "100" so we'll
-        //grab that from the URI to pass to the locator.
-        destination = std::string(pj_strbuf(&sipRuri->user), pj_strlen(&sipRuri->user));
-        lg(Debug) << "Call is destined for " << destination;
-    }
-
-    SipSessionPtr session;
-    try
-    {
-        session = caller->createSession(destination);
-    }
-    catch (const Ice::Exception& ex)
-    {
-        lg(Error) << "Exception caught while trying to create SIP session\n" << ex.what();
-        pjsip_inv_end_session(inv_session, 500, NULL, &tdata);
-        pjsip_inv_send_msg(inv_session, tdata);
-        return;
-    }
-    session->setInviteSession(inv_session);
-    session->setDialog(dlg);
-    PJSipSessionModInfo *session_mod_info = new PJSipSessionModInfo(inv_session, session);
-    dlg_mod_info->mDialogState->mSessionId = session_mod_info->mSessionState->mSessionId;
-    tsx_mod_info->mTransactionState->mSessionId = session_mod_info->mSessionState->mSessionId;
-
-    inv_session->mod_data[mModule.id] = (void *)session_mod_info;
-    dlg->mod_data[mModule.id] = (void *)dlg_mod_info;
-
-    lg(Debug) << "Replicating state on reception of new SIP INVITE.";
-    replicateState(dlg_mod_info, tsx_mod_info, session_mod_info);
-    try
-    {
-        if (replaced_dlg)
-        {
-            // For attended transfers we need to contact the routing service which should then (hopefully) replace the
-            // other session
-        }
-        else
-        {
-            // If this is not an attended transfer we can just route the session as normally
-            std::string operationId = ::IceUtil::generateUUID();
-            RouteSessionCallbackPtr cb = new RouteSessionCallback(inv_session, tdata, session, destination, operationId);
-            Ice::CallbackPtr d = Ice::newCallback(cb, &RouteSessionCallback::callback);
-            mSessionRouter->begin_routeSession(operationId, session->getSessionProxy(), destination, d);
-        }
-    }
-    catch (const Ice::CommunicatorDestroyedException &)
-    {
-        // Everything else doesn't really map so they just become internal server errors
-        pjsip_inv_end_session(inv_session, 500, NULL, &tdata);
-        pjsip_inv_send_msg(inv_session, tdata);
-        return;
-    }
-}
-
-void PJSipSessionModule::handleRefer(pjsip_inv_session *inv, pjsip_rx_data *rdata)
-{
-    const pj_str_t str_refer_to = { (char*)"Refer-To", 8 };
-    pjsip_generic_string_hdr *refer_to =
-        static_cast<pjsip_generic_string_hdr *>(pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL));
-
-    lg(Debug) << "Handling a REFER";
-    if (!refer_to)
-    {
-        // Uh... so they didn't tell us where to REFER this to... yeah no
-        lg(Debug) << "handleRefer() sending 400 due to no refer_to. ";
-        pjsip_dlg_respond(inv->dlg, rdata, 400, NULL, NULL, NULL);
-        return;
-    }
-
-    // TODO: Add support for subscription
-
-    // TODO: Provide method to send back suitable response
-
-    // Now parse the URI to get the actual target they want to refer to
-    pjsip_uri *target_uri = static_cast<pjsip_uri *>(pjsip_parse_uri(inv->dlg->pool, refer_to->hvalue.ptr, refer_to->hvalue.slen, 0));
-
-    // We only support SIP URIs, anything else is rubbish to us
-    if (!PJSIP_URI_SCHEME_IS_SIP(target_uri) && !PJSIP_URI_SCHEME_IS_SIPS(target_uri))
-    {
-        // TODO: Place proper response code in here
-        lg(Debug) << "handleRefer() sending 400 due to non-SIP URI. ";
-        pjsip_dlg_respond(inv->dlg, rdata, 400, NULL, NULL, NULL);
-        return;
-    }
-
-    pjsip_sip_uri *target_sip_uri = (pjsip_sip_uri *)pjsip_uri_get_uri(target_uri);
-
-    // Determine if this is a blind transfer or an attended transfer
-    pj_str_t replaces = pj_str((char*)"Replaces");
-    pjsip_param *replaces_param = pjsip_param_find(&target_sip_uri->other_param, &replaces);
-
-    if (!replaces_param)
-    {
-        replaces_param = pjsip_param_find(&target_sip_uri->header_param, &replaces);
-    }
-
-    if (replaces_param)
-    {
-        pj_str_t to_tag = pj_str((char*)"To-tag");
-        pj_str_t from_tag = pj_str((char*)"From-tag");
-        pjsip_param *to_tag_param = pjsip_param_find(&target_sip_uri->other_param, &to_tag);
-        pjsip_param *from_tag_param = pjsip_param_find(&target_sip_uri->other_param, &from_tag);
-        pjsip_dialog *other_dlg = NULL;
-
-        if (to_tag_param && from_tag_param)
-        {
-            other_dlg = pjsip_ua_find_dialog(&replaces_param->value, &to_tag_param->value, &from_tag_param->value,
-                    PJ_TRUE);
-        }
-        else
-        {
-            // It is possible for the to and from tag value to be present within the Replaces parameter value, so try to
-            // parse it out
-            std::string replaces_value_tmp = std::string(pj_strbuf(&replaces_param->value),
-                    pj_strlen(&replaces_param->value));
-            size_t from_tag_pos = replaces_value_tmp.find(";from-tag=");
-            size_t to_tag_pos = replaces_value_tmp.find(";to-tag=");
-
-            if (from_tag_pos == std::string::npos || to_tag_pos == std::string::npos)
-            {
-                lg(Debug) << "handleRefer() sending 400 due to From or To missing. ";
-                pjsip_dlg_respond(inv->dlg, rdata, 400, NULL, NULL, NULL);
-                return;
-            }
-
-            std::string to_tag_value = replaces_value_tmp.substr(to_tag_pos + 8, from_tag_pos - to_tag_pos - 8);
-            std::string from_tag_value = replaces_value_tmp.substr(from_tag_pos + 10);
-            std::string replaces_value = replaces_value_tmp.substr(0, to_tag_pos);
-
-            pj_str_t to_tag_str = pj_str((char*)to_tag_value.c_str());
-            pj_str_t from_tag_str = pj_str((char*)from_tag_value.c_str());
-            pj_str_t replaces_tag_str = pj_str((char*)replaces_value.c_str());
-
-            other_dlg = pjsip_ua_find_dialog(&replaces_tag_str, &to_tag_str, &from_tag_str, PJ_TRUE);
-        }
-
-        if (!other_dlg)
-        {
-            lg(Debug) << "handleRefer() sending PJSIP_SC_CALL_TSX_DOES_NOT_EXIST due to no other_dlg. ";
-            pjsip_dlg_respond(inv->dlg, rdata, PJSIP_SC_CALL_TSX_DOES_NOT_EXIST, NULL, NULL, NULL);
-            return;
-        }
-
-        pjsip_inv_session *other_inv = pjsip_dlg_get_inv_session(other_dlg);
-
-        if (!other_inv)
-        {
-            lg(Debug) << "handleRefer() sending PJSIP_SC_CALL_TSX_DOES_NOT_EXIST due to no other_inv. ";
-            pjsip_dlg_respond(inv->dlg, rdata, PJSIP_SC_CALL_TSX_DOES_NOT_EXIST, NULL, NULL, NULL);
-            pjsip_dlg_dec_lock(other_dlg);
-            return;
-        }
-
-        if (other_inv->state >= PJSIP_INV_STATE_DISCONNECTED)
-        {
-            lg(Debug) << "handleRefer() sending PJSIP_SC_DECLINE due to state > PJSIP_INV_STATE_DISCONNECTED. ";
-
-            pjsip_dlg_respond(inv->dlg, rdata, PJSIP_SC_DECLINE, NULL, NULL, NULL);
-            pjsip_dlg_dec_lock(other_dlg);
-            return;
-        }
-
-        if (other_inv->state <= PJSIP_INV_STATE_EARLY && other_inv->role != PJSIP_ROLE_UAC)
-        {
-            lg(Debug) << "handleRefer() sending PJSIP_SC_CALL_TSX_DOES_NOT_EXIST due to other_inv->state < PJSIP_INV_STATE_EARLY and role not UAC. ";
-
-            pjsip_dlg_respond(inv->dlg, rdata, PJSIP_SC_CALL_TSX_DOES_NOT_EXIST, NULL, NULL, NULL);
-            pjsip_dlg_dec_lock(other_dlg);
-            return;
-        }
-
-        PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[mModule.id];
-        SipSessionPtr session = session_mod_info->getSessionPtr();
-
-        PJSipSessionModInfo *other_session_mod_info = (PJSipSessionModInfo*)other_inv->mod_data[mModule.id];
-        SipSessionPtr other_session = other_session_mod_info->getSessionPtr();
-
-        try
-        {
-            pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
-            pjsip_tx_data *tdata;
-            pjsip_dlg_create_response(inv->dlg, rdata, 200, NULL, &tdata);
-            std::string operationId = ::IceUtil::generateUUID();
-            ConnectBridgedSessionsCallbackPtr cb(new ConnectBridgedSessionsCallback(inv, tdata, tsx, session, other_session, operationId));
-            Ice::CallbackPtr d = Ice::newCallback(cb, &ConnectBridgedSessionsCallback::callback);
-
-            lg(Debug) << "handleRefer() calling router connectBridgedSessions(). ";
-            mSessionRouter->begin_connectBridgedSessions(operationId, session->getSessionProxy(), other_session->getSessionProxy(), d);
-        }
-        catch (const Ice::CommunicatorDestroyedException &)
-        {
-            lg(Debug) << "handleRefer() sending 503 due to communicator destruction";
-            pjsip_dlg_respond(inv->dlg, rdata, 503, NULL, NULL, NULL);
-            return;
-        }
-        pjsip_dlg_dec_lock(other_dlg);
-    }
-    else
-    {
-        std::string target = std::string(pj_strbuf(&target_sip_uri->user), pj_strlen(&target_sip_uri->user));
-
-        // Now that we have the target user we can pass this into routing and go on our marry way
-        try
-        {
-            std::string operationId = ::IceUtil::generateUUID();
-            PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[mModule.id];
-            SipSessionPtr session = session_mod_info->getSessionPtr();
-            pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
-            pjsip_tx_data *tdata;
-            pjsip_dlg_create_response(inv->dlg, rdata, 200, NULL, &tdata);
-            ConnectBridgedSessionsWithDestinationCallbackPtr cb(
-                    new ConnectBridgedSessionsWithDestinationCallback(inv, tdata, tsx, session, target, operationId));
-            Ice::CallbackPtr d = Ice::newCallback(cb, &ConnectBridgedSessionsWithDestinationCallback::callback);
-
-            lg(Debug) << "handleRefer() calling router connectBridgedSessionsWithDestination(). ";
-            mSessionRouter->begin_connectBridgedSessionsWithDestination(operationId, session->getSessionProxy(), target, d);
-        }
-        catch (const Ice::CommunicatorDestroyedException &)
-        {
-            lg(Debug) << "handleRefer() sending 503 due to communicator destruction";
-            pjsip_dlg_respond(inv->dlg, rdata, 503, NULL, NULL, NULL);
-            return;
-        }
-    }
-}
-
-pj_bool_t PJSipSessionModule::on_rx_request(pjsip_rx_data *rdata)
-{
-    pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
-    switch (rdata->msg_info.msg->line.req.method.id)
-    {
-    case PJSIP_ACK_METHOD:
-	return PJ_FALSE;
-    case PJSIP_INVITE_METHOD:
-        if (dlg == NULL)
-        {
-            handleNewInvite(rdata);
-        }
-        else
-        {
-            lg(Warning) << "on_rx_request called back in mid-dialog?";
-        }
-        break;
-    case PJSIP_OTHER_METHOD:
-        if (dlg && !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_refer_method()))
-        {
-            // We are essentially stopping the pjsip code from sending a 500 here, but this will actually be handled
-            // within the transaction code
-            break;
-        }
-    default:
-	pjsip_endpt_respond_stateless(mEndpoint, rdata, 405, NULL, NULL, NULL);
-	break;
-    }
-
-    return PJ_TRUE;
-}
-
-/* The following four member functions are all stubbed out because they
- * are either unused at the moment or will never be called due to the use
- * of PJSIP's INVITE session module.
- *
- * Linking errors abound if these member functions are not defined, though.
- */
-pj_bool_t PJSipSessionModule::on_rx_response(pjsip_rx_data*)
-{
-    return PJ_FALSE;
-}
-pj_status_t PJSipSessionModule::on_tx_request(pjsip_tx_data*)
-{
-    return PJ_SUCCESS;
-}
-pj_status_t PJSipSessionModule::on_tx_response(pjsip_tx_data*)
-{
-    return PJ_SUCCESS;
-}
-void PJSipSessionModule::on_tsx_state(pjsip_transaction*, pjsip_event*)
-{
-    return;
-}
-
-void PJSipSessionModule::handleInviteResponse(pjsip_inv_session* inv,
-    pjsip_rx_data* rdata, pjsip_dialog*)
-{
-    int respCode = rdata->msg_info.msg->line.status.code;
-    PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[mModule.id];
-    SipSessionPtr session = session_mod_info->getSessionPtr();
-    //Commented because they are currently unused. They
-    //will be once the individual cases are mapped out.
-    //pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
-    //pjsip_module *module = pjsip_ua_instance();
-    //
-    //XXX There are a BUNCH of response codes we need
-    //to add code to handle.
-
-    //Treat all 1XX messages we don't recognize the same as a 180
-    if (respCode > 100 && respCode < 200 && respCode != 183)
-    {
-        respCode = 180;
-    }
-    if (respCode == 100)
-    {
-        //Not sure if PJSIP even bothers passing these up
-        lg(Debug) << "Got 100 response";
-    }
-    else if (respCode == 180)
-    {
-        lg(Debug) << "Got 180 response";
-        std::vector<AsteriskSCF::SessionCommunications::V1::SessionListenerPrx> listeners = session->getListeners();
-        std::vector<AsteriskSCF::SessionCommunications::V1::SessionListenerPrx>::const_iterator listener;
-        lg(Debug) << "Relating ringing state to " << listeners.size() << " listeners";
-        for (listener = listeners.begin(); listener != listeners.end(); ++listener)
-        {
-            try
-            {
-                ListenerCallbackPtr cb(new ListenerCallback(RingingCallbackName));
-                Callback_SessionListener_indicatedPtr ringingCB =
-                    newCallback_SessionListener_indicated(cb, &ListenerCallback::failure);
-                (*listener)->begin_indicated(session->getSessionProxy(), new RingingIndication(), ringingCB);
-            }
-            catch (const Ice::Exception &ex)
-            {
-                lg(Error) << "Ice exception when attempting to relate ringing state: " << ex.what();
-            }
-        }
-    }
-    else if (respCode == 183)
-    {
-        lg(Debug) << "Got 183 response";
-        AsteriskSCF::SessionCommunications::V1::ResponseCodePtr response = new AsteriskSCF::SessionCommunications::V1::ResponseCode();
-        response->isdnCode = 42;
-        std::vector<AsteriskSCF::SessionCommunications::V1::SessionListenerPrx> listeners = session->getListeners();
-        std::vector<AsteriskSCF::SessionCommunications::V1::SessionListenerPrx>::const_iterator listener;
-        lg(Debug) << "Relating progressing state to " << listeners.size() << " listeners";
-        for (listener = listeners.begin(); listener != listeners.end(); ++listener)
-        {
-            try
-            {
-                ListenerCallbackPtr cb(new ListenerCallback(ProgressingCallbackName));
-                Callback_SessionListener_indicatedPtr progressingCB =
-                    newCallback_SessionListener_indicated(cb, &ListenerCallback::failure);
-		ProgressingIndicationPtr progressing(new ProgressingIndication());
-		progressing->response = response;
-                (*listener)->begin_indicated(session->getSessionProxy(), progressing, progressingCB);
-            }
-            catch (const Ice::Exception &ex)
-            {
-                lg(Error) << "Ice exception when attempting to relate progressing state: " << ex.what();
-            }
-        }
-    }
-    else if (respCode == 200)
-    {
-        lg(Debug) << "Got 200 response";
-        if (inv->state != PJSIP_INV_STATE_DISCONNECTED)
-        {
-            std::vector<AsteriskSCF::SessionCommunications::V1::SessionListenerPrx> listeners = session->getListeners();
-            std::vector<AsteriskSCF::SessionCommunications::V1::SessionListenerPrx>::const_iterator listener;
-            lg(Debug) << "Relating connected state to " << listeners.size() << " listeners";
-            for (listener = listeners.begin(); listener != listeners.end(); ++listener)
-            {
-                try
-                {
-                    ListenerCallbackPtr cb(new ListenerCallback(ConnectedCallbackName));
-                    Callback_SessionListener_indicatedPtr connectedCB =
-                        newCallback_SessionListener_indicated(cb, &ListenerCallback::failure);
-                    (*listener)->begin_indicated(session->getSessionProxy(), new ConnectedIndication(), connectedCB);
-                }
-                catch (const Ice::Exception &ex)
-                {
-                    lg(Error) << "Ice exception when attempting to relate connected state: " << ex.what();
-                }
-            }
-        }
-    }
-}
-
-void PJSipSessionModule::invOnStateChanged(pjsip_inv_session *inv, pjsip_event *event)
-{
-    if ((inv->state == PJSIP_INV_STATE_EARLY || inv->state == PJSIP_INV_STATE_CONNECTING) &&
-        event->type == PJSIP_EVENT_TSX_STATE &&
-        inv->role == PJSIP_ROLE_UAC)
-    {
-        //Received a 1XX or 2XX message in response to our initial outgoing INVITE.
-        handleInviteResponse(inv, event->body.tsx_state.src.rdata, inv->dlg);
-    }
-    if (inv->state == PJSIP_INV_STATE_DISCONNECTED)
-    {
-        PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[mModule.id];
-        if (session_mod_info == 0)
-        {
-            return;
-        }
-        SipSessionPtr session = session_mod_info->getSessionPtr();
-        AsteriskSCF::SessionCommunications::V1::ResponseCodePtr response =
-            new AsteriskSCF::SessionCommunications::V1::ResponseCode();
-        if (inv->cause == 486)
-        {
-            response->isdnCode = 17;
-        }
-        else if (PJSIP_IS_STATUS_IN_CLASS(inv->cause, 500))
-        {
-            response->isdnCode = 34;
-        }
-        else
-        {
-            // TODO: See what cause is on a normal call completion
-            response->isdnCode = 0;
-        }
-        std::vector<AsteriskSCF::SessionCommunications::V1::SessionListenerPrx> listeners = session->getListeners();
-        lg(Debug) << "Relating stopped state to " << listeners.size() << " listeners";
-	AsteriskSCF::SessionCommunications::V1::StoppedIndicationPtr stopped(new AsteriskSCF::SessionCommunications::V1::StoppedIndication());
-	stopped->response = response;
-        for (std::vector<AsteriskSCF::SessionCommunications::V1::SessionListenerPrx>::iterator listener =
-                 listeners.begin();
-             listener != listeners.end();
-             ++listener)
-        {
-            try
-            {
-                (*listener)->indicated(session->getSessionProxy(), stopped);
-            }
-            catch (const Ice::Exception &ex)
-            {
-                lg(Error) << "Ice exception when attempting to relate stopped state: " << ex.what();
-            }
-        }
-        session_mod_info->mNeedsRemoval = true;
-        pjsip_dialog *dlg = inv->dlg;
-        PJSipDialogModInfo *dlg_mod_info = (PJSipDialogModInfo*) dlg->mod_data[mModule.id];
-	if (dlg_mod_info)
-	{
-	    dlg_mod_info->mNeedsRemoval = true;
-	}
-        lg(Debug) << "Replicating state on DISCONNECTED inv_state.";
-        replicateState(dlg_mod_info, NULL, session_mod_info);
-        delete session_mod_info;
-	inv->mod_data[mModule.id] = 0;
-	if (dlg_mod_info)
-	{
-	    delete dlg_mod_info;
-	    dlg->mod_data[mModule.id] = 0;
-	}
-    }
-    if (event->type == PJSIP_EVENT_RX_MSG && inv->state == PJSIP_INV_STATE_CONFIRMED)
-    {
-        //We received an ACK for our 2XX response.
-        //See comment in invOnTransactionStateChanged() for explanation of this.
-
-        PJSipSessionModInfo *session_mod_info = static_cast<PJSipSessionModInfo*>(inv->mod_data[mModule.id]);
-        if (session_mod_info)
-        {
-            session_mod_info->updateSessionState(inv);
-        }
-        replicateState(NULL, NULL, session_mod_info);
-
-        //Compare branches to see if this got handled by the transaction layer.
-        if (inv->invite_tsx &&
-                pj_strcmp(&event->body.rx_msg.rdata->msg_info.via->branch_param, &inv->invite_tsx->branch) != 0)
-        {
-            //Mismatched branch!
-            invOnTsxStateChanged(inv, inv->invite_tsx, event);
-        }
-    }
-    if (event->type == PJSIP_EVENT_TSX_STATE && inv->state == PJSIP_INV_STATE_CALLING && inv->role == PJSIP_ROLE_UAC)
-    {
-        //We have sent an INVITE out. We need to set up the transaction and dialog structures
-        //to have the appropriate mod_data and initiate some state replication here as well.
-        //The inv_session's mod_info was set up in SipSession::start() because we had the SipSession
-        //information there.
-        PJSipDialogModInfo *dlg_mod_info = new PJSipDialogModInfo(inv->dlg);
-        PJSipTransactionModInfo *tsx_mod_info = new PJSipTransactionModInfo(inv->invite_tsx);
-        PJSipSessionModInfo *session_mod_info = static_cast<PJSipSessionModInfo *>(inv->mod_data[mModule.id]);
-        inv->invite_tsx->mod_data[mModule.id] = (void *) tsx_mod_info;
-        inv->dlg->mod_data[mModule.id] = (void *) dlg_mod_info;
-        dlg_mod_info->mDialogState->mSessionId = session_mod_info->mSessionState->mSessionId;
-        tsx_mod_info->mTransactionState->mSessionId = session_mod_info->mSessionState->mSessionId;
-        lg(Debug) << "Replicating state on new outbound INVITE.";
-        replicateState(dlg_mod_info, tsx_mod_info, session_mod_info);
-    }
-}
-
-void PJSipSessionModule::invOnNewSession(pjsip_inv_session*, pjsip_event*)
-{
-    //stub
-}
-
-void PJSipSessionModule::invOnTsxStateChanged(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
-{
-    if (tsx->role == PJSIP_ROLE_UAS && tsx->state == PJSIP_TSX_STATE_TRYING &&
-            !pjsip_method_cmp(&tsx->method, pjsip_get_refer_method()))
-    {
-        handleRefer(inv, e->body.tsx_state.src.rdata);
-    }
-    //This will be our key point for updating transaction state.  This function will not be called until after a module
-    //has registered itself as the transaction user, so this won't be called on the initial INVITE we receive.
-    //
-    //Having taken a look at the pjsip inv_session code, the transaction state changed callback is called *after* the
-    //inv_state_changed callback. This means that on both reception and transmission of requests, this is called last,
-    //meaning this is the prime spot to do state replication of dialogs, transactions, sessions.
-    //
-    //There is one exception. If we receive an ACK for an INVITE transaction and the ACK has a different Via branch
-    //parameter than the INVITE and its response, then this callback will not be called at all, and we will need to
-    //perform the proper state replication in the invOnStateChanged function. This can be done simply by calling this
-    //function from there.
-
-    PJSipTransactionModInfo *tsx_mod_info = static_cast<PJSipTransactionModInfo *> (tsx->mod_data[mModule.id]);
-    // TODO: Determine if this is perfectly acceptable, since it occurs when the call is going bye bye
-    if (!tsx_mod_info)
-    {
-        return;
-    }
-    if (tsx->state != PJSIP_TSX_STATE_DESTROYED)
-    {
-        tsx_mod_info->updateTransactionState(tsx);
-    }
-    else
-    {
-        tsx_mod_info->mNeedsRemoval = true;
-    }
-    if (e->type == PJSIP_EVENT_TSX_STATE)
-    {
-        pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx);
-        PJSipDialogModInfo *dlg_mod_info = NULL;
-        if (dlg)
-        {
-            dlg_mod_info = static_cast<PJSipDialogModInfo*>(dlg->mod_data[mModule.id]);
-            if (dlg_mod_info)
-            {
-                dlg_mod_info->updateDialogState(dlg);
-            }
-        }
-        PJSipSessionModInfo *session_mod_info = static_cast<PJSipSessionModInfo*>(inv->mod_data[mModule.id]);
-        if (session_mod_info)
-        {
-            session_mod_info->updateSessionState(inv);
-        }
-        lg(Debug) << "Replicating state on transaction state change to " << tsx->state;
-        replicateState(dlg_mod_info, tsx_mod_info, session_mod_info);
-    }
-    else if (e->type == PJSIP_EVENT_TIMER)
-    {
-        lg(Debug) << "Replicating state on transaction state change to " << tsx->state;
-        replicateState(NULL, tsx_mod_info, NULL);
-    }
-}
-
-void PJSipSessionModule::invOnRxOffer(pjsip_inv_session* inv, const pjmedia_sdp_session*)
-{
-    PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[mModule.id];
-    pjsip_inv_set_sdp_answer(inv, session_mod_info->getSessionPtr()->createSDPOffer());
-}
-
-void PJSipSessionModule::invOnCreateOffer(pjsip_inv_session*, pjmedia_sdp_session**)
-{
-    //stub
-}
-
-void PJSipSessionModule::invOnMediaUpdate(pjsip_inv_session *inv, pj_status_t status)
-{
-    if (status != PJ_SUCCESS)
-    {
-        // We have nothing, zip, nada, kablamo, in common.
-        return;
-    }
-
-    const pjmedia_sdp_session *remote_sdp;
-    if ((status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp)) != PJ_SUCCESS)
-    {
-        // TODO: What happens if we can't get negotiated SDP?
-        return;
-    }
-
-    const pjmedia_sdp_conn *remote_conn = remote_sdp->media[0]->conn ? remote_sdp->media[0]->conn : remote_sdp->conn;
-
-    PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[mModule.id];
-    SipSessionPtr session = session_mod_info->getSessionPtr();
-    std::string destination(pj_strbuf(&remote_conn->addr), pj_strlen(&remote_conn->addr));
-    session->setRemoteDetails(destination, remote_sdp->media[0]->desc.port);
-
-    // Each stream has its own set of formats, so go to that granularity
-    for (unsigned int stream = 0; stream < remote_sdp->media_count; stream++)
-    {
-        // We should have the parsing of the connection information here
-
-        // We should have the parsing of the rtcp attribute here
-
-        FormatSeq formats;
-
-        // Next step is to see what formats exist on this stream
-        for (unsigned int format = 0; format < remote_sdp->media[stream]->desc.fmt_count; format++)
-        {
-            FormatDiscoverySDPPtr params = new FormatDiscoverySDP();
-            params->category = "media_format";
-            std::stringstream(pj_strbuf(&remote_sdp->media[stream]->desc.fmt[format])) >> params->payload;
-            params->type = std::string(pj_strbuf(&remote_sdp->media[stream]->desc.media),
-                    pj_strlen(&remote_sdp->media[stream]->desc.media));
-
-            // Some devices rely solely on the payload for known formats (such as PCMU) so the following format
-            // parameters are optional
-            const pjmedia_sdp_attr *attr;
-            if ((attr = pjmedia_sdp_media_find_attr2(remote_sdp->media[stream], "rtpmap",
-                                    &remote_sdp->media[stream]->desc.fmt[format])))
-            {
-                pjmedia_sdp_rtpmap *rtpmap;
-                if ((pjmedia_sdp_attr_to_rtpmap(inv->pool_active, attr, &rtpmap)) == PJ_SUCCESS)
-                {
-                    params->subtype = std::string(pj_strbuf(&rtpmap->enc_name), pj_strlen(&rtpmap->enc_name));
-                    params->samplerate = rtpmap->clock_rate;
-                }
-            }
-
-            // Next we move on to the format specific parameters
-            if ((attr = pjmedia_sdp_media_find_attr2(remote_sdp->media[stream], "fmtp",
-                                    &remote_sdp->media[stream]->desc.fmt[format])))
-            {
-                pjmedia_sdp_fmtp fmtp;
-                if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS)
-                {
-                    std::string parameter = std::string(pj_strbuf(&fmtp.fmt_param), pj_strlen(&fmtp.fmt_param));
-                    params->parameters.push_back(parameter);
-                }
-            }
-
-            // Next up are attributes that are not specific to the format, such as ptime
-            for (unsigned int attribute = 0; attribute < remote_sdp->media[stream]->attr_count; attribute++)
-            {
-                // Attributes we already touch above OR know aren't helpful to the media format component don't need to
-                // be given to it, obviously
-                if (!pj_strcmp2(&remote_sdp->media[stream]->attr[attribute]->name, "rtpmap") ||
-                    !pj_strcmp2(&remote_sdp->media[stream]->attr[attribute]->name, "fmtp") ||
-                    !pj_strcmp2(&remote_sdp->media[stream]->attr[attribute]->name, "rtcp") ||
-                    !pj_strcmp2(&remote_sdp->media[stream]->attr[attribute]->name, "sendrecv") ||
-                    !pj_strcmp2(&remote_sdp->media[stream]->attr[attribute]->name, "sendonly") ||
-                    !pj_strcmp2(&remote_sdp->media[stream]->attr[attribute]->name, "recvonly"))
-                {
-                    continue;
-                }
-
-                std::string parameter = std::string(pj_strbuf(&remote_sdp->media[stream]->attr[attribute]->name),
-                        pj_strlen(&remote_sdp->media[stream]->attr[attribute]->name)) + ':' +
-                    std::string(pj_strbuf(&remote_sdp->media[stream]->attr[attribute]->value),
-                            pj_strlen(&remote_sdp->media[stream]->attr[attribute]->value));
-                params->parameters.push_back(parameter);
-            }
-
-            try
-            {
-                MediaFormatServicePrx service = MediaFormatServicePrx::uncheckedCast(mServiceLocator->locate(params));
-
-                // It is entirely possible for the service locator to not find a service that knows about this media
-                // format
-                if (service != 0)
-                {
-                    formats.push_back(FormatPtr::dynamicCast(service->getFormat(params)));
-                }
-            }
-            catch (...)
-            {
-                // If we get here the format just isn't supported...
-            }
-        }
-    }
-}
-
-pjsip_redirect_op PJSipSessionModule::invOnRedirected(pjsip_inv_session*, const pjsip_uri*,
-        const pjsip_event*)
-{
-    //stub
-    return PJSIP_REDIRECT_REJECT;
-}
-
-pjsip_dialog *PJSipSessionModule::uaOnDialogForked(pjsip_dialog*, pjsip_rx_data*)
-{
-    //stub
-    return NULL;
-}
-
-QueuePtr PJSipSessionModule::getThreadPoolQueue()
-{
-    return mPoolQueue;
-}
-
-PJSipSessionModuleThreadPoolListener::PJSipSessionModuleThreadPoolListener()
-    : mActiveThreads(0) { }
-
-void PJSipSessionModuleThreadPoolListener::stateChanged(const PoolPtr& pool, Ice::Long active, Ice::Long idle, Ice::Long)
-{
-    //XXX Making this behavior more customizable would be nice
-    //
-    //For now, what we do is kill all idle threads.
-    if (idle > 0)
-    {
-        pool->setSize((int) active);
-    }
-    mActiveThreads = active;
-}
-
-void PJSipSessionModuleThreadPoolListener::queueWorkAdded(const PoolPtr& pool, Ice::Long numNewWork, bool)
-{
-    //XXX Making this behavior more customizable would be nice
-    //
-    //For now, use one thread per work item.
-    int newSize = (int) (mActiveThreads + numNewWork);
-    pool->setSize(newSize);
-}
-
-void PJSipSessionModuleThreadPoolListener::queueEmptied(const PoolPtr& pool)
-{
-    //XXX Making this behavior more customizable would be nice
-    //
-    //For now, kill off everything
-    pool->setSize(0);
-}
-
-SessionWork::SessionWork(const QueuePtr& queue)
-    : mThreadPoolQueue(queue),
-    mInternalQueue(new SuspendableWorkQueue(this)) { }
-
-void SessionWork::workAdded(Ice::Long, bool wasEmpty)
-{
-    if (wasEmpty)
-    {
-        mThreadPoolQueue->enqueueWork(this);
-    }
-}
-
-void SessionWork::workResumable()
-{
-    mThreadPoolQueue->enqueueWork(this);
-}
-
-void SessionWork::emptied()
-{
-    //Empty on purpose. Nothing special
-    //to do here.
-}
-
-void SessionWork::execute()
-{
-    while(mInternalQueue->executeWork());
-}
-
-void SessionWork::enqueueWork(const SuspendableWorkPtr& work)
-{
-    mInternalQueue->enqueueWork(work);
-}
-
-}; //end namespace SipSessionManager
-}; //end namespace AsteriskSCF
diff --git a/src/SipSession.cpp b/src/SipSession.cpp
index caf652e..041d7ae 100644
--- a/src/SipSession.cpp
+++ b/src/SipSession.cpp
@@ -921,7 +921,7 @@ pjmedia_sdp_session *SipSession::createSDPOffer()
 
     std::string address = stream->getLocalAddress();
 
-    lg(Debug) << "                     ADDRESS IS " << address;
+    lg(Debug) << "The local address we are placing in our SDP is " << address;
     if (address.find(":") != std::string::npos)
     {
 	pj_strdup2(mImplPriv->mDialog->pool, &sdp->origin.addr_type, "IP6");
@@ -1047,11 +1047,6 @@ void SipSession::setInviteSession(pjsip_inv_session *session)
 {
     mImplPriv->mInviteSession = session;
 
-    lg(Debug) << "Set the invite session on the session...";
-    if (!session)
-    {
-        lg(Debug) << "But it's NULL???";
-    }
     if (!mImplPriv->mSessionWork)
     {
         PJSipSessionModInfo *session_mod_info =
@@ -1162,7 +1157,7 @@ void SipSession::enqueueSessionWork(const SuspendableWorkPtr& task)
     {
         mImplPriv->mSessionWork->enqueueWork(task);
     }
-    catch (ShuttingDown)
+    catch (const ShuttingDown&)
     {
         //Nothing really to do in this case. We'll just log that the
         //exception occurred.

-----------------------------------------------------------------------


-- 
asterisk-scf/integration/sip.git



More information about the asterisk-scf-commits mailing list