connectionmonitoring/connmon/connectionmonitor/src/connmonasyncstopdaemon.cpp
changeset 71 9f263f780e41
equal deleted inserted replaced
70:ac5daea24fb0 71:9f263f780e41
       
     1 /*
       
     2 * Copyright (c) 2010 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:  Active object that starts a connection stop operation
       
    15 *               in a new thread and waits for it to complete.
       
    16 */
       
    17 
       
    18 #include "ConnMonIAP.h"
       
    19 #include "CPsdFax.h"
       
    20 #include "connmonasyncstopdaemon.h"
       
    21 #include "log.h"
       
    22 
       
    23 TAsyncStopThreadData::TAsyncStopThreadData(
       
    24         TUint32 aIapId,
       
    25         TUint32 aNetId )
       
    26         :
       
    27         iIapId( aIapId ),
       
    28         iNetId( aNetId )
       
    29     {
       
    30     iStep = 0;
       
    31     iErrorCode = 0;
       
    32     }
       
    33 
       
    34 
       
    35 TInt ConnectionStopThreadFunction( TAny* aPtr )
       
    36     {
       
    37     TInt err( KErrNone );
       
    38     TAsyncStopThreadData* threadData = (TAsyncStopThreadData*)aPtr;
       
    39 
       
    40     RSocketServ socketServer;
       
    41     RConnection connection;
       
    42     TConnectionInfo info;
       
    43 
       
    44     err = socketServer.Connect( KCmESockMessageSlots );
       
    45     threadData->iErrorCode = err;
       
    46     if ( !err )
       
    47         {
       
    48         err = connection.Open( socketServer, KAfInet );
       
    49         threadData->iStep++; // Step 1
       
    50         threadData->iErrorCode = err;
       
    51         if ( !err )
       
    52             {
       
    53             info.iIapId = threadData->iIapId;
       
    54             info.iNetId = threadData->iNetId;
       
    55             err = connection.Attach(
       
    56                     TPckg<TConnectionInfo>( info ),
       
    57                     RConnection::EAttachTypeNormal );
       
    58             threadData->iStep++; // Step 2
       
    59             threadData->iErrorCode = err;
       
    60             if ( !err )
       
    61                 {
       
    62                 err = connection.Stop( RConnection::EStopAuthoritative );
       
    63                 threadData->iStep++; // Step 3
       
    64                 threadData->iErrorCode = err;
       
    65                 }
       
    66             connection.Close();
       
    67             }
       
    68         socketServer.Close();
       
    69         }
       
    70 
       
    71     return err;
       
    72     }
       
    73 
       
    74 CConnMonAsyncStopDaemon* CConnMonAsyncStopDaemon::NewL( CConnMonIAP* aIap )
       
    75     {
       
    76     CConnMonAsyncStopDaemon* self = CConnMonAsyncStopDaemon::NewLC( aIap );
       
    77     CleanupStack::Pop( self );
       
    78     return self;
       
    79     }
       
    80 
       
    81 CConnMonAsyncStopDaemon* CConnMonAsyncStopDaemon::NewLC( CConnMonIAP* aIap )
       
    82     {
       
    83     CConnMonAsyncStopDaemon* self = new( ELeave ) CConnMonAsyncStopDaemon( aIap );
       
    84     CleanupStack::PushL( self );
       
    85     self->Construct();
       
    86     return self;
       
    87     }
       
    88 
       
    89 CConnMonAsyncStopDaemon::~CConnMonAsyncStopDaemon()
       
    90     {
       
    91     LOGENTRFN("~CConnMonAsyncStopDaemon()")
       
    92     // Must not be active at this point. Can't be cancelled.
       
    93 
       
    94     delete iData;
       
    95     iData = NULL;
       
    96 
       
    97     LOGEXITFN("~CConnMonAsyncStopDaemon()")
       
    98     }
       
    99 
       
   100 CConnMonAsyncStopDaemon::CConnMonAsyncStopDaemon( CConnMonIAP* aIap )
       
   101         :
       
   102         CActive( EConnMonPriorityHigh ),
       
   103         iIap( aIap ),
       
   104         iPsdFax( NULL ),
       
   105         iData( NULL ),
       
   106         iConnectionType( EConnMonStopTypeUnknown ),
       
   107         iConnectionId( 0 )
       
   108     {
       
   109     }
       
   110 
       
   111 void CConnMonAsyncStopDaemon::Construct()
       
   112     {
       
   113     //LOGENTRFN("CConnMonAsyncStopDaemon::Construct()")
       
   114     CActiveScheduler::Add( this );
       
   115     //LOGEXITFN("CConnMonAsyncStopDaemon::Construct()")
       
   116     }
       
   117 
       
   118 TInt CConnMonAsyncStopDaemon::Start( TUint aConnectionId, TUint32 aIapId, TUint32 aNetId )
       
   119     {
       
   120     LOGENTRFN("CConnMonAsyncStopDaemon::Start()")
       
   121     TInt err( KErrNone );
       
   122     iConnectionType = EConnMonStopTypeInternal;
       
   123     iConnectionId = aConnectionId;
       
   124 
       
   125     LOGIT3("Starting async stop daemon, id %d, iap id %d, net id %d", aConnectionId, aIapId, aNetId)
       
   126     iData = new TAsyncStopThreadData( aIapId, aNetId );
       
   127     if ( !iData )
       
   128         {
       
   129         err = KErrNoMemory;
       
   130         LOGEXITFN1("CConnMonAsyncStopDaemon::Start()", err)
       
   131         return err;
       
   132         }
       
   133 
       
   134     _LIT( KTempName, "connmonstop%d" );
       
   135     TBuf<KConnMonSmallBufferLen> name;
       
   136     name.Format( KTempName(), iConnectionId );
       
   137 
       
   138     RThread thread;
       
   139     err = thread.Create(
       
   140             name,
       
   141             ConnectionStopThreadFunction,
       
   142             KCmStopThreadStackSize,
       
   143             NULL,
       
   144             reinterpret_cast<TAny*>( iData ) );
       
   145     LOGIT1("CConnMonAsyncStopDaemon::Start(): Thread created <%d>", err)
       
   146 
       
   147     if ( !err )
       
   148         {
       
   149         thread.Logon( iStatus );
       
   150         SetActive();
       
   151         thread.Resume();
       
   152         }
       
   153 
       
   154     LOGEXITFN1("CConnMonAsyncStopDaemon::Start()", err)
       
   155     return err;
       
   156     }
       
   157 
       
   158 TInt CConnMonAsyncStopDaemon::Start( const TUint aConnectionId, CPsdFax* aPsdFax )
       
   159     {
       
   160     LOGENTRFN("CConnMonAsyncStopDaemon::Start()")
       
   161     TInt err( KErrNone );
       
   162     iConnectionType = EConnMonStopTypeExternalPsd;
       
   163     iConnectionId = aConnectionId;
       
   164     iPsdFax = aPsdFax;
       
   165 
       
   166     err = iPsdFax->Stop( aConnectionId, iStatus );
       
   167     if ( !err )
       
   168         {
       
   169         SetActive();
       
   170         }
       
   171 
       
   172     LOGEXITFN1("CConnMonAsyncStopDaemon::Start()", err)
       
   173     return err;
       
   174     }
       
   175 
       
   176 void CConnMonAsyncStopDaemon::DoCancel()
       
   177     {
       
   178     LOGENTRFN("CConnMonAsyncStopDaemon::DoCancel()")
       
   179     
       
   180     // The cancel process is synchronous from OS side, and can't be cancelled.
       
   181     // There could also be multiple clients waiting for the same connection stop process.
       
   182     
       
   183     LOGEXITFN("CConnMonAsyncStopDaemon::DoCancel()")
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CConnMonAsyncStopDaemon::RunL
       
   188 // When the separate thread closes the connection and finishes executing,
       
   189 // logon-operation completes and this RunL is executed by active scheduler.
       
   190 // Since this is a oneshot active object, the RunL will delete itself at the end.
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CConnMonAsyncStopDaemon::RunL()
       
   194     {
       
   195     LOGIT(".")
       
   196     LOGIT2("RunL: CConnMonAsyncStopDaemon, status %d, stop type %d", iStatus.Int(), iConnectionType)
       
   197 
       
   198     if ( iData )
       
   199         {
       
   200         LOGIT2("Thread steps done %d/3, err <%d>", iData->iStep, iData->iErrorCode)
       
   201         }
       
   202 
       
   203     iIap->CompleteAsyncStopReqs( iConnectionId, iStatus.Int() );
       
   204 
       
   205     switch ( iConnectionType )
       
   206         {
       
   207         case EConnMonStopTypeInternal:
       
   208             iIap->CleanupConnectionInfo( iConnectionId );
       
   209             break;
       
   210         case EConnMonStopTypeExternalPsd:
       
   211             iPsdFax->CleanupConnectionInfo( iConnectionId );
       
   212             break;
       
   213         default:
       
   214             // error
       
   215             break;
       
   216         }
       
   217 
       
   218     delete this;
       
   219     }
       
   220 
       
   221 // End of file