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

Generated by  doxygen 1.6.2