examples/SDKExamples/ClientServerAsync/client/src/csasyncrequesthandler.cpp

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

Generated by  doxygen 1.6.2