examples/S60CppExamples/ClientServerSync/client/src/timeserversession.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : timeserversession.cpp
00004 *  Part of     : CSSync
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 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 // INCLUDE FILES
00017 #include <e32math.h>
00018 
00019 #include "ClientServerCommon.h"
00020 #include "TimeServerSession.h"
00021 
00022 // FUNCTION PROTOTYPES
00023 static TInt StartServer();
00024 static TInt CreateServerProcess();
00025 
00026 // ========================= MEMBER FUNCTIONS ==================================
00027 
00028 // -----------------------------------------------------------------------------
00029 // RTimeServerSession::RTimeServerSession()
00030 // C++ default constructor can NOT contain any code, that might leave.
00031 // -----------------------------------------------------------------------------
00032 //
00033 RTimeServerSession::RTimeServerSession()
00034 : RSessionBase()
00035     {
00036     // No implementation required
00037     }
00038 
00039 // -----------------------------------------------------------------------------
00040 // RTimeServerSession::Connect()
00041 // Connects to the server and create a session.
00042 // -----------------------------------------------------------------------------
00043 //
00044 TInt RTimeServerSession::Connect()
00045     {
00046     TInt error = ::StartServer();
00047 
00048     if ( KErrNone == error )
00049         {
00050         error = CreateSession( KTimeServerName,
00051                                Version(),
00052                                KDefaultMessageSlots );
00053         }
00054     return error;
00055     }
00056 
00057 // -----------------------------------------------------------------------------
00058 // RTimeServerSession::Version()
00059 // Gets the version number.
00060 // -----------------------------------------------------------------------------
00061 //
00062 TVersion RTimeServerSession::Version() const
00063     {
00064     return( TVersion( KTimeServMajorVersionNumber,
00065                       KTimeServMinorVersionNumber,
00066                       KTimeServBuildVersionNumber ) );
00067     }
00068 
00069 // -----------------------------------------------------------------------------
00070 // RTimeServerSession::RequestTime()
00071 // Issues a request for the time to the server.
00072 // -----------------------------------------------------------------------------
00073 //
00074 void RTimeServerSession::RequestTime( TTime& aTime ) const
00075     {
00076     // Create descriptor to enable copying data between client and server.
00077     // Note: This can be local since this is a synchronous call.
00078     // Note : Using TPtr8 since this is binary information.
00079     TPtr8 descriptor( reinterpret_cast<TUint8*>( &aTime ), sizeof( aTime ),
00080                       sizeof( aTime ) );
00081     
00082     // Package message arguments before sending to the server
00083     TIpcArgs args( &descriptor );
00084         
00085     // This call waits for the server to complete the request before
00086     // proceeding. When it returns, the new time will be in aTime.
00087     SendReceive( ETimeServRequestTime, args );
00088     }
00089 
00090 
00091 // ============================= OTHER FUNCTIONS ===============================
00092 
00093 // -----------------------------------------------------------------------------
00094 // StartServer()
00095 // Starts the server if it is not already running
00096 // -----------------------------------------------------------------------------
00097 //
00098 static TInt StartServer()
00099     {
00100     TInt result;
00101 
00102     TFindServer findTimeServer( KTimeServerName );
00103     TFullName name;
00104 
00105     result = findTimeServer.Next( name );
00106     if ( result == KErrNone )
00107         {
00108         // Server already running
00109         return KErrNone;
00110         }
00111 
00112     RSemaphore semaphore;
00113     result = semaphore.CreateGlobal( KTimeServerSemaphoreName, 0 );
00114     if ( result != KErrNone )
00115         {
00116         return  result;
00117         }
00118 
00119     result = CreateServerProcess();
00120     if ( result != KErrNone )
00121         {
00122         return  result;
00123         }
00124 
00125     semaphore.Wait();
00126     semaphore.Close();
00127 
00128     return KErrNone;
00129     }
00130 
00131 // -----------------------------------------------------------------------------
00132 // CreateServerProcess()
00133 // Creates a server process
00134 // -----------------------------------------------------------------------------
00135 //
00136 static TInt CreateServerProcess()
00137     {
00138     TInt result;
00139 
00140     const TUidType serverUid( KNullUid, KNullUid, KServerUid3 );
00141 
00142     RProcess server;
00143     
00144     result = server.Create( KTimeServerFilename, KNullDesC, serverUid );
00145     
00146     if ( result != KErrNone )
00147         {
00148         return  result;
00149         }
00150 
00151     server.Resume();
00152     server.Close();
00153 
00154     return  KErrNone;
00155     }
00156 
00157 
00158 // End of File

Generated by  doxygen 1.6.2