supl/locationsuplfw/terminalinitiationapi/src/epos_startsuplserver.cpp
changeset 0 667063e416a2
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2002-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:   SUPL Server startup code
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <e32math.h>
       
    22 
       
    23 #include "epos_suplterminal.h"
       
    24 #include "epos_suplterminalipc.h"
       
    25 #include "epos_startsuplserver.h"
       
    26 
       
    27 #ifdef _DEBUG
       
    28 #include <e32debug.h>
       
    29 #endif
       
    30 _LIT(KSuplServerStartupSemaphoreName, "SuplServerStartupSem");
       
    31 
       
    32 /**
       
    33  * Start the server process/thread which lives in an EPOCEXE object.
       
    34  *
       
    35  * \internal
       
    36  * @return KErrNone if successful, any other error code otherwise
       
    37  */
       
    38 TInt RSuplTerminalServer::StartServer()
       
    39     {
       
    40     TRequestStatus started;
       
    41     TInt ret;
       
    42 
       
    43     RSemaphore startupSemaphore;
       
    44     ret = startupSemaphore.CreateGlobal( KSuplServerStartupSemaphoreName, 0 );
       
    45 
       
    46     if ( ret == KErrAlreadyExists )
       
    47         {
       
    48         // The server is starting up, but has not yet started 
       
    49         startupSemaphore.OpenGlobal( KSuplServerStartupSemaphoreName );
       
    50         // wait until the server has started up, Max 5 secs.
       
    51         TInt err = startupSemaphore.Wait(5000000); 
       
    52         startupSemaphore.Close();
       
    53         if (err != KErrNone)
       
    54         	return err;
       
    55         else
       
    56         	return ret;
       
    57         }
       
    58 
       
    59 #ifdef _DEBUG
       
    60     RDebug::Print(_L("SuplApi: Starting SUPL Server..."));
       
    61 #endif
       
    62     const TUidType serverUid(KNullUid, KNullUid, KSuplServerUid3);
       
    63 
       
    64     // Simultaneous launching of two such processes should be detected 
       
    65     // when the second one attempts to create the server object, 
       
    66     // failing with KErrAlreadyExists.
       
    67     //
       
    68     RProcess server;
       
    69     ret = server.Create(KSuplServerImg, KNullDesC, serverUid);
       
    70 
       
    71     if (ret != KErrNone)
       
    72         {
       
    73 #ifdef _DEBUG
       
    74         RDebug::Print(_L("SuplTerminalApi: server start failed %d"), ret);
       
    75 #endif
       
    76         startupSemaphore.Close();
       
    77         return ret;
       
    78         }
       
    79 
       
    80     TRequestStatus died;
       
    81     server.Rendezvous(died);
       
    82 
       
    83     if (died != KRequestPending)
       
    84         {
       
    85         // logon failed - server is not yet running, so cannot have terminated
       
    86         User::WaitForRequest(died); // eat signal
       
    87         server.Kill(0);                         // abort startup
       
    88         }
       
    89     else
       
    90         {
       
    91         server.Resume();
       
    92         }
       
    93     User::WaitForRequest(died);                // wait for start or death
       
    94 
       
    95     // we can't use the 'exit reason' if the server panicked as this
       
    96     // is the panic 'reason' and may be '0' which cannot be distinguished
       
    97     // from KErrNone
       
    98     ret = (server.ExitType() == EExitPanic) ? KErrGeneral : died.Int();
       
    99     server.Close();
       
   100     startupSemaphore.Wait();
       
   101     startupSemaphore.Close();
       
   102     return ret;
       
   103     }
       
   104 
       
   105 //
       
   106 // End of file