dosservices/commondsy/src/CDSYMessageBufferItem.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implementation of CDSYMessageBufferItem class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES 
       
    22 #include "CDSYMessageBufferItem.h"
       
    23 #include "CDSYController.h"
       
    24 #include "CDSYMessage.h"
       
    25 #include "CDSYMessageStructures.h"
       
    26 #include "CDSYDOSServerRequestManager.h"
       
    27 #include "commondsy_debug.h"
       
    28 
       
    29 
       
    30 
       
    31 // EXTERNAL DATA STRUCTURES
       
    32 // EXTERNAL FUNCTION PROTOTYPES  
       
    33 // CONSTANTS
       
    34 #ifdef _DEBUG
       
    35 
       
    36     // 20 seconds
       
    37     const TUint32 KTransactionExpirationTime = 20000000;
       
    38 
       
    39 #else
       
    40 
       
    41     // 10 seconds
       
    42     const TUint32 KTransactionExpirationTime = 10000000;
       
    43 
       
    44 #endif
       
    45 
       
    46 
       
    47 
       
    48 // MACROS
       
    49 // LOCAL CONSTANTS AND MACROS
       
    50 // MODULE DATA STRUCTURES
       
    51 // LOCAL FUNCTION PROTOTYPES
       
    52 // FORWARD DECLARATIONS
       
    53 
       
    54 
       
    55 
       
    56 // ============================= LOCAL FUNCTIONS ===============================
       
    57 
       
    58 
       
    59 
       
    60 // ============================ MEMBER FUNCTIONS ===============================
       
    61 
       
    62 
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CCDSYMessageBufferItem::NewL
       
    66 // -----------------------------------------------------------------------------
       
    67 CCDSYMessageBufferItem* CCDSYMessageBufferItem::NewL( CCDSYController& aCDSYController, CCDSYDOSServerRequestManager& aCDSYDOSServerRequestManager, TCDSYMessage& aCDSYMessage )
       
    68     {
       
    69     COM_TRACE_3( "CDSY - CCDSYMessageBufferItem::NewL(0x%x, 0x%x, 0x%x)", &aCDSYController, &aCDSYDOSServerRequestManager, &aCDSYMessage );
       
    70     
       
    71     CCDSYMessageBufferItem* messageBufferItem = new ( ELeave ) CCDSYMessageBufferItem( aCDSYController, aCDSYDOSServerRequestManager, aCDSYMessage );
       
    72     CleanupStack::PushL( messageBufferItem );
       
    73     messageBufferItem->ConstructL();
       
    74     messageBufferItem->iTransactionID = aCDSYMessage.TransactionID();
       
    75     CleanupStack::Pop( messageBufferItem );
       
    76 
       
    77     COM_TRACE_1( "CDSY - CCDSYMessageBufferItem::NewL - return 0x%x", messageBufferItem );
       
    78     return messageBufferItem;
       
    79     }
       
    80 
       
    81 
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CCDSYMessageBufferItem::NewL
       
    85 // -----------------------------------------------------------------------------
       
    86 CCDSYMessageBufferItem* CCDSYMessageBufferItem::NewL( CCDSYController& aCDSYController, CCDSYDOSServerRequestManager& aCDSYDOSServerRequestManager, TCDSYMessage& aCDSYMessage, RMessagePtr2 aMessage )
       
    87     {
       
    88     COM_TRACE_4( "CDSY - CCDSYMessageBufferItem::NewL(0x%x, 0x%x, 0x%x, 0x%x)", &aCDSYController, &aCDSYDOSServerRequestManager, &aCDSYMessage, &aMessage );
       
    89     
       
    90     TRACE_ASSERT( !aMessage.IsNull() );
       
    91 
       
    92     CCDSYMessageBufferItem* messageBufferItem = new ( ELeave ) CCDSYMessageBufferItem( aCDSYController, aCDSYDOSServerRequestManager, aCDSYMessage );
       
    93     CleanupStack::PushL( messageBufferItem );
       
    94     messageBufferItem->ConstructL();
       
    95     // The message and its data are copied since in asynchronous case there is no active wait
       
    96     // and the stack-allocated data gets deleted when SendMessageL returns
       
    97     if ( aCDSYMessage.Data() != NULL )
       
    98         {
       
    99         messageBufferItem->iMessageData = HBufC8::NewL( aCDSYMessage.Data()->Length() );
       
   100         *messageBufferItem->iMessageData = *aCDSYMessage.Data();
       
   101         }
       
   102     messageBufferItem->iCDSYMessage = new ( ELeave ) TCDSYMessage( aCDSYMessage.Group(), aCDSYMessage.ID(), messageBufferItem->iMessageData, aCDSYMessage.TransactionID() );
       
   103     // The owned flag tells the destructor that the message needs to be deleted
       
   104     messageBufferItem->iMessageOwned = ETrue;
       
   105     messageBufferItem->iClientServerMessage = aMessage;
       
   106     messageBufferItem->iTransactionID = aCDSYMessage.TransactionID();
       
   107     CleanupStack::Pop( messageBufferItem );
       
   108 
       
   109     COM_TRACE_1( "CDSY - CCDSYMessageBufferItem::NewL - return 0x%x", messageBufferItem );
       
   110     return messageBufferItem;
       
   111     }
       
   112 
       
   113 
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CCDSYMessageBufferItem::CCDSYMessageBufferItem
       
   117 // -----------------------------------------------------------------------------
       
   118 CCDSYMessageBufferItem::CCDSYMessageBufferItem( CCDSYController& aCDSYController, CCDSYDOSServerRequestManager& aCDSYDOSServerRequestManager, TCDSYMessage& aCDSYMessage )
       
   119 : iCDSYController( aCDSYController )
       
   120 , iCDSYDOSServerRequestManager( aCDSYDOSServerRequestManager )
       
   121 , iCDSYMessage( &aCDSYMessage )
       
   122 , iMessageData( NULL )
       
   123 , iMessageOwned( EFalse )
       
   124 , iActiveWait( EFalse )
       
   125 , iTimer( NULL )
       
   126 , iTransactionID( 0xFFFFFFFF )
       
   127     {
       
   128     COM_TRACE_3( "CDSY - CCDSYMessageBufferItem::CCDSYMessageBufferItem(0x%x, 0x%x, 0x%x)", &aCDSYController, &aCDSYDOSServerRequestManager, &aCDSYMessage );
       
   129 
       
   130     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::CCDSYMessageBufferItem - return void" );
       
   131     }
       
   132 
       
   133 
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CCDSYMessageBufferItem::~CCDSYMessageBufferItem
       
   137 // -----------------------------------------------------------------------------
       
   138 CCDSYMessageBufferItem::~CCDSYMessageBufferItem()
       
   139     {
       
   140     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::~CCDSYMessageBufferItem()" );
       
   141 
       
   142     delete iTimer;
       
   143     delete iMessageData;
       
   144     if ( iMessageOwned )
       
   145         {
       
   146         delete iCDSYMessage;
       
   147         }
       
   148 
       
   149     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::~CCDSYMessageBufferItem - return void" );
       
   150     }
       
   151 
       
   152 
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CCDSYMessageBufferItem::ConstructL
       
   156 // -----------------------------------------------------------------------------
       
   157 void CCDSYMessageBufferItem::ConstructL()
       
   158     {
       
   159     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::ConstructL()" );
       
   160     
       
   161     iTimer = CPeriodic::NewL( CActive::EPriorityHigh );
       
   162 
       
   163     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::ConstructL - return void" );
       
   164     }
       
   165 
       
   166 
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CCDSYMessageBufferItem::SendMessageL
       
   170 // -----------------------------------------------------------------------------
       
   171 void CCDSYMessageBufferItem::SendMessageL()
       
   172     {
       
   173     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::SendMessage()" );
       
   174     
       
   175     if ( iClientServerMessage.IsNull() )
       
   176         {
       
   177         iCDSYController.ProcessMessageL( *iCDSYMessage );
       
   178         if ( iCDSYMessage->ReturnData() == NULL )
       
   179             {
       
   180             iTimer->Start( KTransactionExpirationTime,  KTransactionExpirationTime, TCallBack( NotifyTimerCallback, this ) );
       
   181             // Response not received, since the datacount is same.
       
   182             BeginActiveWait();
       
   183             }
       
   184         if ( iCDSYMessage->ReturnData() == NULL )
       
   185             {
       
   186             User::Leave( KErrGeneral );
       
   187             }
       
   188         }
       
   189     else
       
   190         {
       
   191         iTimer->Start( KTransactionExpirationTime,  KTransactionExpirationTime, TCallBack( NotifyTimerCallback, this ) );
       
   192         iCDSYController.ProcessMessageL( *iCDSYMessage );
       
   193         }
       
   194 
       
   195     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::SendMessage - return void" );
       
   196     }
       
   197 
       
   198 
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CCDSYMessageBufferItem::ProcessResponseL
       
   202 // -----------------------------------------------------------------------------
       
   203 void CCDSYMessageBufferItem::ProcessResponseL( const TCDSYMessage& aCDSYMessage )
       
   204     {
       
   205     COM_TRACE_1( "CDSY - CCDSYMessageBufferItem::ProcessResponseL(0x%x)", &aCDSYMessage );
       
   206 
       
   207     if ( iClientServerMessage.IsNull() )
       
   208         {
       
   209         TRACE_ASSERT( aCDSYMessage.Data() != NULL );
       
   210         iCDSYMessage->SetReturnDataL( aCDSYMessage.Data() );
       
   211         EndActiveWait();
       
   212         }
       
   213     else
       
   214         {
       
   215         COM_TRACE_( "CDSY - CCDSYMessageBufferItem::ProcessResponseL - Completing async request" );
       
   216         TRACE_ASSERT( aCDSYMessage.Data() != NULL );
       
   217         TRACE_ASSERT( aCDSYMessage.Data()->Length() == sizeof ( TCDSYMsgDataReturn ) );
       
   218         iClientServerMessage.Complete( ( ( TCDSYMsgDataReturn* )aCDSYMessage.Data()->Ptr() )->iReturn );
       
   219         }
       
   220 
       
   221     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::ProcessResponseL - return void" );
       
   222     }
       
   223 
       
   224 
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CCDSYMessageBufferItem::BeginActiveWait
       
   228 // -----------------------------------------------------------------------------
       
   229 void CCDSYMessageBufferItem::BeginActiveWait()
       
   230     {
       
   231     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::BeginActiveWait()" );
       
   232 
       
   233     TRACE_ASSERT( iClientServerMessage.IsNull() );
       
   234 
       
   235     if ( iActiveWait )
       
   236         {
       
   237         TRACE_ASSERT_ALWAYS;
       
   238         }
       
   239     else
       
   240         {
       
   241         iActiveWait = ETrue;
       
   242         iWait.Start();
       
   243         }
       
   244 
       
   245     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::BeginActiveWait - return void" );
       
   246     }
       
   247 
       
   248 
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CCDSYMessageBufferItem::EndActiveWait
       
   252 // -----------------------------------------------------------------------------
       
   253 void CCDSYMessageBufferItem::EndActiveWait()
       
   254     {
       
   255     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::EndActiveWait()" );
       
   256 
       
   257     TRACE_ASSERT( iClientServerMessage.IsNull() );
       
   258     if ( iActiveWait )
       
   259         {
       
   260         iWait.AsyncStop();
       
   261         iActiveWait = EFalse;
       
   262         iTimer->Cancel();
       
   263         }
       
   264 
       
   265     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::EndActiveWait - return void" );
       
   266     }
       
   267 
       
   268 
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CCDSYMessageBufferItem::NotifyTimerCallback
       
   272 // -----------------------------------------------------------------------------
       
   273 TInt CCDSYMessageBufferItem::NotifyTimerCallback( TAny* aPtr )
       
   274     {
       
   275     COM_TRACE_1( "CDSY - CCDSYMessageBufferItem::NotifyTimerCallback(0x%x)", aPtr );
       
   276 
       
   277     TRACE_ASSERT( aPtr != NULL );
       
   278     
       
   279     TInt err;
       
   280     TRAP( err, ( ( CCDSYMessageBufferItem* )aPtr )->DoNotifyTimerCallbackL() );
       
   281     TRACE_ASSERT( err == KErrNone );
       
   282 
       
   283     COM_TRACE_1( "CDSY - CCDSYMessageBufferItem::NotifyTimerCallback - return 0x%x", ETrue );
       
   284     return ETrue;
       
   285     }
       
   286 
       
   287 
       
   288 
       
   289 // -----------------------------------------------------------------------------
       
   290 // CCDSYMessageBufferItem::DoNotifyTimerCallbackL
       
   291 // -----------------------------------------------------------------------------
       
   292 void CCDSYMessageBufferItem::DoNotifyTimerCallbackL()
       
   293     {
       
   294     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::DoNotifyTimerCallbackL()" );
       
   295     
       
   296     TRACE_ASSERT_ALWAYS;
       
   297 
       
   298     iCDSYController.ExpireMessageL( *iCDSYMessage );
       
   299 
       
   300     if ( iClientServerMessage.IsNull() )
       
   301         {
       
   302         EndActiveWait();
       
   303         }
       
   304     else
       
   305         {
       
   306         iCDSYDOSServerRequestManager.Cancel( iCDSYMessage->TransactionID(), KErrGeneral );
       
   307         }
       
   308 
       
   309     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::DoNotifyTimerCallbackL - return void" );
       
   310     }
       
   311 
       
   312 
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CCDSYMessageBufferItem::CancelAsyncCommand
       
   316 // -----------------------------------------------------------------------------
       
   317 void CCDSYMessageBufferItem::CancelAsyncCommand( TInt aReason ) const
       
   318     {
       
   319     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::CancelAsyncCommand()" );
       
   320 
       
   321     iClientServerMessage.Complete( aReason );
       
   322 
       
   323     COM_TRACE_( "CDSY - CCDSYMessageBufferItem::CancelAsyncCommand - return void" );
       
   324     }
       
   325