keepalive/flextimer/server/src/flextimerserver.cpp
branchRCL_3
changeset 58 83ca720e2b9a
parent 57 05bc53fe583b
child 62 bb1f80fb7db2
equal deleted inserted replaced
57:05bc53fe583b 58:83ca720e2b9a
     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:  Implementation of CFlexTimerServer class.
       
    15  *
       
    16  */
       
    17 /*
       
    18  * %version: 2 %
       
    19  */
       
    20 
       
    21 // System include files
       
    22 #include <e32cmn.h>
       
    23 #include <e32std.h>
       
    24 // User include files go here:
       
    25 #include "flextimercommon.h"
       
    26 #include "flextimerserver.h"
       
    27 #include "flextimersession.h"
       
    28 #include "flextimerengine.h"
       
    29 #include "OstTraceDefinitions.h"
       
    30 #ifdef OST_TRACE_COMPILER_IN_USE
       
    31 #include "flextimerserverTraces.h"
       
    32 #endif
       
    33 
       
    34 // ======== MEMBER FUNCTIONS ========
       
    35 
       
    36 // --------------------------------------------------------------------------
       
    37 // Two phased construction, 1st phase
       
    38 // --------------------------------------------------------------------------
       
    39 //
       
    40 EXPORT_C CFlexTimerServer* CFlexTimerServer::NewL(
       
    41         CActive::TPriority aPriority )
       
    42     {
       
    43     CFlexTimerServer* self = new ( ELeave ) CFlexTimerServer( aPriority );
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop( self );
       
    47     return self;
       
    48     }
       
    49 
       
    50 // --------------------------------------------------------------------------
       
    51 // Destructor
       
    52 // --------------------------------------------------------------------------
       
    53 //
       
    54 CFlexTimerServer::~CFlexTimerServer()
       
    55     {
       
    56     // Free allocated resources, CServer2 shutdowns sessions
       
    57     delete iTimerEngine;
       
    58 
       
    59     OstTrace0( TRACE_INTERNAL,
       
    60             CFLEXTIMERSERVER_DEL,
       
    61             "CFlexTimerServer::~CFlexTimerServer: exiting" );
       
    62     }
       
    63 
       
    64 // --------------------------------------------------------------------------
       
    65 // Creation of new sessions, called by server framework
       
    66 // --------------------------------------------------------------------------
       
    67 //
       
    68 CSession2* CFlexTimerServer::NewSessionL( const TVersion& aVersion,
       
    69                                           const RMessage2& /*aMessage*/ ) const
       
    70     {
       
    71     // Version checking, according to two main principles
       
    72     // 1. Newer server will always support older clients (change this if
       
    73     //    compatibility is changed at some phase)
       
    74     // 2. Guaranteed that newer client will be compatible with older server 
       
    75     //    within the same major version
       
    76     if ( aVersion.iMajor > KFlexTimerServMajorVersionNumber )
       
    77         {
       
    78         OstTrace1(
       
    79                 TRACE_INTERNAL,
       
    80                 CFLEXTIMERSERVER_NEWSESSIONLVER,
       
    81                 "CFlexTimerServer::NewSessionL: Invalid major version (%d)",
       
    82                 aVersion.iMajor );
       
    83         User::Leave( KErrNotSupported );
       
    84         }
       
    85 
       
    86     // Create and return session
       
    87     CSession2* session = new ( ELeave ) CFlexTimerSession( iTimerEngine );
       
    88 
       
    89     OstTrace1( TRACE_INTERNAL,
       
    90             CFLEXTIMERSERVER_NEWSESSIONL,
       
    91             "CFlexTimerServer::NewSessionL: Created; session=%x",
       
    92             ( TUint )session );
       
    93 
       
    94     return session;
       
    95     }
       
    96 
       
    97 // --------------------------------------------------------------------------
       
    98 // Thread start function, loops in active scheduler
       
    99 // --------------------------------------------------------------------------
       
   100 //
       
   101 void CFlexTimerServer::StartServerL()
       
   102     {
       
   103     // Called when thread is created, create active scheduler
       
   104     CActiveScheduler* sched = new CActiveScheduler;
       
   105     if ( sched == NULL )
       
   106         {
       
   107         // Leave error code is used as a panic code
       
   108         User::Leave( EFlexTimerServerActiveScheduler );
       
   109         }
       
   110     // Add active scheduler object to cleanup stack before installing it.
       
   111     // It will be deleted, if any of the next operations fails.
       
   112     CleanupStack::PushL( sched );
       
   113     CActiveScheduler::Install( sched );
       
   114 
       
   115     // Create server object and start it
       
   116     CFlexTimerServer* flexServer = NULL;
       
   117     flexServer = CFlexTimerServer::NewL( EPriorityStandard );
       
   118 
       
   119     // Push the server object to cleanup stack before starting it
       
   120     CleanupStack::PushL( flexServer );
       
   121     
       
   122     TInt err = KErrNone;
       
   123     
       
   124     err = flexServer->Start( KFlexTimerServerName );
       
   125     if ( err != KErrNone )
       
   126         {
       
   127         // Leave error code is used as a panic code
       
   128         User::Leave( EFlexTimerServerStartServer );
       
   129         }
       
   130 
       
   131     // Complete open rendezvous in this process (syncs with creator)
       
   132     RProcess::Rendezvous( KErrNone );
       
   133 
       
   134     // Active scheduler start will not leave, thus pop added objects
       
   135     // from the cleanup stack. They will be deleted once the execution
       
   136     // returns from the active scheduler (application is exiting)
       
   137     CleanupStack::Pop( flexServer );
       
   138     CleanupStack::Pop( sched );
       
   139 
       
   140     OstTrace0( TRACE_INTERNAL,
       
   141             CFLEXTIMERSERVER_START,
       
   142             "CFlexTimerServer::StartServerL: Call ActiveScheduler" );
       
   143 
       
   144     // Start active scheduler, thread stays in active scheduler loop
       
   145     CActiveScheduler::Start();
       
   146 
       
   147     OstTrace0( TRACE_INTERNAL,
       
   148             CFLEXTIMERSERVER_EXIT,
       
   149             "CFlexTimerServer::StartServerL: ActiveScheduler exit" );
       
   150 
       
   151     // Delete allocated resources
       
   152     delete flexServer;
       
   153     delete sched;
       
   154     }
       
   155 
       
   156 // --------------------------------------------------------------------------
       
   157 // Constructor
       
   158 // --------------------------------------------------------------------------
       
   159 //
       
   160 CFlexTimerServer::CFlexTimerServer( CActive::TPriority aPriority ) :
       
   161     CServer2( aPriority ), iTimerEngine( NULL )
       
   162     {
       
   163 
       
   164     }
       
   165 
       
   166 // --------------------------------------------------------------------------
       
   167 // Two phased construction, 2nd phase
       
   168 // --------------------------------------------------------------------------
       
   169 //
       
   170 void CFlexTimerServer::ConstructL()
       
   171     {
       
   172     // Create engine, leaving is handled in server creation
       
   173     iTimerEngine = CFlexTimerEngine::NewL();
       
   174     OstTrace0( TRACE_INTERNAL,
       
   175             CFLEXTIMERSERVER_CONSTRUCTL,
       
   176             "CFlexTimerServer::ConstructL: Created engine" );
       
   177     }
       
   178 
       
   179 // ======== GLOBAL FUNCTIONS ========
       
   180 
       
   181 // --------------------------------------------------------------------------
       
   182 // Main function of the server executable.
       
   183 // --------------------------------------------------------------------------
       
   184 //
       
   185 GLDEF_C TInt E32Main()
       
   186     {
       
   187     // Mark memory allocation check 
       
   188     __UHEAP_MARK;
       
   189 
       
   190     // Create cleanup stack
       
   191     CTrapCleanup* cleanup = CTrapCleanup::New();
       
   192     __ASSERT_ALWAYS( cleanup, User::Panic( KFlexTimerServerPanicCat,
       
   193                     EFlexTimerServerCleanupStack ) );
       
   194     // Start server
       
   195     TRAPD( panicCode, CFlexTimerServer::StartServerL() );
       
   196     if ( panicCode != KErrNone )
       
   197         {
       
   198         OstTrace1( TRACE_INTERNAL,
       
   199                 CFLEXTIMERSERVER_MAINERROR,
       
   200                 "CFlexTimerServer - Main: Start server failed (%d)",
       
   201                 panicCode );
       
   202         delete cleanup;
       
   203         User::Panic( KFlexTimerServerPanicCat, panicCode );
       
   204         }
       
   205     else
       
   206         {
       
   207         OstTrace0( TRACE_INTERNAL,
       
   208                 CFLEXTIMERSERVER_MAINEXIT,
       
   209                 "CFlexTimerServer - Main: Application exiting" );
       
   210 
       
   211         // Delete cleanup stack
       
   212         delete cleanup;
       
   213         }
       
   214 
       
   215     // Memory allocation check, panic in UDEB case of memory leak
       
   216     __UHEAP_MARKEND;
       
   217 
       
   218     return KErrNone;
       
   219     }