IMPSengine/client/src/impshandler.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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: 
       
    15 * handler class for imps common client.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32std.h>
       
    22 
       
    23 #include "impsclient.h"
       
    24 #include "impsfields.h"
       
    25 #include "impspacked.h"
       
    26 #include "impskey.h"
       
    27 #include "impshandler.h"
       
    28 #include "impsutils.h"
       
    29 #include "impsservercommon.h"
       
    30 #include "impserrors.h"
       
    31 #include "impsdetailed.h"
       
    32 #include "impsdataaccessor.h"
       
    33 #include "impscdatautils.h"
       
    34 
       
    35 // MACROS
       
    36 #ifndef _DEBUG
       
    37 #define _NO_IMPS_LOGGING_
       
    38 #endif
       
    39 
       
    40 // ================= MEMBER FUNCTIONS =======================
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // TWvBuf::TWvBuf
       
    44 // -----------------------------------------------------------------------------
       
    45 
       
    46 TWvBuf::TWvBuf( HBufC8* aBuf ) :
       
    47         iPtr( aBuf->Des() ) 
       
    48         {}
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // CImpsHandler2::CImpsHandler2()
       
    52 // ----------------------------------------------------------------------------
       
    53 CImpsHandler2::CImpsHandler2( 
       
    54     TInt aPriority, 
       
    55     RImpsClient2& aClient  ) :
       
    56     CActive( aPriority ), 
       
    57     iClient( aClient ),
       
    58     iBody( NULL ),
       
    59     iPtrStore( NULL )
       
    60     {
       
    61     CActiveScheduler::Add( this );
       
    62     }
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // CImpsHandler2::~CImpsHandler2()
       
    66 // ----------------------------------------------------------------------------
       
    67  CImpsHandler2::~CImpsHandler2()
       
    68      {
       
    69 
       
    70      delete iBody;
       
    71      delete iFields;
       
    72      delete iPtrStore;
       
    73      delete iBusyTimer;
       
    74 
       
    75      if ( iDestroyedPtr )
       
    76          {
       
    77          // We are called via own RunL!
       
    78          *iDestroyedPtr = ETrue;
       
    79          iDestroyedPtr = NULL;
       
    80          }
       
    81 
       
    82      // Do not call Cancel() here, because of it is called outside if needed.
       
    83      // This is observer for server thread, so do not cancel yourself!
       
    84      }
       
    85 
       
    86 // ----------------------------------------------------------------------------
       
    87 // CImpsHandler2::StartRun()
       
    88 // ----------------------------------------------------------------------------
       
    89 void CImpsHandler2::StartRun()
       
    90     {
       
    91 
       
    92     // Start to wait push messages from the server
       
    93     iStatus = KRequestPending;
       
    94     SetActive();
       
    95     iFields->Reset();
       
    96 
       
    97     }
       
    98 
       
    99 // ----------------------------------------------------------------------------
       
   100 // CImpsHandler2::RunL()
       
   101 // ----------------------------------------------------------------------------
       
   102 void CImpsHandler2::RunL()
       
   103     {
       
   104 #ifndef _NO_IMPS_LOGGING_
       
   105     CImpsClientLogger::Log(_L("CImpsHandler2::RunL begin status=%d h=%d cli=%d"), 
       
   106         iStatus.Int(), (TInt)this, (TInt)&iClient );
       
   107 #endif
       
   108 
       
   109     // Set the member to point to stack variable
       
   110     TBool   destroyed( EFalse );
       
   111     iDestroyedPtr = &destroyed;
       
   112     // Is the event handled
       
   113     TBool handled( EFalse );
       
   114     TInt err = KErrNone;
       
   115     
       
   116     
       
   117     // Server is busy
       
   118     if ( iStatus == KErrServerBusy )
       
   119         {
       
   120 #ifndef _NO_IMPS_LOGGING_
       
   121     CImpsClientLogger::Log(_L("CImpsHandler2::Starting timer") ); 
       
   122 #endif
       
   123 		iBusyTimer->Start();
       
   124         return;
       
   125         }
       
   126     
       
   127     // Server is cancelled
       
   128     if ( iStatus == KErrCancel || iStatus == KErrServerTerminated )
       
   129         {
       
   130         return;
       
   131         }
       
   132 
       
   133     // special "error" codes for status changes
       
   134     if ( iStatus == KImpsOnlineStatus ||
       
   135          iStatus == KImpsOfflineStatus ||
       
   136          iStatus == KImpsNotLoggedStatus || 
       
   137          iStatus == KImpsErrorShuttingDown )
       
   138         {
       
   139         TImpsServiceStatus newStatus = EImps_ON_LINE;
       
   140         if ( iStatus == KImpsNotLoggedStatus )
       
   141             {
       
   142             newStatus = EImps_NOT_LOGGED;
       
   143             }
       
   144         else if ( iStatus == KImpsErrorShuttingDown )
       
   145             {
       
   146             newStatus = EImps_SHUTTING_DOWN;
       
   147             }
       
   148         if ( iStatusHandler )
       
   149             {
       
   150             TInt err ( KErrNone );
       
   151             TRAP( err, iStatusHandler->HandleStatusChangeL( newStatus , *iClient.CspIdentifier() ));
       
   152             handled = ETrue;
       
   153             }
       
   154         }
       
   155 
       
   156     // Destructor sets this to ETrue. An application may have called unregister
       
   157     if ( destroyed )
       
   158         {       
       
   159         return;
       
   160         }
       
   161     
       
   162     TInt dataLength = iStatus.Int();
       
   163 
       
   164     // Check first if a special OOM error event
       
   165     if ( dataLength > 0 && ( dataLength & KImpsOOMReply ) )
       
   166         {
       
   167         TInt errId = dataLength & KImpsOOMReplyOpId;
       
   168         dataLength = 0;
       
   169         err = KErrNoMemory;
       
   170         // This cannot leave with NULL fields parameter
       
   171         HandleErrorEventL( errId, KErrNoMemory, NULL );
       
   172         }
       
   173 
       
   174     // Read the data if any to iFields
       
   175     // Check if need to read message body separately
       
   176     TBool comp( ETrue );
       
   177     if ( dataLength > 0 )
       
   178         {
       
   179         TRAP( err, comp = ReadDataL( dataLength ) );
       
   180         }
       
   181 
       
   182     if ( !err && !handled && comp )
       
   183         {
       
   184         // Not here if reading of data fails
       
   185         CImpsFields* errF = ( dataLength > 0 ? iFields : NULL );
       
   186         { // These parenthises must be here.
       
   187           // Otherwise the program will crash
       
   188           // This is releated to the destroying this object
       
   189           // and trap frame.
       
   190 
       
   191         // this trap handles all internal errors
       
   192         // errors caused by callbacks are trapped elsewhere
       
   193         TRAP( err, HandleEventL( errF ) ); 
       
   194         if ( err != KErrNone )
       
   195             {
       
   196             SImpsEventData* event = (SImpsEventData*)iEventData.Ptr();
       
   197             HandleErrorEventL( event->iOpCode, err, NULL );
       
   198             }
       
   199         }
       
   200         }
       
   201 
       
   202     // Destructor sets this to ETrue, nice trick.
       
   203     if ( !destroyed )
       
   204         {       
       
   205         // Activate again and reset data.
       
   206         // This must not leave but has to ask for more by
       
   207         // calling either EventHandled() or EventBody(). 
       
   208         StartRun();
       
   209         TInt bufSize = CurrentSize();
       
   210 #ifndef _NO_IMPS_LOGGING_
       
   211     CImpsClientLogger::Log(_L("CImpsHandler2: comp=%d bufSize=%d h=%d cli=%d"), 
       
   212         comp, bufSize, (TInt)this, (TInt)&iClient );
       
   213 #endif
       
   214         if ( comp )
       
   215             {
       
   216             // Message handling is complete. Free the buffer if the 
       
   217             // message was a big one.
       
   218             if ( bufSize > KImpsIPCThreshold )
       
   219                 {
       
   220                 ResetBuffers();
       
   221                 }
       
   222             EventHandled();
       
   223             }
       
   224         else
       
   225             {
       
   226             if ( CurrentSize() < dataLength )
       
   227                 {
       
   228                 // allocate memory if needed
       
   229                 ResetBuffers();
       
   230                 TRAP( err, 
       
   231                     {
       
   232                     iBody = HBufC8::NewL( dataLength );  
       
   233                     iPtrStore = new (ELeave) TWvBuf( iBody );
       
   234                     } );
       
   235                 }
       
   236             if ( err )
       
   237                 {
       
   238                 ResetBuffers();
       
   239                 EventHandled();
       
   240                 }
       
   241             else
       
   242                 {
       
   243                 // Ask rest of the message
       
   244                 EventBody();
       
   245                 }
       
   246             }
       
   247         }
       
   248     else
       
   249         {
       
   250 #ifndef _NO_IMPS_LOGGING_
       
   251         CImpsClientLogger::Log(_L("CImpsHandler2: DESTROYED **** cli=%d"), (TInt)&iClient );
       
   252 #endif
       
   253         }
       
   254 
       
   255     iDestroyedPtr = NULL; 
       
   256 
       
   257 #ifndef _NO_IMPS_LOGGING_
       
   258     CImpsClientLogger::Log(_L("CImpsHandler2: RunL ends cli=%d"), (TInt)&iClient);
       
   259 #endif    
       
   260     }
       
   261 
       
   262 // ----------------------------------------------------------------------------
       
   263 // CImpsHandler2::ReadDataL()
       
   264 // ----------------------------------------------------------------------------
       
   265 TBool CImpsHandler2::ReadDataL( TInt aLength )
       
   266     {
       
   267     // Event data has been given in next event request
       
   268     SImpsEventData* event = (SImpsEventData*)iEventData.Ptr();
       
   269 
       
   270     if ( event->iMessageBody )
       
   271         {
       
   272         // verify iBody.Length and aLength
       
   273        __ASSERT_DEBUG( iBody && aLength > 0 && iBody->Des().Length() == aLength, 
       
   274                        User::Panic( KImpsPanicCategory, EImpsCorrupted ) );
       
   275 
       
   276         // Unpack the streaming
       
   277         iFields->Reset();
       
   278         TImpsPackedEntity packed( iBody );
       
   279         packed.UnpackEntityL( *iFields );
       
   280         }
       
   281     else if ( aLength > CurrentSize() )
       
   282         {
       
   283         // This means event without body
       
   284         return EFalse;
       
   285         }
       
   286 
       
   287     return ETrue;
       
   288     }
       
   289 
       
   290 // ----------------------------------------------------------------------------
       
   291 // CImpsHandler2::DoCancel()
       
   292 // ----------------------------------------------------------------------------
       
   293 void CImpsHandler2::DoCancel()
       
   294     {
       
   295 
       
   296     // Complete the request with error
       
   297     TRequestStatus* s = &iStatus;
       
   298     User::RequestComplete( s, KErrCancel );
       
   299     }
       
   300 
       
   301 // ----------------------------------------------------------------------------
       
   302 // CImpsHandler2:: SetStatusHandlerL
       
   303 // ----------------------------------------------------------------------------
       
   304 void CImpsHandler2::SetStatusHandlerL( MImpsStatusHandler2* aObs)
       
   305     {
       
   306 
       
   307     iStatusHandler = aObs;
       
   308     // register new handler
       
   309     if ( iStatusHandler )
       
   310         {
       
   311         DoRegisterStatusHandlerL(  );
       
   312         }
       
   313     else
       
   314         {
       
   315         DoUnregisterStatusHandlerL( );
       
   316         }
       
   317     }
       
   318 
       
   319 // ----------------------------------------------------------------------------
       
   320 // CImpsHandler2:: SetErrorHandlerL
       
   321 // ----------------------------------------------------------------------------
       
   322 void CImpsHandler2::SetErrorHandlerL( MImpsErrorHandler2* aObs)
       
   323     {
       
   324 
       
   325     iErrorHandler = aObs;
       
   326     // register new handler
       
   327     if ( iErrorHandler )
       
   328         {
       
   329         DoRegisterErrorHandlerL(  );
       
   330         }
       
   331     else
       
   332         {
       
   333         DoUnregisterErrorHandlerL( );
       
   334         }
       
   335     }
       
   336 
       
   337 // ----------------------------------------------------------------------------
       
   338 // CImpsHandler2::DoRegisterStatusHandlerL
       
   339 // ----------------------------------------------------------------------------
       
   340 void CImpsHandler2::DoRegisterStatusHandlerL(  )
       
   341     {
       
   342     TInt ret =  iClient.SendReceive( EImpsServStatusReg, TIpcArgs() );
       
   343     User::LeaveIfError( ret );
       
   344     }
       
   345 
       
   346 // ----------------------------------------------------------------------------
       
   347 // CImpsHandler2::DoUnregisterStatusHandlerL
       
   348 // ----------------------------------------------------------------------------
       
   349 void CImpsHandler2::DoUnregisterStatusHandlerL(  )
       
   350     {
       
   351     TInt ret =  iClient.SendReceive( EImpsServStatusUnreg, TIpcArgs() );
       
   352     User::LeaveIfError( ret );
       
   353     }
       
   354 
       
   355 // ----------------------------------------------------------------------------
       
   356 // CImpsHandler2::DoRegisterErrorHandlerL
       
   357 // ----------------------------------------------------------------------------
       
   358 void CImpsHandler2::DoRegisterErrorHandlerL(  )
       
   359     {
       
   360     TInt ret =  iClient.SendReceive( EImpsServDetailedReg, TIpcArgs() );
       
   361     User::LeaveIfError( ret );
       
   362     }
       
   363 
       
   364 // ----------------------------------------------------------------------------
       
   365 // CImpsHandler2::DoUnregisterErrorHandlerL
       
   366 // ----------------------------------------------------------------------------
       
   367 void CImpsHandler2::DoUnregisterErrorHandlerL(  )
       
   368     {
       
   369     TInt ret =  iClient.SendReceive( EImpsServDetailedUnreg, TIpcArgs() );
       
   370     User::LeaveIfError( ret );
       
   371     }
       
   372 
       
   373 // ----------------------------------------------------------------------------
       
   374 // CImpsHandler2::HandleErrorEventL
       
   375 // ----------------------------------------------------------------------------
       
   376 void CImpsHandler2::HandleErrorEventL(
       
   377             TInt aOpCode, TInt aStatus, CImpsFields* aFields)
       
   378     {
       
   379 
       
   380 #ifndef _NO_IMPS_LOGGING_
       
   381     CImpsClientLogger::Log(_L("CImpsHandler2: HandleErrorEventL opid=%d stat=%d cli=%d"),
       
   382                 aOpCode, aStatus, (TInt)&iClient );
       
   383 #endif
       
   384 
       
   385 
       
   386     if ( !iErrorHandler )
       
   387         {
       
   388         // handler should exist
       
   389         return;
       
   390         }
       
   391 
       
   392     if ( aFields == NULL )
       
   393         {
       
   394 #ifndef _NO_IMPS_LOGGING_
       
   395         CImpsClientLogger::Log(_L("CImpsHandler2: HandleError call"));
       
   396 #endif
       
   397         // This has been internal error
       
   398         iErrorHandler->HandleErrorL( 
       
   399             aStatus,
       
   400             aOpCode,
       
   401             NULL,
       
   402             NULL,
       
   403             *iClient.CspIdentifier() );
       
   404         return;
       
   405         }
       
   406 
       
   407 
       
   408     CImpsDetailed* myDet = new (ELeave) CImpsDetailed(4);
       
   409     CleanupStack::PushL( myDet );       // <<< myDet
       
   410     TPtrC descr;
       
   411     descr.Set( KNullDesC );
       
   412 
       
   413     CImpsKey* myKey = CImpsKey::NewLC();    // <<< myKey
       
   414     CImpsDataAccessor* myAc = CImpsDataAccessor::NewL( aFields );
       
   415     CleanupStack::PushL( myAc );            // <<< myAc
       
   416 
       
   417     TImpsCDataUtils::GetDetailedResultL( myKey, myAc, descr, myDet );
       
   418     CleanupStack::PopAndDestroy(2);     // >>> myKey, myAc
       
   419 
       
   420 #ifndef _NO_IMPS_LOGGING_
       
   421     CImpsClientLogger::Log(_L("CImpsHandler2: HandleError call"),
       
   422                 aOpCode, aStatus );
       
   423 #endif
       
   424 
       
   425     iErrorHandler->HandleErrorL( 
       
   426         aStatus,
       
   427         aOpCode,
       
   428         &descr,
       
   429         myDet,
       
   430         *iClient.CspIdentifier() );
       
   431 
       
   432     CleanupStack::PopAndDestroy(1);  // >>> myDet
       
   433     }
       
   434 
       
   435 // -----------------------------------------------------------------------------
       
   436 // CImpsHandler2::ConstructL
       
   437 // -----------------------------------------------------------------------------
       
   438 void CImpsHandler2::ConstructL()
       
   439     {
       
   440     iFields = CImpsFields::NewL();
       
   441     iBusyTimer = new (ELeave) CImpsHandlerTimer( CActive::EPriorityStandard, *this);
       
   442     }
       
   443 
       
   444 // ----------------------------------------------------------------------------
       
   445 // CImpsHandler2::EventHandled()
       
   446 // ----------------------------------------------------------------------------
       
   447 void CImpsHandler2::EventHandled()
       
   448     {
       
   449     TInt bufSize = CurrentSize();
       
   450 #ifndef _NO_IMPS_LOGGING_
       
   451     CImpsClientLogger::Log(_L("CImpsHandler2: EImpsServNextEvent bufSize=%d h=%d cli=%d"), 
       
   452         bufSize, (TInt)this, (TInt)&iClient );
       
   453 #endif
       
   454     iArgs.Set( 0, &iEventData );
       
   455     iArgs.Set( 1, &iPtrStore->iPtr ); 
       
   456     iArgs.Set( 2, bufSize );      
       
   457     iClient.SendReceive( EImpsServNextEvent, iArgs, iStatus );
       
   458     }
       
   459 
       
   460 // ----------------------------------------------------------------------------
       
   461 // CImpsHandler2::EventBody()
       
   462 // ----------------------------------------------------------------------------
       
   463 void CImpsHandler2::EventBody()
       
   464     {
       
   465     __ASSERT_DEBUG( iBody && iPtrStore, 
       
   466                     User::Panic( KImpsPanicCategory, EImpsCorrupted ) );
       
   467     TInt bufSize = CurrentSize(); 
       
   468 #ifndef _NO_IMPS_LOGGING_
       
   469     CImpsClientLogger::Log(_L("CImpsHandler2: EImpsServEventBody bufSize=%d h=%d cli=%d"), 
       
   470         bufSize, (TInt)this, (TInt)&iClient );
       
   471 #endif
       
   472     iArgs.Set( 0, &iEventData );
       
   473     iArgs.Set( 1, &iPtrStore->iPtr ); 
       
   474     iArgs.Set( 2, bufSize );
       
   475     iClient.SendReceive( EImpsServEventBody, iArgs, iStatus );
       
   476     }
       
   477 
       
   478 // ----------------------------------------------------------------------------
       
   479 // CImpsHandler2::ResetBuffers()
       
   480 // ----------------------------------------------------------------------------
       
   481 void CImpsHandler2::ResetBuffers()
       
   482     {
       
   483     delete iPtrStore;
       
   484     iPtrStore = NULL;
       
   485     delete iBody;
       
   486     iBody = NULL;
       
   487     }
       
   488 
       
   489 //  End of File