pkiutilities/DeviceToken/Src/Generic/Client/DevTokenClientSession.cpp
changeset 0 164170e6151a
child 5 3b17fc5c9564
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2006 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 DevTokenClientSession
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <e32std.h>
       
    21 #include <e32uid.h>
       
    22 #include <e32debug.h>
       
    23 #include "DevTokenUtils.h"
       
    24 #include "DevTokenServerName.h"
       
    25 #include "DevTokenClientSession.h"
       
    26 #include "DevtokenLog.h"
       
    27 
       
    28 //Forward declaration
       
    29 static TInt StartServer();
       
    30 
       
    31 _LIT(KDevTokenServerImg,"DevTokenServer");
       
    32 
       
    33 // Tokentype session class for Device token certificate stores
       
    34 // Both device certificate store and trust site certificate store share
       
    35 // the same token type.
       
    36 
       
    37 // ======== LOCAL FUNCTIONS ========
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // TInt StartServer()
       
    41 // Client-server startup code
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 TInt StartServer()
       
    45     {
       
    46     // Server startup is different for WINS in EKA1 mode ONLY (lack of process
       
    47     // emulation - we load the library in this instance
       
    48     const TUidType serverUid(KNullUid, KNullUid, KUidDevTokenServer);
       
    49 
       
    50     RProcess server;  
       
    51     TInt r = server.Create(KDevTokenServerImg, KNullDesC, serverUid);
       
    52     
       
    53     RDebug::Print( _L(" StartServer ") );
       
    54     
       
    55     if ( r != KErrNone )
       
    56         {
       
    57         RDebug::Print( _L("StartServer1 %d"), r );
       
    58         return r;
       
    59         }
       
    60 
       
    61     // Synchronise with the process to make sure it hasn't died straight away
       
    62     TRequestStatus stat;
       
    63     server.Rendezvous(stat);
       
    64     if ( stat != KRequestPending )
       
    65         {
       
    66         // logon failed - server is not yet running, so cannot have terminated
       
    67         server.Kill(0);       // Abort startup
       
    68         }
       
    69     else
       
    70         {
       
    71         // logon OK - start the server
       
    72         server.Resume();
       
    73         }
       
    74 
       
    75     RDebug::Print( _L(" StartServer 2") );
       
    76 
       
    77     // Wait to synchronise with server - if it dies in the meantime, it
       
    78     // also gets completed
       
    79     User::WaitForRequest(stat); 
       
    80     
       
    81     RDebug::Print( _L(" StartServer 3") );
       
    82     // We can't use the 'exit reason' if the server panicked as this
       
    83     // is the panic 'reason' and may be '0' which cannot be distinguished
       
    84     // from KErrNone
       
    85     r = (server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
       
    86     
       
    87     RDebug::Print( _L(" StartServer 4 %d"), r );
       
    88     
       
    89     server.Close();
       
    90     return (r);
       
    91     }
       
    92     
       
    93     
       
    94 // ======== MEMBER FUNCTIONS ========
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // RDevTokenClientSession::RDevTokenClientSession()
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 RDevTokenClientSession::RDevTokenClientSession()
       
   101     {
       
   102     }
       
   103 
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // RDevTokenClientSession::ClientSessionL()
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 RDevTokenClientSession* RDevTokenClientSession::ClientSessionL()
       
   110     {
       
   111     return new( ELeave ) RDevTokenClientSession();
       
   112     }
       
   113 
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // RDevTokenClientSession::SendRequest()
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 TInt RDevTokenClientSession::SendRequest(TDevTokenMessages aRequest, const TIpcArgs& aArgs) const
       
   120     {
       
   121     return SendReceive(aRequest, aArgs);
       
   122     }
       
   123 
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // RDevTokenClientSession::SendAsyncRequest()
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void RDevTokenClientSession::SendAsyncRequest(TDevTokenMessages aRequest, const TIpcArgs& aArgs, TRequestStatus* aStatus) const
       
   130     {
       
   131     __ASSERT_ALWAYS(aStatus, DevTokenPanic(EBadArgument));
       
   132     if (aStatus)
       
   133         {
       
   134         *aStatus = KRequestPending;
       
   135         SendReceive(aRequest, aArgs, *aStatus);
       
   136         }
       
   137     }
       
   138 
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // TInt RDevTokenClientSession::Connect(EDevTokenEnum aToken)
       
   142 // Connect to the server, attempting to start it if necessary
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 TInt RDevTokenClientSession::Connect(EDevTokenEnum aToken)
       
   146     {
       
   147     // The version is made up of three pieces of information:
       
   148     // 1. iMajor - The token we want to talk to
       
   149     // 2. iMinor - The protocol version number
       
   150     // 3. iBuild - unused
       
   151     TVersion version(aToken, KDevTokenProtolVersion, 0);
       
   152 
       
   153     TInt retry=2;
       
   154     for (;;)
       
   155         {
       
   156         TInt r=CreateSession(KDevTokenServerName, version, 1);
       
   157 
       
   158         TRACE_PRINT("connect");
       
   159         
       
   160         if ( r!=KErrNotFound && r!=KErrServerTerminated )
       
   161             {
       
   162             RDebug::Print( _L("connect1 %d"), r );
       
   163             return r; 
       
   164             }
       
   165 
       
   166         if (--retry==0)
       
   167             {
       
   168             RDebug::Print( _L(" connect2 %d"), r );
       
   169             return r; 
       
   170             }
       
   171 
       
   172         r=StartServer();
       
   173         
       
   174         if ( r!=KErrNone && r!=KErrAlreadyExists )
       
   175             {
       
   176             RDebug::Print( _L(" connect3 %d"), r );
       
   177             return r;  
       
   178             }
       
   179 
       
   180         }
       
   181     }
       
   182 
       
   183 //EOF
       
   184