examples/Base/IPC/AdvancedClientServerExample/ThreadServer/src/threadserversession.cpp

00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include "threadserversession.h"
00032 
00033 
00034 /*****************CThreadServerSession**********************/
00038 CThreadServerSession* CThreadServerSession::NewL(CThreadServer& aServer)
00039         {
00040         CThreadServerSession* s = new(ELeave) CThreadServerSession(aServer);
00041         CleanupStack::PushL(s);
00042         s->ConstructL();
00043         CleanupStack::Pop();
00044         return s;       
00045         }
00049 CThreadServerSession::CThreadServerSession(CThreadServer& aServer)
00050 :iServer(aServer)
00051         {
00052         }
00053 void CThreadServerSession::ConstructL()
00054         {
00055         }
00059 CThreadServerSession::~CThreadServerSession()
00060         {
00061         delete iAsyncRequestHandler;            
00062         iServer.CloseLogicalChannel();
00063         iServer.DecrementRefCount();
00064         }
00069 void CThreadServerSession::CreateL()
00070 //
00071 // 2nd phase construct for sessions - called by the CServer framework
00072 //
00073         {
00074         iServer.IncrementRefCount();            
00075         //load dummy ldd
00076         User::LeaveIfError(iServer.LoadDevice());
00077         iAsyncRequestHandler = CAsyncHandler::NewL(iServer);    
00078         }
00082 void CThreadServerSession::ServiceL(const RMessage2& aMessage)
00083         {
00084         TInt r = KErrNone;
00085         switch(aMessage.Function())
00086                 {                               
00087                 case EThreadServerLoadDeviceDriver:
00088                         aMessage.Complete(iServer.LoadDevice());
00089                         break;
00090                 
00091                 case EThreadServerOpenDriver:
00092                         aMessage.Complete(iServer.OpenLogicalChannel());
00093                         break;
00094                         
00095                 case EThreadServerSendData: //async
00096                         r = iAsyncRequestHandler->ProcessRequest(aMessage);
00097                         if (r!=KErrNone)
00098                                 aMessage.Complete(r);
00099                         break;
00100                         
00101                 case EThreadServerSendDataCancel: //cancel async
00102                         iAsyncRequestHandler->Cancel(); 
00103                         aMessage.Complete(KErrNone); 
00104                         break;
00105                         
00106                 case EThreadServerUnloadDeviceDriver:
00107                         aMessage.Complete(iServer.UnloadDevice());
00108                         break;
00109                         
00110                 default:
00111                         User::Leave(KErrNotSupported);
00112                         break;
00113                 }
00114 
00115         }
00116 /*****************CAsyncHandler**********************/
00120 CThreadServerSession::CAsyncHandler* CThreadServerSession::CAsyncHandler::NewL(CThreadServer& aServer)
00121         {
00122         CAsyncHandler* self = new(ELeave) CAsyncHandler(aServer);
00123         CleanupStack::PushL(self);
00124         self ->ConstructL();
00125         CleanupStack::Pop();
00126         return self;
00127         }
00131 CThreadServerSession::CAsyncHandler::~CAsyncHandler()
00132         {
00133         Cancel();
00134         iMessageArray.Close();
00135         }
00139 void CThreadServerSession::CAsyncHandler::RunL()
00140         {
00141         // complete the request
00142         Complete(iStatus.Int());
00143         
00144         //continue to execute next request if there is any
00145         ExecuteFirstRequestInArray();
00146         }
00150 void CThreadServerSession::CAsyncHandler::DoCancel()
00151         {
00152         iServer.CancelSendData();
00153         // complete the request
00154         Complete(iStatus.Int());
00155         }
00159 CThreadServerSession::CAsyncHandler::CAsyncHandler(CThreadServer& aServer)
00160         :CActive(EPriorityStandard), iServer(aServer)
00161         {
00162         CActiveScheduler::Add(this);
00163         }
00164 
00165 void CThreadServerSession::CAsyncHandler::ConstructL()
00166         {
00167         }
00173 TInt CThreadServerSession::CAsyncHandler::ProcessRequest(const RMessage2& aMessage)
00174         {
00175         TInt ret;
00176         //store message
00177         TMessage newMessage;
00178         newMessage.Message() = aMessage;
00179         ret= iMessageArray.Append(newMessage);
00180         if (ret != KErrNone)
00181                 return ret;
00182         
00183         // only kick off the first request in the array when there is only one in the array
00184         if (iMessageArray.Count() == 1) 
00185                 ret = ExecuteFirstRequestInArray();
00186 
00187         return ret;
00188         }
00193 TInt CThreadServerSession::CAsyncHandler::ExecuteFirstRequestInArray()
00194         {
00195         TInt r = KErrNone;
00196         if (iMessageArray.Count() != 0)
00197                 {
00198                 const RMessage2& message = iMessageArray[0].Message();
00199                 switch (message.Function())
00200                         {
00201                         case EThreadServerSendData:
00202                                 {
00203                                 TBuf8<KThreadServerMaxMessageLen> sendObject;
00204                                 r= message.Read(0, sendObject);
00205                                 if (r == KErrNone)
00206                                         {
00207                                         r = iServer.SendDataToDevice(iStatus, sendObject);
00208                                         if (r==KErrNone)
00209                                                 SetActive();
00210                                         }
00211                                 }
00212                                 break;
00213                                 
00214                         default:
00215                                 break;
00216                         }
00217                 }
00218         return r; 
00219         }
00223 void CThreadServerSession::CAsyncHandler::Complete(TInt aResult)
00224         {
00225         iMessageArray[0].Message().Complete(aResult);
00226         iMessageArray.Remove(0);
00227         iServer.UpdateDriverState(CThreadServer::ELogicalChannelOpened);
00228         }
00229 
00230 /***************TMessage****************/
00234 const RMessage2& CThreadServerSession::CAsyncHandler::TMessage::Message() const
00235         {
00236         return iMessage;
00237         }
00238 RMessage2& CThreadServerSession::CAsyncHandler::TMessage::Message()
00239         {
00240         return iMessage; 
00241         }
00242 
00243 //EOF

Generated by  doxygen 1.6.2