contentstorage/srvsrc/casrv.cpp
changeset 85 7feec50967db
child 86 e492551a0d54
equal deleted inserted replaced
4:1a2a00e78665 85:7feec50967db
       
     1 /*
       
     2  * Copyright (c) 2008 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:  ?Description
       
    15  *
       
    16  */
       
    17 // INCLUDE FILES
       
    18 #include <eikenv.h>
       
    19 #include <eikappui.h>
       
    20 #include "casrv.h"
       
    21 #include "casrvdef.h"
       
    22 #include "casrvsession.h"
       
    23 #include "casrvengutils.h"
       
    24 #include "catimeout.h"
       
    25 #include "castorageproxy.h"
       
    26 #include "casrvmanager.h"
       
    27 
       
    28 // ==================== LOCAL FUNCTIONS ====================
       
    29 
       
    30 /**
       
    31  * Stop the Active Scheduler.
       
    32  * @param aPtr Not used.
       
    33  * @return KErrNone.
       
    34  */
       
    35 LOCAL_C TInt StopScheduler( TAny* /*aPtr*/)
       
    36     {
       
    37     // Called by the exit timer, after all clients disconnected (plus a small
       
    38     // delay). Stop the scheduler, this will enable he thread exit.
       
    39     CActiveScheduler::Stop();
       
    40     return KErrNone;
       
    41     }
       
    42 
       
    43 /**
       
    44  * Create a server.
       
    45  * @param Pointer to created server (if created) returned here.
       
    46  * @return Error code.
       
    47  */
       
    48 LOCAL_C TInt CreateServer( CCaSrv*& aServer )
       
    49     {
       
    50     // The TRAP is not working in the same stack frame where the
       
    51     // CTrapCleanup was created. This is why we need this function.
       
    52     TRAPD( err, aServer = CCaSrv::NewL() );
       
    53     return err;
       
    54     }
       
    55 
       
    56 // ==================== GLOBAL FUNCTIONS ====================
       
    57 
       
    58 // ---------------------------------------------------------
       
    59 // 
       
    60 // ---------------------------------------------------------
       
    61 //
       
    62 EXPORT_C TInt RunCaServer()
       
    63     {
       
    64     __UHEAP_MARK;
       
    65     CTrapCleanup* trapCleanup = NULL;
       
    66     CActiveScheduler* activeScheduler = NULL;
       
    67     CCaSrv* server = NULL;
       
    68 
       
    69     TInt err = User::RenameThread( KCaSrvName );
       
    70     if( !err )
       
    71         {
       
    72         // Create a trap cleanup, make and install an active scheduler.
       
    73         err = KErrNoMemory;
       
    74         trapCleanup = CTrapCleanup::New();
       
    75         if( trapCleanup )
       
    76             {
       
    77             activeScheduler = new CActiveScheduler();
       
    78             if( activeScheduler )
       
    79                 {
       
    80                 CActiveScheduler::Install( activeScheduler );
       
    81                 err = CreateServer( server ); // Not pushed (no leaving).
       
    82                 if( !err )
       
    83                     {
       
    84                     err = server->Start( KCaSrvName );
       
    85                     }
       
    86                 }
       
    87             else
       
    88                 {
       
    89                 err = KErrNoMemory;
       
    90                 }
       
    91             }
       
    92         }
       
    93     // Let the caller know how it went.
       
    94     RProcess::Rendezvous( err );
       
    95     if( !err )
       
    96         {
       
    97         CActiveScheduler::Start(); // Start off. Exit timer will stop it.
       
    98         }
       
    99     CActiveScheduler::Install( NULL );
       
   100     delete activeScheduler;
       
   101     delete server;
       
   102     delete trapCleanup;
       
   103     __UHEAP_MARKEND;
       
   104     return err;
       
   105     }
       
   106 
       
   107 // ==================== MEMBER FUNCTIONS ====================
       
   108 
       
   109 // ---------------------------------------------------------
       
   110 // 
       
   111 // ---------------------------------------------------------
       
   112 //
       
   113 CCaSrv* CCaSrv::NewL()
       
   114     {
       
   115     CCaSrv* srv = new ( ELeave ) CCaSrv();
       
   116     CleanupStack::PushL( srv );
       
   117     srv->ConstructL();
       
   118     CleanupStack::Pop( srv );
       
   119     return srv;
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------
       
   123 // 
       
   124 // ---------------------------------------------------------
       
   125 //
       
   126 CCaSrv::~CCaSrv()
       
   127     {
       
   128     // Cancel requests and delete all sessions first.
       
   129     // Base class would do it for us but that's too late - our sessions
       
   130     // call the server back (SessionClosed, RemoveContainer, etc.).
       
   131     Cancel();
       
   132     CSession2* session;
       
   133     iSessionIter.SetToFirst();
       
   134     while( ( session = iSessionIter++ ) != NULL )
       
   135         {
       
   136         delete session;
       
   137         }
       
   138     delete iSrvManager;
       
   139     delete iSrvEngUtils;
       
   140     delete iStorageProxy;
       
   141     delete iExitTimer;
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------
       
   145 // 
       
   146 // ---------------------------------------------------------
       
   147 //
       
   148 CCaStorageProxy* CCaSrv::GetStorageProxy()
       
   149     {
       
   150     return iStorageProxy;
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------
       
   154 // 
       
   155 // ---------------------------------------------------------
       
   156 //
       
   157 CCaSrv::CCaSrv() :
       
   158     CServer2( EPriorityNormal, CServer2::TServerType( EIpcSession_Sharable ) )
       
   159     {
       
   160     }
       
   161 
       
   162 // ---------------------------------------------------------
       
   163 // 
       
   164 // ---------------------------------------------------------
       
   165 //
       
   166 void CCaSrv::ConstructL()
       
   167     {
       
   168     iSessionCount = 0;
       
   169     iExitTimer = CTimeout::NewL( CActive::EPriorityStandard, TCallBack(
       
   170             StopScheduler, NULL ) );
       
   171     iStorageProxy = CCaStorageProxy::NewL();
       
   172     iSrvEngUtils = CCaSrvEngUtils::NewL();
       
   173     iSrvManager = CCaSrvManager::NewL( *iStorageProxy, iSrvEngUtils );
       
   174     }
       
   175 
       
   176 // ---------------------------------------------------------
       
   177 // 
       
   178 // ---------------------------------------------------------
       
   179 //
       
   180 CSession2* CCaSrv::NewSessionL( const TVersion& aVersion,
       
   181         const RMessage2& /*aMessage*/) const
       
   182     {
       
   183     TVersion version( KCaMajorVersion, KCaMinorVersion, KCaBuild );
       
   184     if( !User::QueryVersionSupported( version, aVersion ) )
       
   185         {
       
   186         User::Leave( KErrNotSupported );
       
   187         }
       
   188     CSession2* session;
       
   189     session = CCaSrvSession::NewL( const_cast<CCaSrv&> ( *this ) );
       
   190     iExitTimer->Cancel(); // We have a client, cancel exit (if pending).
       
   191     return session;
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------
       
   195 // 
       
   196 // ---------------------------------------------------------
       
   197 //
       
   198 void CCaSrv::IncreaseSessionCount()
       
   199     {
       
   200     iSessionCount++;
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------
       
   204 // 
       
   205 // ---------------------------------------------------------
       
   206 //
       
   207 void CCaSrv::DecreaseSessionCount()
       
   208     {
       
   209     iSessionCount--;
       
   210     if( iSessionCount == 0 )
       
   211         {
       
   212         iExitTimer->Cancel();
       
   213         CActiveScheduler* currentScheduler = CActiveScheduler::Current();
       
   214         // No more sessions; schedule self-deletion.
       
   215         if( currentScheduler )
       
   216             {
       
   217             iExitTimer->After(
       
   218                     TTimeIntervalMicroSeconds32( KCaSrvExitDelay ) );
       
   219             }
       
   220         }
       
   221     }
       
   222 
       
   223 //  End of File