IMPSengine/engsrv/src/impssendreceive.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: Class for Data send and recevive.
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <apparc.h>
       
    22 #include    <bautils.h>
       
    23 #include    "impssendreceive.h"
       
    24 #include    "Impsutils.h"
       
    25 #include    "impserrors.h"
       
    26 #include    "impstimer.h"
       
    27 #include    "impsactiveconnmonitor.h"
       
    28 #include    "Impsdatautils.h"
       
    29 
       
    30 // MACROS
       
    31 #ifndef _DEBUG
       
    32 #define _NO_IMPS_LOGGING_
       
    33 #endif
       
    34 
       
    35 #ifdef LOCAL_IMPS
       
    36 #ifndef _FAKE_RESPONSE
       
    37 #define _FAKE_RESPONSE
       
    38 #endif
       
    39 #endif
       
    40 
       
    41 // ================= MEMBER FUNCTIONS =======================
       
    42 
       
    43 // C++ default constructor can NOT contain any code, that
       
    44 // might leave.
       
    45 //
       
    46 CImpsSendReceive2::CImpsSendReceive2(
       
    47     RFs& aFs,
       
    48     MImpsCSPSession& aServer,
       
    49     CBufFlat& aOutputBuffer )
       
    50         : iServer( aServer ),
       
    51         iOutputBuffer( aOutputBuffer ),
       
    52         iOpenId( 0 ),
       
    53         iFs( aFs ),
       
    54         iSender( NULL ),
       
    55         iRequList( _FOFF( CTrReq, iLink ) ),  //lint !e413
       
    56         iConnMan( NULL ),
       
    57         iSAP ( NULL ),
       
    58         iTrState( EImpsTrInit )
       
    59     {
       
    60 
       
    61     }
       
    62 
       
    63 // default constructor can leave.
       
    64 void CImpsSendReceive2::ConstructL( TDesC8& aMimeType )
       
    65     {
       
    66 #ifndef _NO_IMPS_LOGGING_
       
    67     CImpsClientLogger::Log( _L( "SendReceive2: rel200541.3+" ) );
       
    68 #endif
       
    69     iMimeType = aMimeType;
       
    70     iConnMan = CImpsConnManager::NewL( *this, CActive::EPriorityUserInput + 1 );
       
    71     MMsgConnManager& man = iConnMan->ManagerHandleL();
       
    72     iSender = NewImpsSenderL( *this, man, aMimeType );
       
    73     }
       
    74 
       
    75 // Two-phased constructor.
       
    76 CImpsSendReceive2* CImpsSendReceive2::NewL(
       
    77     RFs&  aFs,
       
    78     MImpsCSPSession& aServer,
       
    79     CBufFlat& aOutputBuffer,
       
    80     TDesC8& aMimeType )
       
    81     {
       
    82     CImpsSendReceive2* self = new ( ELeave ) CImpsSendReceive2(
       
    83         aFs,
       
    84         aServer,
       
    85         aOutputBuffer );
       
    86 
       
    87     CleanupStack::PushL( self );
       
    88     self->ConstructL( aMimeType );
       
    89     CleanupStack::Pop();
       
    90 
       
    91     return self;
       
    92     }
       
    93 
       
    94 
       
    95 // Destructor
       
    96 CImpsSendReceive2::~CImpsSendReceive2()
       
    97     {
       
    98 #ifndef _NO_IMPS_LOGGING_
       
    99     CImpsClientLogger::Log( _L( "SendReceive2: destructor begins" ) );
       
   100 #endif
       
   101     delete iConnMan;
       
   102     delete iSAP;
       
   103 
       
   104     if ( iSender )
       
   105         {
       
   106         // In error case this may be null
       
   107         iSender->Destroy();
       
   108         }
       
   109     DeleteAllIds();
       
   110     // delete iObserver;
       
   111 
       
   112 #ifndef _NO_IMPS_LOGGING_
       
   113     CImpsClientLogger::Log( _L( "SendReceive2: destructor ends" ) );
       
   114 #endif
       
   115 
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------
       
   119 // CImpsSendReceive2::CloseTr
       
   120 // ---------------------------------------------------------
       
   121 //
       
   122 void CImpsSendReceive2::CloseTr( )
       
   123     {
       
   124 #ifndef _NO_IMPS_LOGGING_
       
   125     CImpsClientLogger::Log( _L( "SendReceive2: CloseTr" ) );
       
   126 #endif
       
   127 
       
   128     DeleteAllIds();
       
   129 
       
   130     if ( iTrState == EImpsTrClosing )
       
   131         {
       
   132 #ifndef _NO_IMPS_LOGGING_
       
   133         CImpsClientLogger::Log( _L( "SendReceive2: CloseTr IGNORED ***" ) );
       
   134 #endif
       
   135         return;
       
   136         }
       
   137     // cancel open AP
       
   138     if ( iTrState == EImpsTrAPOpening )
       
   139         {
       
   140 #ifndef _NO_IMPS_LOGGING_
       
   141         CImpsClientLogger::Log( _L( "SendReceive2: CANCEL open ***" ) );
       
   142 #endif
       
   143         NewTrState( EImpsTrClosing );
       
   144         iConnMan->CloseAP( ETrue );
       
   145         return;
       
   146         }
       
   147 
       
   148     if ( iSender )
       
   149         {
       
   150         // In error cases iSender may be null
       
   151 #ifndef _NO_IMPS_LOGGING_
       
   152         CImpsClientLogger::Log( _L( "SendReceive2: CloseTr CLOSE DATA CHANNEL" ) );
       
   153 #endif
       
   154         iSender->Close( );
       
   155         iSender->Destroy();
       
   156         iSender = NULL;
       
   157         NewTrState( EImpsTrAPOpen );
       
   158         }
       
   159     else if ( iTrState == EImpsTrCloseDelayed )
       
   160         {
       
   161         NewTrState( EImpsTrAPOpen );
       
   162         }
       
   163 
       
   164     if ( iTrState == EImpsTrAPOpen )
       
   165         {
       
   166 #ifndef _NO_IMPS_LOGGING_
       
   167         CImpsClientLogger::Log( _L( "SendReceive2: CloseTr START TO CLOSE AP" ) );
       
   168 #endif
       
   169         // This causes APStatusEvent call finally.
       
   170         NewTrState( EImpsTrClosing );
       
   171         iConnMan->CloseAP( ETrue );
       
   172         }
       
   173 
       
   174     // This is needed in CImpsServer so that transient server can be closed
       
   175     else if ( iTrState == EImpsTrInit )
       
   176         {
       
   177         iServer.TransportStatus( EInternal_NO_IAP );
       
   178         }
       
   179     }
       
   180 
       
   181 // ---------------------------------------------------------
       
   182 // CImpsSendReceive2::Close2
       
   183 // Delayed close operation
       
   184 // ---------------------------------------------------------
       
   185 //
       
   186 TInt CImpsSendReceive2::Close2( )
       
   187     {
       
   188 
       
   189 #ifndef _NO_IMPS_LOGGING_
       
   190     CImpsClientLogger::Log( _L( "SendReceive2: Close2" ) );
       
   191 #endif
       
   192 
       
   193     __ASSERT_DEBUG( iConnMan,
       
   194                     User::Panic( KImpsPanicCategory, EImpsCorrupted ) );
       
   195 
       
   196     TInt ret = KErrNone;
       
   197 
       
   198     if ( iTrState == EImpsTrAPOpening )
       
   199         {
       
   200         CloseTr();
       
   201         return ret;
       
   202         }
       
   203     else if ( iTrState == EImpsTrClosing || iTrState == EImpsTrCloseDelayed )
       
   204         {
       
   205         // There is pending close request that will call callback sooner or later.
       
   206 #ifndef _NO_IMPS_LOGGING_
       
   207         CImpsClientLogger::Log( _L( "SendReceive2: Close2 ignored ***" ) );
       
   208 #endif
       
   209         return ret;
       
   210         }
       
   211     else if ( iTrState == EImpsTrInit )
       
   212         {
       
   213         // Nothing to do, callback will not be called.
       
   214         // This error must be handled in calling method.
       
   215 #ifndef _NO_IMPS_LOGGING_
       
   216         CImpsClientLogger::Log( _L( "SendReceive2: iTrState was EImpsTrInit ***" ) );
       
   217 #endif
       
   218         return KErrCancel;
       
   219         }
       
   220 
       
   221     DeleteAllIds();
       
   222 
       
   223     if ( iSender )
       
   224         {
       
   225         // In error cases iSender may be null
       
   226 #ifndef _NO_IMPS_LOGGING_
       
   227         CImpsClientLogger::Log( _L( "SendReceive2: Close2 CLOSE DATA CHANNEL" ) );
       
   228 #endif
       
   229         iSender->Close( );
       
   230         iSender->Destroy();
       
   231         iSender = NULL;
       
   232         NewTrState( EImpsTrAPOpen );
       
   233         }
       
   234 
       
   235     // yield control for scheduler before close takes place
       
   236     iConnMan->StartDelayed( );
       
   237     NewTrState( EImpsTrCloseDelayed );
       
   238 
       
   239     return ret;
       
   240 
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------
       
   244 // CImpsSendReceive2::IsConnected
       
   245 // ---------------------------------------------------------
       
   246 //
       
   247 TBool CImpsSendReceive2::isConnected( )
       
   248     {
       
   249     TBool ret( EFalse );
       
   250     TRAPD( errx, ( ret = DoIsConnectedL() ) );
       
   251     if ( errx || !ret )
       
   252         {
       
   253         return EFalse;
       
   254         }
       
   255 
       
   256     if ( iSender && iTrState == EImpsTrDataOpen )
       
   257         {
       
   258         return ETrue;
       
   259         }
       
   260 
       
   261     return EFalse;
       
   262     }
       
   263 
       
   264 // ---------------------------------------------------------
       
   265 // CImpsSendReceive2::DoIsConnectedL
       
   266 // ---------------------------------------------------------
       
   267 //
       
   268 TBool CImpsSendReceive2::DoIsConnectedL( )
       
   269     {
       
   270     MMsgConnManager& man = iConnMan->ManagerHandleL();
       
   271     if ( !man.Status() )
       
   272         {
       
   273         return EFalse;
       
   274         }
       
   275     return ETrue;
       
   276     }
       
   277 
       
   278 // ---------------------------------------------------------
       
   279 // CImpsSendReceive2::OpenL
       
   280 // ---------------------------------------------------------
       
   281 //
       
   282 void CImpsSendReceive2::OpenL(
       
   283     const TDesC& aTID,
       
   284     TInt aXmlLog,
       
   285     const TDesC& aSAP,
       
   286     TInt  aIAP,
       
   287     TBool aDelayed )
       
   288     {
       
   289 #ifndef _NO_IMPS_LOGGING_
       
   290     CImpsClientLogger::Log( _L( "SendReceive2: OpenL Begins iTrState=%d" ), iTrState );
       
   291 #endif
       
   292 
       
   293     delete iSAP;
       
   294     iSAP = NULL;
       
   295     iSAP = aSAP.AllocL();
       
   296 
       
   297     iIAP = aIAP;
       
   298     DeleteId( iOpenId );
       
   299     iOpenId = 0;
       
   300     iOpenId = AddIdL( aTID ); // saved so that it can be deleted later
       
   301     iXmlLog = aXmlLog;
       
   302 
       
   303     if ( iTrState == EImpsTrClosing || iTrState == EImpsTrCloseDelayed ||
       
   304          iTrState == EImpsTrClosingPDP )
       
   305         {
       
   306         // just parameters saved
       
   307 #ifndef _NO_IMPS_LOGGING_
       
   308         CImpsClientLogger::Log( _L( "SendReceive2: Leaves(KErrNotReady) ***" ) );
       
   309 #endif
       
   310         DeleteId( iOpenId );
       
   311         iOpenId = 0;
       
   312         User::Leave( KErrNotReady );
       
   313         }
       
   314 
       
   315     OpenL( aTID, aDelayed );
       
   316 
       
   317 #ifndef _NO_IMPS_LOGGING_
       
   318     CImpsClientLogger::Log( _L( "SendReceive2: OpenL ends" ) );
       
   319 #endif
       
   320 
       
   321     }
       
   322 
       
   323 // ---------------------------------------------------------
       
   324 // CImpsSendReceive2::OpenL
       
   325 // ---------------------------------------------------------
       
   326 //
       
   327 void CImpsSendReceive2::OpenL( const TDesC& aTID, TBool aDelayed )
       
   328     {
       
   329     // Prevent opening twice data channel or connection
       
   330     if ( iTrState == EImpsTrAPOpening )
       
   331         {
       
   332 #ifndef _NO_IMPS_LOGGING_
       
   333         CImpsClientLogger::Log( _L( "SendReceive2: Leaves(KErrNotReady) ****" ) );
       
   334 #endif
       
   335         User::Leave( KErrNotReady );
       
   336         }
       
   337 
       
   338     // Handle NULL TID so that Login primitive is not sent later,
       
   339     // only channels are opened when recovering from flight mode.
       
   340     // The old state is ignored, because of
       
   341     // this should be called after NO_IAP only.
       
   342 
       
   343     if ( aTID.Length() == 0 )
       
   344         {
       
   345         iOpenId = 0;
       
   346         }
       
   347 
       
   348     if ( iSender && iTrState == EImpsTrDataOpen )
       
   349         {
       
   350         iSender->CancelAll();
       
   351         iSender->Destroy();
       
   352         iSender = NULL;
       
   353         NewTrState( EImpsTrAPOpen );
       
   354         }
       
   355     if ( iTrState == EImpsTrAPOpen )
       
   356         {
       
   357         NewTrState( EImpsTrClosing );
       
   358         iConnMan->CloseAP( ETrue );
       
   359         }
       
   360     else if ( iTrState == EImpsTrInit )
       
   361         {
       
   362         Open2L( iIAP, aDelayed );
       
   363         }
       
   364     }
       
   365 
       
   366 // ---------------------------------------------------------
       
   367 // CImpsSendReceive2::Open2L
       
   368 // ---------------------------------------------------------
       
   369 //
       
   370 void CImpsSendReceive2::Open2L( TInt aIAP, TBool aDelayed )
       
   371     {
       
   372 #ifndef _NO_IMPS_LOGGING_
       
   373     CImpsClientLogger::Log( _L( "SendReceive2: Open2L Begins aIAP=%d" ), aIAP );
       
   374 #endif
       
   375 
       
   376     __ASSERT_DEBUG( iConnMan,
       
   377                     User::Panic( KImpsPanicCategory, EImpsCorrupted ) );
       
   378 
       
   379     iConnMan->OpenAPL( aIAP, aDelayed );
       
   380     NewTrState( EImpsTrAPOpening );
       
   381     }
       
   382 
       
   383 
       
   384 // ---------------------------------------------------------
       
   385 // CImpsSendReceive2::ClosePDP
       
   386 // ---------------------------------------------------------
       
   387 //
       
   388 void CImpsSendReceive2::ClosePDP()
       
   389     {
       
   390 #ifndef _NO_IMPS_LOGGING_
       
   391     CImpsClientLogger::Log( _L( "SendReceive2: ClosePDP iTrState=%d" ), iTrState );
       
   392 #endif
       
   393 
       
   394     if ( iTrState == EImpsTrInit )
       
   395         {
       
   396         iServer.TransportStatus( EInternal_NO_IAP );
       
   397         }
       
   398 
       
   399     if ( iTrState != EImpsTrDataOpen )
       
   400         {
       
   401 #ifndef _NO_IMPS_LOGGING_
       
   402         CImpsClientLogger::Log( _L( "SendReceive2: ClosePDP error ***" ) );
       
   403 #endif
       
   404         return;
       
   405         }
       
   406 
       
   407     DeleteAllIds();
       
   408 
       
   409     if ( iSender )
       
   410         {
       
   411         // In error cases iSender may be null
       
   412 #ifndef _NO_IMPS_LOGGING_
       
   413         CImpsClientLogger::Log( _L( "SendReceive2: ClosePDP CLOSE DATA CHANNEL" ) );
       
   414 #endif
       
   415         iSender->Close( );
       
   416         iSender->Destroy();
       
   417         iSender = NULL;
       
   418         }
       
   419 
       
   420 #ifndef _NO_IMPS_LOGGING_
       
   421     CImpsClientLogger::Log( _L( "SendReceive2: ClosePDP START TO CLOSE AP" ) );
       
   422 #endif
       
   423     // This causes APStatusEvent call finally.
       
   424     NewTrState( EImpsTrClosingPDP );
       
   425     iConnMan->CloseAP( EFalse );
       
   426     }
       
   427 
       
   428 // ---------------------------------------------------------
       
   429 //CImpsSendReceive2::OpenPDPL
       
   430 // ---------------------------------------------------------
       
   431 //
       
   432 void CImpsSendReceive2::OpenPDPL(
       
   433     const TDesC& aSAP,
       
   434     TInt  aIAP )
       
   435     {
       
   436 #ifndef _NO_IMPS_LOGGING_
       
   437     CImpsClientLogger::Log( _L( "SendReceive2: OpenPDPL iTrState=%d" ), iTrState );
       
   438 #endif
       
   439 
       
   440     __ASSERT_DEBUG( iConnMan,
       
   441                     User::Panic( KImpsPanicCategory, EImpsCorrupted ) );
       
   442 
       
   443     // Prevent opening twice data channel or connection
       
   444     if ( iTrState == EImpsTrAPOpening )
       
   445         {
       
   446 #ifndef _NO_IMPS_LOGGING_
       
   447         CImpsClientLogger::Log( _L( "SendReceive2: OpenPDPL Leaves(corrupted) ***" ) );
       
   448 #endif
       
   449         User::Leave( EImpsCorrupted );
       
   450         }
       
   451 
       
   452     delete iSAP;
       
   453     iSAP = NULL;
       
   454     iSAP = aSAP.AllocL();
       
   455 
       
   456     iIAP = aIAP;
       
   457 
       
   458     if ( iTrState == EImpsTrClosedPDP || iTrState == EImpsTrInit )
       
   459         {
       
   460         iConnMan->OpenAPL( aIAP, EFalse );
       
   461         NewTrState( EImpsTrOpeningPDP );
       
   462         }
       
   463     else
       
   464         {
       
   465 #ifndef _NO_IMPS_LOGGING_
       
   466         CImpsClientLogger::Log( _L( "SendReceive2: OpenPDPL Leaves(corrupted) ****" ) );
       
   467 #endif
       
   468         User::Leave( EImpsCorrupted );
       
   469         }
       
   470     }
       
   471 
       
   472 // ---------------------------------------------------------
       
   473 // CImpsSendReceive2::Cancel
       
   474 // ---------------------------------------------------------
       
   475 //
       
   476 void CImpsSendReceive2::Cancel()
       
   477     {
       
   478     if ( iSender )
       
   479         {
       
   480         iSender->CancelAll();
       
   481         }
       
   482     }
       
   483 
       
   484 // ---------------------------------------------------------
       
   485 // CImpsSendReceive2::SendAndWaitL
       
   486 // ---------------------------------------------------------
       
   487 //
       
   488 void CImpsSendReceive2::SendAndWaitL(
       
   489     const TDesC& aTID,
       
   490     TInt aExpiryTime )
       
   491     {
       
   492 #ifndef _NO_IMPS_LOGGING_
       
   493     CImpsClientLogger::Log( _L( "SendReceive2: SendAndWait begins %d" ), iTrState );
       
   494 #endif
       
   495 
       
   496     if ( !iSender || iTrState != EImpsTrDataOpen )
       
   497         {
       
   498 #ifndef _NO_IMPS_LOGGING_
       
   499         CImpsClientLogger::Log( _L( "SendReceive2: SendAndWaitL Leaves(KErrNotReady) ****" ) );
       
   500 #endif
       
   501         User::Leave( KErrNotReady );
       
   502         }
       
   503 
       
   504     // XML log ***************************************************************
       
   505 #ifdef _DEBUG
       
   506     if ( iXmlLog&0x1 )
       
   507         {
       
   508         if ( iXmlLog&0x2 || aTID.Length() > 0 ) // This works with WBXML too
       
   509             {
       
   510             TFileName fileName = _L( "c:\\logs\\impss\\" );
       
   511             if ( BaflUtils::PathExists( iFs, fileName ) )
       
   512                 {
       
   513                 _LIT( KRelated, "Sent.xml" );
       
   514                 iParse.Set( fileName, &KRelated, NULL );
       
   515                 fileName = iParse.FullName();
       
   516                 TRAPD( errxy, CApaApplication::GenerateFileName( iFs, fileName ) );
       
   517                 if ( errxy )
       
   518                     {
       
   519 #ifndef _NO_IMPS_LOGGING_
       
   520                     CImpsClientLogger::Log( _L( "SendReceive2: SendAndWaitL Leaves(%d) ****" ), errxy );
       
   521 #endif
       
   522                     User::Leave( errxy );
       
   523                     }
       
   524 
       
   525                 RFile file;
       
   526                 TInt xmlError = file.Create( iFs, fileName, EFileWrite | EFileShareExclusive );
       
   527                 if ( xmlError == KErrNone )
       
   528                     {
       
   529                     // the data is supposed to be in the encode buffer
       
   530                     TPtr8 ptr = iOutputBuffer.Ptr( 0 );
       
   531                     file.Write( ptr );
       
   532                     file.Flush();
       
   533                     }
       
   534 
       
   535                 // done - close files
       
   536                 file.Close();
       
   537 
       
   538 #ifndef _NO_IMPS_LOGGING_
       
   539                 CImpsClientLogger::Log( _L( "SendReceive2: LogFile = %S" ), &fileName );
       
   540 #endif
       
   541                 }
       
   542             }
       
   543         }
       
   544 #endif // _DEBUG        
       
   545     // XML log ends **********************************************************
       
   546 
       
   547     TInt myId = AddIdL( aTID );
       
   548 
       
   549 #ifdef _FAKE_RESPONSE
       
   550     // local transport does not support more than one request at a time
       
   551     if ( iSender->PendingRequests() >= KImpsMaxPending )
       
   552         {
       
   553 #ifndef _NO_IMPS_LOGGING_
       
   554         CImpsClientLogger::Log( _L( "SendReceive2: SendAndWaitL Leaves(KErrInUse) ****" ) );
       
   555 #endif
       
   556         User::Leave( KErrInUse );
       
   557         }
       
   558 #endif
       
   559 
       
   560 
       
   561 #ifndef _NO_IMPS_LOGGING_
       
   562     CImpsClientLogger::Log( _L( "SendReceive2: Call transport SendL" ) );
       
   563 #endif
       
   564 
       
   565     TRAPD( er, iSender->SendL( myId, iOutputBuffer.Ptr( 0 ), aExpiryTime ) );
       
   566     if ( er )
       
   567         {
       
   568         // If send fails immediatly the callback is not called and thus
       
   569         // queue must be cleaned now.
       
   570         DeleteId( myId );
       
   571 #ifndef _NO_IMPS_LOGGING_
       
   572         CImpsClientLogger::Log( _L( "SendReceive2: SendAndWaitL Leaves(%d) ****" ), er );
       
   573 #endif
       
   574         User::Leave( er );
       
   575         }
       
   576 
       
   577 #ifndef _NO_IMPS_LOGGING_
       
   578     CImpsClientLogger::Log( _L( "SendReceive2: SendAndWait ends" ) );  //lint !e539
       
   579 #endif
       
   580 
       
   581     }
       
   582 
       
   583 // ---------------------------------------------------------
       
   584 // CImpsSendReceive2::CancelTrans
       
   585 // ---------------------------------------------------------
       
   586 //
       
   587 void CImpsSendReceive2::CancelTrans( const TDesC& aTID )
       
   588     {
       
   589 #ifndef _NO_IMPS_LOGGING_
       
   590     CImpsClientLogger::Log( _L( "SendReceive2: CancelTrans" ) );  //lint !e539
       
   591 #endif
       
   592     TInt opid = TidToOpid( aTID );
       
   593     if ( opid != KImpsNullId )
       
   594         {
       
   595         if ( iSender )
       
   596             {
       
   597             iSender->CancelTransaction( opid );
       
   598             }
       
   599         DeleteId( opid );
       
   600         }
       
   601     }
       
   602 
       
   603 // ---------------------------------------------------------
       
   604 // CImpsSendReceive2::CancelLoginTrans
       
   605 // ---------------------------------------------------------
       
   606 //
       
   607 TBool CImpsSendReceive2::CancelLoginTrans( const TDesC& aTID )
       
   608     {
       
   609 #ifndef _NO_IMPS_LOGGING_
       
   610     CImpsClientLogger::Log( _L( "SendReceive2: CancelLoginTransL TID=%S" ), &aTID );  //lint !e539
       
   611 #endif
       
   612     TBool ret( KErrNone );
       
   613     if ( iTrState == EImpsTrAPOpening || iTrState == EImpsTrAPOpen )
       
   614         {
       
   615         CloseTr();
       
   616         }
       
   617     else if ( iTrState == EImpsTrDataOpen )
       
   618         {
       
   619         TInt opid = TidToOpid( aTID );
       
   620         if ( opid != KImpsNullId )
       
   621             {
       
   622             if ( iSender )
       
   623                 {
       
   624                 iSender->CancelTransaction( opid );
       
   625                 }
       
   626             DeleteId( opid );
       
   627             ret = ETrue;
       
   628             }
       
   629         }
       
   630     return ret;
       
   631     }
       
   632 
       
   633 // ---------------------------------------------------------
       
   634 // CImpsSendReceive2::AddIdL
       
   635 // ---------------------------------------------------------
       
   636 //
       
   637 TInt CImpsSendReceive2::AddIdL( const TDesC& aTid )
       
   638     {
       
   639     GenerateId();
       
   640 
       
   641     CTrReq* requ = new ( ELeave ) CTrReq(
       
   642         aTid,
       
   643         iID );
       
   644 
       
   645     iRequList.AddLast( *requ );
       
   646 
       
   647     return iID;
       
   648     }
       
   649 
       
   650 // ---------------------------------------------------------
       
   651 // CImpsSendReceive2::OpidToTid
       
   652 // ---------------------------------------------------------
       
   653 //
       
   654 TPtrC CImpsSendReceive2::OpidToTid( TInt aId )
       
   655     {
       
   656     TDblQueIter<CTrReq> rIter( iRequList );
       
   657 
       
   658     rIter.SetToFirst();
       
   659 
       
   660     while ( rIter )
       
   661         {
       
   662         CTrReq* requ = rIter;
       
   663         if ( requ->iOpId == aId )
       
   664             {
       
   665             return requ->iTID;
       
   666             }
       
   667         rIter++;
       
   668         }
       
   669     return TPtrC();
       
   670     }
       
   671 
       
   672 // ---------------------------------------------------------
       
   673 // CImpsSendReceive2::TidToOpid
       
   674 // ---------------------------------------------------------
       
   675 //
       
   676 TInt CImpsSendReceive2::TidToOpid( const TDesC& aId )
       
   677     {
       
   678     TDblQueIter<CTrReq> rIter( iRequList );
       
   679 
       
   680     rIter.SetToFirst();
       
   681 
       
   682     while ( rIter )
       
   683         {
       
   684         CTrReq* requ = rIter;
       
   685         if ( !requ->iTID.Compare( aId ) )
       
   686             {
       
   687             return requ->iOpId;
       
   688             }
       
   689         rIter++;
       
   690         }
       
   691     return KImpsNullId;
       
   692     }
       
   693 
       
   694 // ---------------------------------------------------------
       
   695 // CImpsSendReceive2::DeleteId
       
   696 // ---------------------------------------------------------
       
   697 //
       
   698 TBool CImpsSendReceive2::DeleteId( TInt aId )
       
   699     {
       
   700 
       
   701     TDblQueIter<CTrReq> rIter( iRequList );
       
   702 
       
   703     rIter.SetToFirst();
       
   704 
       
   705     while ( rIter )
       
   706         {
       
   707         CTrReq* requ = rIter;
       
   708         rIter++;
       
   709         if ( requ->iOpId == aId )
       
   710             {
       
   711             requ->Destroy();
       
   712             return ETrue;
       
   713             }
       
   714         }
       
   715     return EFalse;
       
   716     }
       
   717 
       
   718 // ---------------------------------------------------------
       
   719 // CImpsSendReceive2::DeleteAllIds
       
   720 // ---------------------------------------------------------
       
   721 //
       
   722 void CImpsSendReceive2::DeleteAllIds()
       
   723     {
       
   724     // Delete all buffered requests from this client,
       
   725     TDblQueIter<CTrReq> rIter( iRequList );
       
   726 
       
   727     rIter.SetToFirst();
       
   728 
       
   729     while ( rIter )
       
   730         {
       
   731         CTrReq* requ = rIter;
       
   732         rIter++;
       
   733         requ->Destroy();
       
   734         }
       
   735     }
       
   736 
       
   737 // ---------------------------------------------------------
       
   738 // CImpsSendReceive2::TransportResponse
       
   739 // ---------------------------------------------------------
       
   740 //
       
   741 void CImpsSendReceive2::TransportResponse(
       
   742     TInt aID,
       
   743     TInt aError,
       
   744     TInt aHttpStatus,
       
   745     HBufC8* aCspMessage )
       
   746     {
       
   747     TInt myErr = KErrNone;
       
   748     HBufC8** dataPtr = &aCspMessage;
       
   749     TRAP( myErr, DoTransportResponseL( aID, aError, aHttpStatus, dataPtr ) );
       
   750     if ( myErr )
       
   751         {
       
   752 #ifndef _NO_IMPS_LOGGING_
       
   753         CImpsClientLogger::Log( _L( "SendReceive2: DoTransportResponseL leaves = %d" ), myErr ); //lint !e725
       
   754 #endif
       
   755         DeleteId( aID );
       
   756         }
       
   757     // This ensures the data buffer deletion in error cases
       
   758     delete *dataPtr;
       
   759 
       
   760 #ifndef _NO_IMPS_LOGGING_
       
   761     CImpsClientLogger::Log( _L( "SendReceive2: TransportResponse ends" ), myErr );
       
   762 #endif
       
   763 
       
   764     }
       
   765 
       
   766 // ---------------------------------------------------------
       
   767 // CImpsSendReceive2::DoTransportResponseL
       
   768 // ---------------------------------------------------------
       
   769 //
       
   770 void CImpsSendReceive2::DoTransportResponseL(
       
   771     TInt aID,
       
   772     TInt aError,
       
   773     TInt aHttpStatus,
       
   774     HBufC8** aCspPtr )
       
   775     {
       
   776 
       
   777 #ifndef _NO_IMPS_LOGGING_
       
   778     CImpsClientLogger::Log( _L( "SendReceive2: DoTransportResponse start err=%d http=%d" ), aError, aHttpStatus );
       
   779 #endif
       
   780 
       
   781     HBufC8* cspMsg = *aCspPtr;
       
   782 
       
   783     TPtrC tid = OpidToTid( aID );
       
   784     TInt origState = iTrState;
       
   785     // error handling
       
   786     if ( aError || ( aHttpStatus != 200 && aHttpStatus != 0 ) )
       
   787         {
       
   788         if ( aHttpStatus != 200 && aHttpStatus != 0 )
       
   789             {
       
   790             aError = KImpsErrorResponseStatus;
       
   791             }
       
   792         iServer.TransportErrorL( tid, aError );
       
   793         DeleteId( aID );
       
   794         return;
       
   795         }
       
   796     if ( cspMsg && cspMsg->Size() > iServer.MaxParserSize() )
       
   797         {
       
   798         iServer.TransportErrorL( tid, KImpsErrorMessageTooBig );
       
   799         DeleteId( aID );
       
   800         return;
       
   801         }
       
   802 
       
   803     if ( origState == EImpsTrDataOpen )
       
   804         {
       
   805 
       
   806         // XML log ***************************************************************
       
   807 #ifdef _DEBUG
       
   808         // XML log
       
   809         if ( iXmlLog&1 && cspMsg )
       
   810             {
       
   811             TPtr8 ptr = cspMsg->Des();
       
   812             if ( !( !( iXmlLog&2 ) && !ptr.Length( ) ) )
       
   813                 {
       
   814                 TFileName fileName = _L( "c:\\logs\\impss\\" );
       
   815                 if ( BaflUtils::PathExists( iFs, fileName ) )
       
   816                     {
       
   817                     _LIT( KRelated, "Receive.xml" );
       
   818                     iParse.Set( fileName, &KRelated, NULL );
       
   819                     fileName = iParse.FullName();
       
   820                     User::LeaveIfError( CApaApplication::GenerateFileName( iFs, fileName ) );
       
   821 
       
   822                     RFile file;
       
   823                     TInt xmlError = file.Create( iFs, fileName, EFileWrite | EFileShareExclusive );
       
   824                     if ( xmlError == KErrNone )
       
   825                         {
       
   826                         TPtr8 ptr2 = cspMsg->Des();
       
   827                         file.Write( ptr2 );
       
   828                         file.Flush();
       
   829                         }
       
   830 
       
   831                     // done - close files
       
   832                     file.Close();
       
   833 
       
   834 #ifndef _NO_IMPS_LOGGING_
       
   835                     CImpsClientLogger::Log( _L( "SendReceive2: LogFile = %S" ), &fileName );
       
   836 #endif
       
   837                     }
       
   838                 }
       
   839             }// if ( iXmlLog&1 && cspMsg )
       
   840 #endif // _DEBUG
       
   841         // XML log ends **********************************************************
       
   842 
       
   843         // we catch error and continue ...
       
   844         TRAPD( erxx, iServer.TransportResponseL( aCspPtr ) );
       
   845         if ( erxx )
       
   846             {
       
   847 #ifndef _NO_IMPS_LOGGING_
       
   848             CImpsClientLogger::Log( _L( "SendReceive2:TransportResponseL ERROR= %d" ), erxx );
       
   849 #endif
       
   850             }
       
   851 
       
   852         // This makes csp session ready for next response xxx
       
   853         iServer.GetNextMessageL();
       
   854         }
       
   855 
       
   856     DeleteId( aID );
       
   857 
       
   858     return;
       
   859     }
       
   860 
       
   861 // ---------------------------------------------------------
       
   862 // CImpsSendReceive2::APStatusEvent
       
   863 // This methods handles both completion of Connection Manager
       
   864 // operations and connection status events.
       
   865 // ---------------------------------------------------------
       
   866 //
       
   867 void CImpsSendReceive2::APStatusEvent(
       
   868     TImpsConnectionState aConnectionState,
       
   869     TInt aStatus,
       
   870     TBool aOperation,
       
   871     TBool aAuthClose )
       
   872     {
       
   873 #ifndef _NO_IMPS_LOGGING_
       
   874     CImpsClientLogger::Log( _L( "SendReceive2: APStatusEvent %d %d op=%d trstate=%d authClose=%d" ),
       
   875                             ( TInt )aConnectionState, aStatus, aOperation, iTrState, aAuthClose );
       
   876 #endif
       
   877 
       
   878     TInt errx = KErrNone;
       
   879     if ( aOperation )
       
   880         {
       
   881         TRAP( errx, DoHandleRespL( aConnectionState, aStatus ) );
       
   882         }
       
   883     else    // !oper
       
   884         {
       
   885         TRAP( errx, DoHandleTrEventL( aConnectionState, aAuthClose ) );
       
   886         }
       
   887     }
       
   888 
       
   889 // ---------------------------------------------------------
       
   890 // CImpsSendReceive2::DoHandleRespL
       
   891 // ---------------------------------------------------------
       
   892 //
       
   893 void CImpsSendReceive2::DoHandleRespL(
       
   894     TImpsConnectionState aConnectionState,
       
   895     TInt aStatus )
       
   896     {
       
   897 #ifndef _NO_IMPS_LOGGING_
       
   898     CImpsClientLogger::Log( _L( "SendReceive2: DoHandleRespL %d %d trstate=%d" ),
       
   899                             ( TInt )aConnectionState, aStatus, iTrState );
       
   900 #endif
       
   901     TInt err = aStatus;
       
   902     TInt errx = KErrNone;
       
   903     TImpsTrState origTr = iTrState;
       
   904 
       
   905     // Regular cases handled here
       
   906     if ( !err )
       
   907         {
       
   908         switch ( origTr )
       
   909             {
       
   910             case EImpsTrAPOpening:
       
   911                 NewTrState( EImpsTrAPOpen );
       
   912                 // create iSender if needed
       
   913                 errx = DoOpenDataCh();
       
   914                 if ( errx )
       
   915                     {
       
   916                     TPtrC tid = OpidToTid( iOpenId );
       
   917                     iServer.TransportErrorL( tid, errx );
       
   918                     }
       
   919                 else
       
   920                     {
       
   921                     NewTrState( EImpsTrDataOpen );
       
   922                     // This was response for open request!
       
   923                     // If iOpenId is zero then this was a special case just
       
   924                     // to open channels
       
   925                     if ( iOpenId )
       
   926                         {
       
   927                         // Now we can send the message
       
   928                         // iID is set in Open
       
   929                         TPtrC tid = OpidToTid( iOpenId );
       
   930                         TRAPD( errxy, SendAndWaitL( tid ) );
       
   931                         if ( errxy )
       
   932                             {
       
   933                             // notice: if this leaves then ON_LINE event is
       
   934                             // not sent to a CSP class.
       
   935                             iServer.TransportErrorL( tid, errxy );
       
   936                             }
       
   937                         }
       
   938                     // Send ON_LINE status after data channel is opened
       
   939                     iServer.TransportStatus( EInternal_ON_LINE );
       
   940                     } // else
       
   941                 break;
       
   942             case EImpsTrClosing:
       
   943                 iIAP = 0;
       
   944                 NewTrState( EImpsTrInit );
       
   945                 iServer.TransportStatus( EInternal_NO_IAP );
       
   946                 break;
       
   947             case EImpsTrClosingPDP:
       
   948                 iIAP = 0;
       
   949                 NewTrState( EImpsTrClosedPDP );
       
   950                 iServer.TransportStatus( EInternal_NO_IAP );
       
   951                 break;
       
   952             case EImpsTrOpeningPDP:
       
   953                 NewTrState( EImpsTrAPOpen );
       
   954                 // create iSender if needed
       
   955                 errx = DoOpenDataCh();
       
   956                 if ( errx )
       
   957                     {
       
   958                     NewTrState( EImpsTrAPOpen );
       
   959                     iServer.TransportStatus( EInternal_OFF_LINE );
       
   960                     }
       
   961                 else
       
   962                     {
       
   963                     NewTrState( EImpsTrDataOpen );
       
   964                     // Send ON_LINE status not until data channel is opened
       
   965                     iServer.TransportStatus( EInternal_IAP_OPEN );
       
   966                     }
       
   967                 break;
       
   968             default:
       
   969 #ifndef _NO_IMPS_LOGGING_
       
   970                 CImpsClientLogger::Log( _L( "SendReceive2: DoHandleRespL no action ****" ) );
       
   971 #endif
       
   972                 break;
       
   973             };
       
   974         }
       
   975 
       
   976     // Error handling
       
   977     else // (err)
       
   978         {
       
   979         // Connection command failed
       
   980 #ifndef _NO_IMPS_LOGGING_
       
   981         CImpsClientLogger::Log( _L( "SendReceive2: DoHandleRespL error case" ) ); //lint !e725
       
   982 #endif
       
   983 
       
   984         if ( origTr == EImpsTrAPOpening || origTr == EImpsTrClosing )
       
   985             {
       
   986             NewTrState( EImpsTrInit );
       
   987             HBufC8* nullPtr = NULL;
       
   988             TRAP( errx, DoTransportResponseL( iOpenId, err, 200, &nullPtr ) );
       
   989             DeleteId( iOpenId );
       
   990             iOpenId = 0;
       
   991             if ( origTr == EImpsTrClosing )
       
   992                 {
       
   993                 // If closing fails then send NO_IAP event to CSP session so that
       
   994                 // it can start to delete itself.
       
   995                 iServer.TransportStatus( EInternal_NO_IAP );
       
   996                 }
       
   997             }
       
   998         else if ( origTr == EImpsTrOpeningPDP )
       
   999             {
       
  1000             // This is error from OpenIAP, not data channel opening
       
  1001 #ifndef _NO_IMPS_LOGGING_
       
  1002             CImpsClientLogger::Log( _L( "SendReceive2: err: E2 ****" ) ); //lint !e725
       
  1003 #endif
       
  1004             NewTrState( EImpsTrClosedPDP );
       
  1005             DeleteId( iOpenId );
       
  1006             iOpenId = 0;
       
  1007             // KImpsErrorNoIAP error code has a special error handling
       
  1008             // in CSPSession class.
       
  1009             iServer.TransportErrorL( KNullDesC, KImpsErrorNoIAP );
       
  1010             }
       
  1011         else if ( origTr == EImpsTrClosingPDP )
       
  1012             {
       
  1013             // This is error from Close IAP, not data channel closing
       
  1014             // This is handled like Ok case, so don't call transportError callback.
       
  1015             iIAP = 0;
       
  1016             NewTrState( EImpsTrClosedPDP );
       
  1017             iServer.TransportStatus( EInternal_NO_IAP );
       
  1018             }
       
  1019         if ( aConnectionState == EImpsNoAP )
       
  1020             {
       
  1021 #ifndef _NO_IMPS_LOGGING_
       
  1022             CImpsClientLogger::Log( _L
       
  1023                                     ( "SendReceive2: APStatusEvent NO_IAP error ****" ) );
       
  1024 #endif
       
  1025             }
       
  1026         }
       
  1027     }
       
  1028 
       
  1029 // ---------------------------------------------------------
       
  1030 // CImpsSendReceive2::DoHandleTrEventL
       
  1031 // ---------------------------------------------------------
       
  1032 //
       
  1033 void CImpsSendReceive2::DoHandleTrEventL(
       
  1034     TImpsConnectionState aConnectionState,
       
  1035     TBool aAuthClose )
       
  1036     {
       
  1037 #ifndef _NO_IMPS_LOGGING_
       
  1038     CImpsClientLogger::Log( _L( "SendReceive2: DoHandleTrEventL %d trstate=%d" ),
       
  1039                             ( TInt )aConnectionState, iTrState );
       
  1040 #endif
       
  1041 
       
  1042     EImpsInternalStatus myStatus = EInternal_NOT_LOGGED;
       
  1043     switch ( aConnectionState )
       
  1044         {
       
  1045         case EImpsOffline:
       
  1046             myStatus = EInternal_OFF_LINE;
       
  1047             break;
       
  1048         case EImpsNoAP:
       
  1049             if ( iTrState == EImpsTrDataOpen ||
       
  1050                  iTrState == EImpsTrOpeningPDP )
       
  1051                 {
       
  1052                 NewTrState( EImpsTrClosedPDP );
       
  1053                 iSender->Close();
       
  1054                 }
       
  1055             else
       
  1056                 {
       
  1057                 NewTrState( EImpsTrInit );
       
  1058                 }
       
  1059             // shut down routines
       
  1060             iIAP = 0;
       
  1061             myStatus = aAuthClose ? EInternal_NO_IAP_AUTH : EInternal_NO_IAP;
       
  1062             break;
       
  1063         default:
       
  1064             myStatus = EInternal_ON_LINE;
       
  1065             break;
       
  1066         };
       
  1067     iServer.TransportStatus( myStatus );
       
  1068     }
       
  1069 // ---------------------------------------------------------
       
  1070 // CImpsSendReceive2::Destroy
       
  1071 // ---------------------------------------------------------
       
  1072 //
       
  1073 void CImpsSendReceive2::Destroy()
       
  1074     {
       
  1075 #ifndef _NO_IMPS_LOGGING_
       
  1076     CImpsClientLogger::Log( _L( "SendReceive2: Destroy" ) );
       
  1077 #endif
       
  1078     NewTrState( EImpsTrInit );
       
  1079     if ( iSender )
       
  1080         {
       
  1081         iSender->Destroy();
       
  1082         iSender = NULL;
       
  1083         }
       
  1084     if ( iConnMan )
       
  1085         {
       
  1086         iConnMan->Destroy();
       
  1087         delete iConnMan;
       
  1088         iConnMan = NULL;
       
  1089         }
       
  1090 
       
  1091     }
       
  1092 
       
  1093 // ---------------------------------------------------------
       
  1094 // CImpsSendReceive2::ConnManagerHandleL
       
  1095 // ---------------------------------------------------------
       
  1096 //
       
  1097 MMsgConnManager& CImpsSendReceive2::ConnManagerHandleL()
       
  1098     {
       
  1099     return iConnMan->ManagerHandleL();
       
  1100     }
       
  1101 
       
  1102 // -----------------------------------------------------------------------------
       
  1103 // CImpsSendReceive2::NewTrState
       
  1104 // -----------------------------------------------------------------------------
       
  1105 void CImpsSendReceive2::NewTrState( TImpsTrState aNew )
       
  1106     {
       
  1107 #ifndef _NO_IMPS_LOGGING_
       
  1108     CImpsClientLogger::Log( _L( "SendReceive2: NewTrState old=%d new=%d" ),
       
  1109                             iTrState, aNew );
       
  1110 #endif
       
  1111     iTrState = aNew;
       
  1112     }
       
  1113 
       
  1114 // -----------------------------------------------------------------------------
       
  1115 // CImpsSendReceive2::DoOpenDataCh()
       
  1116 // This method does not change iTrState itself
       
  1117 // -----------------------------------------------------------------------------
       
  1118 TInt CImpsSendReceive2::DoOpenDataCh()
       
  1119     {
       
  1120     TInt errx = KErrNone;
       
  1121     if ( ! iSender )
       
  1122         {
       
  1123         TRAP( errx,
       
  1124               { MMsgConnManager& man = iConnMan->ManagerHandleL();
       
  1125                 iSender = NewImpsSenderL( *this, man, iMimeType );
       
  1126               } );
       
  1127         }
       
  1128     if ( errx )
       
  1129         {
       
  1130 #ifndef _NO_IMPS_LOGGING_
       
  1131         CImpsClientLogger::Log(
       
  1132             _L( "SendReceive2: DataCh open ERROR *** %d" ), errx );
       
  1133 #endif
       
  1134         return errx;
       
  1135         }
       
  1136     TRAP( errx, iSender->OpenL( SAP() ) );
       
  1137     if ( errx )
       
  1138         {
       
  1139 #ifndef _NO_IMPS_LOGGING_
       
  1140         CImpsClientLogger::Log(
       
  1141             _L( "SendReceive2: DataCh open ERROR **** %d" ), errx );
       
  1142 #endif
       
  1143         if ( errx != KErrAlreadyExists )
       
  1144             {
       
  1145             iSender->Close( );
       
  1146             iSender->Destroy();
       
  1147             iSender = NULL;
       
  1148             return errx;
       
  1149             }
       
  1150         }
       
  1151     return KErrNone;
       
  1152     }
       
  1153 
       
  1154 // -----------------------------------------------------------------------------
       
  1155 // CTrReq::CTrReq
       
  1156 // -----------------------------------------------------------------------------
       
  1157 
       
  1158 CTrReq::CTrReq(
       
  1159     const TDesC& aTID,
       
  1160     TInt aOpId ):
       
  1161         iOpId( aOpId )
       
  1162     {
       
  1163     iTID = aTID;
       
  1164     }
       
  1165 
       
  1166 CTrReq::~CTrReq()
       
  1167     {
       
  1168 
       
  1169     }
       
  1170 
       
  1171 void CTrReq::Destroy()
       
  1172     {
       
  1173     iLink.Deque();
       
  1174     delete this;
       
  1175     }
       
  1176 
       
  1177 
       
  1178 //  End of File