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

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Mon Oct 4 10:09:38 CDT 2010


branch "master" has been updated
       via  1cd2b0d09a71149eee60bace1c1f7fad5dac7d50 (commit)
       via  f077a7808c81bd744a84244556ed5428c4716932 (commit)
      from  e6bf7724dbf3fbf38df56f8b4f947e1bdb22b4d7 (commit)

Summary of changes:
 src/CMakeLists.txt                          |    3 +
 src/PJSipManager.cpp                        |    2 +-
 src/PJSipManager.h                          |    4 +-
 src/PJSipModule.cpp                         |  142 +++++++++++++
 src/{PJSipSessionModule.h => PJSipModule.h} |   43 ++---
 src/PJSipSessionModule.cpp                  |  285 ++++++---------------------
 src/PJSipSessionModule.h                    |   69 +++----
 src/PJSipSessionModuleConstruction.cpp      |  129 ++++++++++++
 src/SipSession.cpp                          |    2 +-
 9 files changed, 388 insertions(+), 291 deletions(-)
 create mode 100644 src/PJSipModule.cpp
 copy src/{PJSipSessionModule.h => PJSipModule.h} (58%)
 create mode 100644 src/PJSipSessionModuleConstruction.cpp


- Log -----------------------------------------------------------------
commit 1cd2b0d09a71149eee60bace1c1f7fad5dac7d50
Merge: f077a78 e6bf772
Author: Mark Michelson <mmichelson at digium.com>
Date:   Mon Oct 4 10:07:31 2010 -0500

    Merge branch 'master' of git.asterisk.org:asterisk-scf/integration/sip
    
    In addition to the merge, I have also reduced some of the dependency on
    the data model since the PJSIP module is now easily accessible from all
    PJSIP callbacks.
    
    Conflicts:
    	src/PJSipSessionModule.cpp
    	src/SipSession.cpp

diff --cc src/PJSipSessionModule.cpp
index c4209bf,a0592f7..549e7fa
--- a/src/PJSipSessionModule.cpp
+++ b/src/PJSipSessionModule.cpp
@@@ -34,9 -34,138 +34,13 @@@ using namespace AsteriskSCF::SessionCom
  using namespace AsteriskSCF::Media::V1;
  using namespace AsteriskSCF::SIP::V1;
  
- PJSipSessionModInfo::PJSipSessionModInfo(pjsip_inv_session *inv_session) : mSessionState(new SipSessionStateItem), mInviteState(new SipInviteSessionStateItem), mNeedsReplication(true), mNeedsRemoval(false)
 -//Arbitrarily chosen URI size. Used for times when I need
 -//to retrieve PJSIP URI strings.
 -const int URI_SIZE = 64;
 -
 -PJSipDialogModInfo::PJSipDialogModInfo(pjsip_dialog *dialog) : mDialogState(new SipDialogStateItem) , mNeedsReplication(true), mNeedsRemoval(false)
 -{
 -   //XXX Is there a way to tell ICE to make the default
 -   //constructor for SipStateItem set key?
 -   mDialogState->key = IceUtil::generateUUID();
 -   updateDialogState(dialog);
 -}
 -
 -PJSipDialogModInfo::~PJSipDialogModInfo()
 -{
 -   mDialogState = 0;
 -}
 -
 -/**
 - * Copies data from the pjsip_dialog structure into
 - * our state data.
 - *
 - * XXX This could likely be optimized to only update
 - * stuff that needs updating. Important first step
 - * is to get it working.
 - */
 -void PJSipDialogModInfo::updateDialogState(pjsip_dialog *dialog)
 -{
 -   mDialogState->mCallId = std::string(pj_strbuf(&dialog->call_id->id), pj_strlen(&dialog->call_id->id));
 -   mDialogState->mIsDialogEstablished = dialog->state == PJSIP_DIALOG_STATE_ESTABLISHED ? true : false;
 -   mDialogState->mIsSecure = dialog->secure == PJ_TRUE ? true : false;
 -
 -   mDialogState->mLocalCSeq = dialog->local.cseq;
 -   mDialogState->mLocalTag = std::string(pj_strbuf(&dialog->local.info->tag), pj_strlen(&dialog->local.info->tag));
 -   char localUri[URI_SIZE];
 -   pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, dialog->local.info->uri, localUri, sizeof(localUri));
 -   mDialogState->mLocalUri = localUri;
 -
 -   mDialogState->mRemoteCSeq = dialog->remote.cseq;
 -   mDialogState->mRemoteTag = std::string(pj_strbuf(&dialog->remote.info->tag), pj_strlen(&dialog->remote.info->tag));
 -   char remoteUri[URI_SIZE];
 -   pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, dialog->remote.info->uri, remoteUri, sizeof(remoteUri));
 -   mDialogState->mRemoteUri = remoteUri;
 -   
 -   //This is a place where the whole "only change what you need" optimization
 -   //will come in super-handy since we have to erase and rebuild the route set
 -   //every time. My recollection is that the route set can only be established
 -   //in the first transaction of a dialog so this is purdy wasteful.
 -   mDialogState->mRouteSet.erase(mDialogState->mRouteSet.begin(), mDialogState->mRouteSet.end());
 -   for (pjsip_route_hdr *hdr_iter = dialog->route_set.next; hdr_iter != &dialog->route_set; hdr_iter = hdr_iter->next)
 -   {
 - 	 char routeUri[URI_SIZE];
 - 	 pjsip_uri_print(PJSIP_URI_IN_ROUTING_HDR, hdr_iter->name_addr.uri, routeUri, sizeof(routeUri));
 -
 -      //XXX I need to see the order in which the route set is represented
 - 	 //in a pjsip dialog. push_back could be completely incorrect here,
 - 	 //or it may only be correct depending on whether we're the UAC or UAS.
 - 	 //Also, for now we're ignoring the optional display name portion of URIs
 - 	 //in the route set. They're not strictly necessary for routing calls correctly.
 - 	 mDialogState->mRouteSet.push_back(routeUri);
 -   }
 -   //XXX I'm not 100% sure how to get the transport from a pjsip_dialog, so for now,
 -   //I'll just hardcode this as UDP until we start wanting to actually implement
 -   //other transports.
 -   mDialogState->mTransport = "UDP";
 -   mDialogState->mUacHas2xx = dialog->uac_has_2xx == PJ_TRUE ? true : false;
 -   mDialogState->mIsUac = (dialog->role == PJSIP_ROLE_UAC || dialog->role == PJSIP_UAC_ROLE) ? true : false;
 -   mNeedsReplication = true;
 -}
 -
 -PJSipTransactionModInfo::PJSipTransactionModInfo(pjsip_transaction *transaction) : mTransactionState(new SipTransactionStateItem), mNeedsReplication(true), mNeedsRemoval(false)
 -{
 -   mTransactionState->key = IceUtil::generateUUID();
 -   updateTransactionState(transaction);
 -}
 -
 -PJSipTransactionModInfo::~PJSipTransactionModInfo()
 -{
 -   mTransactionState = 0;
 -}
 -
 -void PJSipTransactionModInfo::updateTransactionState(pjsip_transaction *transaction)
 -{
 -   mTransactionState->mBranch = std::string(pj_strbuf(&transaction->branch), pj_strlen(&transaction->branch));
 -   mTransactionState->mIsClient = (transaction->role == PJSIP_ROLE_UAC || transaction->role == PJSIP_UAC_ROLE) ? true : false;
 -   mTransactionState->mCurrentState = transactionStateTranslate(transaction->state);
 -   mNeedsReplication = true;
 -}
 -
 -TransactionState PJSipTransactionModInfo::transactionStateTranslate(pjsip_tsx_state_e state)
 -{
 -   TransactionState retState;
 -   switch (state)
 -   {
 -   case PJSIP_TSX_STATE_NULL:
 -      retState = TransactionStateNull;
 - 	 break;
 -   case PJSIP_TSX_STATE_CALLING:
 -      retState = TransactionStateCalling;
 - 	 break;
 -   case PJSIP_TSX_STATE_TRYING:
 -      retState = TransactionStateTrying;
 - 	 break;
 -   case PJSIP_TSX_STATE_PROCEEDING:
 -      retState = TransactionStateProceeding;
 - 	 break;
 -   case PJSIP_TSX_STATE_COMPLETED:
 -      retState = TransactionStateCompleted;
 - 	 break;
 -   case PJSIP_TSX_STATE_CONFIRMED:
 -      retState = TransactionStateConfirmed;
 - 	 break;
 -   case PJSIP_TSX_STATE_TERMINATED:
 -      retState = TransactionStateTerminated;
 - 	 break;
 -   case PJSIP_TSX_STATE_DESTROYED:
 -      retState = TransactionStateDestroyed;
 - 	 break;
 -   default:
 -      std::cerr << "[ERROR] Unknown PJSIP Transaction state encountered: " << state << std::endl;
 - 	 retState = TransactionStateNull;
 - 	 break;
 -   }
 -   return retState;
 -}
 -
