⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sipexsipengine.cpp

📁 an example for sip for symbian
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
* ==============================================================================
*  Name        : SIPExSIPEngine.cpp
*  Part of     : SIPExSIPEngine
*  Interface   : 
*  Description : 
*  Version     : 
*
*  Copyright (c) 2004-2006 Nokia Corporation.
*  This material, including documentation and any related 
*  computer programs, is protected by copyright controlled by 
*  Nokia Corporation.
* ==============================================================================
*/

// INCLUDE FILES

#include "SIPExSIPEngine.h"
#include "SIPExSIPStateBase.h"

#include <Uri8.h>
#include <sipstrings.h>
#include <SipStrConsts.h>

// CONSTANTS

_LIT8( KSdpNoInfoDesC8, "-" );

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CSIPExSIPEngine::NewL
// Two-phased constructor.
// -----------------------------------------------------------------------------
EXPORT_C CSIPExSIPEngine* CSIPExSIPEngine::NewL( 
    TUid aAppUid,
	MSIPExSIPEngineObserver* aObserver )
	{
	CSIPExSIPEngine* self = new( ELeave ) CSIPExSIPEngine();
	CleanupStack::PushL( self );
	self->ConstructL( aAppUid, aObserver );
	CleanupStack::Pop( self );
	
	return self;
	}


// -----------------------------------------------------------------------------
// Destructor.
// -----------------------------------------------------------------------------
CSIPExSIPEngine::~CSIPExSIPEngine()
	{
	SdpCodecStringPool::Close();
	
	delete iIdle;
	delete iClientEstablishing;
	delete iClientOffering;
	delete iServerOffering;
	delete iServerEstablishing;
	delete iEstablished;
	delete iTerminating;

	delete iClientTx;
	delete iServerTx;

	delete iDialogAssoc;

	delete iConnection;
	delete iProfile;
	delete iProfileRegistry;
	delete iSIP;
	}


// -----------------------------------------------------------------------------
// CSIPExSIPEngine::CSIPExSIPEngine
// C++ default constructor.
// -----------------------------------------------------------------------------
CSIPExSIPEngine::CSIPExSIPEngine()
	{
	}


// -----------------------------------------------------------------------------
// CSIPExSIPEngine::ConstructL
// Symbian 2nd phase constructor. Can leave.
// -----------------------------------------------------------------------------
void CSIPExSIPEngine::ConstructL( 
    TUid aAppUid,
	MSIPExSIPEngineObserver* aObserver )
	{
	// Make note of the observer class.
	iObserver = aObserver;

	// Create instances of the CSIP and Profile Registry classes
	iSIP = CSIP::NewL( aAppUid, *this );
	iProfileRegistry = CSIPProfileRegistry::NewL( *iSIP, *this );

	// Open SDP Codec String Pool
	StringPoolL();

	// Set our local address
	const TUint32 KInetAddr = INET_ADDR(127,0,0,1);
	iLocalAddr.SetAddress( KInetAddr );

	// Set up the state machine
	// Create instances of the state classes.
	iIdle = CSIPExSIPIdleState::NewL();
	iClientEstablishing = CSIPExSIPClientEstablishingState::NewL();
	iClientOffering = CSIPExSIPClientOfferingState::NewL();
	iServerOffering = CSIPExSIPServerOfferingState::NewL();
	iServerEstablishing = CSIPExSIPServerEstablishingState::NewL();
	iEstablished = CSIPExSIPEstablishedState::NewL();
	iTerminating = CSIPExSIPTerminatingState::NewL();

	// Create the links for state transitions.
	iIdle->LinkStates( *iClientEstablishing, *iServerOffering );
	iClientEstablishing->LinkStates( *iIdle, *iClientOffering, *iEstablished );
	iClientOffering->LinkStates( *iIdle, *iEstablished, *iIdle );
	iServerOffering->LinkStates( *iServerEstablishing, *iIdle );
	iServerEstablishing->LinkStates( *iEstablished, *iIdle );
	iEstablished->LinkStates( *iTerminating, *iIdle );
	iTerminating->LinkStates( *iIdle );

	// Set current state data
	iCurrentState = iIdle;
	iConnState = CSIPConnection::EInactive;
	}


