multimediacommsengine/mmcecli/src/mceserverstarter.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 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:    
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 #include <e32math.h>
       
    24 #include "mceserverstarter.h"
       
    25 #include "mceclientserver.h"
       
    26 
       
    27 #define RETURN_IF_ERROR(err) {TInt _err=err; if(_err!=KErrNone) {return _err;}}
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // MceServerStarter::Start
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 TInt MceServerStarter::Start ()
       
    34 	{
       
    35 	TInt err = KErrNone;
       
    36 
       
    37     TFindServer findServer( KMceServerName );
       
    38     TFullName name;
       
    39 	if ( findServer.Next( name ) == KErrNone) 
       
    40         {
       
    41         return KErrNone; // Server already running
       
    42         }
       
    43 		
       
    44 	RSemaphore semaphore;
       
    45 	err = semaphore.CreateGlobal( KMceServerSemaphoreName , 0 );
       
    46 	if ( err != KErrNone )
       
    47 		{
       
    48 		return err;
       
    49 		}
       
    50 
       
    51 	err = CreateServerProcess( semaphore );
       
    52 	semaphore.Close ();
       
    53 
       
    54     return err;
       
    55     
       
    56 	}
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // MceServerStarter::CreateServerProcess
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 TInt MceServerStarter::CreateServerProcess (RSemaphore& aSemaphore)
       
    63 	{
       
    64 	const TUidType serverUid (KNullUid, KNullUid, KServerUid3);
       
    65 
       
    66 
       
    67 #if ((defined (__WINS__) || defined(__WINSCW__)) && !defined (EKA2))
       
    68 
       
    69 	RLibrary lib;
       
    70     RETURN_IF_ERROR (lib.Load(KMceServerFilename,serverUid))
       
    71 
       
    72     //  Get the WinsMain function
       
    73 	TLibraryFunction functionWinsMain = lib.Lookup (1);
       
    74 
       
    75     //  Call it and cast the result to a thread function
       
    76 	TThreadFunction serverThreadFunction = 
       
    77         reinterpret_cast<TThreadFunction> (functionWinsMain());
       
    78 
       
    79 	TName threadName (KSipServerName);
       
    80 
       
    81 	// Append a random number to make it unique
       
    82 	threadName.AppendNum (Math::Random(), EHex);
       
    83 
       
    84 	RThread server;
       
    85     TInt err = server.Create (threadName,
       
    86                               serverThreadFunction, // thread's main function
       
    87                               KDefaultStackSize,
       
    88                               NULL,
       
    89                               &lib,
       
    90                               NULL,
       
    91                               KServerMinHeapSize,
       
    92                               KServerMaxHeapSize,
       
    93                               EOwnerProcess );
       
    94 
       
    95 	lib.Close ();	// if successful, server thread has handle to library now
       
    96     RETURN_IF_ERROR (err)
       
    97 
       
    98 	server.SetPriority (EPriorityMore);
       
    99 
       
   100 #else // HW build
       
   101 
       
   102 	RProcess server;
       
   103 	RETURN_IF_ERROR (server.Create( KMceServerName, KNullDesC, serverUid ) )
       
   104 
       
   105 #endif
       
   106 
       
   107 
       
   108 	server.Resume ();
       
   109 	aSemaphore.Wait();
       
   110 	TInt exitReason = server.ExitReason();
       
   111 	server.Close ();
       
   112 
       
   113     return exitReason;
       
   114 	}