realtimenetprots/sipfw/SIP/Transaction/src/InviteClientTa.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name          : InviteClientTa.cpp
       
    15 // Part of       : Transaction
       
    16 // Version       : SIP/5.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "SipAssert.h"
       
    22 #include "siperr.h"
       
    23 #include "Lwtimer.h"
       
    24 #include "uricontainer.h"
       
    25 #include "sipcseqheader.h"
       
    26 #include "siprequest.h"
       
    27 #include "sipresponse.h"
       
    28 #include "sipstrings.h"
       
    29 #include "sipstrconsts.h"
       
    30 #include "TimerValues.h"
       
    31 #include "SIPMessageUtility.h"
       
    32 #include "SIPRequestUtility.h"
       
    33 
       
    34 #include "InviteClientTa.h"
       
    35 #include "Transmitter.h"
       
    36 #include "TransactionState.h"
       
    37 #include "TransactionTimer.h"
       
    38 #include "RestoreTransactionState.h"
       
    39 
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CInviteClientTransaction::NewL
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CInviteClientTransaction* CInviteClientTransaction::NewL(
       
    46 							CUserAgentBase& aUserAgent,
       
    47 							CTransmitter& aTransmitter,
       
    48 							MTimerManager& aTimers,
       
    49 							CTransactionState& aInitialState,
       
    50 							TTimerValues& aTimerValues,
       
    51 							MTransactionStore& aTransactionStore,
       
    52 							TBool aRetransmitInvite)
       
    53 	{
       
    54 	CInviteClientTransaction* self =
       
    55         new (ELeave) CInviteClientTransaction(aUserAgent,							
       
    56 									          aTransmitter,
       
    57 									          aTimers,
       
    58 									          aInitialState,
       
    59 									          aTimerValues,
       
    60 									          aTransactionStore,
       
    61 									          aRetransmitInvite);
       
    62 	return self;
       
    63 	}
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CInviteClientTransaction::CInviteClientTransaction
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CInviteClientTransaction::CInviteClientTransaction(
       
    70 						CUserAgentBase& aUserAgent,
       
    71 						CTransmitter& aTransmitter,
       
    72 						MTimerManager& aTimers,
       
    73 						CTransactionState& aInitialState,
       
    74 						TTimerValues& aTimerValues,
       
    75 						MTransactionStore& aTransactionStore,
       
    76 						TBool aRetransmitInvite) :
       
    77 	CClientTransaction(aUserAgent,
       
    78 					   aTransmitter,
       
    79 					   aTimers,
       
    80 					   aInitialState,
       
    81 					   aTimerValues,
       
    82 					   aTransactionStore),
       
    83 	iRetransmitInvite(aRetransmitInvite),iDTimerErrCode(KErrNone)
       
    84 	{
       
    85 	}
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CInviteClientTransaction::~CInviteClientTransaction
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 CInviteClientTransaction::~CInviteClientTransaction()
       
    92 	{
       
    93     //Scope operator to silence lint warning
       
    94     CInviteClientTransaction::CancelAllTimers();
       
    95 
       
    96 	if (iTaOwnsRequest)
       
    97 		{
       
    98 		delete iOutgoingMsg;
       
    99 		}
       
   100 
       
   101 	//CInviteClientTransaction can own iTransmitter
       
   102 	if (iDeleteMgr)
       
   103 		{
       
   104 		delete iTransmitter;
       
   105 		}
       
   106 	}
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CInviteClientTransaction::IsInviteTransaction
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 TBool CInviteClientTransaction::IsInviteTransaction() const
       
   113 	{
       
   114 	return ETrue;
       
   115 	}
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CInviteClientTransaction::ReceiveResponseL
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 void CInviteClientTransaction::ReceiveResponseL(CSIPResponse* aResponse,
       
   122 									const CTransactionState& aCurrent,
       
   123                                     const CTransactionState& aCompleted,
       
   124                                     CTransactionTimer* aTimer,
       
   125                           			CTransactionTimer* aTimer2)
       
   126 	{
       
   127 	__TEST_INVARIANT;
       
   128 	__SIP_ASSERT_LEAVE(aResponse, KErrArgument);
       
   129 
       
   130 	CSIPRequest* ack = NULL;
       
   131 	TBool isErrResponse = aResponse->IsErrorResponse();
       
   132 	if ( isErrResponse )
       
   133 	    {
       
   134 	    ack = MakeAckLC(*aResponse);
       
   135         ChangeState(aCompleted);
       
   136 	    }
       
   137 
       
   138 	TRestoreTransactionState restoreState(*this, aCurrent, aTimer, aTimer2);
       
   139     CleanupStack::PushL(restoreState.CleanupItem());
       
   140     
       
   141 	TRAPD(err,PassResponseToUserAgentL(aResponse));
       
   142 	if( err != KErrNone )
       
   143 		{
       
   144 		if( err == KErrNoMemory || !isErrResponse )
       
   145 			{
       
   146 			User::Leave(err);
       
   147 			}
       
   148 		else
       
   149 			{
       
   150 			iDTimerErrCode = err;
       
   151 			}
       
   152 		}
       
   153 		
       
   154     CleanupStack::Pop(); //cleanupItem  
       
   155 
       
   156     if (ack)
       
   157     	{
       
   158     	CleanupStack::Pop(ack);
       
   159 		SendAck(ack);
       
   160     	}
       
   161 
       
   162 	__TEST_INVARIANT;
       
   163 	}
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CInviteClientTransaction::SendAck
       
   167 // Can't leave as response's ownership has been passed. Cancel CTransmitter if
       
   168 // needed so ACK can be sent. Transaction doesn't own INVITE, so ACK is put to
       
   169 // iOutgoingMsg, in place of INVITE.
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CInviteClientTransaction::SendAck(CSIPRequest* aAck)
       
   173 	{
       
   174 	__TEST_INVARIANT;
       
   175 
       
   176 	if (IsTransmitterSending())
       
   177 		{
       
   178 		iTransmitter->Cancel();
       
   179 		}
       
   180 
       
   181 	iOutgoingMsg = aAck;
       
   182 	iTaOwnsRequest = ETrue;
       
   183 
       
   184 	TRAPD(err, SendToTransmitterL());
       
   185 	if (err != KErrNone)
       
   186 		{
       
   187 		TRAP_IGNORE(TerminatedL(err))
       
   188 		}
       
   189 
       
   190 	__TEST_INVARIANT;
       
   191 	}
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CInviteClientTransaction::MakeAckLC
       
   195 // Create ACK just once. Copy To-header from the response.
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 CSIPRequest* CInviteClientTransaction::MakeAckLC(CSIPResponse& aResp) const
       
   199 	{
       
   200 	__TEST_INVARIANT;
       
   201 	__SIP_ASSERT_LEAVE(!iTaOwnsRequest, KErrAlreadyExists);	
       
   202 
       
   203 	CSIPRequest* ack =
       
   204 		CSIPRequest::NewLC(SIPStrings::StringF(SipStrConsts::EAck));
       
   205 	CopyHeadersFromInviteToAckL(*ack);
       
   206 
       
   207 	RStringF to = SIPStrings::StringF(SipStrConsts::EToHeader);
       
   208 	__SIP_ASSERT_LEAVE(aResp.HasHeader(to), KErrArgument);
       
   209 	CSIPMessageUtility::CopyHeadersL(aResp, *ack, to);
       
   210 
       
   211 	SIPRequestUtility::FillNewMaxForwardsL(*ack);
       
   212 	CSIPMessageUtility::CopyAuthorizationHeadersL(iOutgoingMsg, *ack);
       
   213 	SIPRequestUtility::FillSupportedSecAgreeL(*ack);
       
   214 
       
   215     __TEST_INVARIANT;
       
   216     return ack;
       
   217 	}
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CInviteClientTransaction::CopyHeadersFromInviteToAckL
       
   221 // Copy INVITE's Via header to ACK. Both have the same CSeq sequence number.
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void
       
   225 CInviteClientTransaction::CopyHeadersFromInviteToAckL(CSIPRequest& aAck) const
       
   226 	{
       
   227 	__TEST_INVARIANT;
       
   228     __SIP_ASSERT_LEAVE(iOutgoingMsg, KErrNotFound);
       
   229 
       
   230     RStringF from   = SIPStrings::StringF(SipStrConsts::EFromHeader);
       
   231     RStringF callId = SIPStrings::StringF(SipStrConsts::ECallIDHeader);
       
   232     RStringF via    = SIPStrings::StringF(SipStrConsts::EViaHeader);
       
   233     RStringF cseq   = SIPStrings::StringF(SipStrConsts::ECSeqHeader);
       
   234 
       
   235     __SIP_ASSERT_LEAVE(iOutgoingMsg->HasHeader(from) &&
       
   236     				   iOutgoingMsg->HasHeader(callId) &&
       
   237     				   iOutgoingMsg->HasHeader(via) &&
       
   238     				   iOutgoingMsg->HasHeader(cseq), KErrSIPMalformedMessage);
       
   239 
       
   240 	const CURIContainer* origReqUri =
       
   241         static_cast<CSIPRequest*>(iOutgoingMsg)->RequestURI();
       
   242     __SIP_ASSERT_LEAVE(origReqUri != NULL, KErrSIPMalformedMessage);
       
   243 
       
   244 	CURIContainer* reqUri = CURIContainer::NewLC(*origReqUri);
       
   245 	aAck.SetRequestURIL(reqUri);
       
   246 	CleanupStack::Pop(reqUri);
       
   247 
       
   248 	CSIPMessageUtility::CopyHeadersL(*iOutgoingMsg, aAck, from);
       
   249 	CSIPMessageUtility::CopyHeadersL(*iOutgoingMsg, aAck, callId);
       
   250 	CSIPMessageUtility::CopyHeadersL(*iOutgoingMsg, aAck, via);
       
   251 
       
   252 	TSglQueIter<CSIPHeaderBase> iter = iOutgoingMsg->Headers(cseq);
       
   253 	CSIPMessageUtility::FillCSeqL(aAck,
       
   254 								  static_cast<CSIPCSeqHeader&>(*iter).Seq(),
       
   255 								  SIPStrings::StringF(SipStrConsts::EAck));
       
   256 	CSIPMessageUtility::CopyHeadersL(*iOutgoingMsg,
       
   257 							 aAck,
       
   258 							 SIPStrings::StringF(SipStrConsts::ERouteHeader));
       
   259 	}
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // CInviteClientTransaction::StartTimerAL
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CInviteClientTransaction::StartTimerAL()
       
   266 	{
       
   267 	__TEST_INVARIANT;
       
   268     __SIP_ASSERT_LEAVE(!iTimerA, KErrAlreadyExists);
       
   269 
       
   270 	if (iTimerADuration == 0)
       
   271 		{
       
   272 		iTimerADuration = iTimerValues.T1();
       
   273 		}
       
   274 	else
       
   275 		{
       
   276 		iTimerADuration = iTimerADuration << 1;
       
   277 		}
       
   278 
       
   279 	iTimerA = CTimerRetransmit::NewL(iTimers, this, iTimerADuration);
       
   280 
       
   281 	__TEST_INVARIANT;
       
   282 	}
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CInviteClientTransaction::StartTimerBUnlessExistsL
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 void CInviteClientTransaction::StartTimerBUnlessExistsL()
       
   289 	{
       
   290 	__TEST_INVARIANT;
       
   291 
       
   292 	if (!iTimerB)
       
   293 		{
       
   294 		iTimerB = CTimerTerminateTa::NewL(iTimers,
       
   295 										  this,
       
   296 										  iTimerValues.Duration64xT1(),
       
   297 										  KErrTimedOut);
       
   298 		}
       
   299 
       
   300 	__TEST_INVARIANT;
       
   301 	}
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CInviteClientTransaction::StartTimerDUnlessExistsL
       
   305 // Duration is 0s for reliable and 65*T1 for unreliable transports.
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void CInviteClientTransaction::StartTimerDUnlessExistsL()
       
   309 	{
       
   310 	__TEST_INVARIANT;
       
   311 
       
   312     if (!iTimerD)
       
   313     	{
       
   314 		TUint32 duration = 0;
       
   315 		if (IsUnreliableTransportUsed())
       
   316 			{
       
   317 			duration = iTimerValues.Duration64xT1() + iTimerValues.T1();
       
   318 			}
       
   319 	    iTimerD = CTimerTerminateTa::NewL(iTimers, this, duration,iDTimerErrCode);
       
   320     	}
       
   321 
       
   322 	__TEST_INVARIANT;
       
   323 	}
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // CInviteClientTransaction::StartTimerProceedingL
       
   327 // -----------------------------------------------------------------------------
       
   328 //
       
   329 CTransactionTimer* CInviteClientTransaction::StartTimerProceedingL()
       
   330 	{		
       
   331 	__TEST_INVARIANT;
       
   332     __SIP_ASSERT_LEAVE(!iTimerProceeding, KErrAlreadyExists);
       
   333 
       
   334 	iTimerProceeding =
       
   335         CTimerTerminateTa::NewL(iTimers,
       
   336 								this,
       
   337 								CTimerTerminateTa::KProceedingTimerDuration,
       
   338 								KErrTimedOut);
       
   339 	__TEST_INVARIANT;
       
   340 	return iTimerProceeding;
       
   341 	}
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CInviteClientTransaction::StartTimerNATL
       
   345 // If 1xx comes before SendCompleteL, timer exists when handling SendCompleteL.
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 CTransactionTimer*  CInviteClientTransaction::StartTimerNATL()
       
   349 	{
       
   350 	__TEST_INVARIANT;
       
   351 
       
   352 	if (!iTimerNAT && iRetransmitInvite && IsUnreliableTransportUsed())
       
   353 		{
       
   354 		iTimerNAT = CTimerRetransmit::NewL(iTimers,
       
   355 									   this,
       
   356 									   CTimerRetransmit::KNATBindingInterval);
       
   357 		}
       
   358 
       
   359 	__TEST_INVARIANT;
       
   360 	return iTimerNAT;
       
   361 	}
       
   362 
       
   363 // -----------------------------------------------------------------------------
       
   364 // CInviteClientTransaction::StopTimerA
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 void CInviteClientTransaction::StopTimerA()
       
   368 	{
       
   369 	__TEST_INVARIANT;
       
   370 
       
   371 	delete iTimerA;
       
   372 	iTimerA = NULL;
       
   373 
       
   374 	__TEST_INVARIANT;
       
   375 	}
       
   376 
       
   377 // -----------------------------------------------------------------------------
       
   378 // CInviteClientTransaction::StopTimerB
       
   379 // -----------------------------------------------------------------------------
       
   380 //
       
   381 void CInviteClientTransaction::StopTimerB()
       
   382 	{
       
   383 	__TEST_INVARIANT;
       
   384 
       
   385 	delete iTimerB;
       
   386 	iTimerB = NULL;
       
   387 
       
   388 	__TEST_INVARIANT;
       
   389 	}
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CInviteClientTransaction::StopTimerD
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 void CInviteClientTransaction::StopTimerD()
       
   396 	{
       
   397 	__TEST_INVARIANT;
       
   398 
       
   399 	delete iTimerD;
       
   400 	iTimerD = NULL;
       
   401 
       
   402 	__TEST_INVARIANT;
       
   403 	}
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CInviteClientTransaction::StopTimerProceeding
       
   407 // -----------------------------------------------------------------------------
       
   408 //
       
   409 void CInviteClientTransaction::StopTimerProceeding()
       
   410 	{
       
   411 	__TEST_INVARIANT;
       
   412 
       
   413 	delete iTimerProceeding;
       
   414 	iTimerProceeding = NULL;
       
   415 
       
   416 	__TEST_INVARIANT;
       
   417 	}
       
   418 
       
   419 // -----------------------------------------------------------------------------
       
   420 // CInviteClientTransaction::StopTimerNAT
       
   421 // -----------------------------------------------------------------------------
       
   422 //
       
   423 void CInviteClientTransaction::StopTimerNAT()
       
   424 	{
       
   425 	__TEST_INVARIANT;
       
   426 
       
   427 	delete iTimerNAT;
       
   428 	iTimerNAT = NULL;
       
   429 
       
   430 	__TEST_INVARIANT;
       
   431 	}
       
   432 
       
   433 // -----------------------------------------------------------------------------
       
   434 // CInviteClientTransaction::DeleteTimer
       
   435 // -----------------------------------------------------------------------------
       
   436 //
       
   437 void CInviteClientTransaction::DeleteTimer(const CTransactionTimer& aTimer)
       
   438 	{
       
   439 	__TEST_INVARIANT;    
       
   440 
       
   441 	if (&aTimer == iTimerA)
       
   442 		{
       
   443 		StopTimerA();
       
   444 		}
       
   445 
       
   446 	if (&aTimer == iTimerB)
       
   447 		{
       
   448 		StopTimerB();		
       
   449 		}
       
   450 
       
   451 	if (&aTimer == iTimerD)
       
   452 		{
       
   453 		StopTimerD();		
       
   454 		}
       
   455 
       
   456 	if (&aTimer == iTimerProceeding)
       
   457 		{
       
   458 		StopTimerProceeding();
       
   459 		}
       
   460 
       
   461 	if (&aTimer == iTimerNAT)
       
   462 		{		
       
   463 		StopTimerNAT();
       
   464 		}
       
   465 
       
   466 	__TEST_INVARIANT;
       
   467 	}
       
   468 
       
   469 // -----------------------------------------------------------------------------
       
   470 // CInviteClientTransaction::CancelAllTimers
       
   471 // -----------------------------------------------------------------------------
       
   472 //
       
   473 void CInviteClientTransaction::CancelAllTimers()
       
   474 	{
       
   475 	__TEST_INVARIANT;
       
   476 
       
   477 	StopTimerA();
       
   478 	StopTimerB();
       
   479 	StopTimerD();
       
   480 	StopTimerProceeding();
       
   481 	StopTimerNAT();
       
   482 
       
   483 	__TEST_INVARIANT;
       
   484 	}
       
   485 
       
   486 // -----------------------------------------------------------------------------
       
   487 // CInviteClientTransaction::Ptr
       
   488 // -----------------------------------------------------------------------------
       
   489 //
       
   490 CInviteClientTransaction&
       
   491 CInviteClientTransaction::Ptr(CTransaction& aTransaction)
       
   492 	{
       
   493 	return static_cast<CInviteClientTransaction&>(aTransaction);
       
   494 	}
       
   495 
       
   496 // -----------------------------------------------------------------------------
       
   497 // CInviteClientTransaction::__DbgTestInvariant
       
   498 // iTaOwnsRequest can be ETrue only if iOutgoingMsg is ACK.
       
   499 // -----------------------------------------------------------------------------
       
   500 //
       
   501 
       
   502 void CInviteClientTransaction::__DbgTestInvariant() const
       
   503 	{
       
   504 	if (iOutgoingMsg &&
       
   505 		iTaOwnsRequest &&
       
   506 		!CSIPMessageUtility::IsAck(*iOutgoingMsg) &&
       
   507 		(iRetransmitInvite && !iTimerNAT ||
       
   508 		 !iRetransmitInvite && iTimerNAT))
       
   509 		{
       
   510 		User::Invariant();
       
   511 		}
       
   512 	}