00001 /* 00002 * ============================================================================== 00003 * Name : csasyncrequesthandler.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 // INCLUDE FILES 00017 #include <e32svr.h> 00018 00019 #include "CSAsync.pan" 00020 #include "CSAsyncRequestHandler.h" 00021 #include "ClientServerCommon.h" 00022 #include "AsyncTimeObserver.h" 00023 00024 // ========================= MEMBER FUNCTIONS ================================== 00025 00026 // ----------------------------------------------------------------------------- 00027 // CCSAsyncRequestHandler::NewL() 00028 // Two-phased constructor. 00029 // ----------------------------------------------------------------------------- 00030 CCSAsyncRequestHandler* CCSAsyncRequestHandler::NewL( 00031 MAsyncTimeObserver& aObserver ) 00032 { 00033 CCSAsyncRequestHandler* self = NewLC( aObserver ); 00034 CleanupStack::Pop( self ); 00035 return( self ) ; 00036 } 00037 00038 // ----------------------------------------------------------------------------- 00039 // CCSAsyncRequestHandler::NewLC() 00040 // Two-phased constructor. 00041 // ----------------------------------------------------------------------------- 00042 CCSAsyncRequestHandler* CCSAsyncRequestHandler::NewLC( 00043 MAsyncTimeObserver& aObserver ) 00044 { 00045 CCSAsyncRequestHandler* self = 00046 new ( ELeave ) CCSAsyncRequestHandler( aObserver ); 00047 CleanupStack::PushL( self ); 00048 self->ConstructL(); 00049 return self; 00050 } 00051 00052 // ----------------------------------------------------------------------------- 00053 // CCSAsyncRequestHandler::ConstructL() 00054 // Symbian 2nd phase constructor can leave. 00055 // ----------------------------------------------------------------------------- 00056 // 00057 void CCSAsyncRequestHandler::ConstructL() 00058 { 00059 User::LeaveIfError( iSession.Connect() ); 00060 } 00061 00062 // ----------------------------------------------------------------------------- 00063 // CCSAsyncRequestHandler::CCSAsyncRequestHandler() 00064 // C++ default constructor can NOT contain any code, that might leave. 00065 // ----------------------------------------------------------------------------- 00066 CCSAsyncRequestHandler::CCSAsyncRequestHandler( MAsyncTimeObserver& aObserver ) 00067 : CActive( EPriorityStandard ), iObserver( aObserver ) 00068 { 00069 CActiveScheduler::Add( this ); 00070 } 00071 00072 // ----------------------------------------------------------------------------- 00073 // CCSAsyncRequestHandler::~CCSAsyncRequestHandler() 00074 // Destructor. 00075 // ----------------------------------------------------------------------------- 00076 // 00077 CCSAsyncRequestHandler::~CCSAsyncRequestHandler() 00078 { 00079 Cancel(); // Causes call to DoCancel() 00080 iSession.Close(); 00081 } 00082 00083 // ----------------------------------------------------------------------------- 00084 // CCSAsyncRequestHandler::RequestTime() 00085 // Sends a request to the server for an update to the time. 00086 // ----------------------------------------------------------------------------- 00087 // 00088 void CCSAsyncRequestHandler::RequestTime() 00089 { 00090 if ( !IsActive() ) 00091 { 00092 iSession.RequestTime( iTime, iStatus ); 00093 SetActive(); 00094 } 00095 } 00096 00097 // ----------------------------------------------------------------------------- 00098 // CCSAsyncRequestHandler::CancelRequest() 00099 // Cancels an outstanding request. 00100 // ----------------------------------------------------------------------------- 00101 // 00102 void CCSAsyncRequestHandler::CancelRequest() 00103 { 00104 Cancel() ; // Causes call to DoCancel() 00105 } 00106 00107 // ----------------------------------------------------------------------------- 00108 // CCSAsyncRequestHandler::RunL() 00109 // Invoked to handle responses from the server. 00110 // ----------------------------------------------------------------------------- 00111 // 00112 void CCSAsyncRequestHandler::RunL() 00113 { 00114 switch ( iStatus.Int() ) 00115 { 00116 case ETimeServRequestTimeComplete: 00117 // The server has completed the request, signalled the client 00118 // thread and the clients active scheduler runs the active object. 00119 // Now do something with it 00120 iObserver.HandleTimeUpdate(); 00121 RequestTime(); // Add this line to make the clock keep ticking 00122 break ; 00123 00124 case KErrCancel: 00125 // The request was canceled 00126 break ; 00127 00128 case KErrNotReady: 00129 // We requested a new time before completing the previous request 00130 default: 00131 User::Panic( KCSAsyncClient, ECSAsyncBadState ); // Unexpected error 00132 break; 00133 } 00134 } 00135 00136 // ----------------------------------------------------------------------------- 00137 // CCSAsyncRequestHandler::DoCancel() 00138 // Cancels any outstanding operation. 00139 // ----------------------------------------------------------------------------- 00140 // 00141 void CCSAsyncRequestHandler::DoCancel() 00142 { 00143 iSession.CancelRequestTime(); 00144 } 00145 00146 // ----------------------------------------------------------------------------- 00147 // CCSAsyncRequestHandler::Time() const 00148 // Gets a copy of the last time received from the server. 00149 // ----------------------------------------------------------------------------- 00150 // 00151 TTime CCSAsyncRequestHandler::Time() const 00152 { 00153 return iTime; 00154 } 00155 00156 // End of File