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

Generated by  doxygen 1.6.2