examples/S60CppExamples/ClientServerAsync/server/src/timesession.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : timesession.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 <e32svr.h>
00019 
00020 #include "TimeSession.h"
00021 #include "ClientServerCommon.h"
00022 #include "TimeServer.h"
00023 
00024 // ========================= MEMBER FUNCTIONS ==================================
00025 
00026 // -----------------------------------------------------------------------------
00027 // CTimeServerSession::NewL()
00028 // Two-phased constructor.
00029 // -----------------------------------------------------------------------------
00030 //
00031 CTimeServerSession* CTimeServerSession::NewL( CTimeServer& aServer )
00032     {
00033     CTimeServerSession* self = CTimeServerSession::NewLC( aServer );
00034     CleanupStack::Pop( self );
00035     return self;
00036     }
00037 
00038 // -----------------------------------------------------------------------------
00039 // CTimeServerSession::NewLC()
00040 // Two-phased constructor.
00041 // -----------------------------------------------------------------------------
00042 //
00043 CTimeServerSession* CTimeServerSession::NewLC( CTimeServer& aServer )
00044     {
00045     CTimeServerSession* self = new ( ELeave ) CTimeServerSession( aServer );
00046     CleanupStack::PushL( self );
00047     self->ConstructL();
00048     return self;
00049     }
00050 
00051 // -----------------------------------------------------------------------------
00052 // CTimeServerSession::ConstructL()
00053 // Symbian 2nd phase constructor can leave.
00054 // -----------------------------------------------------------------------------
00055 //
00056 void CTimeServerSession::ConstructL()
00057     {
00058     iServer.IncrementSessions();
00059     }
00060 
00061 // -----------------------------------------------------------------------------
00062 // CTimeServerSession::CTimeServerSession()
00063 // C++ default constructor can NOT contain any code, that might leave.
00064 // -----------------------------------------------------------------------------
00065 //
00066 CTimeServerSession::CTimeServerSession(  CTimeServer& aServer )
00067 : iServer( aServer )
00068     {
00069     // Implementation not required
00070     }
00071 
00072 // -----------------------------------------------------------------------------
00073 // CTimeServerSession::~CTimeServerSession()
00074 // Destructor.
00075 // -----------------------------------------------------------------------------
00076 //
00077 CTimeServerSession::~CTimeServerSession()
00078     {
00079     iServer.DecrementSessions();
00080     }
00081 
00082 // -----------------------------------------------------------------------------
00083 // CTimeServerSession::ServiceL()
00084 // Service request from client.
00085 // -----------------------------------------------------------------------------
00086 //
00087 void CTimeServerSession::ServiceL( const RMessage2& aMessage )
00088     {
00089     switch ( aMessage.Function() )
00090         {
00091         case ETimeServRequestTime:
00092             RequestTimeL( aMessage );
00093             break;
00094 
00095         case ETimeServCancelRequestTime:
00096             if ( iWaitingForTick )
00097                 {
00098                 iMessage.Complete( KErrCancel );
00099                 iWaitingForTick = EFalse;
00100                 }
00101             aMessage.Complete( KErrNone );
00102             break;
00103 
00104         default:
00105             PanicClient( aMessage, EBadRequest );
00106         }
00107     }
00108 
00109 // -----------------------------------------------------------------------------
00110 // CTimeServerSession::RequestTimeL()
00111 // Called as a result of the client requesting the time.
00112 // -----------------------------------------------------------------------------
00113 //
00114 void CTimeServerSession::RequestTimeL( const RMessage2& aMessage )
00115     {
00116     if ( iWaitingForTick )
00117         {
00118         // We're already busy
00119         PanicClient( aMessage, EReqAlreadyPending );
00120         }
00121     else
00122         {
00123         // Keep a copy of message - for later use
00124         iMessage = aMessage ;
00125 
00126         // Start the work performed by the server
00127         // (just a wait for new second in this case)
00128         iServer.WaitForTickL();
00129         iWaitingForTick = ETrue;
00130         }
00131     }
00132 
00133 // -----------------------------------------------------------------------------
00134 // CTimeServerSession::SendTimeToClient()
00135 // Sends current time back to requesting client.
00136 // -----------------------------------------------------------------------------
00137 //
00138 void CTimeServerSession::SendTimeToClient()
00139     {
00140     if ( iWaitingForTick )
00141         {
00142         iWaitingForTick = EFalse;
00143 
00144         // Get current time to return to client
00145         TTime time;
00146         time.HomeTime();
00147 
00148         // Create a descriptor which points to the 'time' structure
00149         TPtr8 ptr( reinterpret_cast<TUint8*>( &time ), sizeof( time ),
00150                    sizeof( time ) );
00151 
00152         // Write the 'time' data back to the client        
00153         TRAPD( res, iMessage.WriteL( 0, ptr, 0 ) );
00154         
00155         if ( res != KErrNone )
00156             {
00157             PanicClient( iMessage, EBadDescriptor );
00158             }
00159         
00160         iMessage.Complete( ETimeServRequestTimeComplete );
00161         }
00162     }
00163 
00164 // -----------------------------------------------------------------------------
00165 // CTimeServerSession::PanicClient()
00166 // Causes the client thread to panic.
00167 // -----------------------------------------------------------------------------
00168 //
00169 void CTimeServerSession::PanicClient( const RMessagePtr2& aMessage, 
00170                                       TInt aPanic ) const
00171     {
00172     aMessage.Panic( KCSAsyncServer, aPanic ); // Note: this panics the client thread,
00173                                      // not server
00174     }
00175 
00176 // End of File

Generated by  doxygen 1.6.2