IMPSengine/engsrv/src/impsactivecirmonitor.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2003-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:  Im circle Manager.
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <e32std.h>
       
    22 #include    "impsactivecirmonitor.h"
       
    23 #include    "impsutils.h"
       
    24 #include    <msgconnmanagerapi.h>
       
    25 
       
    26 
       
    27 // CONSTANTS
       
    28 
       
    29 // TCP/IP CIR ping interval in seconds
       
    30 const TInt KImpsPingInt = 900;
       
    31 
       
    32 // MACROS
       
    33 #ifndef _DEBUG
       
    34 #define _NO_IMPS_LOGGING_
       
    35 #endif
       
    36 
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 
       
    39 // MODULE DATA STRUCTURES
       
    40 
       
    41 
       
    42 // ================= MEMBER FUNCTIONS =======================
       
    43 
       
    44 // C++ default constructor can NOT contain any code, that
       
    45 // might leave.
       
    46 //
       
    47 
       
    48 //**********************************
       
    49 // CImpsCirManager
       
    50 //**********************************
       
    51 CImpsCirManager::CImpsCirManager(
       
    52     MImpsCSPSession& aServer,
       
    53     TInt aPriority )
       
    54         : CActive( aPriority ),
       
    55         iServer( aServer ),
       
    56         iState( EImpsCirOffline ),
       
    57         iCanceled( EFalse ),
       
    58         iManager( NULL ),
       
    59         iTCPWatcher( NULL ),
       
    60         iUDPWatcher( NULL ),
       
    61         iChannels ( 0 ),
       
    62         iReqChannels ( 0 )
       
    63     {
       
    64     // Add this to the scheduler
       
    65     CActiveScheduler::Add( this );
       
    66     }
       
    67 
       
    68 // EPOC default constructor can leave.
       
    69 void CImpsCirManager::ConstructL()
       
    70     {
       
    71 #ifndef _NO_IMPS_LOGGING_
       
    72     CImpsClientLogger::Log( _L( "CImpsCirManager: Constructor rel200517.1+" ) );
       
    73 #endif
       
    74     }
       
    75 
       
    76 // Two-phased constructor.
       
    77 CImpsCirManager* CImpsCirManager::NewL(
       
    78     MImpsCSPSession& aServer,
       
    79     TInt aPriority )
       
    80     {
       
    81     CImpsCirManager* self = new ( ELeave ) CImpsCirManager(
       
    82         aServer, aPriority );
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85     CleanupStack::Pop();
       
    86     return self;
       
    87     }
       
    88 
       
    89 
       
    90 // Destructor
       
    91 CImpsCirManager::~CImpsCirManager()
       
    92     {
       
    93 #ifndef _NO_IMPS_LOGGING_
       
    94     CImpsClientLogger::Log( _L( "CImpsCirManager: Destructor" ) );
       
    95 #endif
       
    96     Cancel();
       
    97     if ( iTCPWatcher )
       
    98         {
       
    99         delete iTCPWatcher;
       
   100         iTCPWatcher = NULL;
       
   101         }
       
   102     if ( iUDPWatcher )
       
   103         {
       
   104         iUDPWatcher->Destroy();
       
   105         iUDPWatcher = NULL;
       
   106         }
       
   107 
       
   108     // REcomSession::FinalClose() is not called here but in the engine core
       
   109     // just before active acheduler is stopped.
       
   110 
       
   111     }  //lint !e1740 iManager is not deleted here, it's only reference
       
   112 
       
   113 // ---------------------------------------------------------
       
   114 // CImpsCirManager::OpenChL
       
   115 // ---------------------------------------------------------
       
   116 //
       
   117 void CImpsCirManager::OpenChL(
       
   118     TImpsCirChannels aChannels,
       
   119     MMsgConnManager& aManager,
       
   120     TUint aServerport,
       
   121     const TDesC& aServerAddress,
       
   122     const TDesC8& aCSPSessionID,
       
   123     TUint aUDPPort )
       
   124     {
       
   125     if  ( ( iState == EImpsCirOnline ) || ( aChannels == 0 ) )
       
   126         {
       
   127 #ifndef _NO_IMPS_LOGGING_
       
   128         CImpsClientLogger::Log( _L( "CImpsCirManager: OpenChL ignored" ) );
       
   129 #endif
       
   130         iServer.CirChOpened();
       
   131         // Nothing to open
       
   132         return;
       
   133         }
       
   134 
       
   135 #ifndef _NO_IMPS_LOGGING_
       
   136     CImpsClientLogger::Log( _L( "CImpsCirManager: OpenChL" ) );
       
   137 #endif
       
   138 
       
   139     iManager = &aManager;
       
   140     iReqChannels = aChannels;
       
   141     iUDPPort = aUDPPort;
       
   142 
       
   143     // Start TCP watcher first if required
       
   144     if ( ( aChannels & KImpsCirTcp ) && ~( iChannels & KImpsCirTcp ) )
       
   145         {
       
   146         DoOpenTCPL( aServerport, aServerAddress,
       
   147                     aCSPSessionID );
       
   148         // Another watcher started in RunL if needed
       
   149         }
       
   150     // UPD start here if TCP not required
       
   151     else if ( ( aChannels & KImpsCirUdp ) && ~( iChannels & KImpsCirUdp ) )
       
   152         {
       
   153         DoOpenUDP( iUDPPort );
       
   154         }
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------
       
   158 // CImpsCirManager::DoOpenTCPL
       
   159 // ---------------------------------------------------------
       
   160 //
       
   161 void CImpsCirManager::DoOpenTCPL(
       
   162     TUint aServerport,
       
   163     const TDesC& aServerAddress,
       
   164     const TDesC8& aCSPSessionID )
       
   165     {
       
   166 #ifndef _NO_IMPS_LOGGING_
       
   167     CImpsClientLogger::Log( _L( "CImpsCirManager: DoOpenTCPL" ) );
       
   168 #endif
       
   169     // If opening fails then this method leaves. TCP is tried to open
       
   170     // so CSP session gets information about failure.
       
   171     // Start watchers, TCP watcher first
       
   172     iTCPWatcher = CImpsTcpCirWatcher::NewL( KImpsPingInt, *this, *iManager );
       
   173     iState = EImpsCirOpeningTcp;
       
   174     ActivateMe();
       
   175     iTCPWatcher->StartTCPWatcherL( iStatus, aServerport, aServerAddress, aCSPSessionID );
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------
       
   179 // CImpsCirManager::DoOpenUDP
       
   180 // ---------------------------------------------------------
       
   181 //
       
   182 void CImpsCirManager::DoOpenUDP(
       
   183     TUint aUDPPort )
       
   184     {
       
   185 #ifndef _NO_IMPS_LOGGING_
       
   186     CImpsClientLogger::Log( _L( "CImpsCirManager: DoOpenUDP" ) );
       
   187 #endif
       
   188     // UPD watcher registeration
       
   189     TInt err = KErrNone;
       
   190     TRAP( err, iUDPWatcher = CImpsUdpCirWatcher::NewL() );
       
   191     if ( err )
       
   192         {
       
   193         // UDP open erros are reported to csp session asynchronously via RunL
       
   194         DoHandleError( err, EImpsUdpCir );
       
   195         }
       
   196     TRAP( err, iUDPWatcher->RegisterCirReceiverL( *this, aUDPPort ) );
       
   197     if ( err )
       
   198         {
       
   199         // UDP open erros are reported to csp session asynchronously via RunL
       
   200         DoHandleError( err, EImpsUdpCir );
       
   201         }
       
   202     else
       
   203         {
       
   204         iChannels = iChannels | KImpsCirUdp;
       
   205         }
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------
       
   209 // CImpsCirManager::CloseCh
       
   210 // ---------------------------------------------------------
       
   211 //
       
   212 void CImpsCirManager::CloseCh(
       
   213     TImpsCirChannels aChannels )
       
   214     {
       
   215 #ifndef _NO_IMPS_LOGGING_
       
   216     CImpsClientLogger::Log( _L( "CImpsCirManager: CloseCh" ) );
       
   217 #endif
       
   218 
       
   219     // If active then have to wait
       
   220     Cancel();
       
   221 
       
   222     if ( !( aChannels & iChannels ) ||
       
   223          iState == EImpsCirOffline ||
       
   224          ( !iTCPWatcher && !iUDPWatcher ) )
       
   225         {
       
   226         // Nothing to close
       
   227         return;
       
   228         }
       
   229 
       
   230     // Channels that required to be closed
       
   231     iReqChannels = aChannels;
       
   232 
       
   233     // Close TCP
       
   234     if ( ( iReqChannels & KImpsCirTcp ) &&
       
   235          ( iChannels & KImpsCirTcp ) )
       
   236         {
       
   237         iTCPWatcher->StopTCPWatcher();
       
   238         iChannels = iChannels & ~KImpsCirTcp;
       
   239         }
       
   240     // Close UDP
       
   241     if ( ( iReqChannels & KImpsCirUdp ) &&
       
   242          ( iChannels & KImpsCirUdp ) )
       
   243         {
       
   244         TInt err = KErrNone;
       
   245         TRAP( err, iUDPWatcher->UnregisterCirReceiverL( *this ) );
       
   246         iChannels = iChannels & ~KImpsCirUdp;
       
   247         }
       
   248 
       
   249     // Destructor handles the rest of the shut down activies
       
   250 
       
   251     iState = EImpsCirOffline;
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------
       
   255 // CImpsCirManager::CirReceivedL
       
   256 // ---------------------------------------------------------
       
   257 //
       
   258 void CImpsCirManager::CirReceivedL(
       
   259     const TDesC8& aMessage,
       
   260     const TImpsSupportedCirTypes aCirType )
       
   261     {
       
   262 #ifndef _NO_IMPS_LOGGING_
       
   263     CImpsClientLogger::Log( _L( "CImpsCirManager: CirReceivedL type=%d" ), aCirType );
       
   264 #endif
       
   265 
       
   266     // notice: This is not very clear how to handle multiple IP-CIR channels
       
   267     // Better keep on listening them
       
   268     /*
       
   269     if ( ( aCirType == EImpsTcpCir ) && ( iChannels & KImpsCirUdp ) )
       
   270         {
       
   271         // Stop UDP if another CIR channel in use
       
   272         iUDPWatcher->StopUDPWatcher();
       
   273         }
       
   274     else if ( ( aCirType == EImpsUdpCir ) && ( iChannels & KImpsCirTcp ) )
       
   275         {
       
   276         // Stop TCP if another CIR channel in use
       
   277         iTCPWatcher->StopTCPWatcher();
       
   278         }
       
   279         */
       
   280 
       
   281     iServer.CirMessageL( aMessage );
       
   282 
       
   283     }
       
   284 
       
   285 // ---------------------------------------------------------
       
   286 // CImpsCirManager::HandleErrorL
       
   287 // ---------------------------------------------------------
       
   288 //
       
   289 void CImpsCirManager::HandleErrorL(
       
   290     const TInt aReceiverError,
       
   291     const TImpsSupportedCirTypes aCirType )
       
   292     {
       
   293     DoHandleError( aReceiverError, aCirType );
       
   294     }
       
   295 
       
   296 // ---------------------------------------------------------
       
   297 // CImpsCirManager::DoHandleError
       
   298 // ---------------------------------------------------------
       
   299 //
       
   300 void CImpsCirManager::DoHandleError(
       
   301     const TInt aReceiverError,
       
   302     const TImpsSupportedCirTypes aCirType )
       
   303     {
       
   304 
       
   305 #ifndef _NO_IMPS_LOGGING_
       
   306     CImpsClientLogger::Log( _L( "CImpsCirManager: DoHandleError %d type=%d" ),
       
   307                             aReceiverError, aCirType );
       
   308 #endif
       
   309 
       
   310     // We suppose that this is not called if this is active, but
       
   311     // better to check anyway. Then do the following actions.
       
   312     // - Yield to ifself with an error code.
       
   313     // - In RunL stop channels
       
   314     // - Then call iServer.CirChError() that destroys us.
       
   315 
       
   316     if ( !IsActive() )
       
   317         {
       
   318         iStatus = KRequestPending;
       
   319         SetActive();
       
   320         iCanceled = EFalse;
       
   321         TRequestStatus* s = &iStatus;
       
   322         User::RequestComplete( s, aReceiverError );
       
   323         }
       
   324     }
       
   325 
       
   326 // ---------------------------------------------------------
       
   327 // CImpsCirManager::RunL
       
   328 // ---------------------------------------------------------
       
   329 //
       
   330 void CImpsCirManager::RunL( )
       
   331     {
       
   332 
       
   333 #ifndef _NO_IMPS_LOGGING_
       
   334     CImpsClientLogger::Log( _L( "CImpsCirManager: RunL %d" ), iStatus.Int() );
       
   335 #endif
       
   336 
       
   337     if ( iCanceled )
       
   338         {
       
   339 #ifndef _NO_IMPS_LOGGING_
       
   340         CImpsClientLogger::Log( _L( "CImpsCirManager: RunL ignored" ) );
       
   341 #endif
       
   342         iCanceled = EFalse;
       
   343         if ( iChannels )
       
   344             {
       
   345             iState = EImpsCirOnline;
       
   346             }
       
   347         else
       
   348             {
       
   349             iState = EImpsCirOffline;
       
   350             }
       
   351         return;
       
   352         }
       
   353 
       
   354     // Error handling, common for both open methods and HandleErrorL methods.
       
   355     // CSP session entity will call destrcutor, so no need to do else than
       
   356     // close watcher
       
   357     if ( iStatus.Int() != KErrNone )
       
   358         {
       
   359         if ( iState == EImpsCirOnline )
       
   360             {
       
   361             if ( iChannels & KImpsCirTcp )
       
   362                 {
       
   363                 iTCPWatcher->StopTCPWatcher();
       
   364                 }
       
   365             if ( iChannels & KImpsCirUdp )
       
   366                 {
       
   367                 TInt err = KErrNone;
       
   368                 TRAP( err, iUDPWatcher->UnregisterCirReceiverL( *this ) );
       
   369                 }
       
   370             iState = EImpsCirOffline;
       
   371             }
       
   372         iServer.CirChError();
       
   373         return;
       
   374         }
       
   375 
       
   376     // asynchronous TCP CIR opening completed
       
   377     if ( iState == EImpsCirOpeningTcp )
       
   378         {
       
   379         iChannels = iChannels | KImpsCirTcp;
       
   380         // if UDP were supported
       
   381         if ( ( iReqChannels & KImpsCirUdp ) &&
       
   382              !( iChannels & KImpsCirUdp ) )
       
   383             {
       
   384             // load UDP listener synchronously
       
   385             DoOpenUDP( iUDPPort );
       
   386             }
       
   387         }
       
   388 
       
   389     // Everything is OK and opened
       
   390     iState = EImpsCirOnline;
       
   391     iServer.CirChOpened();
       
   392     }
       
   393 
       
   394 // ---------------------------------------------------------
       
   395 // CImpsCirManager::DoCancel
       
   396 // ---------------------------------------------------------
       
   397 //
       
   398 void CImpsCirManager::DoCancel()
       
   399     {
       
   400 #ifndef _NO_IMPS_LOGGING_
       
   401     CImpsClientLogger::Log( _L( "CImpsCirManager: DoCancel" ) );
       
   402 #endif
       
   403     // This just waits IAP connection operation to end.
       
   404     iCanceled = ETrue;
       
   405     if ( iState == EImpsCirOpeningTcp )
       
   406         {
       
   407         // This returns KErrCancel if an operation is not ready
       
   408         iTCPWatcher->StopTCPWatcher();
       
   409         }
       
   410     }
       
   411 
       
   412 // ---------------------------------------------------------
       
   413 // CImpsCirManager::ActivateMe
       
   414 // ---------------------------------------------------------
       
   415 //
       
   416 TInt CImpsCirManager::ActivateMe(  )
       
   417     {
       
   418     if ( !IsActive() )
       
   419         {
       
   420         iStatus = KRequestPending;
       
   421         SetActive();
       
   422         iCanceled = EFalse;
       
   423         return KErrNone;
       
   424         }
       
   425     else
       
   426         {
       
   427         return KErrNotReady;
       
   428         }
       
   429     }
       
   430 
       
   431 
       
   432 
       
   433 //  End of File
       
   434