examples/SDKExamples/ClientServerAsync/client/src/timeserversession.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : timeserversession.cpp
00004 *  Part of     : CSAsync
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation and/or its subsidiary(-ies).
00010 *  All rights reserved.
00011 *  This component and the accompanying materials are made available
00012 *  under the terms of "Eclipse Public License v1.0"
00013 *  which accompanies this distribution, and is available
00014 *  at the URL "http://www.eclipse.org/legal/epl-v10.html".
00015 * ==============================================================================
00016 */
00017 
00018 
00019 // INCLUDE FILES
00020 #include <e32math.h>
00021 
00022 #include "ClientServerCommon.h"
00023 #include "TimeServerSession.h"
00024 
00025 // FUNCTION PROTOTYPES
00026 static TInt StartServer();
00027 static TInt CreateServerProcess();
00028 
00029 // ========================= MEMBER FUNCTIONS ==================================
00030 
00031 // -----------------------------------------------------------------------------
00032 // RTimeServerSession::RTimeServerSession()
00033 // C++ default constructor can NOT contain any code, that might leave.
00034 // -----------------------------------------------------------------------------
00035 //
00036 RTimeServerSession::RTimeServerSession()
00037 : RSessionBase(), iTimeBuffer( NULL, 0, 0 )
00038     {
00039     // No implementation required
00040     }
00041 
00042 // -----------------------------------------------------------------------------
00043 // RTimeServerSession::Connect()
00044 // Connects to the server and create a session.
00045 // -----------------------------------------------------------------------------
00046 //
00047 TInt RTimeServerSession::Connect()
00048     {
00049     TInt error = ::StartServer();
00050 
00051     if ( KErrNone == error )
00052         {
00053         error = CreateSession( KTimeServerName,
00054                                Version(),
00055                                KDefaultMessageSlots );
00056         }
00057     return error;
00058     }
00059 
00060 // -----------------------------------------------------------------------------
00061 // RTimeServerSession::Version()
00062 // Gets the version number.
00063 // -----------------------------------------------------------------------------
00064 //
00065 TVersion RTimeServerSession::Version() const
00066     {
00067     return( TVersion( KTimeServMajorVersionNumber,
00068                       KTimeServMinorVersionNumber,
00069                       KTimeServBuildVersionNumber ) );
00070     }
00071 
00072 // -----------------------------------------------------------------------------
00073 // RTimeServerSession::RequestTime()
00074 // Issues a request for the time to the server.
00075 // -----------------------------------------------------------------------------
00076 //
00077 void RTimeServerSession::RequestTime( TTime& aTime, TRequestStatus& aStatus )
00078     {
00079     iTimeBuffer.Set( reinterpret_cast<TUint8*>( &aTime ), sizeof( aTime ),
00080                      sizeof( aTime ) );
00081     
00082     // Package message arguments before sending to the server
00083     TIpcArgs args( &iTimeBuffer );
00084     
00085     // This call completes immediately, however the server will not
00086     // complete the request until later, so don't pass any local
00087     // descriptors as they will be out of scope by the time the server
00088     // attempts to write to them
00089     SendReceive( ETimeServRequestTime, args, aStatus );
00090     }
00091 
00092 // -----------------------------------------------------------------------------
00093 // RTimeServerSession::CancelRequestTime()
00094 // Cancels any outstanding request to the server.
00095 // -----------------------------------------------------------------------------
00096 void RTimeServerSession::CancelRequestTime() const
00097     {
00098     SendReceive( ETimeServCancelRequestTime, TIpcArgs(NULL) );
00099     }
00100 
00101 // ============================= OTHER FUNCTIONS ===============================
00102 
00103 // -----------------------------------------------------------------------------
00104 // StartServer()
00105 // Starts the server if it is not already running
00106 // -----------------------------------------------------------------------------
00107 //
00108 static TInt StartServer()
00109     {
00110     TInt result;
00111 
00112     TFindServer findTimeServer( KTimeServerName );
00113     TFullName name;
00114 
00115     result = findTimeServer.Next( name );
00116     if ( result == KErrNone )
00117         {
00118         // Server already running
00119         return KErrNone;
00120         }
00121 
00122     RSemaphore semaphore;
00123     result = semaphore.CreateGlobal( KTimeServerSemaphoreName, 0 );
00124     if ( result != KErrNone )
00125         {
00126         return  result;
00127         }
00128 
00129     result = CreateServerProcess();
00130     if ( result != KErrNone )
00131         {
00132         return  result;
00133         }
00134 
00135     semaphore.Wait();
00136     semaphore.Close();
00137 
00138     return KErrNone;
00139     }
00140 
00141 // -----------------------------------------------------------------------------
00142 // CreateServerProcess()
00143 // Creates a server process
00144 // -----------------------------------------------------------------------------
00145 //
00146 static TInt CreateServerProcess()
00147     {
00148     TInt result;
00149 
00150     const TUidType serverUid( KNullUid, KNullUid, KServerUid3 );
00151 
00152     RProcess server;
00153 
00154     result = server.Create( KTimeServerFilename, KNullDesC, serverUid );
00155 
00156     if ( result != KErrNone )
00157         {
00158         return  result;
00159         }
00160 
00161     server.Resume();
00162     server.Close();
00163 
00164     return  KErrNone;
00165     }
00166 
00167 
00168 // End of File

Generated by  doxygen 1.6.2