+ PJSipSessionModInfo::PJSipSessionModInfo(pjsip_inv_session *inv_session, 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);
  }
  
@@@ -248,7 -381,7 +252,6 @@@ void PJSipSessionModule::handleNewInvit
  	//handle the request. For now, just plunge forward.
  	
  	pjsip_dialog *dlg;
- 	pjsip_module module = SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule();
 -	pjsip_module *module = SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule();
  
  	//XXX The NULL parameter should be replaced with
  	//An appropriate Contact header. Leaving it NULL makes
@@@ -264,7 -397,7 +267,7 @@@
  
  	pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
  	PJSipTransactionModInfo *tsx_mod_info = new PJSipTransactionModInfo(tsx);
- 	tsx->mod_data[module.id] = (void *)tsx_mod_info;
 -	tsx->mod_data[module->id] = (void *)tsx_mod_info;
++	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.
@@@ -328,12 -467,15 +337,15 @@@
  	SipSessionPtr session = caller->createSession(destination);
  	session->setInviteSession(inv_session);
  	session->setDialog(dlg);
- 	PJSipSessionModInfo *session_mod_info = new PJSipSessionModInfo(inv_session);
- 	session_mod_info->setSessionPtr(session);
+ 	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[module.id] = (void *)session_mod_info;
- 	dlg->mod_data[module.id] = (void *)dlg_mod_info;
 -	inv_session->mod_data[module->id] = (void *)session_mod_info;
 -	dlg->mod_data[module->id] = (void *)dlg_mod_info;
++	inv_session->mod_data[mModule.id] = (void *)session_mod_info;
++	dlg->mod_data[mModule.id] = (void *)dlg_mod_info;
  
+ 	std::cerr << "[DEBUG] Replicating state on reception of new SIP INVITE." << std::endl;
+ 	replicateState(dlg_mod_info, tsx_mod_info, session_mod_info);
  	AsteriskSCF::SessionCommunications::V1::SessionRouterPrx router = dataModel.getSessionRouter();
  	try
  	{
@@@ -349,14 -492,12 +362,12 @@@
  	{
  	   // 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;
  	}
- 	// All the actual call routing is taken care of, so let's tell the state replicator
- 	// what all we learned.
- 	replicateState(dlg_mod_info, tsx_mod_info, session_mod_info);
  }
  
 -static pj_bool_t sessionOnReceiveRequest(pjsip_rx_data *rdata)
 +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)
