realtimenetprots/sipfw/SIP/ConnectionMgr/src/CErrorHandler.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2002-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          : CErrorHandler.cpp
       
    15 // Part of       : ConnectionMgr
       
    16 // Version       : SIP/4.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "CErrorHandler.h"
       
    23 #include "CTransport.h"
       
    24 #include "sipuri.h"
       
    25 #include "siprequest.h"
       
    26 #include "sipresponse.h"
       
    27 #include "siptoheader.h"
       
    28 #include "sipfromheader.h"
       
    29 #include "sipviaheader.h"
       
    30 #include "sipcallidheader.h"
       
    31 #include "sipcseqheader.h"
       
    32 #include "sipstrings.h"
       
    33 #include "sipstrconsts.h"
       
    34 #include "TSIPTransportParams.h"
       
    35 #include "CommonConsts.h"
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CTransportErrorHandler::NewL
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CTransportErrorHandler* CTransportErrorHandler::NewL(CTransportBase& aTransport)
       
    42 	{
       
    43 	CTransportErrorHandler* self = NewLC(aTransport);
       
    44 	CleanupStack::Pop(self);
       
    45 	return self;	
       
    46 	}
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CTransportErrorHandler::NewLC
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CTransportErrorHandler*
       
    53 CTransportErrorHandler::NewLC(CTransportBase& aTransport)
       
    54 	{
       
    55 	CTransportErrorHandler* self =
       
    56 		new (ELeave) CTransportErrorHandler(aTransport);
       
    57 	CleanupStack::PushL(self);
       
    58 	return self;	
       
    59 	}
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CTransportErrorHandler::SendErrorResponseL
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void CTransportErrorHandler::SendErrorResponseL(
       
    66     const TSIPTransportParams& aParams,
       
    67     TUint aResponseCode,	
       
    68     RStringF aReasonPhrase, 
       
    69 	CSIPMessage& aMessage,
       
    70 	const TInetAddr& aAddr)
       
    71 	{	
       
    72 	if (!aMessage.HasHeader(SIPStrings::StringF(SipStrConsts::EViaHeader)))
       
    73 	    {
       
    74 	    User::Leave(KErrArgument);
       
    75 	    }
       
    76 	    
       
    77 	if ( !IsActive() )
       
    78 		{
       
    79 		if (iTransport->ProtocolType() == KProtocolInetUdp)
       
    80 			{
       
    81 			iTransport->CancelResponseSend(iId, ETrue);
       
    82 			delete iResponse; 
       
    83 			iResponse = 0;
       
    84 			CreateResponseL(aResponseCode, aReasonPhrase, aMessage);
       
    85 			iAddr = aAddr;
       
    86 			iTransport->Send(aParams, *iResponse, iId, iStatus);
       
    87 			}
       
    88 		else
       
    89 			{
       
    90 			CreateResponseL(aResponseCode, aReasonPhrase, aMessage);
       
    91 			iAddr = aAddr;
       
    92 			iTransport->Send(aParams, *iResponse, iAddr, iStatus);
       
    93 			}
       
    94 		SetActive();
       
    95 		}
       
    96 		
       
    97 	}
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CTransportErrorHandler::CreateResponseL
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CTransportErrorHandler::CreateResponseL(TUint aResponseCode,	
       
   104 							RStringF aReasonPhrase, 
       
   105 							CSIPMessage& aMessage)
       
   106 	{
       
   107 	iResponse = CSIPResponse::NewL(aResponseCode, aReasonPhrase);
       
   108 	SetViaToResL(aMessage);
       
   109 	if (aMessage.HasHeader(SIPStrings::StringF(SipStrConsts::EToHeader)))
       
   110 	    {
       
   111 		SetToToResL(aMessage);
       
   112 	    }
       
   113 	if (aMessage.HasHeader(SIPStrings::StringF(SipStrConsts::EFromHeader)))
       
   114 	    {
       
   115 		SetFromToResL(aMessage);
       
   116 	    }
       
   117 	if (aMessage.HasHeader(SIPStrings::StringF(SipStrConsts::ECallIDHeader)))
       
   118 	    {
       
   119 		SetCallIdToResL(aMessage);
       
   120 	    }
       
   121 	if (aMessage.HasHeader(SIPStrings::StringF(SipStrConsts::ECSeqHeader)))
       
   122 	    {
       
   123 		SetCSeqToResL(aMessage);
       
   124 	    }
       
   125 
       
   126 	}
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CTransportErrorHandler::RunL
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 void CTransportErrorHandler::RunL()
       
   133 	{
       
   134 	TUint protocol( iTransport->ProtocolType() );
       
   135 	if( protocol == KProtocolInetTcp || protocol == KProtocolTls )
       
   136 		{
       
   137 		delete iResponse;
       
   138 		iResponse = 0;
       
   139 		}
       
   140 	}
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CTransportErrorHandler::DoCancel
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 void CTransportErrorHandler::DoCancel()
       
   147 	{
       
   148 	iTransport->CancelSend(iStatus);
       
   149 	if(iResponse)
       
   150 		{
       
   151 		delete iResponse;
       
   152 		iResponse = 0;
       
   153 		}
       
   154 	}
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CTransportErrorHandler::RunError
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 TInt CTransportErrorHandler::RunError(TInt aError)
       
   161 	{
       
   162 	if(aError == KErrNoMemory)
       
   163 		{
       
   164 		return aError;
       
   165 		}
       
   166 	return KErrNone;
       
   167 	}
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CTransportErrorHandler::SetViaToResL
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 void CTransportErrorHandler::SetViaToResL(CSIPMessage& aMessage)
       
   174 	{
       
   175 	TSglQueIter<CSIPHeaderBase> iter = 
       
   176 	        aMessage.Headers(SIPStrings::StringF(SipStrConsts::EViaHeader));
       
   177 	CSIPHeaderBase* header;
       
   178 	iter.SetToFirst();
       
   179 	while((header = iter++) != 0)
       
   180 		{
       
   181 		CSIPHeaderBase* viaHeader = 
       
   182 		                    static_cast<CSIPViaHeader*>(header)->CloneL();
       
   183 		CleanupStack::PushL(viaHeader);
       
   184 		iResponse->AddHeaderL(static_cast<CSIPViaHeader*>(viaHeader));
       
   185 		CleanupStack::Pop(viaHeader);
       
   186 		}
       
   187 	}
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CTransportErrorHandler::SetToToResL
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CTransportErrorHandler::SetToToResL(CSIPMessage& aMessage)
       
   194 	{
       
   195 	TSglQueIter<CSIPHeaderBase> iter = 
       
   196 	        aMessage.Headers(SIPStrings::StringF(SipStrConsts::EToHeader));
       
   197 	CSIPHeaderBase* header = 0;
       
   198 	iter.SetToFirst();
       
   199 	header = iter;
       
   200 	CSIPToHeader* toHeader =
       
   201 		CSIPToHeader::NewLC(static_cast<CSIPToHeader&>(*header));
       
   202 	iResponse->AddHeaderL(toHeader);
       
   203 	CleanupStack::Pop(toHeader);
       
   204 	}
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CTransportErrorHandler::SetFromToResL
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CTransportErrorHandler::SetFromToResL(CSIPMessage& aMessage)
       
   211 	{
       
   212 	TSglQueIter<CSIPHeaderBase> iter = 
       
   213 	        aMessage.Headers(SIPStrings::StringF(SipStrConsts::EFromHeader));
       
   214 	CSIPHeaderBase* header = 0;
       
   215 	iter.SetToFirst();
       
   216 	header = iter;
       
   217 	CSIPFromHeader* fromHeader =
       
   218 		CSIPFromHeader::NewLC(static_cast<CSIPFromHeader&>(*header));
       
   219 	iResponse->AddHeaderL(fromHeader);
       
   220 	CleanupStack::Pop(fromHeader);
       
   221 	}
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CTransportErrorHandler::SetCallIdToResL
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 void CTransportErrorHandler::SetCallIdToResL(CSIPMessage& aMessage)
       
   228 	{
       
   229 	TSglQueIter<CSIPHeaderBase> iter =
       
   230 		aMessage.Headers(SIPStrings::StringF(SipStrConsts::ECallIDHeader));
       
   231 	CSIPHeaderBase* header = 0;
       
   232 	iter.SetToFirst();
       
   233 	header = iter;
       
   234 	CSIPHeaderBase* callIdHeader = 
       
   235 	                        static_cast<CSIPCallIDHeader*>(header)->CloneL();
       
   236 	CleanupStack::PushL(callIdHeader);
       
   237 	iResponse->AddHeaderL(static_cast<CSIPCallIDHeader*>(callIdHeader));
       
   238 	CleanupStack::Pop(callIdHeader);
       
   239 	}
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // CTransportErrorHandler::SetCSeqToResL
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CTransportErrorHandler::SetCSeqToResL(CSIPMessage& aMessage)
       
   246 	{
       
   247 	TSglQueIter<CSIPHeaderBase> iter = aMessage.Headers(
       
   248 	                        SIPStrings::StringF(SipStrConsts::ECSeqHeader));
       
   249 	CSIPHeaderBase* header = 0;
       
   250 	iter.SetToFirst();
       
   251 	header = iter;
       
   252 
       
   253     CSIPHeaderBase* cseqheader = static_cast<CSIPCSeqHeader*>(header)->CloneL();
       
   254     CleanupStack::PushL(cseqheader);
       
   255 	iResponse->AddHeaderL(static_cast<CSIPCSeqHeader*>(cseqheader));
       
   256 	CleanupStack::Pop(cseqheader);
       
   257 	}
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CTransportErrorHandler::~CTransportErrorHandler
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 CTransportErrorHandler::~CTransportErrorHandler()
       
   264 	{
       
   265 	Cancel();
       
   266 	delete iResponse;
       
   267 	}
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // CTransportErrorHandler::CTransportErrorHandler
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 CTransportErrorHandler::CTransportErrorHandler(CTransportBase& aTransport) :
       
   274 	CActive(CActive::EPriorityStandard),
       
   275 	iTransport(&aTransport)
       
   276 	{
       
   277 	CActiveScheduler::Add(this);
       
   278 	iResponse = 0;
       
   279 	iId = 0;
       
   280 	}