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

00001 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include "threadserversession.h"
00017 
00018 
00019 /*****************CThreadServerSession**********************/
00023 CThreadServerSession* CThreadServerSession::NewL(CThreadServer& aServer)
00024         {
00025         CThreadServerSession* s = new(ELeave) CThreadServerSession(aServer);
00026         CleanupStack::PushL(s);
00027         s->ConstructL();
00028         CleanupStack::Pop();
00029         return s;       
00030         }
00034 CThreadServerSession::CThreadServerSession(CThreadServer& aServer)
00035 :iServer(aServer)
00036         {
00037         }
00038 void CThreadServerSession::ConstructL()
00039         {
00040         }
00044 CThreadServerSession::~CThreadServerSession()
00045         {
00046         delete iAsyncRequestHandler;            
00047         iServer.CloseLogicalChannel();
00048         iServer.DecrementRefCount();
00049         }
00054 void CThreadServerSession::CreateL()
00055 //
00056 // 2nd phase construct for sessions - called by the CServer framework
00057 //
00058         {
00059         iServer.IncrementRefCount();            
00060         //load dummy ldd
00061         User::LeaveIfError(iServer.LoadDevice());
00062         iAsyncRequestHandler = CAsyncHandler::NewL(iServer);    
00063         }
00067 void CThreadServerSession::ServiceL(const RMessage2& aMessage)
00068         {
00069         TInt r = KErrNone;
00070         switch(aMessage.Function())
00071                 {                               
00072                 case EThreadServerLoadDeviceDriver:
00073                         aMessage.Complete(iServer.LoadDevice());
00074                         break;
00075                 
00076                 case EThreadServerOpenDriver:
00077                         aMessage.Complete(iServer.OpenLogicalChannel());
00078                         break;
00079                         
00080                 case EThreadServerSendData: //async
00081                         r = iAsyncRequestHandler->ProcessRequest(aMessage);
00082                         if (r!=KErrNone)
00083                                 aMessage.Complete(r);
00084                         break;
00085                         
00086                 case EThreadServerSendDataCancel: //cancel async
00087                         iAsyncRequestHandler->Cancel(); 
00088                         aMessage.Complete(KErrNone); 
00089                         break;
00090                         
00091                 case EThreadServerUnloadDeviceDriver:
00092                         aMessage.Complete(iServer.UnloadDevice());
00093                         break;
00094                         
00095                 default:
00096                         User::Leave(KErrNotSupported);
00097                         break;
00098                 }
00099 
00100         }
00101 /*****************CAsyncHandler**********************/
00105 CThreadServerSession::CAsyncHandler* CThreadServerSession::CAsyncHandler::NewL(CThreadServer& aServer)
00106         {
00107         CAsyncHandler* self = new(ELeave) CAsyncHandler(aServer);
00108         CleanupStack::PushL(self);
00109         self ->ConstructL();
00110         CleanupStack::Pop();
00111         return self;
00112         }
00116 CThreadServerSession::CAsyncHandler::~CAsyncHandler()
00117         {
00118         Cancel();
00119         iMessageArray.Close();
00120         }
00124 void CThreadServerSession::CAsyncHandler::RunL()
00125         {
00126         // complete the request
00127         Complete(iStatus.Int());
00128         
00129         //continue to execute next request if there is any
00130         ExecuteFirstRequestInArray();
00131         }
00135 void CThreadServerSession::CAsyncHandler::DoCancel()
00136         {
00137         iServer.CancelSendData();
00138         // complete the request
00139         Complete(iStatus.Int());
00140         }
00144 CThreadServerSession::CAsyncHandler::CAsyncHandler(CThreadServer& aServer)
00145         :CActive(EPriorityStandard), iServer(aServer)
00146         {
00147         CActiveScheduler::Add(this);
00148         }
00149 
00150 void CThreadServerSession::CAsyncHandler::ConstructL()
00151         {
00152         }
00158 TInt CThreadServerSession::CAsyncHandler::ProcessRequest(const RMessage2& aMessage)
00159         {
00160         TInt ret;
00161         //store message
00162         TMessage newMessage;
00163         newMessage.Message() = aMessage;
00164         ret= iMessageArray.Append(newMessage);
00165         if (ret != KErrNone)
00166                 return ret;
00167         
00168         // only kick off the first request in the array when there is only one in the array
00169         if (iMessageArray.Count() == 1) 
00170                 ret = ExecuteFirstRequestInArray();
00171 
00172         return ret;
00173         }
00178 TInt CThreadServerSession::CAsyncHandler::ExecuteFirstRequestInArray()
00179         {
00180         TInt r = KErrNone;
00181         if (iMessageArray.Count() != 0)
00182                 {
00183                 const RMessage2& message = iMessageArray[0].Message();
00184                 switch (message.Function())
00185                         {
00186                         case EThreadServerSendData:
00187                                 {
00188                                 TBuf8<KThreadServerMaxMessageLen> sendObject;
00189                                 r= message.Read(0, sendObject);
00190                                 if (r == KErrNone)
00191                                         {
00192                                         r = iServer.SendDataToDevice(iStatus, sendObject);
00193                                         if (r==KErrNone)
00194                                                 SetActive();
00195                                         }
00196                                 }
00197                                 break;
00198                                 
00199                         default:
00200                                 break;
00201                         }
00202                 }
00203         return r; 
00204         }
00208 void CThreadServerSession::CAsyncHandler::Complete(TInt aResult)
00209         {
00210         iMessageArray[0].Message().Complete(aResult);
00211         iMessageArray.Remove(0);
00212         iServer.UpdateDriverState(CThreadServer::ELogicalChannelOpened);
00213         }
00214 
00215 /***************TMessage****************/
00219 const RMessage2& CThreadServerSession::CAsyncHandler::TMessage::Message() const
00220         {
00221         return iMessage;
00222         }
00223 RMessage2& CThreadServerSession::CAsyncHandler::TMessage::Message()
00224         {
00225         return iMessage; 
00226         }
00227 
00228 //EOF

Generated by  doxygen 1.6.2