@@@ -378,34 -519,10 +389,34 @@@
  	return PJ_TRUE;
  }
  
 -static void handle_invite_response(pjsip_inv_session *inv, pjsip_rx_data *rdata, pjsip_dialog *dlg)
 +/* 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 *rdata)
 +{
 +	return PJ_FALSE;
 +}
 +pj_status_t PJSipSessionModule::on_tx_request(pjsip_tx_data *tdata)
 +{
 +	return PJ_SUCCESS;
 +}
 +pj_status_t PJSipSessionModule::on_tx_response(pjsip_tx_data *tdata)
 +{
 +	return PJ_SUCCESS;
 +}
 +void PJSipSessionModule::on_tsx_state(pjsip_transaction *tsx, pjsip_event *e)
 +{
 +	return;
 +}
 +
 +void PJSipSessionModule::handleInviteResponse(pjsip_inv_session *inv,
 +		pjsip_rx_data *rdata, pjsip_dialog *dlg)
  {
  	int respCode = rdata->msg_info.msg->line.status.code;
- 	PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id];
 -	PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id];
++	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.
@@@ -473,7 -592,7 +484,7 @@@ void PJSipSessionModule::invOnStateChan
     }
     if (inv->state == PJSIP_INV_STATE_DISCONNECTED)
     {
-       PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id];
 -      PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id];
++      PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[mModule.id];
        if (session_mod_info == 0)
        {
  	 return;
@@@ -502,13 -621,14 +513,14 @@@
        }
  	  session_mod_info->mNeedsRemoval = true;
  	  pjsip_dialog *dlg = inv->dlg;
- 	  PJSipDialogModInfo *dlg_mod_info = (PJSipDialogModInfo*) dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id];
 -	  PJSipDialogModInfo *dlg_mod_info = (PJSipDialogModInfo*) dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id];
++	  PJSipDialogModInfo *dlg_mod_info = (PJSipDialogModInfo*) dlg->mod_data[mModule.id];
  	  dlg_mod_info->mNeedsRemoval = true;
+ 	  std::cerr << "Replicating state on DISCONNECTED inv_state." << std::endl;
  	  replicateState(dlg_mod_info, NULL, session_mod_info);
        delete session_mod_info;
  	  delete dlg_mod_info;
- 	  dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = 0;
- 	  inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = 0;
 -	  dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = 0;
 -	  inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = 0;
++	  dlg->mod_data[mModule.id] = 0;
++	  inv->mod_data[mModule.id] = 0;
     }
     if (event->type == PJSIP_EVENT_RX_MSG && inv->state == PJSIP_INV_STATE_CONFIRMED)
     {
@@@ -530,9 -650,12 +542,12 @@@
  	  //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[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id]);
- 	  inv->invite_tsx->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = (void *) tsx_mod_info;
- 	  inv->dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = (void *) dlg_mod_info;
 -	  PJSipSessionModInfo *session_mod_info = static_cast<PJSipSessionModInfo *>(inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id]);
 -	  inv->invite_tsx->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = (void *) tsx_mod_info;
 -	  inv->dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = (void *) dlg_mod_info;
++	  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;
+ 	  std::cerr << "Replicating state on new outbound INVITE." << std::endl;
  	  replicateState(dlg_mod_info, tsx_mod_info, session_mod_info);
     }
  }
@@@ -561,8 -684,8 +576,7 @@@ void PJSipSessionModule::invOnTsxStateC
  	//proper state replication in the invOnStateChanged function. This can be
  	//done simply by calling this function from there.
  
-    pjsip_module module = SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule();
- 	PJSipTransactionModInfo *tsx_mod_info = static_cast<PJSipTransactionModInfo *> (tsx->mod_data[module.id]);
 -   pjsip_module *module = SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule();
 -	PJSipTransactionModInfo *tsx_mod_info = static_cast<PJSipTransactionModInfo *> (tsx->mod_data[module->id]);
++	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)
  	{
@@@ -582,13 -705,13 +596,13 @@@
  		PJSipDialogModInfo *dlg_mod_info = NULL;
  		if (dlg)
  		{
- 			dlg_mod_info = static_cast<PJSipDialogModInfo*>(dlg->mod_data[module.id]);
 -			dlg_mod_info = static_cast<PJSipDialogModInfo*>(dlg->mod_data[module->id]);
++			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[module.id]);
 -		PJSipSessionModInfo *session_mod_info = static_cast<PJSipSessionModInfo*>(inv->mod_data[module->id]);
++		PJSipSessionModInfo *session_mod_info = static_cast<PJSipSessionModInfo*>(inv->mod_data[mModule.id]);
  		if (session_mod_info)
  		{
  			session_mod_info->updateSessionState(inv);
@@@ -601,12 -726,13 +617,13 @@@
  	}
  }
  
 -static void invOnReceiveOffer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer)
 +void PJSipSessionModule::invOnRxOffer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer)
  {
- 	//stub
 -   PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id];
++   PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[mModule.id];
+    pjsip_inv_set_sdp_answer(inv, session_mod_info->getSessionPtr()->createSDPOffer());
  }
  
 -static void invOnCreateOffer(pjsip_inv_session *inv, pjmedia_sdp_session **p_offer)
 +void PJSipSessionModule::invOnCreateOffer(pjsip_inv_session *inv, pjmedia_sdp_session **p_offer)
  {
  	//stub
  }
@@@ -628,7 -754,7 +645,7 @@@ void PJSipSessionModule::invOnMediaUpda
  
     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[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id];
 -   PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id];
++   PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[mModule.id];
     SipSessionPtr session = session_mod_info->getSessionPtr();
     session->setRemoteDetails(pj_strbuf(&remote_conn->addr), remote_sdp->media[0]->desc.port);
  
diff --cc src/SipSession.cpp
index 310ca69,9a6602a..a1842ec
--- a/src/SipSession.cpp
+++ b/src/SipSession.cpp
@@@ -349,10 -350,9 +350,9 @@@ void SipSession::start(const Ice::Curre
     }
  
     // Record our session within the dialog so code handling pjsip events can do STUFF
-    PJSipSessionModInfo *session_mod_info = new PJSipSessionModInfo(inviteSession);
     SipSessionPtr session = new SipSession(*this);
-    session_mod_info->setSessionPtr(session);
+    PJSipSessionModInfo *session_mod_info = new PJSipSessionModInfo(inviteSession, session);
 -   inviteSession->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = (void*)session_mod_info;
 +   inviteSession->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = (void*)session_mod_info;
  
     // Create the actual INVITE packet
     pjsip_tx_data *packet;

commit f077a7808c81bd744a84244556ed5428c4716932
Author: Mark Michelson <mmichelson at digium.com>
Date:   Fri Oct 1 17:33:47 2010 -0500

    Create a base PJSIP module type and allow PJSipSessionModule to derive from it.
    
    The result of this is that all PJSIP callbacks now call back into class members,
    meaning that all class members and private methods will be available as desired
    during a PJSIP callback.
    
    Because data is much more handy now, it will allow for some of the more convoluted
    methods of retrieving data to be eliminated, yo.

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 0938b12..97412e3 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -29,7 +29,10 @@ hydra_component_add_file(SipChannelService SipSession.cpp)
 hydra_component_add_file(SipChannelService SipSession.h)
 hydra_component_add_file(SipChannelService PJSipManager.cpp)
 hydra_component_add_file(SipChannelService PJSipManager.h)
+hydra_component_add_file(SipChannelService PJSipModule.cpp)
+hydra_component_add_file(SipChannelService PJSipModule.h)
 hydra_component_add_file(SipChannelService PJSipSessionModule.cpp)
+hydra_component_add_file(SipChannelService PJSipSessionModuleConstruction.cpp)
 hydra_component_add_file(SipChannelService PJSipSessionModule.h)
 hydra_component_add_file(SipChannelService SipStateReplicatorListener.cpp)
 hydra_component_add_file(SipChannelService SipStateReplicator.h)
diff --git a/src/PJSipManager.cpp b/src/PJSipManager.cpp
index 5afb5a6..3f85f7c 100644
--- a/src/PJSipManager.cpp
+++ b/src/PJSipManager.cpp
@@ -65,7 +65,7 @@ pjsip_endpoint *PJSipManager::getEndpoint()
 	return mEndpoint;
 }
 
-pjsip_module *PJSipManager::getSessionModule()
+pjsip_module &PJSipManager::getSessionModule()
 {
 	return mSessionModule->getModule();
 }
diff --git a/src/PJSipManager.h b/src/PJSipManager.h
index c6d7ec7..3c7dd0d 100644
--- a/src/PJSipManager.h
+++ b/src/PJSipManager.h
@@ -43,9 +43,9 @@ public:
 	pjsip_endpoint *getEndpoint();
 
 	/**
-	 * Get a pointer to the PJSIP session module
+	 * Get a reference to the PJSIP session module
 	 */