// -----------------------------------------------------------------------------
// CSIPExSIPEngine::EnableProfileL
// First get the default profile from the registry, then, if it is
// an IETF profile, enable it.
// If non-IETF profile is default, call error callback with KErrNotSupported
// and leave
// Not part of the state machine.
// -----------------------------------------------------------------------------
EXPORT_C TBool CSIPExSIPEngine::EnableProfileL()
	{
	// Check for existing profile
	if ( iProfile )
		{
		delete iProfile;
		iProfile = NULL;
		}
    TBool registered( EFalse );
    
    // Leaves with KErrNotFound if default profile is not found
	iProfile = iProfileRegistry->DefaultProfileL();

    // Safety check that DefaultProfile() didn't return NULL pointer.
    if ( !iProfile )
        {
        iObserver->ProfileError( KErrNotFound );
        _LIT8( KProfileError, "Profile not found. Define a profile:");
        iObserver->WriteLog(KProfileError());
		User::Leave( KErrNotFound );
        }
    // Leaves if profile type is not EInternet
    else if ( iProfile->Type().iSIPProfileClass != TSIPProfileTypeInfo::EInternet )
		{
		delete iProfile;
		iProfile = NULL;
		iObserver->ProfileError( KErrNotSupported );
		User::Leave( KErrNotSupported );
		}
	else
		{
		const TDesC8* aor = NULL;
    	iProfile->GetParameter( KSIPUserAor, aor );  	
    	iObserver->WriteLog( *aor );
		iProfileRegistry->EnableL( *iProfile, *this );
		
		// check whether profile was immediately set to registered state
		iProfile->GetParameter( KSIPProfileRegistered, registered );
		}
		
    return registered;
	}
		

// -----------------------------------------------------------------------------
// CSIPExSIPEngine::DisableProfileL
// Disable the profile given as a parameter.
// Not part of the state machine.
// -----------------------------------------------------------------------------
EXPORT_C void CSIPExSIPEngine::DisableProfileL()
	{
	if ( iProfile )
		{
		iProfileRegistry->Disable( *iProfile );
		delete iProfile;
		iProfile = NULL;		
		}
	}


// -----------------------------------------------------------------------------
// CSIPExSIPEngine::SendInviteL
// Send INVITE to the remote peer given as a parameter.
// -----------------------------------------------------------------------------
EXPORT_C void CSIPExSIPEngine::SendInviteL( const TDesC8& aSipUri )
	{
	iCurrentState->SendInviteL( *this, aSipUri );
	}


// -----------------------------------------------------------------------------
// CSIPExSIPEngine::CancelInvite
// CANCEL a previously sent INVITE.
// -----------------------------------------------------------------------------
EXPORT_C void CSIPExSIPEngine::CancelInviteL()
	{
	iCurrentState->CancelInviteL( *this );
	}


// -----------------------------------------------------------------------------
// CSIPExSIPEngine::AcceptInvite
// Accept a received INVITE with 200 (OK).
// -----------------------------------------------------------------------------
EXPORT_C void CSIPExSIPEngine::AcceptInviteL(const TInetAddr& aIPAddr )
	{
	iLocalAddr = aIPAddr;
	iCurrentState->AcceptInviteL( *this );
	}


// -----------------------------------------------------------------------------
// CSIPExSIPEngine::DeclineInvite
// Decline a received INVITE with 486 (Busy Here).
// -----------------------------------------------------------------------------
EXPORT_C void CSIPExSIPEngine::DeclineInviteL()
	{
	iCurrentState->DeclineInviteL( *this );
	}


// -----------------------------------------------------------------------------
// CSIPExSIPEngine::EndSession
// End the dialog with BYE.
// -----------------------------------------------------------------------------
EXPORT_C void CSIPExSIPEngine::EndSessionL()
	{
	iCurrentState->EndSessionL( *this );
	}


