realtimenetprots/sipfw/ClientResolver/Server/src/CSIPCRSessionReceiver.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2005-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          : CSipCRSessionReceiver.cpp
       
    15 // Part of       : SIP Client Resolver
       
    16 // Version       : 1.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include "CSIPCRSessionReceiver.h"
       
    23 #include "CSIPCRResponseQueue.h"
       
    24 #include "CSIPCRITCUtility.h"
       
    25 #include "CSIPCRResponseItem.h"
       
    26 #include "sipresponse.h"
       
    27 #include "SIPCRSerializer.h"
       
    28 
       
    29 // ----------------------------------------------------------------------------
       
    30 // CSIPCRSessionReceiver::NewLC
       
    31 // ----------------------------------------------------------------------------
       
    32 //
       
    33 CSIPCRSessionReceiver* CSIPCRSessionReceiver::NewL(CSIPCRITCUtility& aITC)
       
    34     {
       
    35     CSIPCRSessionReceiver* self = CSIPCRSessionReceiver::NewLC(aITC);
       
    36     CleanupStack::Pop (self);
       
    37     return self;
       
    38     }
       
    39     
       
    40 // ----------------------------------------------------------------------------
       
    41 // CSIPCRSessionReceiver::NewLC
       
    42 // ----------------------------------------------------------------------------
       
    43 //
       
    44 CSIPCRSessionReceiver* CSIPCRSessionReceiver::NewLC(CSIPCRITCUtility& aITC)
       
    45     {
       
    46     CSIPCRSessionReceiver* self = new(ELeave)CSIPCRSessionReceiver (aITC);
       
    47     CleanupStack::PushL(self);
       
    48     self->ConstructL ();
       
    49     return self;
       
    50 	}
       
    51 
       
    52 // ----------------------------------------------------------------------------
       
    53 // CSIPCRSessionReceiver::ConstructL
       
    54 // ----------------------------------------------------------------------------
       
    55 //
       
    56 void CSIPCRSessionReceiver::ConstructL()
       
    57     {
       
    58     User::LeaveIfError(iReceiverMutex.CreateLocal());
       
    59     iResponseQueue = new(ELeave)CSIPCRResponseQueue;
       
    60     }
       
    61 
       
    62 // ----------------------------------------------------------------------------
       
    63 // CSIPCRSessionReceiver::~CSIPCRSessionReceiver
       
    64 // ----------------------------------------------------------------------------
       
    65 //
       
    66 CSIPCRSessionReceiver::~CSIPCRSessionReceiver()
       
    67 	{
       
    68 	delete iResponseQueue;
       
    69 	iReceiverMutex.Close();
       
    70 	}
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // CSIPCRSessionReceiver::ConstructL
       
    74 // ----------------------------------------------------------------------------
       
    75 //
       
    76 CSIPCRSessionReceiver::CSIPCRSessionReceiver(CSIPCRITCUtility& aITC)
       
    77 : iITC(aITC),
       
    78   iClientReadyToReceive(EFalse)
       
    79     {
       
    80     }
       
    81     
       
    82 // ----------------------------------------------------------------------------
       
    83 // CSIPCRSessionReceiver::ClientReadyToReceiveL
       
    84 // ----------------------------------------------------------------------------
       
    85 //
       
    86 void CSIPCRSessionReceiver::ClientReadyToReceiveL(const RMessage2& aMessage)
       
    87     {
       
    88     CSIPCRITCUtility::WaitForMutexLC(iReceiverMutex);
       
    89     
       
    90     __ASSERT_ALWAYS (!iClientReadyToReceive, User::Leave (KErrAlreadyExists));
       
    91 
       
    92     if (iResponseQueue->IsEmpty())
       
    93 		{
       
    94         iClientReadyToReceive = ETrue;
       
    95         iReceiveRMessage = aMessage;
       
    96 		}
       
    97     else
       
    98         {
       
    99         CSIPCRResponseItem& item = iResponseQueue->First();
       
   100         TInt error = item.Error();
       
   101         iITC.WriteRequestIdL(aMessage, item.RequestId());
       
   102         if (error == KErrNone)
       
   103             {
       
   104             if (item.SIPResponse() == 0)
       
   105                 {
       
   106                 iITC.WriteClientResolvedL(aMessage, ESIPCRChannelComplete);
       
   107                 iITC.WriteChannelUidL(aMessage,item.Channel());
       
   108                 iResponseQueue->RemoveFirst();
       
   109                 }
       
   110             else
       
   111                 {
       
   112                 iITC.WriteClientResolvedL(aMessage, ESIPCRClientNotFound);
       
   113                 iITC.WriteResponseSizeL(aMessage, item.BufSizes());
       
   114                 }
       
   115             }
       
   116         else
       
   117             {
       
   118             iResponseQueue->RemoveFirst();
       
   119             }
       
   120         iITC.Complete (aMessage,error);
       
   121         }
       
   122         
       
   123     CleanupStack::PopAndDestroy(); // signals iReceiverMutex     
       
   124     }
       
   125 
       
   126 // ----------------------------------------------------------------------------
       
   127 // CSIPCRSessionReceiver::CancelClientReceiveL
       
   128 // ----------------------------------------------------------------------------
       
   129 //
       
   130 void CSIPCRSessionReceiver::CancelClientReceiveL()
       
   131     {
       
   132     CSIPCRITCUtility::WaitForMutexLC(iReceiverMutex);
       
   133     
       
   134     __ASSERT_ALWAYS (iClientReadyToReceive, User::Leave (KErrNotFound));
       
   135     iClientReadyToReceive = EFalse;
       
   136     iITC.Complete (iReceiveRMessage, KErrCancel);
       
   137     
       
   138     CleanupStack::PopAndDestroy(); // signals iReceiverMutex 
       
   139     }
       
   140 
       
   141 // ----------------------------------------------------------------------------
       
   142 // CSIPCRSessionReceiver::WriteSipResponseToClientL
       
   143 // ----------------------------------------------------------------------------
       
   144 //
       
   145 void CSIPCRSessionReceiver::WriteSipResponseToClientL(
       
   146     const RMessage2& aMessage)
       
   147     {
       
   148     CSIPCRITCUtility::WaitForMutexLC(iReceiverMutex);
       
   149     
       
   150     __ASSERT_ALWAYS (!iResponseQueue->IsEmpty(), User::Leave (KErrNotFound));
       
   151     CSIPCRResponseItem& item = iResponseQueue->First();
       
   152     __ASSERT_ALWAYS (item.RequestId() != 0,User::Leave (KErrGeneral));
       
   153     __ASSERT_ALWAYS (item.SIPResponse() != 0,User::Leave (KErrGeneral));
       
   154     __ASSERT_ALWAYS (item.SIPResponseContent() != 0,User::Leave (KErrGeneral));   
       
   155     iITC.WriteSIPResponseL(aMessage, *item.SIPResponse());
       
   156     iITC.WriteSIPResponseContentL(aMessage, *item.SIPResponseContent());
       
   157     iResponseQueue->RemoveFirst();
       
   158     
       
   159     CleanupStack::PopAndDestroy(); // signals iReceiverMutex 
       
   160     }
       
   161 
       
   162 // ----------------------------------------------------------------------------
       
   163 // CSIPCRSessionReceiver::AddErrorResponseL
       
   164 // ----------------------------------------------------------------------------
       
   165 //
       
   166 void CSIPCRSessionReceiver::AddErrorResponseL(
       
   167     TUint32 aRequestId, 
       
   168     CSIPResponse* aResponse)
       
   169     {
       
   170     CSIPCRITCUtility::WaitForMutexLC(iReceiverMutex);
       
   171     
       
   172     __ASSERT_ALWAYS (aResponse, User::Leave(KErrArgument));  
       
   173     CBufFlat* response = SIPCRSerializer::ExternalizeL(*aResponse);
       
   174     CleanupStack::PushL(response);
       
   175     HBufC8* content = aResponse->TakeContentOwnershipL();
       
   176     CleanupStack::PushL(content);
       
   177     CSIPCRResponseItem* item = 
       
   178         new(ELeave)CSIPCRResponseItem(aRequestId,response,content);
       
   179     CleanupStack::Pop(content);
       
   180     CleanupStack::Pop(response);
       
   181     CleanupStack::PushL(item);    
       
   182     if (iClientReadyToReceive)
       
   183         {
       
   184         iITC.WriteRequestIdL(iReceiveRMessage, aRequestId);
       
   185         iITC.WriteClientResolvedL(iReceiveRMessage, ESIPCRClientNotFound);
       
   186         iITC.WriteResponseSizeL(iReceiveRMessage, item->BufSizes());
       
   187         User::LeaveIfError(iResponseQueue->AddLast(item));
       
   188         iITC.Complete (iReceiveRMessage,KErrNone);
       
   189         iClientReadyToReceive = EFalse;
       
   190         }
       
   191     else
       
   192         {
       
   193         User::LeaveIfError(iResponseQueue->AddLast(item));
       
   194         }
       
   195     CleanupStack::Pop(item);
       
   196     CleanupStack::PopAndDestroy(); // signals iReceiverMutex 
       
   197     
       
   198     }
       
   199 
       
   200 // ----------------------------------------------------------------------------
       
   201 // CSIPCRSessionReceiver::RequestCompletedL
       
   202 // ----------------------------------------------------------------------------
       
   203 //
       
   204 void CSIPCRSessionReceiver::RequestCompletedL(
       
   205     TUint32 aRequestId, 
       
   206     TUid aChannel)
       
   207     {
       
   208     CSIPCRITCUtility::WaitForMutexLC(iReceiverMutex);
       
   209     
       
   210     __ASSERT_ALWAYS (aChannel.iUid != 0, User::Leave(KErrArgument));
       
   211     __ASSERT_ALWAYS (aRequestId, User::Leave(KErrArgument));
       
   212     if (iClientReadyToReceive)
       
   213         {
       
   214         iITC.WriteRequestIdL(iReceiveRMessage, aRequestId);
       
   215         iITC.WriteClientResolvedL(iReceiveRMessage, ESIPCRChannelComplete);
       
   216         iITC.WriteChannelUidL(iReceiveRMessage, aChannel);
       
   217         iITC.Complete (iReceiveRMessage,KErrNone);
       
   218         iClientReadyToReceive = EFalse;
       
   219         }
       
   220     else
       
   221         {
       
   222         CSIPCRResponseItem* item = 
       
   223             new(ELeave)CSIPCRResponseItem(aRequestId,aChannel);
       
   224         CleanupStack::PushL(item);
       
   225         User::LeaveIfError(iResponseQueue->AddLast(item));
       
   226         CleanupStack::Pop(item);
       
   227         }
       
   228         
       
   229     CleanupStack::PopAndDestroy(); // signals iReceiverMutex     
       
   230     }
       
   231     
       
   232 // ----------------------------------------------------------------------------
       
   233 // CSIPCRSessionReceiver::ErrorReceivedL
       
   234 // ----------------------------------------------------------------------------
       
   235 //
       
   236 void CSIPCRSessionReceiver::ErrorReceivedL(
       
   237     TUint32 aRequestId,
       
   238     TInt aError)   
       
   239     {
       
   240     CSIPCRITCUtility::WaitForMutexLC(iReceiverMutex);
       
   241     
       
   242     if (aError != KErrNone)
       
   243         {
       
   244         if (iClientReadyToReceive)
       
   245             {
       
   246             iITC.WriteRequestIdL (iReceiveRMessage, aRequestId);
       
   247             iITC.Complete(iReceiveRMessage,aError);
       
   248             iClientReadyToReceive = EFalse;
       
   249             }
       
   250         else
       
   251             {
       
   252             CSIPCRResponseItem* item = 
       
   253                 new(ELeave)CSIPCRResponseItem(aRequestId,aError);
       
   254             CleanupStack::PushL(item);
       
   255             User::LeaveIfError(iResponseQueue->AddLast(item));
       
   256             CleanupStack::Pop(item);
       
   257             }
       
   258         }
       
   259         
       
   260     CleanupStack::PopAndDestroy(); // signals iReceiverMutex     
       
   261     }
       
   262 
       
   263 // ----------------------------------------------------------------------------
       
   264 // CSIPCRSessionReceiver::RemoveByRequestId
       
   265 // ----------------------------------------------------------------------------
       
   266 //
       
   267 void CSIPCRSessionReceiver::RemoveByRequestId(TUint32 aRequestId)
       
   268     {
       
   269     iReceiverMutex.Wait();
       
   270     iResponseQueue->RemoveByRequestId(aRequestId);
       
   271     iReceiverMutex.Signal();
       
   272     }
       
   273     
       
   274 // ----------------------------------------------------------------------------
       
   275 // CSIPCRSessionReceiver::RemoveAll
       
   276 // ----------------------------------------------------------------------------
       
   277 //    
       
   278 void CSIPCRSessionReceiver::RemoveAll()
       
   279     {
       
   280     iReceiverMutex.Wait();
       
   281     iResponseQueue->RemoveAll();
       
   282     iReceiverMutex.Signal();
       
   283     }
       
   284 
       
   285 // End of File