-	pjsip_module *getSessionModule();
+	pjsip_module &getSessionModule();
 	/**
 	 * Register the PJSipSessionModule, responsible
 	 * for basic call handling
diff --git a/src/PJSipModule.cpp b/src/PJSipModule.cpp
new file mode 100644
index 0000000..1d1a7ce
--- /dev/null
+++ b/src/PJSipModule.cpp
@@ -0,0 +1,142 @@
+/*
+ * Asterisk Scalable Communications Framework
+ *
+ * Copyright (C) 2010 -- Digium, Inc.
+ *
+ * All rights reserved.
+ */
+
+#include <IceUtil/UUID.h>
+#include "PJSipModule.h"
+
+namespace AsteriskSCF
+{
+
+namespace SipChannelService
+{
+
+//Arbitrarily chosen URI size. Used for times when I need
+//to retrieve PJSIP URI strings.
+const int URI_SIZE = 64;
+
+PJSipDialogModInfo::PJSipDialogModInfo(pjsip_dialog *dialog) : mDialogState(new SipDialogStateItem) , mNeedsReplication(true), mNeedsRemoval(false)
+{
+   //XXX Is there a way to tell ICE to make the default
+   //constructor for SipStateItem set key?
+   mDialogState->key = IceUtil::generateUUID();
+   updateDialogState(dialog);
+}
+
+PJSipDialogModInfo::~PJSipDialogModInfo()
+{
+   mDialogState = 0;
+}
+
+/**
+ * Copies data from the pjsip_dialog structure into
+ * our state data.
+ *
+ * XXX This could likely be optimized to only update
+ * stuff that needs updating. Important first step
+ * is to get it working.
+ */
+void PJSipDialogModInfo::updateDialogState(pjsip_dialog *dialog)
+{
+   mDialogState->mCallId = std::string(pj_strbuf(&dialog->call_id->id), pj_strlen(&dialog->call_id->id));
+   mDialogState->mIsDialogEstablished = dialog->state == PJSIP_DIALOG_STATE_ESTABLISHED ? true : false;
+   mDialogState->mIsSecure = dialog->secure == PJ_TRUE ? true : false;
+
+   mDialogState->mLocalCSeq = dialog->local.cseq;
+   mDialogState->mLocalTag = std::string(pj_strbuf(&dialog->local.info->tag), pj_strlen(&dialog->local.info->tag));
+   char localUri[URI_SIZE];
+   pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, dialog->local.info->uri, localUri, sizeof(localUri));
+   mDialogState->mLocalUri = localUri;
+
+   mDialogState->mRemoteCSeq = dialog->remote.cseq;
+   mDialogState->mRemoteTag = std::string(pj_strbuf(&dialog->remote.info->tag), pj_strlen(&dialog->remote.info->tag));
+   char remoteUri[URI_SIZE];
+   pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, dialog->remote.info->uri, remoteUri, sizeof(remoteUri));
+   mDialogState->mRemoteUri = remoteUri;
+   
+   //This is a place where the whole "only change what you need" optimization
+   //will come in super-handy since we have to erase and rebuild the route set
+   //every time. My recollection is that the route set can only be established
+   //in the first transaction of a dialog so this is purdy wasteful.
+   mDialogState->mRouteSet.erase(mDialogState->mRouteSet.begin(), mDialogState->mRouteSet.end());
+   for (pjsip_route_hdr *hdr_iter = dialog->route_set.next; hdr_iter != &dialog->route_set; hdr_iter = hdr_iter->next)
+   {
+ 	 char routeUri[URI_SIZE];
+ 	 pjsip_uri_print(PJSIP_URI_IN_ROUTING_HDR, hdr_iter->name_addr.uri, routeUri, sizeof(routeUri));
+
+      //XXX I need to see the order in which the route set is represented
+ 	 //in a pjsip dialog. push_back could be completely incorrect here,
+ 	 //or it may only be correct depending on whether we're the UAC or UAS.
+ 	 //Also, for now we're ignoring the optional display name portion of URIs
+ 	 //in the route set. They're not strictly necessary for routing calls correctly.
+ 	 mDialogState->mRouteSet.push_back(routeUri);
+   }
+   //XXX I'm not 100% sure how to get the transport from a pjsip_dialog, so for now,
+   //I'll just hardcode this as UDP until we start wanting to actually implement
+   //other transports.
+   mDialogState->mTransport = "UDP";
+   mNeedsReplication = true;
+}
+
+PJSipTransactionModInfo::PJSipTransactionModInfo(pjsip_transaction *transaction) : mTransactionState(new SipTransactionStateItem), mNeedsReplication(true), mNeedsRemoval(false)
+{
+   mTransactionState->key = IceUtil::generateUUID();
+   updateTransactionState(transaction);
+}
+
+PJSipTransactionModInfo::~PJSipTransactionModInfo()
+{
+   mTransactionState = 0;
+}
+
+void PJSipTransactionModInfo::updateTransactionState(pjsip_transaction *transaction)
+{
+   mTransactionState->mBranch = std::string(pj_strbuf(&transaction->branch), pj_strlen(&transaction->branch));
+   mTransactionState->mIsClient = (transaction->role == PJSIP_ROLE_UAC || transaction->role == PJSIP_UAC_ROLE) ? true : false;
+   mTransactionState->mCurrentState = transactionStateTranslate(transaction->state);
+   mNeedsReplication = true;
+}
+
+TransactionState PJSipTransactionModInfo::transactionStateTranslate(pjsip_tsx_state_e state)
+{
+   TransactionState retState;
+   switch (state)
+   {
+   case PJSIP_TSX_STATE_NULL:
+      retState = TransactionStateNull;
+ 	 break;
+   case PJSIP_TSX_STATE_CALLING:
+      retState = TransactionStateCalling;
+ 	 break;
+   case PJSIP_TSX_STATE_TRYING:
+      retState = TransactionStateTrying;
+ 	 break;
+   case PJSIP_TSX_STATE_PROCEEDING:
+      retState = TransactionStateProceeding;
+ 	 break;
+   case PJSIP_TSX_STATE_COMPLETED:
+      retState = TransactionStateCompleted;
+ 	 break;
+   case PJSIP_TSX_STATE_CONFIRMED:
+      retState = TransactionStateConfirmed;
+ 	 break;
+   case PJSIP_TSX_STATE_TERMINATED:
+      retState = TransactionStateTerminated;
+ 	 break;
+   case PJSIP_TSX_STATE_DESTROYED:
+      retState = TransactionStateDestroyed;
+ 	 break;
+   default:
+      std::cerr << "[ERROR] Unknown PJSIP Transaction state encountered: " << state << std::endl;
+ 	 retState = TransactionStateNull;
+ 	 break;
+   }
+   return retState;
+}
+
+};
+};
diff --git a/src/PJSipSessionModule.h b/src/PJSipModule.h
similarity index 59%
copy from src/PJSipSessionModule.h
copy to src/PJSipModule.h
index 208fadb..3f57528 100644
--- a/src/PJSipSessionModule.h
+++ b/src/PJSipModule.h
@@ -8,12 +8,13 @@
 
 #pragma once
 
+#include <string>
+
 #include <pjsip.h>
 #include <pjsip_ua.h>
 #include <pjlib.h>
 
-#include "SipStateReplicator.h"
-#include "SipSession.h"
+#include "SipStateReplicationIf.h"
 
 namespace AsteriskSCF
 {
@@ -21,15 +22,24 @@ namespace AsteriskSCF
 namespace SipChannelService
 {
 
-class PJSipSessionModule
+using namespace AsteriskSCF::SIP::V1;
+
+class PJSipModule
 {
 public:
-	PJSipSessionModule();
-	pjsip_module *getModule();
-private:
+	virtual pj_status_t load(pjsip_endpoint *endpoint) = 0;
+	virtual pj_status_t start() = 0;
+	virtual pj_status_t stop() = 0;
+	virtual pj_status_t unload() = 0;
+	virtual pj_bool_t on_rx_request(pjsip_rx_data *rdata) = 0;
+	virtual pj_bool_t on_rx_response(pjsip_rx_data *rdata) = 0;
+	virtual pj_status_t on_tx_request(pjsip_tx_data *tdata) = 0;
+	virtual pj_status_t on_tx_response(pjsip_tx_data *tdata) = 0;
+	virtual void on_tsx_state(pjsip_transaction *tsx, pjsip_event *event) = 0;
+	pjsip_module &getModule() { return mModule; };
+protected:
 	pjsip_module mModule;
-	pjsip_inv_callback mInvCallback;
-	pjsip_ua_init_param mUaParam;
+private:
 	const std::string mName;
 };
 
@@ -57,22 +67,5 @@ private:
 	TransactionState transactionStateTranslate(pjsip_tsx_state_e state);
 };
 
-class PJSipSessionModInfo
-{
-public:
-	PJSipSessionModInfo(pjsip_inv_session *inv_session);
-	~PJSipSessionModInfo();
-	void updateSessionState(pjsip_inv_session *inv_session);
-	SipSessionPtr getSessionPtr();
-	void setSessionPtr(SipSessionPtr sessionPtr);
-	SipSessionStateItemPtr mSessionState;
-	SipInviteSessionStateItemPtr mInviteState;
-	bool mNeedsReplication;
-	bool mNeedsRemoval;
-private:
-	InviteSessionState inviteStateTranslate(pjsip_inv_state state);
-	SipSessionPtr mSession;
-};
-
 }; //end namespace SipChannelService
 }; //end namespace AsteriskSCF
diff --git a/src/PJSipSessionModule.cpp b/src/PJSipSessionModule.cpp
index a57f2e8..c4209bf 100644
--- a/src/PJSipSessionModule.cpp
+++ b/src/PJSipSessionModule.cpp
@@ -34,129 +34,6 @@ using namespace AsteriskSCF::SessionCommunications::V1;
 using namespace AsteriskSCF::Media::V1;
 using namespace AsteriskSCF::SIP::V1;
 