// -----------------------------------------------------------------------------
// CSIPExSIPEngine::CreateIML
// Create and send an Instant Message to recipient defined with parameter.
// This is implemented with the MESSAGE method and is sent outside of a
// dialog.
// -----------------------------------------------------------------------------
EXPORT_C void CSIPExSIPEngine::CreateIML( 
    const TDesC8& aMessage,
	const TDesC8& aSipUri )
	{
	_LIT8( KMediaType, "SIPEx" );	// Part of content type
	_LIT8( KMediaSubType, "InstantMessage" );	// Part of content type

	// Create the necessary elements of the IM
	CSIPRequestElements* reqElem = CreateReqElementsLC( aSipUri );
	CSIPToHeader* toHeader = CreateToHeaderLC( aSipUri );
	reqElem->SetToHeaderL( toHeader );
	CleanupStack::Pop( toHeader );

    // Create the From header value using info from the profile
    const TDesC8* aor = NULL;
    iProfile->GetParameter( KSIPUserAor, aor ); 
	__ASSERT_ALWAYS( aor && *aor != KNullDesC8, User::Leave( KErrNotFound ) );

    CSIPAddress* addr = CSIPAddress::DecodeL( *aor );
	CleanupStack::PushL( addr );
    CSIPFromHeader* fromHeader = CSIPFromHeader::NewL( addr );
	CleanupStack::Pop( addr );

	CleanupStack::PushL( fromHeader );
	reqElem->SetFromHeaderL( fromHeader );
	CleanupStack::Pop( fromHeader );
	

	reqElem->SetMethodL( SIPStrings::StringF( SipStrConsts::EMessage ) );

	// Get reference to the message elements from the request
	// elements, create and insert content type header (ownership
	// of the content type object is transferred)
	CSIPMessageElements& msgElem = reqElem->MessageElements();
	CSIPContentTypeHeader* ct =
   		CSIPContentTypeHeader::NewLC( KMediaType, KMediaSubType );
	msgElem.SetContentL( aMessage.AllocL(), ct );
	CleanupStack::Pop( ct );

	// Get the current connection
	CSIPConnection& conn = ConnectionL();

	// Send the request using the connection (ownership of the
	// request elements object is transferred)
	CSIPClientTransaction* ctx = conn.SendRequestL( reqElem );
	CleanupStack::Pop( reqElem );

	delete ctx;
	}
	

// -----------------------------------------------------------------------------
// CSIPExSIPEngine::IMReceivedL
// An Instant Message is received from the network. Respond with 200 (OK)
// and alert the Engine Observer.
// -----------------------------------------------------------------------------
void CSIPExSIPEngine::IMReceivedL( CSIPServerTransaction* aTransaction  )
	{
	const CSIPRequestElements* reqElem = aTransaction->RequestElements();
	const CSIPFromHeader* fromHeader = reqElem->FromHeader();
	
    CSIPResponseElements* respElem =
    	CSIPResponseElements::NewLC( 200, 
    	    SIPStrings::StringF( SipStrConsts::EPhraseOk ) );
	// Use the transaction to send 200 (OK)
	aTransaction->SendResponseL( respElem );
	CleanupStack::Pop( respElem );

	// Inform the observer of the Instant Message
	iObserver->IMReceived(
	    fromHeader->SIPAddress().Uri8().Uri().Extract( EUriUserinfo ),
		reqElem->MessageElements().Content());	

	// We no longer need aTransaction. Just delete it.
	delete aTransaction;
	aTransaction = NULL;
	}
	

// -----------------------------------------------------------------------------
// CSIPExSIPEngine::IMReceived
// Call the IMReceivedL method, trapping the possible errors.
// -----------------------------------------------------------------------------
void CSIPExSIPEngine::IMReceived( CSIPServerTransaction* aTransaction  )
	{
	TRAPD( err, IMReceivedL( aTransaction ));

	if ( err != KErrNone )
		{
		iObserver->EngineError( err );
		}
	}
	

// -----------------------------------------------------------------------------
// CSIPExSIPEngine::SetCurrentState
// Sets the new current state of the state machine.
// -----------------------------------------------------------------------------
void CSIPExSIPEngine::SetCurrentState( CSIPExSIPStateBase* aState )
	{
	iCurrentState = aState;
	}
	

// -----------------------------------------------------------------------------
// CSIPExSIPEngine::ConnectionL
// Returns the active connection.
// -----------------------------------------------------------------------------
CSIPConnection& CSIPExSIPEngine::ConnectionL()
	{
	CSIPConnection* conn = CurrentConnection();
	if ( !conn )
		{
		TUint32 iapId( 0 );
	    User::LeaveIfError( iProfile->GetParameter( KSIPAccessPointId, iapId ) );
		iConnection = CSIPConnection::NewL( *iSIP, iapId, *this );
		return *iConnection;
		}
	return *conn;
	}
	

// -----------------------------------------------------------------------------
// CSIPExSIPEngine::Profile
// Returns the enabled profile.
// -----------------------------------------------------------------------------
CSIPProfile& CSIPExSIPEngine::Profile()
	{
	return *iProfile;
	}
	

// -----------------------------------------------------------------------------
// CSIPExSIPEngine::SetServerTx
// Sets the current Server Transaction.
// -----------------------------------------------------------------------------
void CSIPExSIPEngine::SetServerTx( CSIPServerTransaction* aTx )
	{
	delete iServerTx;
	iServerTx = aTx;
	}
	

// -----------------------------------------------------------------------------
// CSIPExSIPEngine::ServerTx
// Returns the current Server Transaction.
// -----------------------------------------------------------------------------
CSIPServerTransaction& CSIPExSIPEngine::ServerTx()
	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -