realtimenetprots/sipfw/SIP/Transaction/src/Transaction.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          : Transaction.cpp
       
    15 // Part of       : Transaction
       
    16 // Version       : SIP/5.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "SipAssert.h"
       
    22 #include "siperr.h"
       
    23 #include "sipstrings.h"
       
    24 #include "sipstrconsts.h"
       
    25 #include "Lwtimer.h"
       
    26 #include "siprequest.h"
       
    27 #include "sipresponse.h"
       
    28 #include "UserAgentBase.h"
       
    29 #include "Transmitter.h"
       
    30 #include "SIPMessageUtility.h"
       
    31 
       
    32 #include "CTransaction.h"
       
    33 #include "TransactionState.h"
       
    34 #include "SipLogs.h"
       
    35 // -----------------------------------------------------------------------------
       
    36 // CTransaction::CTransaction
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CTransaction::CTransaction(CUserAgentBase& aUserAgent,
       
    40 						   CTransmitter& aTransmitter,
       
    41 						   MTimerManager& aTimers,
       
    42 						   CTransactionState& aInitialState,
       
    43 						   TTimerValues& aTimerValues) :
       
    44 	iUserAgent(&aUserAgent),
       
    45 	iTransmitter(&aTransmitter),
       
    46 	iTimers(aTimers),
       
    47 	iTransportProtocol(SIPStrings::StringF(SipStrConsts::EUDP)),
       
    48 	iTransportParams(aUserAgent.TransportParams()),
       
    49 	iTimerValues(aTimerValues),
       
    50 	iTerminated(EFalse),
       
    51 	iState(&aInitialState)
       
    52 	{
       
    53 #if defined(USE_SIP_LOGS)
       
    54 	WriteStateToLog(*iState);
       
    55 #endif
       
    56 	}
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CTransaction::~CTransaction
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CTransaction::~CTransaction()
       
    63 	{
       
    64 	}
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CTransaction::TransactionId
       
    68 // This function is only used by UAS. CClientTransaction overloads this.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 TTransactionId CTransaction::TransactionId() const
       
    72 	{
       
    73 	__TEST_INVARIANT;
       
    74 	__ASSERT_DEBUG(iUserAgent,
       
    75 				   User::Panic(_L("CTransaction:TaId"), KErrGeneral));
       
    76 	return iUserAgent->TransactionId();
       
    77 	}
       
    78 
       
    79 #if defined(USE_SIP_LOGS)
       
    80 // -----------------------------------------------------------------------------
       
    81 // CTransaction::WriteStateToLog
       
    82 // Only INVITE transactions write logs.
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 void CTransaction::WriteStateToLog(const CTransactionState& aState) const
       
    86 	{
       
    87 	const TDesC8& log = aState.Log();
       
    88 
       
    89 	if (log.Length() > 0)
       
    90 		{
       
    91 		//If INVITE-UAC stops transaction as a 2xx was received, UAC writes the
       
    92 		//transaction state to log as transaction won't have the TransactionId.
       
    93 		TTransactionId id = TransactionId();
       
    94 		if (id != KEmptyTransactionId)
       
    95 			{
       
    96 			TBuf8<40> state;
       
    97 			if (iTerminated)
       
    98 				{
       
    99 				state.Copy(_L8("Terminated"));
       
   100 				}
       
   101 			else
       
   102 				{				
       
   103                 if (log.Compare(
       
   104                     CTransactionState::EnteringThisStateIsntLogged()) == 0)
       
   105 					{
       
   106 					return;
       
   107 					}
       
   108 				state.Copy(log);
       
   109 				}
       
   110 
       
   111 			__SIP_INT_LOG1( "TU transaction, taID", id )
       
   112 			__SIP_DES8_LOG( "TU transaction, state", state )
       
   113 			}
       
   114 		}
       
   115 	}
       
   116 #endif
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CTransaction::SendRequestL
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 void CTransaction::SendRequestL(CSIPRequest& aReq,
       
   123 								const TInetAddr& aRemoteAddr,
       
   124 								RStringF aProtocol,
       
   125 								TBool aForceUDP,
       
   126 								const TSIPTransportParams& aParams,
       
   127 								CUri8* aOutboundProxy)
       
   128 	{
       
   129 	__TEST_INVARIANT;
       
   130     __SIP_ASSERT_LEAVE(CSIPMessageUtility::CheckTransport(aProtocol),
       
   131                        KErrArgument);
       
   132 	if (iTerminated)
       
   133 		{
       
   134 		User::Leave(KErrSIPInvalidTransactionState);
       
   135 		}
       
   136 
       
   137 	iState->SendRequestL(*this,
       
   138 						 aReq,
       
   139 						 aRemoteAddr,
       
   140 						 aProtocol,
       
   141 						 aForceUDP,
       
   142 						 aParams,
       
   143 						 aOutboundProxy);
       
   144 	__TEST_INVARIANT;
       
   145 	}
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CTransaction::SendResponseL
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CTransaction::SendResponseL(CSIPResponse* aResp,
       
   152 								 RStringF aProtocol,
       
   153 								 const TSIPTransportParams& aParams)
       
   154 	{
       
   155 	__TEST_INVARIANT;
       
   156 	
       
   157 	CSIPInternalStates::TState state;
       
   158     this->GetState(state);
       
   159     __SIP_INT_LOG1("CTransaction::SendResponseL Transaciton state is: ", state)
       
   160 
       
   161 	    
       
   162 	TTransactionId Id= this->TransactionId();
       
   163     __SIP_INT_LOG1("CTransaction::SendResponseL Transaciton ID is: ", Id)
       
   164 
       
   165 	__SIP_ASSERT_LEAVE(aResp, KErrArgument);
       
   166 	__SIP_ASSERT_LEAVE(CSIPMessageUtility::CheckTransport(aProtocol),
       
   167                        KErrArgument);
       
   168 	if (iTerminated)
       
   169 		{
       
   170 		User::Leave(KErrSIPInvalidTransactionState);
       
   171 		}
       
   172 
       
   173     iState->SendResponseL(*this, aResp, aProtocol, aParams);
       
   174 
       
   175 	__TEST_INVARIANT;
       
   176 	}
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CTransaction::ReceiveL
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void CTransaction::ReceiveL(CSIPRequest* aRequest)
       
   183     {
       
   184     __TEST_INVARIANT;
       
   185     __SIP_ASSERT_LEAVE(aRequest, KErrArgument);
       
   186 	__SIP_MESSAGE_LOG("TransactionUser", *aRequest)
       
   187 	
       
   188 	if (iTerminated)
       
   189 		{
       
   190 		delete aRequest;
       
   191 		}
       
   192 	else
       
   193 		{
       
   194 		iState->ReceiveL(*this, aRequest);
       
   195 		}
       
   196 
       
   197 	__TEST_INVARIANT;
       
   198     }
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CTransaction::ReceiveL
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 void CTransaction::ReceiveL(CSIPResponse* aResponse)
       
   205     {
       
   206     __TEST_INVARIANT;
       
   207     __SIP_ASSERT_LEAVE(aResponse, KErrArgument);
       
   208 	__SIP_MESSAGE_LOG("TransactionUser", *aResponse)
       
   209 	
       
   210 	if (iTerminated)
       
   211 		{
       
   212 		delete aResponse;
       
   213 		}
       
   214 	else
       
   215 		{
       
   216 		iState->ReceiveL(*this, aResponse);
       
   217 		}
       
   218 
       
   219 	__TEST_INVARIANT;
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CTransaction::UpdateTransportProtocol
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 TBool CTransaction::UpdateTransportProtocol(CSIPMessage& aMsg)
       
   227 	{
       
   228 	__TEST_INVARIANT;
       
   229 	return CSIPMessageUtility::TransportProtocol(aMsg, iTransportProtocol);
       
   230 	}
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CTransaction::LeaveOccurred
       
   234 // Use overloaded TerminatedL (not Terminated), to free detached transaction.
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CTransaction::LeaveOccurred(TInt aReason)
       
   238 	{
       
   239 	__TEST_INVARIANT;
       
   240 	__ASSERT_DEBUG(aReason != KErrNone,
       
   241 				   User::Panic(_L("CTransaction:LeaveOccurred"), KErrArgument));
       
   242 
       
   243     if (aReason != KErrSIPInvalidDialogResponse)
       
   244     	{
       
   245 	    TRAP_IGNORE(TerminatedL(aReason))
       
   246 
       
   247 		if (iUserAgent)
       
   248 			{
       
   249 			iUserAgent->Stop(aReason);
       
   250 			}
       
   251     	}
       
   252 
       
   253 	__TEST_INVARIANT;
       
   254 	}
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CTransaction::TransportParams
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 const TSIPTransportParams& CTransaction::TransportParams() const
       
   261 	{
       
   262 	__TEST_INVARIANT;
       
   263 	return iTransportParams;	
       
   264 	}
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CTransaction::TimerExpiredL
       
   268 // Ignore timer if terminated. Occurs e.g. if ClientTransaction has timers E and
       
   269 // F, timer F expires so transaction terminates, but timer E isn't stopped until
       
   270 // Transaction is deleted.
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 void CTransaction::TimerExpiredL(TTimerId aTimerId, TAny* aTimerParam)
       
   274 	{
       
   275 	__TEST_INVARIANT;
       
   276 	
       
   277     // Check if it is a Invite transaction
       
   278 	if (IsInviteTransaction())
       
   279 	{
       
   280 		__SIP_LOG("It is invite transaction CTransaction::TimerExpiredL")
       
   281 	}
       
   282 	else
       
   283 	{
       
   284 		__SIP_LOG("It is non-invite transaction CTransaction::TimerExpiredL")
       
   285 	}
       
   286 	
       
   287 	// Get the transacton state
       
   288 	CSIPInternalStates::TState state;
       
   289 	this->GetState(state);
       
   290 	__SIP_INT_LOG1("CTransaction::TimerExpiredL Transaciton state is: ", state)
       
   291 	
       
   292 	TTransactionId Id= this->TransactionId();
       
   293     __SIP_INT_LOG1("CTransaction::TimerExpiredL Transaciton ID is: ", Id)
       
   294 
       
   295 	if (!iTerminated)
       
   296 		{
       
   297 		iState->TimerExpiredL(*this, aTimerId, aTimerParam);
       
   298 		}
       
   299 
       
   300 	__TEST_INVARIANT;
       
   301 	}
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CTransaction::SendCompleteL
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 void CTransaction::SendCompleteL()
       
   308 	{
       
   309 	__TEST_INVARIANT;
       
   310 
       
   311 	if (!iTerminated)
       
   312 		{
       
   313 		iState->SendCompleteL(*this);
       
   314 		}
       
   315 
       
   316 	__TEST_INVARIANT;
       
   317 	}
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 // CTransaction::SendFailedL
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 void CTransaction::SendFailedL(TInt aError)
       
   324 	{
       
   325 	__TEST_INVARIANT;
       
   326 
       
   327 	if (!iTerminated)
       
   328 		{
       
   329 		iState->SendFailedL(*this, aError);
       
   330 		}
       
   331 	
       
   332 	__TEST_INVARIANT;
       
   333 	}
       
   334 
       
   335 // -----------------------------------------------------------------------------
       
   336 // CTransaction::LeaveFromTransmitter
       
   337 // -----------------------------------------------------------------------------
       
   338 //
       
   339 void CTransaction::LeaveFromTransmitter(TInt aReason)
       
   340 	{
       
   341 	__TEST_INVARIANT;
       
   342 	__ASSERT_DEBUG(aReason != KErrNone,
       
   343 				   User::Panic(_L("CTa:LeaveFromTransmitter"), KErrArgument));
       
   344 
       
   345 	LeaveOccurred(aReason);
       
   346 
       
   347 	__TEST_INVARIANT;
       
   348 	}
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // CTransaction::IsUnreliableTransportUsed
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 TBool CTransaction::IsUnreliableTransportUsed() const
       
   355 	{
       
   356 	__TEST_INVARIANT;
       
   357 	return iTransportProtocol == SIPStrings::StringF(SipStrConsts::EUDP);
       
   358 	}
       
   359 
       
   360 // -----------------------------------------------------------------------------
       
   361 // CTransaction::IsTransmitterSending
       
   362 // -----------------------------------------------------------------------------
       
   363 //
       
   364 TBool CTransaction::IsTransmitterSending() const
       
   365 	{
       
   366 	__TEST_INVARIANT;
       
   367 	return iTransmitter && iTransmitter->IsActive();
       
   368 	}
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CTransaction::IsInviteTransaction
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 TBool CTransaction::IsInviteTransaction() const
       
   375 	{
       
   376 	__TEST_INVARIANT;
       
   377 	return EFalse;
       
   378 	}
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 // CTransaction::State
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 const CTransactionState* CTransaction::State() const
       
   385 	{
       
   386 	__TEST_INVARIANT;
       
   387 	return iState;
       
   388 	}
       
   389 
       
   390 // -----------------------------------------------------------------------------
       
   391 // CTransaction::ChangeState
       
   392 // -----------------------------------------------------------------------------
       
   393 //
       
   394 void CTransaction::ChangeState(const CTransactionState& aNewState)
       
   395 	{
       
   396 	__TEST_INVARIANT;
       
   397     
       
   398 	iState = &aNewState;
       
   399 
       
   400 #if defined(USE_SIP_LOGS)	
       
   401 	WriteStateToLog(aNewState);
       
   402 #endif
       
   403 
       
   404 	__TEST_INVARIANT;
       
   405 	}
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // CTransaction::ClearSendBuffer
       
   409 // If buffer has ACK, CInviteClientTransaction created and owns it.
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 TBool CTransaction::ClearSendBuffer()
       
   413 	{
       
   414 	__TEST_INVARIANT;
       
   415 
       
   416 	if (iOutgoingMsg && CSIPMessageUtility::IsAck(*iOutgoingMsg))
       
   417 		{
       
   418 		return ETrue;
       
   419 		}
       
   420 
       
   421 	//Can't use iOutgoingMsg anymore. Cancel a possible sending so ConnectionMgr
       
   422 	//won't use it either.
       
   423 	iOutgoingMsg = NULL;
       
   424 	if (iTransmitter)
       
   425     	{
       
   426 		iTransmitter->Cancel();
       
   427     	}
       
   428 
       
   429 	//A terminated transaction can continue without iOutgoingMsg
       
   430 	return iTerminated || iState->CanContinueWithoutOutgoingMsg();
       
   431 	}
       
   432 
       
   433 // -----------------------------------------------------------------------------
       
   434 // CTransaction::GetState
       
   435 // -----------------------------------------------------------------------------
       
   436 //
       
   437 void CTransaction::GetState(CSIPInternalStates::TState& aState)
       
   438     {
       
   439     __TEST_INVARIANT;
       
   440 
       
   441     if (iTerminated)
       
   442         {
       
   443         aState = CSIPInternalStates::ETransactionTerminated;        
       
   444         }
       
   445     else
       
   446         {        
       
   447         iState->GetState(aState);
       
   448         }
       
   449     }
       
   450 
       
   451 // -----------------------------------------------------------------------------
       
   452 // CTransaction::Terminated
       
   453 // -----------------------------------------------------------------------------
       
   454 //
       
   455 void CTransaction::Terminated()
       
   456 	{
       
   457 	__TEST_INVARIANT;
       
   458 
       
   459 	if (!iTerminated)
       
   460 		{
       
   461 		iTerminated = ETrue;
       
   462 
       
   463 #if defined(USE_SIP_LOGS)
       
   464 		WriteStateToLog(*iState);
       
   465 #endif
       
   466         if (iTransmitter)
       
   467         	{
       
   468 			iTransmitter->DetachCallback(this);
       
   469         	}
       
   470 		}
       
   471 
       
   472 	__TEST_INVARIANT;
       
   473 	}
       
   474 
       
   475 // -----------------------------------------------------------------------------
       
   476 // CTransaction::TerminatedL
       
   477 // -----------------------------------------------------------------------------
       
   478 //
       
   479 void CTransaction::TerminatedL(TInt aReason)
       
   480 	{
       
   481 	__TEST_INVARIANT;
       
   482 #if defined(USE_SIP_LOGS)
       
   483         WriteStateToLog(*iState);
       
   484 #endif
       
   485 
       
   486 	if (!iTerminated)
       
   487 		{
       
   488 		Terminated();
       
   489 
       
   490 		if (iUserAgent)
       
   491 			{
       
   492 			iUserAgent->TransactionEndsL(aReason);
       
   493             }
       
   494 		}
       
   495 
       
   496 	__TEST_INVARIANT;
       
   497     }
       
   498 
       
   499 // -----------------------------------------------------------------------------
       
   500 // CTransaction::HasTerminated
       
   501 // -----------------------------------------------------------------------------
       
   502 //
       
   503 TBool CTransaction::HasTerminated() const
       
   504 	{
       
   505     __TEST_INVARIANT;
       
   506 	return iTerminated;
       
   507 	}
       
   508 
       
   509 // -----------------------------------------------------------------------------
       
   510 // CTransaction::DetachFromUserAgent
       
   511 // -----------------------------------------------------------------------------
       
   512 //
       
   513 void CTransaction::DetachFromUserAgent()
       
   514 	{
       
   515 	__TEST_INVARIANT;
       
   516     __SIP_ASSERT_RETURN(iUserAgent, KErrNotFound);
       
   517 
       
   518 	iUserAgent = NULL;
       
   519 
       
   520 	__TEST_INVARIANT;
       
   521 	}
       
   522 
       
   523 // -----------------------------------------------------------------------------
       
   524 // CTransaction::TransportProtocol
       
   525 // -----------------------------------------------------------------------------
       
   526 //
       
   527 RStringF CTransaction::TransportProtocol() const
       
   528 	{
       
   529 	__TEST_INVARIANT;
       
   530 
       
   531 	return iTransportProtocol;
       
   532 	}
       
   533 
       
   534 // -----------------------------------------------------------------------------
       
   535 // CTransaction::__DbgTestInvariant
       
   536 // iUserAgent can be NULL. Detached transaction sets iTransmitter to NULL.
       
   537 // -----------------------------------------------------------------------------
       
   538 //
       
   539 
       
   540 void CTransaction::__DbgTestInvariant() const
       
   541 	{
       
   542 	if (!iState)
       
   543 		{
       
   544 		User::Invariant();
       
   545 		}
       
   546 	}
       
   547