-//Arbitrarily chosen URI size. Used for times when I need
-//to retrieve PJSIP URI strings.
-const int URI_SIZE = 64;
-
-PJSipDialogModInfo::PJSipDialogModInfo(pjsip_dialog *dialog) : mDialogState(new SipDialogStateItem) , mNeedsReplication(true), mNeedsRemoval(false)
-{
-   //XXX Is there a way to tell ICE to make the default
-   //constructor for SipStateItem set key?
-   mDialogState->key = IceUtil::generateUUID();
-   updateDialogState(dialog);
-}
-
-PJSipDialogModInfo::~PJSipDialogModInfo()
-{
-   mDialogState = 0;
-}
-
-/**
- * Copies data from the pjsip_dialog structure into
- * our state data.
- *
- * XXX This could likely be optimized to only update
- * stuff that needs updating. Important first step
- * is to get it working.
- */
-void PJSipDialogModInfo::updateDialogState(pjsip_dialog *dialog)
-{
-   mDialogState->mCallId = std::string(pj_strbuf(&dialog->call_id->id), pj_strlen(&dialog->call_id->id));
-   mDialogState->mIsDialogEstablished = dialog->state == PJSIP_DIALOG_STATE_ESTABLISHED ? true : false;
-   mDialogState->mIsSecure = dialog->secure == PJ_TRUE ? true : false;
-
-   mDialogState->mLocalCSeq = dialog->local.cseq;
-   mDialogState->mLocalTag = std::string(pj_strbuf(&dialog->local.info->tag), pj_strlen(&dialog->local.info->tag));
-   char localUri[URI_SIZE];
-   pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, dialog->local.info->uri, localUri, sizeof(localUri));
-   mDialogState->mLocalUri = localUri;
-
-   mDialogState->mRemoteCSeq = dialog->remote.cseq;
-   mDialogState->mRemoteTag = std::string(pj_strbuf(&dialog->remote.info->tag), pj_strlen(&dialog->remote.info->tag));
-   char remoteUri[URI_SIZE];
-   pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, dialog->remote.info->uri, remoteUri, sizeof(remoteUri));
-   mDialogState->mRemoteUri = remoteUri;
-   
-   //This is a place where the whole "only change what you need" optimization
-   //will come in super-handy since we have to erase and rebuild the route set
-   //every time. My recollection is that the route set can only be established
-   //in the first transaction of a dialog so this is purdy wasteful.
-   mDialogState->mRouteSet.erase(mDialogState->mRouteSet.begin(), mDialogState->mRouteSet.end());
-   for (pjsip_route_hdr *hdr_iter = dialog->route_set.next; hdr_iter != &dialog->route_set; hdr_iter = hdr_iter->next)
-   {
- 	 char routeUri[URI_SIZE];
- 	 pjsip_uri_print(PJSIP_URI_IN_ROUTING_HDR, hdr_iter->name_addr.uri, routeUri, sizeof(routeUri));
-
-      //XXX I need to see the order in which the route set is represented
- 	 //in a pjsip dialog. push_back could be completely incorrect here,
- 	 //or it may only be correct depending on whether we're the UAC or UAS.
- 	 //Also, for now we're ignoring the optional display name portion of URIs
- 	 //in the route set. They're not strictly necessary for routing calls correctly.
- 	 mDialogState->mRouteSet.push_back(routeUri);
-   }
-   //XXX I'm not 100% sure how to get the transport from a pjsip_dialog, so for now,
-   //I'll just hardcode this as UDP until we start wanting to actually implement
-   //other transports.
-   mDialogState->mTransport = "UDP";
-   mNeedsReplication = true;
-}
-
-PJSipTransactionModInfo::PJSipTransactionModInfo(pjsip_transaction *transaction) : mTransactionState(new SipTransactionStateItem), mNeedsReplication(true), mNeedsRemoval(false)
-{
-   mTransactionState->key = IceUtil::generateUUID();
-   updateTransactionState(transaction);
-}
-
-PJSipTransactionModInfo::~PJSipTransactionModInfo()
-{
-   mTransactionState = 0;
-}
-
-void PJSipTransactionModInfo::updateTransactionState(pjsip_transaction *transaction)
-{
-   mTransactionState->mBranch = std::string(pj_strbuf(&transaction->branch), pj_strlen(&transaction->branch));
-   mTransactionState->mIsClient = (transaction->role == PJSIP_ROLE_UAC || transaction->role == PJSIP_UAC_ROLE) ? true : false;
-   mTransactionState->mCurrentState = transactionStateTranslate(transaction->state);
-   mNeedsReplication = true;
-}
-
-TransactionState PJSipTransactionModInfo::transactionStateTranslate(pjsip_tsx_state_e state)
-{
-   TransactionState retState;
-   switch (state)
-   {
-   case PJSIP_TSX_STATE_NULL:
-      retState = TransactionStateNull;
- 	 break;
-   case PJSIP_TSX_STATE_CALLING:
-      retState = TransactionStateCalling;
- 	 break;
-   case PJSIP_TSX_STATE_TRYING:
-      retState = TransactionStateTrying;
- 	 break;
-   case PJSIP_TSX_STATE_PROCEEDING:
-      retState = TransactionStateProceeding;
- 	 break;
-   case PJSIP_TSX_STATE_COMPLETED:
-      retState = TransactionStateCompleted;
- 	 break;
-   case PJSIP_TSX_STATE_CONFIRMED:
-      retState = TransactionStateConfirmed;
- 	 break;
-   case PJSIP_TSX_STATE_TERMINATED:
-      retState = TransactionStateTerminated;
- 	 break;
-   case PJSIP_TSX_STATE_DESTROYED:
-      retState = TransactionStateDestroyed;
- 	 break;
-   default:
-      std::cerr << "[ERROR] Unknown PJSIP Transaction state encountered: " << state << std::endl;
- 	 retState = TransactionStateNull;
- 	 break;
-   }
-   return retState;
-}
-
 PJSipSessionModInfo::PJSipSessionModInfo(pjsip_inv_session *inv_session) : mSessionState(new SipSessionStateItem), mInviteState(new SipInviteSessionStateItem), mNeedsReplication(true), mNeedsRemoval(false)
 {
    mSessionState->key = IceUtil::generateUUID();
@@ -243,7 +120,7 @@ InviteSessionState PJSipSessionModInfo::inviteStateTranslate(pjsip_inv_state sta
    return retState;
 }
 
-static void replicateState(PJSipDialogModInfo *dlgInfo, PJSipTransactionModInfo *tsxInfo,
+void PJSipSessionModule::replicateState(PJSipDialogModInfo *dlgInfo, PJSipTransactionModInfo *tsxInfo,
 		PJSipSessionModInfo *sessionInfo)
 {
 	SipStateItemSeq setItems;
@@ -331,31 +208,27 @@ static void replicateState(PJSipDialogModInfo *dlgInfo, PJSipTransactionModInfo
 	}
 }
 
-static pj_status_t sessionLoad(pjsip_endpoint *endpt)
+pj_status_t PJSipSessionModule::load(pjsip_endpoint *endpt)
 {
-	//stub
 	return PJ_SUCCESS;
 }
 
-static pj_status_t sessionStart()
+pj_status_t PJSipSessionModule::start()
 {
-	//stub
 	return PJ_SUCCESS;
 }
 
-static pj_status_t sessionStop()
+pj_status_t PJSipSessionModule::stop()
 {
-	//stub
 	return PJ_SUCCESS;
 }
 
-static pj_status_t sessionUnload()
+pj_status_t PJSipSessionModule::unload()
 {
-	//stub
 	return PJ_SUCCESS;
 }
 
-static void handle_new_invite(pjsip_rx_data *rdata)
+void PJSipSessionModule::handleNewInvite(pjsip_rx_data *rdata)
 {
 	//What do we do here?
 	//
@@ -375,7 +248,7 @@ static void handle_new_invite(pjsip_rx_data *rdata)
 	//handle the request. For now, just plunge forward.
 	
 	pjsip_dialog *dlg;
-	pjsip_module *module = SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule();
+	pjsip_module module = SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule();
 
 	//XXX The NULL parameter should be replaced with
 	//An appropriate Contact header. Leaving it NULL makes
@@ -391,7 +264,7 @@ static void handle_new_invite(pjsip_rx_data *rdata)
 
 	pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
 	PJSipTransactionModInfo *tsx_mod_info = new PJSipTransactionModInfo(tsx);
-	tsx->mod_data[module->id] = (void *)tsx_mod_info;
+	tsx->mod_data[module.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.
@@ -458,8 +331,8 @@ static void handle_new_invite(pjsip_rx_data *rdata)
 	PJSipSessionModInfo *session_mod_info = new PJSipSessionModInfo(inv_session);
 	session_mod_info->setSessionPtr(session);
 
-	inv_session->mod_data[module->id] = (void *)session_mod_info;
-	dlg->mod_data[module->id] = (void *)dlg_mod_info;
+	inv_session->mod_data[module.id] = (void *)session_mod_info;
+	dlg->mod_data[module.id] = (void *)dlg_mod_info;
 
 	AsteriskSCF::SessionCommunications::V1::SessionRouterPrx router = dataModel.getSessionRouter();
 	try
@@ -483,7 +356,7 @@ static void handle_new_invite(pjsip_rx_data *rdata)
 	replicateState(dlg_mod_info, tsx_mod_info, session_mod_info);
 }
 
-static pj_bool_t sessionOnReceiveRequest(pjsip_rx_data *rdata)
+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)
@@ -491,7 +364,7 @@ static pj_bool_t sessionOnReceiveRequest(pjsip_rx_data *rdata)
 	case PJSIP_INVITE_METHOD:
 		if (dlg == NULL)
 		{
-			handle_new_invite(rdata);
+			handleNewInvite(rdata);
 		}
 		else
 		{
@@ -505,10 +378,34 @@ static pj_bool_t sessionOnReceiveRequest(pjsip_rx_data *rdata)
 	return PJ_TRUE;
 }
 
-static void handle_invite_response(pjsip_inv_session *inv, pjsip_rx_data *rdata, pjsip_dialog *dlg)
+/* 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 *rdata)
+{
+	return PJ_FALSE;
+}
+pj_status_t PJSipSessionModule::on_tx_request(pjsip_tx_data *tdata)
+{
+	return PJ_SUCCESS;
+}
+pj_status_t PJSipSessionModule::on_tx_response(pjsip_tx_data *tdata)
+{
+	return PJ_SUCCESS;
+}
+void PJSipSessionModule::on_tsx_state(pjsip_transaction *tsx, pjsip_event *e)
+{
+	return;
+}
+
+void PJSipSessionModule::handleInviteResponse(pjsip_inv_session *inv,
+		pjsip_rx_data *rdata, pjsip_dialog *dlg)
 {
 	int respCode = rdata->msg_info.msg->line.status.code;
-	PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id];
+	PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id];
 	SipSessionPtr session = session_mod_info->getSessionPtr();
 	//Commented because they are currently unused. They
 	//will be once the individual cases are mapped out.
@@ -565,20 +462,18 @@ static void handle_invite_response(pjsip_inv_session *inv, pjsip_rx_data *rdata,
 	}
 }
 
-static void invOnTransactionStateChanged(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *event);
-
-static void invOnStateChanged(pjsip_inv_session *inv, pjsip_event *event)
+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.
-	  handle_invite_response(inv, event->body.tsx_state.src.rdata, inv->dlg);
+	  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[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id];
+      PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id];
       if (session_mod_info == 0)
       {
 	 return;
@@ -607,13 +502,13 @@ static void invOnStateChanged(pjsip_inv_session *inv, pjsip_event *event)
       }
 	  session_mod_info->mNeedsRemoval = true;
 	  pjsip_dialog *dlg = inv->dlg;
-	  PJSipDialogModInfo *dlg_mod_info = (PJSipDialogModInfo*) dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id];
+	  PJSipDialogModInfo *dlg_mod_info = (PJSipDialogModInfo*) dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id];
 	  dlg_mod_info->mNeedsRemoval = true;
 	  replicateState(dlg_mod_info, NULL, session_mod_info);
       delete session_mod_info;
 	  delete dlg_mod_info;
-	  dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = 0;
-	  inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = 0;
+	  dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = 0;
+	  inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = 0;
    }
    if (event->type == PJSIP_EVENT_RX_MSG && inv->state == PJSIP_INV_STATE_CONFIRMED)
    {
@@ -624,7 +519,7 @@ static void invOnStateChanged(pjsip_inv_session *inv, pjsip_event *event)
 	  if (inv->invite_tsx && pj_strcmp(&event->body.rx_msg.rdata->msg_info.via->branch_param, &inv->invite_tsx->branch) != 0)
 	  {
 	     //Mismatched branch!
-		 invOnTransactionStateChanged(inv, inv->invite_tsx, event);
+		 invOnTsxStateChanged(inv, inv->invite_tsx, event);
 	  }
    }
    if (event->type == PJSIP_EVENT_TSX_STATE && inv->state == PJSIP_INV_STATE_CALLING && inv->role == PJSIP_ROLE_UAC)
@@ -635,19 +530,19 @@ static void invOnStateChanged(pjsip_inv_session *inv, pjsip_event *event)
 	  //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[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id]);
-	  inv->invite_tsx->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = (void *) tsx_mod_info;
-	  inv->dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = (void *) dlg_mod_info;
+	  PJSipSessionModInfo *session_mod_info = static_cast<PJSipSessionModInfo *>(inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id]);
+	  inv->invite_tsx->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = (void *) tsx_mod_info;
+	  inv->dlg->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = (void *) dlg_mod_info;
 	  replicateState(dlg_mod_info, tsx_mod_info, session_mod_info);
    }
 }
 
-static void invOnNewSession(pjsip_inv_session *inv, pjsip_event *event)
+void PJSipSessionModule::invOnNewSession(pjsip_inv_session *inv, pjsip_event *event)
 {
 	//stub
 }
 
-static void invOnTransactionStateChanged(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
+void PJSipSessionModule::invOnTsxStateChanged(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
 {
 	//This will be our key point for updating transaction state.
 	//This function will not be called until after a module has registered
@@ -666,8 +561,8 @@ static void invOnTransactionStateChanged(pjsip_inv_session *inv, pjsip_transacti
 	//proper state replication in the invOnStateChanged function. This can be
 	//done simply by calling this function from there.
 
-   pjsip_module *module = SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule();
-	PJSipTransactionModInfo *tsx_mod_info = static_cast<PJSipTransactionModInfo *> (tsx->mod_data[module->id]);
+   pjsip_module module = SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule();
+	PJSipTransactionModInfo *tsx_mod_info = static_cast<PJSipTransactionModInfo *> (tsx->mod_data[module.id]);
 	// TODO: Determine if this is perfectly acceptable, since it occurs when the call is going bye bye
 	if (!tsx_mod_info)
 	{
@@ -687,13 +582,13 @@ static void invOnTransactionStateChanged(pjsip_inv_session *inv, pjsip_transacti
 		PJSipDialogModInfo *dlg_mod_info = NULL;
 		if (dlg)
 		{
-			dlg_mod_info = static_cast<PJSipDialogModInfo*>(dlg->mod_data[module->id]);
+			dlg_mod_info = static_cast<PJSipDialogModInfo*>(dlg->mod_data[module.id]);
 			if (dlg_mod_info)
 			{
 				dlg_mod_info->updateDialogState(dlg);
 			}
 		}
-		PJSipSessionModInfo *session_mod_info = static_cast<PJSipSessionModInfo*>(inv->mod_data[module->id]);
+		PJSipSessionModInfo *session_mod_info = static_cast<PJSipSessionModInfo*>(inv->mod_data[module.id]);
 		if (session_mod_info)
 		{
 			session_mod_info->updateSessionState(inv);
@@ -706,17 +601,17 @@ static void invOnTransactionStateChanged(pjsip_inv_session *inv, pjsip_transacti
 	}
 }
 
-static void invOnReceiveOffer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer)
+void PJSipSessionModule::invOnRxOffer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer)
 {
 	//stub
 }
 
-static void invOnCreateOffer(pjsip_inv_session *inv, pjmedia_sdp_session **p_offer)
+void PJSipSessionModule::invOnCreateOffer(pjsip_inv_session *inv, pjmedia_sdp_session **p_offer)
 {
 	//stub
 }
 
-static void invOnMediaUpdate(pjsip_inv_session *inv, pj_status_t status)
+void PJSipSessionModule::invOnMediaUpdate(pjsip_inv_session *inv, pj_status_t status)
 {
    if (status != PJ_SUCCESS)
    {
@@ -733,7 +628,7 @@ static void invOnMediaUpdate(pjsip_inv_session *inv, pj_status_t status)
 
    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[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id];
+   PJSipSessionModInfo *session_mod_info = (PJSipSessionModInfo*)inv->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id];
    SipSessionPtr session = session_mod_info->getSessionPtr();
    session->setRemoteDetails(pj_strbuf(&remote_conn->addr), remote_sdp->media[0]->desc.port);
 
@@ -816,74 +711,18 @@ static void invOnMediaUpdate(pjsip_inv_session *inv, pj_status_t status)
       }
    }
 }
-//Comment this out for now so pjsip will send the ACK for us.
-//static void invOnSendAck(pjsip_inv_session *inv, pjsip_rx_data *rdata)
-//{
-//	//stub
-//}
 
-static pjsip_redirect_op invOnRedirected(pjsip_inv_session *inv, const pjsip_uri *target, const pjsip_event *e)
+pjsip_redirect_op PJSipSessionModule::invOnRedirected(pjsip_inv_session *inv, const pjsip_uri *target, const pjsip_event *e)
 {
 	//stub
 	return PJSIP_REDIRECT_REJECT;
 }
 
-static pjsip_dialog *uaOnDialogForked(pjsip_dialog *first_set, pjsip_rx_data *rdata)
+pjsip_dialog *PJSipSessionModule::uaOnDialogForked(pjsip_dialog *first_set, pjsip_rx_data *rdata)
 {
 	//stub
 	return NULL;
 }
 
-PJSipSessionModule::PJSipSessionModule() : mName("Session Module")
-{
-	// XXX Set the module name here. It's a pain in the neck
-	// to deal with pj_str_t in C++...
-	char name[] = "AsteriskSCF";
-	mModule.name = pj_str(name);
-	mModule.priority = PJSIP_MOD_PRIORITY_APPLICATION;
-	mModule.load = sessionLoad;
-	mModule.start = sessionStart;
-	mModule.stop = sessionStop;
-	mModule.unload = sessionUnload;
-	mModule.on_rx_request = sessionOnReceiveRequest;
-	mModule.on_rx_response = NULL;
-	mModule.on_tx_request = NULL;
-	mModule.on_tx_response = NULL;
-	mModule.on_tsx_state = NULL;
-
-	if (pjsip_ua_instance()->id == -1)
-	{
-		pj_bzero(&mUaParam, sizeof(&mUaParam));
-		mUaParam.on_dlg_forked = uaOnDialogForked;
-	}
-
-	if (pjsip_inv_usage_instance()->id == -1)
-	{
-		pj_bzero(&mInvCallback, sizeof(mInvCallback));
-		mInvCallback.on_state_changed = invOnStateChanged;
-		mInvCallback.on_new_session = invOnNewSession;
-		mInvCallback.on_tsx_state_changed = invOnTransactionStateChanged;
-		mInvCallback.on_rx_offer = invOnReceiveOffer;
-		mInvCallback.on_create_offer = invOnCreateOffer;
-		mInvCallback.on_media_update = invOnMediaUpdate;
-		//mInvCallback.on_send_ack = invOnSendAck;
-		mInvCallback.on_redirected = invOnRedirected;
-	}
-	
-	SipChannelServiceDataModel &dataModel = SipChannelServiceDataModel::getInstance();
-	PJSipManager *manager = dataModel.getPJSipManager();
-	pjsip_endpoint *endpt = manager->getEndpoint();
-	pjsip_tsx_layer_init_module(endpt);
-	pjsip_ua_init_module(endpt, &mUaParam);
-	pjsip_100rel_init_module(endpt);
-	pjsip_inv_usage_init(endpt, &mInvCallback);
-	pjsip_endpt_register_module(endpt, &mModule);
-}
-
-pjsip_module *PJSipSessionModule::getModule()
-{
-	return &mModule;
-}
-
 }; //end namespace SipChannelService
 }; //end namespace AsteriskSCF
diff --git a/src/PJSipSessionModule.h b/src/PJSipSessionModule.h
index 208fadb..8d58dac 100644
--- a/src/PJSipSessionModule.h
+++ b/src/PJSipSessionModule.h
@@ -14,6 +14,7 @@
 
 #include "SipStateReplicator.h"
 #include "SipSession.h"
+#include "PJSipModule.h"
 
 namespace AsteriskSCF
 {
@@ -21,42 +22,6 @@ namespace AsteriskSCF
 namespace SipChannelService
 {
 
-class PJSipSessionModule
-{
-public:
-	PJSipSessionModule();
-	pjsip_module *getModule();
-private:
-	pjsip_module mModule;
-	pjsip_inv_callback mInvCallback;
-	pjsip_ua_init_param mUaParam;
-	const std::string mName;
-};
-
-class PJSipDialogModInfo
-{
-public:
-	PJSipDialogModInfo(pjsip_dialog *dialog);
-	~PJSipDialogModInfo();
-	void updateDialogState(pjsip_dialog *dialog);
-	SipDialogStateItemPtr mDialogState;
-	bool mNeedsReplication;
-	bool mNeedsRemoval;
-};
-
-class PJSipTransactionModInfo
-{
-public:
-	PJSipTransactionModInfo(pjsip_transaction *transaction);
-	~PJSipTransactionModInfo();
-	void updateTransactionState(pjsip_transaction *transaction);
-	SipTransactionStateItemPtr mTransactionState;
-	bool mNeedsReplication;
-	bool mNeedsRemoval;
-private:
-	TransactionState transactionStateTranslate(pjsip_tsx_state_e state);
-};
-
 class PJSipSessionModInfo
 {
 public:
@@ -74,5 +39,37 @@ private:
 	SipSessionPtr mSession;
 };
 
+class PJSipSessionModule : public PJSipModule
+{
+public:
+	PJSipSessionModule();
+	pj_status_t load(pjsip_endpoint *endpoint);
+	pj_status_t start();
+	pj_status_t stop();
+	pj_status_t unload();
+	pj_bool_t on_rx_request(pjsip_rx_data *rdata);
+	pj_bool_t on_rx_response(pjsip_rx_data *rdata);
+	pj_status_t on_tx_request(pjsip_tx_data *tdata);
+	pj_status_t on_tx_response(pjsip_tx_data *tdata);
+	void on_tsx_state(pjsip_transaction *tsx, pjsip_event *event);
+	void invOnStateChanged(pjsip_inv_session *inv, pjsip_event *e);
+	void invOnNewSession(pjsip_inv_session *inv, pjsip_event *e);
+	void invOnTsxStateChanged(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e);
+	void invOnRxOffer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer);
+	void invOnCreateOffer(pjsip_inv_session *inv, pjmedia_sdp_session **p_offer);
+	void invOnMediaUpdate(pjsip_inv_session *inv, pj_status_t status);
+	pjsip_redirect_op invOnRedirected(pjsip_inv_session *inv, const pjsip_uri *target, const pjsip_event *e);
+	pjsip_dialog *uaOnDialogForked(pjsip_dialog *first_set, pjsip_rx_data *rdata);
+	// Missing onsendack for now
+private:
+	void replicateState(PJSipDialogModInfo *dlgInfo, PJSipTransactionModInfo *tsxInfo,
+		PJSipSessionModInfo *sessionInfo);
+	void handleNewInvite(pjsip_rx_data *rdata);
+	void handleInviteResponse(pjsip_inv_session *inv, pjsip_rx_data *rdata, pjsip_dialog *dlg);
+	pjsip_inv_callback mInvCallback;
+	pjsip_ua_init_param mUaParam;
+	const std::string mName;
+};
+
 }; //end namespace SipChannelService
 }; //end namespace AsteriskSCF
diff --git a/src/PJSipSessionModuleConstruction.cpp b/src/PJSipSessionModuleConstruction.cpp
new file mode 100644
index 0000000..5cb830a
--- /dev/null
+++ b/src/PJSipSessionModuleConstruction.cpp
@@ -0,0 +1,129 @@
+/*
+ * Asterisk Scalable Communications Framework
+ *
+ * Copyright (C) 2010 -- Digium, Inc.
+ *
+ * All rights reserved.
+ */
+
+#include "PJSipSessionModule.h"
+#include "SipChannelServiceDataModel.h"
+
+namespace AsteriskSCF
+{
+
+namespace SipChannelService
+{
+
+static PJSipSessionModule *sessionModule;
+
+static pj_status_t sessionLoad(pjsip_endpoint *endpoint)
+{
+	return sessionModule->load(endpoint);
+}
+
+static pj_status_t sessionStart()
+{
+	return sessionModule->unload();
+}
+
+static pj_status_t sessionStop()
+{
+	return sessionModule->stop();
+}
+
+static pj_status_t sessionUnload()
+{
+	return sessionModule->unload();
+}
+
+static pj_bool_t sessionOnRxRequest(pjsip_rx_data *rdata)
+{
+	return sessionModule->on_rx_request(rdata);
+}
+
+static void invOnStateChanged(pjsip_inv_session *inv, pjsip_event *e)
+{
+	sessionModule->invOnStateChanged(inv, e);
+}
+
+static void invOnNewSession(pjsip_inv_session *inv, pjsip_event *e)
+{
+	sessionModule->invOnNewSession(inv, e);
+}
+
+static void invOnTsxStateChanged(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
+{
+	sessionModule->invOnTsxStateChanged(inv, tsx, e);
+}
+
+static void invOnRxOffer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer)
+{
+	sessionModule->invOnRxOffer(inv, offer);
+}
+
+static void invOnCreateOffer(pjsip_inv_session *inv, pjmedia_sdp_session **p_offer)
+{
+	sessionModule->invOnCreateOffer(inv, p_offer);
+}
+
+static void invOnMediaUpdate(pjsip_inv_session *inv, pj_status_t status)
+{
+	sessionModule->invOnMediaUpdate(inv, status);
+}
+
+static pjsip_redirect_op invOnRedirected(pjsip_inv_session *inv, const pjsip_uri *target, const pjsip_event *e)
+{
+	return sessionModule->invOnRedirected(inv, target, e);
+}
+
+static pjsip_dialog *uaOnDialogForked(pjsip_dialog *first_set, pjsip_rx_data *rdata)
+{
+	return sessionModule->uaOnDialogForked(first_set, rdata);
+}
+
+PJSipSessionModule::PJSipSessionModule() : mName("Session Module")
+{
+	mModule.priority = PJSIP_MOD_PRIORITY_APPLICATION;
+	mModule.load = sessionLoad;
+	mModule.start = sessionStart;
+	mModule.stop = sessionStop;
+	mModule.unload = sessionUnload;
+	mModule.on_rx_request = sessionOnRxRequest;
+	mModule.on_rx_response = NULL;
+	mModule.on_tx_request = NULL;
+	mModule.on_tx_response = NULL;
+	mModule.on_tsx_state = NULL;
+
+	if (pjsip_ua_instance()->id == -1)
+	{
+		pj_bzero(&mUaParam, sizeof(&mUaParam));
+		mUaParam.on_dlg_forked = AsteriskSCF::SipChannelService::uaOnDialogForked;
+	}
+
+	if (pjsip_inv_usage_instance()->id == -1)
+	{
+		pj_bzero(&mInvCallback, sizeof(mInvCallback));
+		mInvCallback.on_state_changed = AsteriskSCF::SipChannelService::invOnStateChanged;
+		mInvCallback.on_new_session = AsteriskSCF::SipChannelService::invOnNewSession;
+		mInvCallback.on_tsx_state_changed = AsteriskSCF::SipChannelService::invOnTsxStateChanged;
+		mInvCallback.on_rx_offer = AsteriskSCF::SipChannelService::invOnRxOffer;
+		mInvCallback.on_create_offer = AsteriskSCF::SipChannelService::invOnCreateOffer;
+		mInvCallback.on_media_update = AsteriskSCF::SipChannelService::invOnMediaUpdate;
+		//mInvCallback.on_send_ack = invOnSendAck;
+		mInvCallback.on_redirected = AsteriskSCF::SipChannelService::invOnRedirected;
+	}
+	
+	SipChannelServiceDataModel &dataModel = SipChannelServiceDataModel::getInstance();
+	PJSipManager *manager = dataModel.getPJSipManager();
+	pjsip_endpoint *endpt = manager->getEndpoint();
+	pjsip_tsx_layer_init_module(endpt);
+	pjsip_ua_init_module(endpt, &mUaParam);
+	pjsip_100rel_init_module(endpt);
+	pjsip_inv_usage_init(endpt, &mInvCallback);
+	pjsip_endpt_register_module(endpt, &mModule);
+	sessionModule = this;
+}
+
+}; //end namespace SipChannelService
+}; //end namespace AsteriskSCF
diff --git a/src/SipSession.cpp b/src/SipSession.cpp
index 7373a6e..310ca69 100644
--- a/src/SipSession.cpp
+++ b/src/SipSession.cpp
@@ -352,7 +352,7 @@ void SipSession::start(const Ice::Current&)
    PJSipSessionModInfo *session_mod_info = new PJSipSessionModInfo(inviteSession);
    SipSessionPtr session = new SipSession(*this);
    session_mod_info->setSessionPtr(session);
-   inviteSession->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule()->id] = (void*)session_mod_info;
+   inviteSession->mod_data[SipChannelServiceDataModel::getInstance().getPJSipManager()->getSessionModule().id] = (void*)session_mod_info;
 
    // Create the actual INVITE packet
    pjsip_tx_data *packet;

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


-- 
asterisk-scf/integration/sip.git



More information about the asterisk-scf-commits mailing list