accessoryservices/accessoryserver/src/Server/AccServer.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2002-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:  Server side server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "AccServer.h"
       
    21 #include "AccClientServerMessages.h"
       
    22 #include "AccSrvMainSession.h"
       
    23 #include "AccSrvConnectionController.h"
       
    24 #include "AsyProxyStarter.h"
       
    25 #include "AsyProxyAPI.h"
       
    26 #include "AccSrvServerModel.h"
       
    27 #include "AccSrvIdleListener.h"
       
    28 #include "acc_debug.h"
       
    29 #include <e32svr.h>
       
    30 
       
    31 // EXTERNAL DATA STRUCTURES
       
    32 
       
    33 // EXTERNAL FUNCTION PROTOTYPES
       
    34 
       
    35 // CONSTANTS
       
    36 
       
    37 // MACROS
       
    38 
       
    39 // LOCAL CONSTANTS AND MACROS
       
    40 
       
    41 // MODULE DATA STRUCTURES
       
    42 
       
    43 // LOCAL FUNCTION PROTOTYPES
       
    44 
       
    45 // FORWARD DECLARATIONS
       
    46 
       
    47 // ============================= LOCAL FUNCTIONS ===============================
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // PanicClient Causes client to panic.
       
    51 // Returns: void
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 void PanicClient(
       
    55     const RMessage2& aMessage,  // Message
       
    56     TAccServerError aPanic )    // Panic error
       
    57     {
       
    58     COM_TRACE_1( "[AccFW:AccServer] PanicClient(0x%x)", aPanic );
       
    59 
       
    60     aMessage.Panic( KAccServerName, aPanic );
       
    61 
       
    62     COM_TRACE_( "[AccFW:AccServer] PanicClient - return void" );
       
    63     }
       
    64 
       
    65 
       
    66 // ============================ MEMBER FUNCTIONS ===============================
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CAccServer::CAccServer
       
    70 // C++ default constructor can NOT contain any code, that
       
    71 // might leave.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CAccServer::CAccServer( TInt aPriority, TServerType aType ) :
       
    75     CPolicyServer( aPriority, accessoryServerPolicy, aType),
       
    76     iObjectConIx( NULL ),
       
    77     iProxy( NULL ),
       
    78     iProxyStarter( NULL ),
       
    79     iConnectionController( NULL )
       
    80     {
       
    81     COM_TRACE_2( "[AccFW:AccServer] CAccServer::CAccServer(0x%x, 0x%x)", aPriority, aType );
       
    82 
       
    83     COM_TRACE_( "[AccFW:AccServer] CAccServer::CAccServer" );
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CAccServer::ConstructL
       
    88 // Symbian 2nd phase constructor can leave.
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CAccServer::ConstructL()
       
    92     {
       
    93     COM_TRACE_( "[AccFW:AccServer] CAccServer::ConstructL()" );
       
    94 
       
    95     iObjectConIx = CObjectConIx::NewL();
       
    96 
       
    97     iServerModel          = CAccSrvServerModel::NewL();
       
    98     iConnectionController = CAccSrvConnectionController::NewL( iServerModel );
       
    99  	  //self destroy itself when idle state is once detect
       
   100 	  CAccSrvIdleListener::NewL( iServerModel);
       
   101 
       
   102     CreateASYProxyL();
       
   103 
       
   104     // CASYProxyStarter exists because server wants to know
       
   105     // when the asys are ready to go
       
   106     iProxyStarter = CASYProxyStarter::NewL( iProxy, iConnectionController );
       
   107 
       
   108     iProxyStarter->LoadASYs();
       
   109 
       
   110     StartL( KAccServerName ); // Should be the last line
       
   111 
       
   112     COM_TRACE_( "[AccFW:AccServer] CAccServer::ConstructL - return void" );
       
   113     }
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CAccServer::NewLC
       
   117 // Two-phased constructor.
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 CAccServer* CAccServer::NewLC()
       
   121     {
       
   122     COM_TRACE_( "[AccFW:AccServer] CAccServer::NewLC()" );
       
   123 
       
   124     CAccServer* self = new ( ELeave ) CAccServer( EPriorityNormal,
       
   125                                                 ESharableSessions );
       
   126     CleanupStack::PushL( self );
       
   127     self->ConstructL();
       
   128 
       
   129     COM_TRACE_1( "[AccFW:AccServer] CAccServer::NewLC - return 0x%x", &self );
       
   130     return self;
       
   131     }
       
   132 
       
   133 // Destructor
       
   134 CAccServer::~CAccServer()
       
   135     {
       
   136     COM_TRACE_( "[AccFW:AccServer] CAccServer::~CAccServer()" );
       
   137 
       
   138     if( iObjectConIx )
       
   139         {
       
   140         delete iObjectConIx;
       
   141         }
       
   142     if( iConnectionController )
       
   143         {
       
   144         delete iConnectionController;
       
   145         }
       
   146     if( iServerModel )
       
   147         {
       
   148         delete iServerModel;
       
   149         }
       
   150     if( iProxy )
       
   151         {
       
   152         delete iProxy;
       
   153         }
       
   154     if( iProxyStarter )
       
   155         {
       
   156         delete iProxyStarter;
       
   157         }
       
   158 
       
   159     COM_TRACE_( "[AccFW:AccServer] CAccServer::~CAccServer" );
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CAccServer::CreateASYProxyL
       
   164 // Creates instance of ASYProxy
       
   165 // (other items were commented in a header).
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 void CAccServer::CreateASYProxyL()
       
   169     {
       
   170     COM_TRACE_( "[AccFW:AccServer] CAccServer::CreateASYProxyL()" );
       
   171 
       
   172     TUid AsyProxyUid3;
       
   173     AsyProxyUid3.iUid = KAsyProxyTInt32;
       
   174 
       
   175     // Dynamically load the DLL
       
   176     const TUidType AsyProxyUid( KNullUid, KNullUid, AsyProxyUid3 );
       
   177 
       
   178     // Use RLibrary object to interface to the DLL
       
   179     RLibrary library;
       
   180     TInt result = library.Load( KAsyProxyName, AsyProxyUid );
       
   181 
       
   182     COM_TRACE_1( "[AccFW:AccServer] CAccServer::CreateASYProxyL - result = 0x%x ", result );
       
   183 
       
   184 #ifdef _DEBUG
       
   185 
       
   186     if ( result != KErrNone )
       
   187         {
       
   188         COM_TRACE_( "[AccFW:AccServer] CAccServer::CreateASYProxyL - Panics now" );
       
   189         User::Panic( KASYProxyNotFound, KErrNotFound );
       
   190         }
       
   191 
       
   192 #endif
       
   193 
       
   194     // Function at ordinal 1 creates new CAsyProxy
       
   195     TLibraryFunction entry = library.Lookup( 1 );
       
   196 
       
   197     // Call the function to create new CASYProxy
       
   198     iProxy = ( CASYProxyAPI* ) entry();
       
   199 
       
   200     COM_TRACE_1( "[AccFW:AccServer] CAccServer::CreateASYProxyL - iProxy = 0x%x", &iProxy );
       
   201     COM_TRACE_( "[AccFW:AccServer] CAccServer::CreateASYProxyL - return void" );
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CAccServer::NewContainerL
       
   206 // Creates container for object containers
       
   207 // (other items were commented in a header).
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 CObjectCon* CAccServer::NewContainerL()
       
   211     {
       
   212     COM_TRACE_( "[AccFW:AccServer] CAccServer::NewContainerL()" );
       
   213 
       
   214     COM_TRACE_( "[AccFW:AccServer] CAccServer::NewContainerL - return" );
       
   215     return iObjectConIx->CreateL();
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CAccServer::NewSessionL
       
   220 // Creates main session for server
       
   221 // (other items were commented in a header).
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 CSession2* CAccServer::NewSessionL( const TVersion& /*aVersion*/,
       
   225                                     const RMessage2& aMessage) const
       
   226     {
       
   227     COM_TRACE_( "[AccFW:AccServer] CAccServer::NewSessionL()" );
       
   228 
       
   229     RThread clientThread;
       
   230     User::LeaveIfError( aMessage.Client( clientThread ) );
       
   231     CAccSrvMainSession *self = CAccSrvMainSession::NewL(
       
   232                                             clientThread,
       
   233                                             *const_cast<CAccServer*> ( this ),
       
   234                                             iProxy,
       
   235                                             iConnectionController,
       
   236                                             iServerModel );
       
   237     clientThread.Close();
       
   238 
       
   239     COM_TRACE_1( "[AccFW:AccServer] CAccServer::NewSessionL - return 0x%x", &self );
       
   240     return self;
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CAccServer::RunError
       
   245 // Error handling for AO's RunL
       
   246 // (other items were commented in a header).
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 TInt CAccServer::RunError( TInt aError )
       
   250     {
       
   251     COM_TRACE_1( "[AccFW:AccServer] CAccServer::RunError(0x%x)", aError );
       
   252 
       
   253     if (aError == KErrBadDescriptor)
       
   254         {
       
   255         // A bad descriptor error implies a badly programmed client, so panic it;
       
   256         // otherwise report the error to the client
       
   257         PanicClient( Message(), EAccServerErrorBadDescriptor );
       
   258         }
       
   259     else
       
   260         {
       
   261         Message().Complete( aError );
       
   262         }
       
   263 
       
   264     // The leave will result in an early return from CServer::RunL(), skipping
       
   265     // the call to request another message. So do that now in order to keep the
       
   266     // server running.
       
   267     ReStart();
       
   268 
       
   269     COM_TRACE_1( "[AccFW:AccServer] CAccServer::RunError - return 0x%x", KErrNone );
       
   270     return KErrNone; // handled the error fully
       
   271     }
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CAccServer::PanicServer
       
   275 // Raises panic for this server
       
   276 // (other items were commented in a header).
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 void CAccServer::PanicServer( TAccServerError aPanic )
       
   280     {
       
   281     COM_TRACE_1( "[AccFW:AccServer] CAccServer::PanicServer(0x%x)", aPanic );
       
   282 
       
   283     User::Panic( KAccServerName, aPanic );
       
   284 
       
   285     COM_TRACE_( "[AccFW:AccServer] CAccServer::PanicServer - return void" );
       
   286     }
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CAccServer::ThreadFunctionL
       
   290 // Constructs the server
       
   291 // Creates, initializes and starts the ActiveScheduler.
       
   292 // (other items were commented in a header).
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CAccServer::ThreadFunctionL()
       
   296     {
       
   297     COM_TRACE_( "[AccFW:AccServer] CAccServer::ThreadFunctionL()" );
       
   298 
       
   299     // Construct active scheduler
       
   300     CActiveScheduler* activeScheduler = new ( ELeave ) CActiveScheduler;
       
   301     CleanupStack::PushL( activeScheduler );
       
   302 
       
   303     // Install active scheduler
       
   304     // We don't need to check whether an active scheduler is already installed
       
   305     // as this is a new thread, so there won't be one
       
   306     CActiveScheduler::Install( activeScheduler );
       
   307 
       
   308     // Rename thread
       
   309     User::RenameThread( KAccServerName );
       
   310     // Construct our server
       
   311     CAccServer::NewLC(); // anonymous
       
   312 
       
   313     // Call Rendezvous to improve startup time
       
   314     RProcess::Rendezvous( KErrNone );
       
   315 
       
   316     // Start handling requests
       
   317     CActiveScheduler::Start();
       
   318 
       
   319     CleanupStack::PopAndDestroy( 2, activeScheduler ); // anonymous CAccServer
       
   320 
       
   321     COM_TRACE_( "[AccFW:AccServer] CAccServer::ThreadFunctionL - return void" );
       
   322     }
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CAccServer::ThreadFunction
       
   326 // Server's thread entry function
       
   327 // (other items were commented in a header).
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 TInt CAccServer::ThreadFunction()
       
   331     {
       
   332     COM_TRACE_( "[AccFW:AccServer] CAccServer::ThreadFunction()" );
       
   333 
       
   334     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   335     if ( !cleanupStack )
       
   336         {
       
   337         PanicServer( EAccServerErrorCreateTrapCleanup );
       
   338         }
       
   339 
       
   340     TRAPD( err, ThreadFunctionL() );
       
   341     if ( err != KErrNone )
       
   342         {
       
   343         PanicServer( EAccServerErrorCreateServer );
       
   344         }
       
   345 
       
   346     delete cleanupStack;
       
   347     cleanupStack = NULL;
       
   348 
       
   349     COM_TRACE_1( "[AccFW:AccServer] CAccServer::ThreadFunction - return 0x%x", KErrNone );
       
   350     return KErrNone;
       
   351     }
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CAccServer::RemoveContainer
       
   355 // -----------------------------------------------------------------------------
       
   356 //
       
   357 void CAccServer::RemoveContainer( CObjectCon* aObjectCon )
       
   358     {
       
   359     COM_TRACE_( "[AccFW:AccServer] CAccServer::RemoveContainer()" );
       
   360 
       
   361     iObjectConIx->Remove( aObjectCon );
       
   362 
       
   363     COM_TRACE_( "[AccFW:AccServer] CAccServer::RemoveContainer() - return" );
       
   364     }
       
   365 
       
   366 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // E32Main implements...
       
   370 // Binary's main function
       
   371 // Returns: TInt: Symbian error code
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 GLDEF_C TInt E32Main()
       
   375 {
       
   376     API_TRACE_( "[AccFW:AccServer] E32Main()" );
       
   377 
       
   378     TInt err = 0;
       
   379     err = CAccServer::ThreadFunction();
       
   380 
       
   381     API_TRACE_1( "[AccFW:AccServer] E32Main - return 0x%x", err );
       
   382     return err;
       
   383 }
       
   384 
       
   385 //  End of File