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

Generated by  doxygen 1.6.2