realtimenetprots/sipfw/SIP/sipapi/src/RegBindingImplementation.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          : RegBindingImplementation.cpp
       
    15 // Part of       : SIPAPI
       
    16 // Version       : SIP/5.0 
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include <uri8.h>
       
    22 #include "siperr.h"
       
    23 #include "sipdefs.h"
       
    24 #include "SipAssert.h"
       
    25 #include "sipclientconnection.h"
       
    26 #include "SipConnectionImplementation.h"
       
    27 #include "RegBindingImplementation.h"
       
    28 #include "sipregistrationstate.h"
       
    29 #include "sipclienttransaction.h"
       
    30 #include "sipmessageelements.h"
       
    31 #include "sipresponseelements.h"
       
    32 #include "siprefresh.h"
       
    33 #include "siprouteheader.h"
       
    34 #include "sipcontactheader.h"
       
    35 #include "siptoheader.h"
       
    36 #include "sipfromheader.h"
       
    37 #include "sipconnectioncallback.h"
       
    38 #include "sipstrings.h"
       
    39 #include "sipstrconsts.h"
       
    40 #include <sipregistrationbinding.h>
       
    41 
       
    42 #ifdef CPPUNIT_TEST
       
    43 #include "TestCleanupStack.h"
       
    44 #endif
       
    45 
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CRegBindingImplementation::NewL
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CRegBindingImplementation*
       
    52 CRegBindingImplementation::NewL(CSIPRegistrationBinding& aRegistration,
       
    53 								CSIPConnection& aConnection,
       
    54 					   		    CSIPToHeader* aAor,
       
    55 					   		    CSIPContactHeader* aContact,
       
    56 					   		    CSIPRefresh* aRefresh,
       
    57 					   		    CSIPRouteHeader* aOutboundProxy,
       
    58 					   		    CUri8* aRemoteUri,
       
    59                        		    CSIPFromHeader* aFrom)
       
    60     {
       
    61     __ASSERT_ALWAYS(aAor && aContact, User::Leave(KErrArgument));
       
    62 
       
    63     TInt expires = aContact->ExpiresParameter();
       
    64     __ASSERT_ALWAYS(expires >= static_cast<TInt>(KSIPMinExpirationValue) ||
       
    65     				expires == KErrNotFound, User::Leave(KErrArgument));
       
    66     CRegBindingImplementation* self =
       
    67     	new (ELeave) CRegBindingImplementation(aRegistration, aConnection);
       
    68     CleanupStack::PushL(self);
       
    69     self->ConstructL(aAor,
       
    70                      aContact,
       
    71                      aRefresh,
       
    72                      aOutboundProxy,
       
    73                      aRemoteUri,
       
    74                      aFrom);	
       
    75     CleanupStack::Pop(self);
       
    76     return self;
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CRegBindingImplementation::CRegBindingImplementation
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CRegBindingImplementation::CRegBindingImplementation(
       
    84 									CSIPRegistrationBinding& aRegistration,
       
    85 									CSIPConnection& aConnection) :
       
    86 	iRegistration(aRegistration),
       
    87     iConnection(&aConnection)
       
    88     {
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CRegBindingImplementation::ConstructL
       
    93 // aAor and aContact have been checked in NewLC so they aren't checked again
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CRegBindingImplementation::ConstructL(CSIPToHeader* aAor,
       
    97 								  		   CSIPContactHeader* aContact,                                  
       
    98 							  	  		   CSIPRefresh* aRefresh,
       
    99 								  		   CSIPRouteHeader* aOutboundProxy,
       
   100 								  		   CUri8* aRemoteUri,
       
   101                                   		   CSIPFromHeader* aFrom)
       
   102     {
       
   103     CheckConnectionL();
       
   104 
       
   105     iConnection->Implementation().AddRegistrationL(*this);
       
   106     iState = iConnection->Implementation().InitialRegisterStateL();
       
   107     iContact = aContact;
       
   108     iAor = aAor;
       
   109     iRefresh = aRefresh;
       
   110     if (iRefresh)
       
   111         {
       
   112         iRefresh->SetRefreshOwner(*this);
       
   113         iRefresh->SetRequestType(SIPStrings::StringF(SipStrConsts::ERegister));
       
   114         }
       
   115 
       
   116     iOutboundProxy = aOutboundProxy;
       
   117     iRemoteUri = aRemoteUri;
       
   118     iFrom = aFrom;
       
   119     iSendWithExpires = ETrue;
       
   120     iCacheOutboundProxyIP = EFalse;
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CRegBindingImplementation::~CRegBindingImplementation
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 CRegBindingImplementation::~CRegBindingImplementation()
       
   128     {
       
   129     if (iConnection)
       
   130         {
       
   131         CSIPClientConnection* clientConn =
       
   132         	iConnection->Implementation().ClientConnection();
       
   133         if (clientConn)
       
   134         	{
       
   135         	clientConn->TerminateRegistration(iRegistrationId);
       
   136         	}
       
   137         iConnection->Implementation().RemoveRegistration(*this);
       
   138         }
       
   139 
       
   140     for (TInt i = 0; i < iTransactions.Count(); i++)
       
   141         {
       
   142         iTransactions[i]->Detach(*this);
       
   143         }
       
   144     iTransactions.Reset();
       
   145 
       
   146     delete iRefresh;
       
   147     delete iOutboundProxy;
       
   148     delete iContact;
       
   149     delete iAor;
       
   150     delete iRemoteUri;
       
   151     delete iFrom;
       
   152     delete iRegisteredContact;
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CRegBindingImplementation::IsContextActive
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 TBool CRegBindingImplementation::IsContextActive() const
       
   160     {
       
   161     __TEST_INVARIANT;
       
   162     return iState && iState->IsContextActive();
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CRegBindingImplementation::ContextId
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 TUint32 CRegBindingImplementation::ContextId() const
       
   170     {
       
   171     __TEST_INVARIANT;
       
   172     return iRegistrationId;
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CRegBindingImplementation::RegisterL
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 CSIPClientTransaction*
       
   180 CRegBindingImplementation::RegisterL(CSIPMessageElements* aElements)
       
   181     {
       
   182     __TEST_INVARIANT;
       
   183 
       
   184     CheckElementsL(aElements);
       
   185     return StateL().RegisterL(*this, aElements);
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CRegBindingImplementation::DoRegisterL
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 CSIPClientTransaction*
       
   193 CRegBindingImplementation::DoRegisterL(CSIPMessageElements* aElements)
       
   194     {
       
   195     __TEST_INVARIANT;
       
   196     CheckConnectionL();
       
   197 
       
   198 	CSIPClientTransaction* ta = CreateClientTransactionL();
       
   199 	CleanupStack::PushL(ta);
       
   200 
       
   201     CSIPMessageElements* elem = aElements;
       
   202     if (!aElements)
       
   203         {
       
   204         elem = CSIPMessageElements::NewLC();
       
   205         }
       
   206 
       
   207     //Put contact temporarily to elem. Once REGISTER is sent, clear it from elem
       
   208     CleanupStack::PushL(TCleanupItem(DetachContactHeader, elem));
       
   209     elem->AddHeaderL(iContact);
       
   210 
       
   211     CSIPFromHeader* from = iFrom;
       
   212     if (!iFrom)
       
   213         {
       
   214         from = CSIPFromHeader::NewLC(*iAor);
       
   215         }
       
   216 
       
   217     TUint32 requestId(0);
       
   218     iConnection->Implementation().ClientConnectionL().SendRegisterL(requestId,
       
   219                                                    *iAor,
       
   220                                                    *from,
       
   221                                                    *elem,
       
   222                                                    iRemoteUri,
       
   223                                                    iOutboundProxy,
       
   224                                                    (iRefresh != NULL),
       
   225                                                    iSendWithExpires,
       
   226                                                    iCacheOutboundProxyIP);
       
   227     if (!iFrom)
       
   228         {
       
   229         CleanupStack::PopAndDestroy(from);
       
   230         }
       
   231     CleanupStack::PopAndDestroy(); //TCleanupItem
       
   232 
       
   233     if (aElements)
       
   234         {
       
   235         delete aElements;
       
   236         }
       
   237     else
       
   238         {
       
   239         CleanupStack::PopAndDestroy(elem);
       
   240         }
       
   241     CleanupStack::Pop(ta);
       
   242 	ta->SetRequestId(requestId);
       
   243 
       
   244     __TEST_INVARIANT;
       
   245     return ta;
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CRegBindingImplementation::DeregisterL
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 CSIPClientTransaction*
       
   253 CRegBindingImplementation::DeregisterL(CSIPMessageElements* aElements)
       
   254     {
       
   255     __TEST_INVARIANT;    
       
   256     return StateL().DeregisterL(*this, aElements);
       
   257     }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CRegBindingImplementation::DoDeregisterL
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 CSIPClientTransaction*
       
   264 CRegBindingImplementation::DoDeregisterL(CSIPMessageElements* aElements)
       
   265     {
       
   266     __TEST_INVARIANT;
       
   267     CheckConnectionL();
       
   268 
       
   269     CSIPClientTransaction* ta = CSIPClientTransaction::NewLC(
       
   270     	SIPStrings::StringF(SipStrConsts::ERegister), *this);
       
   271 
       
   272     TUint32 requestId(0);
       
   273 	iConnection->Implementation().ClientConnectionL().UnregisterL(
       
   274 												iRegistrationId,
       
   275 					                            requestId,
       
   276                                                 aElements);
       
   277     CleanupStack::Pop(ta);
       
   278 	ta->SetRequestId(requestId);    
       
   279     delete aElements;
       
   280 
       
   281     __TEST_INVARIANT;
       
   282     return ta;
       
   283     }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CRegBindingImplementation::UpdateL
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 CSIPClientTransaction*
       
   290 CRegBindingImplementation::UpdateL(CSIPMessageElements* aElements,
       
   291 								   TUint* aExpirationValue)
       
   292     {
       
   293     __TEST_INVARIANT;
       
   294     return StateL().UpdateL(*this, aElements, aExpirationValue);
       
   295     }
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // CRegBindingImplementation::DoUpdateL
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 CSIPClientTransaction*
       
   302 CRegBindingImplementation::DoUpdateL(CSIPMessageElements* aElements,
       
   303                             	     TUint* aExpirationValue)
       
   304     {
       
   305     __TEST_INVARIANT;    
       
   306     CheckConnectionL();
       
   307 
       
   308     CSIPClientTransaction* ta = CSIPClientTransaction::NewLC(
       
   309     					SIPStrings::StringF(SipStrConsts::ERegister),		                
       
   310 		                *this);
       
   311 
       
   312 	CSIPMessageElements* elem = aElements;
       
   313     if (!aElements)
       
   314         {
       
   315         elem = CSIPMessageElements::NewLC();
       
   316         }
       
   317 
       
   318     if (aExpirationValue)
       
   319         {
       
   320         iContact->SetExpiresParameterL(*aExpirationValue);
       
   321         }
       
   322 
       
   323     //Put contact temporarily to elem. Once REGISTER is sent, clear it from elem
       
   324 	CleanupStack::PushL(TCleanupItem(DetachContactHeader, elem));
       
   325     elem->AddHeaderL(iContact);
       
   326 
       
   327     TUint32 requestId(0);
       
   328     iConnection->Implementation().ClientConnectionL().UpdateRegistrationL(
       
   329     													iRegistrationId,
       
   330                                                         requestId,
       
   331                                                         elem);
       
   332 	CleanupStack::PopAndDestroy(); //TCleanupItem
       
   333     if (aElements)
       
   334         {
       
   335         delete aElements;
       
   336         }
       
   337     else
       
   338         {
       
   339         CleanupStack::PopAndDestroy(elem);
       
   340         }
       
   341 
       
   342     CleanupStack::Pop(ta);
       
   343 	ta->SetRequestId(requestId);
       
   344 
       
   345     __TEST_INVARIANT;
       
   346     return ta;
       
   347     }
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CRegBindingImplementation::SetOutboundProxyL
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 void
       
   354 CRegBindingImplementation::SetOutboundProxyL(CSIPRouteHeader* aOutboundProxy)
       
   355     {
       
   356     __TEST_INVARIANT;
       
   357 
       
   358 	if (IsContextActive())
       
   359 		{
       
   360     	CheckConnectionL();
       
   361 
       
   362 		CSIPClientConnection& conn =
       
   363 			iConnection->Implementation().ClientConnectionL();
       
   364 	    if (aOutboundProxy)
       
   365     	    {
       
   366         	conn.SetOutboundProxyL(iRegistrationId,	*aOutboundProxy);
       
   367 			}
       
   368 		else
       
   369 			{
       
   370         	conn.RemoveOutboundProxy(iRegistrationId);
       
   371 			}
       
   372         }
       
   373 
       
   374     //Remove old proxy when leave can not occur
       
   375     delete iOutboundProxy;
       
   376     iOutboundProxy = aOutboundProxy;
       
   377 
       
   378     __TEST_INVARIANT;
       
   379     }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CRegBindingImplementation::Aor
       
   383 // -----------------------------------------------------------------------------
       
   384 //
       
   385 const CSIPToHeader& CRegBindingImplementation::Aor() const
       
   386     {
       
   387     __TEST_INVARIANT;
       
   388     return *iAor;
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CRegBindingImplementation::ContactHeader
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 const CSIPContactHeader& CRegBindingImplementation::ContactHeader() const
       
   396     {
       
   397     __TEST_INVARIANT;
       
   398     return *iContact;
       
   399     }
       
   400     
       
   401 // -----------------------------------------------------------------------------
       
   402 // CRegBindingImplementation::RegisteredContact
       
   403 // -----------------------------------------------------------------------------
       
   404 //
       
   405 const CSIPContactHeader* CRegBindingImplementation::RegisteredContact() const
       
   406     {
       
   407     __TEST_INVARIANT;
       
   408     return iRegisteredContact;
       
   409     }    
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CRegBindingImplementation::ContactHeader
       
   413 // -----------------------------------------------------------------------------
       
   414 //
       
   415 CSIPContactHeader& CRegBindingImplementation::ContactHeader()
       
   416     {
       
   417     __TEST_INVARIANT;
       
   418     return *iContact;
       
   419     }
       
   420 
       
   421 // -----------------------------------------------------------------------------
       
   422 // CRegBindingImplementation::OutboundProxy
       
   423 // -----------------------------------------------------------------------------
       
   424 //
       
   425 const CSIPRouteHeader* CRegBindingImplementation::OutboundProxy() const
       
   426     {
       
   427     __TEST_INVARIANT;
       
   428     return iOutboundProxy;
       
   429     }
       
   430 
       
   431 // -----------------------------------------------------------------------------
       
   432 // CRegBindingImplementation::SIPRefresh
       
   433 // -----------------------------------------------------------------------------
       
   434 //
       
   435 const CSIPRefresh* CRegBindingImplementation::SIPRefresh() const
       
   436     {
       
   437     __TEST_INVARIANT;
       
   438     return iRefresh;
       
   439     }
       
   440 
       
   441 // -----------------------------------------------------------------------------
       
   442 // CRegBindingImplementation::SIPRefresh
       
   443 // -----------------------------------------------------------------------------
       
   444 //
       
   445 CSIPRefresh* CRegBindingImplementation::SIPRefresh()
       
   446     {
       
   447     __TEST_INVARIANT;
       
   448     return iRefresh;
       
   449     }
       
   450     
       
   451 // -----------------------------------------------------------------------------
       
   452 // CRegBindingImplementation::Connection
       
   453 // -----------------------------------------------------------------------------
       
   454 //
       
   455 CSIPConnection* CRegBindingImplementation::Connection()
       
   456     {
       
   457     __TEST_INVARIANT;
       
   458     return iConnection;    
       
   459     }
       
   460 
       
   461 // -----------------------------------------------------------------------------
       
   462 // CRegBindingImplementation::Connection
       
   463 // -----------------------------------------------------------------------------
       
   464 //
       
   465 const CSIPConnection* CRegBindingImplementation::Connection() const
       
   466     {
       
   467     __TEST_INVARIANT;
       
   468     return iConnection;
       
   469     }
       
   470 
       
   471 // -----------------------------------------------------------------------------
       
   472 // CRegBindingImplementation::IncomingResponseL
       
   473 // -----------------------------------------------------------------------------
       
   474 //
       
   475 TBool
       
   476 CRegBindingImplementation::IncomingResponseL(CSIPResponseElements* aElements,
       
   477 				                             TUint32 aRequestId,
       
   478 							                 TUint32 aRegistrationId,
       
   479 							                 TUint32 aRefreshId,
       
   480 		                                     CConnectionCallback& aCallback)
       
   481     {
       
   482     __TEST_INVARIANT;
       
   483     __SIP_ASSERT_LEAVE(aElements != NULL, KErrArgument);
       
   484 
       
   485     if (iState)
       
   486         {
       
   487         return iState->IncomingResponseL(*this,
       
   488                                          aElements,
       
   489 		                                 aRequestId,
       
   490 					                     aRegistrationId,
       
   491 					                     aRefreshId,
       
   492                                          aCallback);
       
   493         }
       
   494     delete aElements;    
       
   495     return EFalse;
       
   496     }
       
   497 
       
   498 // -----------------------------------------------------------------------------
       
   499 // CRegBindingImplementation::ErrorOccured
       
   500 // -----------------------------------------------------------------------------
       
   501 //
       
   502 TBool CRegBindingImplementation::ErrorOccured(TInt aError,
       
   503                                      		  TUint32 aRequestId,
       
   504                                      		  CConnectionCallback& aCallback)
       
   505     {
       
   506     __TEST_INVARIANT;
       
   507 	return iState && iState->ErrorOccured(*this, aError, aRequestId, aCallback);
       
   508     }
       
   509 
       
   510 // -----------------------------------------------------------------------------
       
   511 // CRegBindingImplementation::SetRegistrationId
       
   512 // -----------------------------------------------------------------------------
       
   513 //
       
   514 void CRegBindingImplementation::SetRegistrationId(TUint32 aRegistrationId)
       
   515     {
       
   516     __TEST_INVARIANT;
       
   517 
       
   518     iRegistrationId = aRegistrationId;
       
   519 
       
   520     __TEST_INVARIANT;
       
   521     }
       
   522 
       
   523 // -----------------------------------------------------------------------------
       
   524 // CRegBindingImplementation::UpdateRefreshState
       
   525 // -----------------------------------------------------------------------------
       
   526 //
       
   527 void CRegBindingImplementation::UpdateRefreshState(TUint aStatusCode) const
       
   528     {
       
   529     __TEST_INVARIANT;
       
   530     __SIP_ASSERT_RETURN(iRefresh, KErrNotFound);
       
   531 
       
   532     iRefresh->UpdateRefreshState(aStatusCode);
       
   533     }
       
   534 
       
   535 // -----------------------------------------------------------------------------
       
   536 // CRegBindingImplementation::FindTransaction
       
   537 // -----------------------------------------------------------------------------
       
   538 //
       
   539 CSIPClientTransaction*
       
   540 CRegBindingImplementation::FindTransaction(TUint32 aRequestId)
       
   541     {
       
   542     __TEST_INVARIANT;
       
   543 
       
   544     for (TInt i = 0; i < iTransactions.Count(); i++)
       
   545         {
       
   546         if (iTransactions[i]->RequestId() == aRequestId)
       
   547             {
       
   548             return iTransactions[i];
       
   549             }
       
   550         }
       
   551 
       
   552     return NULL;
       
   553     }
       
   554 
       
   555 // -----------------------------------------------------------------------------
       
   556 // CRegBindingImplementation::ConnectionDeleted
       
   557 // As CSIPClientConnection is deleted, SIP client stops all registrations. So no
       
   558 // need to use TerminateRegistration. Clear iState as this object won't know
       
   559 // when the CSIP owning the state handlers is deleted.
       
   560 // -----------------------------------------------------------------------------
       
   561 //
       
   562 void CRegBindingImplementation::ConnectionDeleted()
       
   563     {
       
   564     __TEST_INVARIANT;
       
   565 
       
   566     iConnection = NULL;
       
   567     iState = NULL;
       
   568 
       
   569     __TEST_INVARIANT;
       
   570     }
       
   571 
       
   572 // -----------------------------------------------------------------------------
       
   573 // CRegBindingImplementation::CheckConnectionL
       
   574 // Don't use invariant here as CheckConnectionL is used from ConstructL.
       
   575 // -----------------------------------------------------------------------------
       
   576 //
       
   577 void CRegBindingImplementation::CheckConnectionL() const
       
   578     {
       
   579     __ASSERT_ALWAYS(iConnection, User::Leave(KErrSIPResourceNotAvailable));    
       
   580     }
       
   581 
       
   582 // -----------------------------------------------------------------------------
       
   583 // CRegBindingImplementation::StateL
       
   584 // -----------------------------------------------------------------------------
       
   585 //
       
   586 const CRegistrationState& CRegBindingImplementation::StateL() const
       
   587     {
       
   588     __TEST_INVARIANT;
       
   589 	__ASSERT_ALWAYS(iState != NULL, User::Leave(KErrSIPResourceNotAvailable));
       
   590 
       
   591     return *iState;
       
   592     }
       
   593 
       
   594 // -----------------------------------------------------------------------------
       
   595 // CRegBindingImplementation::ClientConnectionL
       
   596 // -----------------------------------------------------------------------------
       
   597 //
       
   598 CSIPClientConnection& CRegBindingImplementation::ClientConnectionL()
       
   599     {
       
   600     __TEST_INVARIANT;
       
   601 
       
   602     CSIPClientConnection* clientConn = ClientConnection();
       
   603     __ASSERT_ALWAYS(clientConn, User::Leave(KErrSIPResourceNotAvailable));
       
   604 
       
   605     return *clientConn;
       
   606     }
       
   607 
       
   608 // -----------------------------------------------------------------------------
       
   609 // CRegBindingImplementation::ClientConnection
       
   610 // -----------------------------------------------------------------------------
       
   611 //
       
   612 CSIPClientConnection* CRegBindingImplementation::ClientConnection()
       
   613 	{
       
   614 	__TEST_INVARIANT;
       
   615 
       
   616 	if (iConnection)
       
   617 		{
       
   618 		return iConnection->Implementation().ClientConnection();
       
   619 		}
       
   620 	return NULL;
       
   621 	}
       
   622 
       
   623 // -----------------------------------------------------------------------------
       
   624 // CRegBindingImplementation::SIPConnectionL
       
   625 // -----------------------------------------------------------------------------
       
   626 //
       
   627 CSIPConnection& CRegBindingImplementation::SIPConnectionL()
       
   628     {
       
   629     __TEST_INVARIANT;
       
   630     CheckConnectionL();    
       
   631 
       
   632     return *iConnection;
       
   633     }
       
   634 
       
   635 // -----------------------------------------------------------------------------
       
   636 // CRegBindingImplementation::ChangeState
       
   637 // -----------------------------------------------------------------------------
       
   638 //
       
   639 void CRegBindingImplementation::ChangeState(const CRegistrationState* aNewState)
       
   640     {
       
   641     __TEST_INVARIANT;
       
   642 
       
   643 	iState = aNewState;	
       
   644 
       
   645     if (iRefresh)
       
   646         {
       
   647         if (iState)
       
   648             {
       
   649             iRefresh->ChangeState(iState->RefreshState());
       
   650             }
       
   651         else
       
   652             {
       
   653             iRefresh->ChangeState(CSIPRefresh::ETerminated);
       
   654             }
       
   655         }
       
   656 
       
   657     __TEST_INVARIANT;
       
   658 	}
       
   659 
       
   660 // -----------------------------------------------------------------------------
       
   661 // CRegBindingImplementation::ConnectionLost
       
   662 // -----------------------------------------------------------------------------
       
   663 //
       
   664 void CRegBindingImplementation::ConnectionLost()
       
   665     {
       
   666     __TEST_INVARIANT;
       
   667 
       
   668     if (iState)
       
   669         {
       
   670         iState->ConnectionLost(*this);
       
   671         }
       
   672     
       
   673     for (TInt i = 0; i < iTransactions.Count(); i++)
       
   674         {
       
   675         iTransactions[i]->ChangeState(CSIPTransactionBase::ETerminated);
       
   676         }
       
   677 
       
   678     __TEST_INVARIANT;
       
   679     }
       
   680 
       
   681 // -----------------------------------------------------------------------------
       
   682 // CRegBindingImplementation::operator==
       
   683 // -----------------------------------------------------------------------------
       
   684 //
       
   685 TBool CRegBindingImplementation::operator==(
       
   686 	const CRegBindingImplementation& aRegistration) const
       
   687     {
       
   688     __TEST_INVARIANT;
       
   689 
       
   690     return ( ContextId() && aRegistration.ContextId() &&
       
   691         	 (ContextId() == aRegistration.ContextId()) ) ||
       
   692     	   this == &aRegistration;
       
   693     }
       
   694 
       
   695 // -----------------------------------------------------------------------------
       
   696 // CRegBindingImplementation::Binding
       
   697 // -----------------------------------------------------------------------------
       
   698 //
       
   699 CSIPRegistrationBinding& CRegBindingImplementation::Binding() const
       
   700 	{
       
   701 	__TEST_INVARIANT;
       
   702 	return iRegistration;
       
   703 	}
       
   704 
       
   705 // -----------------------------------------------------------------------------
       
   706 // CRegBindingImplementation::UpdateRegisteredContactL
       
   707 // -----------------------------------------------------------------------------
       
   708 //	
       
   709 void CRegBindingImplementation::UpdateRegisteredContactL()
       
   710     {
       
   711     CSIPContactHeader* contact = ClientConnectionL().ContactL(iRegistrationId);
       
   712     delete iRegisteredContact;
       
   713     iRegisteredContact = contact;
       
   714     }
       
   715 
       
   716 // -----------------------------------------------------------------------------
       
   717 // CRegBindingImplementation::RemoveRegisteredContact
       
   718 // -----------------------------------------------------------------------------
       
   719 //	
       
   720 void CRegBindingImplementation::RemoveRegisteredContact()
       
   721     {
       
   722     delete iRegisteredContact;
       
   723     iRegisteredContact = NULL;
       
   724     }
       
   725 
       
   726 // -----------------------------------------------------------------------------
       
   727 // CRegBindingImplementation::UpdateRefreshL
       
   728 // Application may not update or terminate a registration refresh.
       
   729 // -----------------------------------------------------------------------------
       
   730 //
       
   731 CSIPClientTransaction*
       
   732 CRegBindingImplementation::UpdateRefreshL(CSIPRefresh& /*aRefresh*/,
       
   733                        		              CSIPMessageElements* /*aElements*/,
       
   734                        		              TBool /*aTerminate*/)
       
   735     {
       
   736     User::Leave(KErrSIPInvalidRegistrationState);
       
   737     return NULL;
       
   738     }
       
   739 
       
   740 // -----------------------------------------------------------------------------
       
   741 // CRegBindingImplementation::DeletingRefresh
       
   742 // No need to use CSIPClientConnection::TerminateRefresh, as destructor calls
       
   743 // TerminateRegistration freeing possible refresh from the SIP stack.
       
   744 // -----------------------------------------------------------------------------
       
   745 //
       
   746 void CRegBindingImplementation::DeletingRefresh(CSIPRefresh& /*aRefresh*/,
       
   747                                        		    TUint32 /*aRefreshId*/)
       
   748     {
       
   749     __TEST_INVARIANT;
       
   750 
       
   751     iRefresh = NULL;
       
   752 
       
   753     __TEST_INVARIANT;
       
   754     }
       
   755 
       
   756 // -----------------------------------------------------------------------------
       
   757 // CRegBindingImplementation::TransactionAssociation
       
   758 // -----------------------------------------------------------------------------
       
   759 //
       
   760 MTransactionAssociation& CRegBindingImplementation::TransactionAssociation()
       
   761 	{
       
   762 	__TEST_INVARIANT;
       
   763 	return *this;
       
   764 	}
       
   765 
       
   766 // -----------------------------------------------------------------------------
       
   767 // CRegBindingImplementation::CheckIfStandAlone
       
   768 // -----------------------------------------------------------------------------
       
   769 //
       
   770 TInt CRegBindingImplementation::CheckIfStandAlone()
       
   771 	{
       
   772 	__TEST_INVARIANT;
       
   773 	return KErrSIPInvalidRegistrationState;
       
   774 	}
       
   775 
       
   776 // -----------------------------------------------------------------------------
       
   777 // CRegBindingImplementation::AddTransactionL
       
   778 // -----------------------------------------------------------------------------
       
   779 //
       
   780 void
       
   781 CRegBindingImplementation::AddTransactionL(CSIPTransactionBase& aTransaction)
       
   782     {
       
   783     __TEST_INVARIANT;
       
   784 	__SIP_ASSERT_LEAVE(aTransaction.IsSIPClientTransaction(), KErrArgument);
       
   785 
       
   786 	/*lint -e826 */
       
   787 	iTransactions.AppendL(static_cast<CSIPClientTransaction*>(&aTransaction));    
       
   788 
       
   789     __TEST_INVARIANT;
       
   790     }
       
   791 
       
   792 // -----------------------------------------------------------------------------
       
   793 // CRegBindingImplementation::RemoveTransaction
       
   794 // -----------------------------------------------------------------------------
       
   795 //
       
   796 void CRegBindingImplementation::RemoveTransaction(
       
   797 									const CSIPTransactionBase& aTransaction)
       
   798     {
       
   799     __TEST_INVARIANT;
       
   800     __SIP_ASSERT_RETURN(aTransaction.IsSIPClientTransaction(), KErrArgument);
       
   801 
       
   802 	/*lint -e826 */
       
   803     TInt pos = iTransactions.Find(
       
   804     	static_cast<const CSIPClientTransaction*>(&aTransaction));
       
   805     if (pos != KErrNotFound)
       
   806         {
       
   807         iTransactions.Remove(pos);
       
   808         }
       
   809 
       
   810     __TEST_INVARIANT;
       
   811     }
       
   812 
       
   813 // -----------------------------------------------------------------------------
       
   814 // CRegBindingImplementation::CreateClientTransactionL
       
   815 // -----------------------------------------------------------------------------
       
   816 //
       
   817 CSIPClientTransaction*
       
   818 CRegBindingImplementation::CreateClientTransactionL()
       
   819     {
       
   820     __TEST_INVARIANT;
       
   821     return CSIPClientTransaction::NewL(
       
   822     						SIPStrings::StringF(SipStrConsts::ERegister),
       
   823                             *this,
       
   824                             iRefresh);
       
   825     }
       
   826 
       
   827 // -----------------------------------------------------------------------------
       
   828 // CRegBindingImplementation::HandleError
       
   829 // -----------------------------------------------------------------------------
       
   830 //
       
   831 TBool
       
   832 CRegBindingImplementation::HandleError(CConnectionCallback& aCallback,
       
   833                                        TInt aError,
       
   834                                        TUint32 aRequestId,
       
   835                                        const CRegistrationState& aUnregistered)
       
   836     {
       
   837     __TEST_INVARIANT;
       
   838     __SIP_ASSERT_RETURN_VALUE(aError != KErrNone, EFalse);
       
   839 
       
   840     RemoveRegisteredContact();
       
   841 	ChangeState(&aUnregistered);
       
   842 
       
   843 	CConnectionCallback::TCallbackMethod callback =
       
   844 		CConnectionCallback::EErrorOccuredInRegistration;	
       
   845 	CSIPClientTransaction* ta = FindTransaction(aRequestId);
       
   846     if (ta)
       
   847         {
       
   848         ta->ChangeState(CSIPTransactionBase::ETerminated);
       
   849         callback =
       
   850         	CConnectionCallback::EErrorOccuredInRegistrationWithTransaction;
       
   851         }
       
   852 
       
   853 	aCallback.Set(callback, ta, &Binding(), NULL, NULL, aError);
       
   854 
       
   855 	__TEST_INVARIANT;
       
   856     return ETrue;
       
   857     }
       
   858 
       
   859 // -----------------------------------------------------------------------------
       
   860 // CRegBindingImplementation::CheckElementsL
       
   861 // -----------------------------------------------------------------------------
       
   862 //
       
   863 void CRegBindingImplementation::CheckElementsL(
       
   864 								const CSIPMessageElements* aElements) const
       
   865     {
       
   866     __TEST_INVARIANT;
       
   867 
       
   868     if (aElements)
       
   869         {
       
   870         RStringF contact = SIPStrings::StringF(SipStrConsts::EContactHeader);
       
   871         RStringF expires = SIPStrings::StringF(SipStrConsts::EExpiresHeader);
       
   872 
       
   873         const RPointerArray<CSIPHeaderBase>& headers = aElements->UserHeaders();
       
   874         for (TInt i = 0; i < headers.Count(); i++)
       
   875             {
       
   876             if (headers[i]->Name() == contact || headers[i]->Name() == expires)
       
   877                 {
       
   878                 User::Leave(KErrSIPMalformedMessage);
       
   879                 }
       
   880             }
       
   881         }
       
   882     }
       
   883 
       
   884 // -----------------------------------------------------------------------------
       
   885 // CRegBindingImplementation::DetachContactHeader
       
   886 // -----------------------------------------------------------------------------
       
   887 //
       
   888 void CRegBindingImplementation::DetachContactHeader(TAny* aElements)
       
   889 	{
       
   890     __SIP_ASSERT_RETURN(aElements, KErrArgument);
       
   891     DetachHeaders(aElements, SIPStrings::StringF(SipStrConsts::EContactHeader));
       
   892 	}
       
   893 
       
   894 // -----------------------------------------------------------------------------
       
   895 // CRegBindingImplementation::DetachHeaders
       
   896 // -----------------------------------------------------------------------------
       
   897 //
       
   898 void CRegBindingImplementation::DetachHeaders(TAny* aElements, RStringF aName)
       
   899 	{
       
   900     __SIP_ASSERT_RETURN(aElements, KErrArgument);
       
   901 
       
   902     CSIPMessageElements* elem =
       
   903     	reinterpret_cast<CSIPMessageElements*>(aElements);
       
   904     const RPointerArray<CSIPHeaderBase>& headers = elem->UserHeaders();
       
   905 
       
   906 	for (TInt i = headers.Count() - 1; i >= 0; i--)
       
   907         {
       
   908         if (headers[i]->Name() == aName)
       
   909             {
       
   910             elem->DetachUserHeader(headers[i]);
       
   911             }
       
   912         }
       
   913 	}
       
   914 // -----------------------------------------------------------------------------
       
   915 // CRegBindingImplementation::SetPropertyL
       
   916 // -----------------------------------------------------------------------------
       
   917 //
       
   918 void CRegBindingImplementation::SetPropertyL(TUint32 aProperty,TBool aValue)
       
   919 	{
       
   920 	__ASSERT_ALWAYS(!IsContextActive(), User::Leave(KErrSIPInvalidRegistrationState));
       
   921 	switch(aProperty)
       
   922 		{
       
   923 		case KSIPSendWithExpires:
       
   924 			{
       
   925 			iSendWithExpires = aValue;
       
   926 			break;
       
   927 			}
       
   928 		case KSIPCacheOutboundProxyIP:
       
   929 			{
       
   930 			iCacheOutboundProxyIP = aValue;
       
   931 			break;
       
   932 			}
       
   933 		default:
       
   934 			User::Leave(KErrNotFound);
       
   935 		}
       
   936 	}
       
   937 
       
   938 // -----------------------------------------------------------------------------
       
   939 // CRegBindingImplementation::__DbgTestInvariant
       
   940 // -----------------------------------------------------------------------------
       
   941 //
       
   942 void CRegBindingImplementation::__DbgTestInvariant() const
       
   943 	{
       
   944     if (!iContact || !iAor)
       
   945         {
       
   946 		User::Invariant();
       
   947 		}
       
   948 	}