examples/Base/IPC/AdvancedClientServerExample/ProcessServer/src/processserversession.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 "processserversession.h"
00032 
00033 
00034 /*****************CProcessServerSession**********************/
00038 CProcessServerSession* CProcessServerSession::NewL(CProcessServer& aServer)
00039         {
00040         CProcessServerSession* s = new(ELeave) CProcessServerSession(aServer);
00041         CleanupStack::PushL(s);
00042         s->ConstructL();
00043         CleanupStack::Pop();
00044         return s;       
00045         }
00049 CProcessServerSession::CProcessServerSession(CProcessServer& aServer)
00050 :iServer(aServer)
00051         {
00052         
00053         }
00054 
00055 void CProcessServerSession::ConstructL()
00056         {
00057         }
00061 CProcessServerSession::~CProcessServerSession()
00062         {
00063         delete iAsyncRequestHandler;            
00064         iServer.CloseLogicalChannel();
00065         iServer.DecrementRefCount();
00066         }
00071 void CProcessServerSession::CreateL()
00072 //
00073 // 2nd phase constructor for sessions - called by the CServer framework
00074 //
00075         {
00076         iServer.IncrementRefCount();            
00077         //load dummy ldd
00078         User::LeaveIfError(iServer.LoadDevice());
00079         iAsyncRequestHandler = CAsyncHandler::NewL(iServer);    
00080         }
00084 void CProcessServerSession::ServiceL(const RMessage2& aMessage)
00085         {
00086         TInt r = KErrNone;
00087         switch(aMessage.Function())
00088                 {                               
00089                 case ELoadDeviceDriver:
00090                         aMessage.Complete(iServer.LoadDevice());
00091                         break;
00092                 
00093                 case EOpenDriver:
00094                         aMessage.Complete(iServer.OpenLogicalChannel());
00095                         break;
00096                         
00097                 case EDummyLDDSendData: //async
00098                         r = iAsyncRequestHandler->ProcessRequest(aMessage);
00099                         if (r!=KErrNone)
00100                                 aMessage.Complete(r);
00101                         break;
00102                         
00103                 case EDummyLDDSendDataCancel: //cancel async
00104                         iAsyncRequestHandler->Cancel(); 
00105                         aMessage.Complete(KErrNone); 
00106                         break;
00107                         
00108                 case EUnloadDeviceDriver:
00109                         aMessage.Complete(iServer.UnloadDevice());
00110                         break;
00111                         
00112                 default:
00113                         User::Leave(KErrNotSupported);
00114                         break;
00115                 }
00116 
00117         }
00118                 
00119 
00120 
00121 /*****************CAsyncHandler**********************/
00125 CProcessServerSession::CAsyncHandler* CProcessServerSession::CAsyncHandler::NewL(CProcessServer& aServer)
00126         {
00127         CAsyncHandler* self = new(ELeave) CAsyncHandler(aServer);
00128         CleanupStack::PushL(self);
00129         self ->ConstructL();
00130         CleanupStack::Pop();
00131         return self;
00132         }
00136 CProcessServerSession::CAsyncHandler::~CAsyncHandler()
00137         {
00138         Cancel();
00139         iMessageArray.Close();
00140         }
00144 void CProcessServerSession::CAsyncHandler::RunL()
00145         {
00146         // complete the request
00147         Complete(iStatus.Int());
00148         
00149         //continue to execute next request if there is any
00150         ExecuteFirstRequestInArray();
00151         }
00155 void CProcessServerSession::CAsyncHandler::DoCancel()
00156         {
00157         iServer.CancelSendData();
00158         // complete the request
00159         Complete(iStatus.Int());
00160         }
00164 CProcessServerSession::CAsyncHandler::CAsyncHandler(CProcessServer& aServer)
00165         :CActive(EPriorityStandard), iServer(aServer)
00166         {
00167         CActiveScheduler::Add(this);
00168         }
00169 
00170 void CProcessServerSession::CAsyncHandler::ConstructL()
00171         {
00172         
00173         }
00179 TInt CProcessServerSession::CAsyncHandler::ProcessRequest(const RMessage2& aMessage)
00180         {
00181         TInt ret;
00182         //store message
00183         TMessage newMessage;
00184         newMessage.Message() = aMessage;
00185         ret= iMessageArray.Append(newMessage);
00186         if (ret != KErrNone)
00187                 return ret;
00188         
00189         // kick off the first request in the array when there is only one in the array 
00190         if (iMessageArray.Count() == 1) 
00191                 ret= ExecuteFirstRequestInArray();
00192 
00193         return ret;
00194         }
00199 TInt CProcessServerSession::CAsyncHandler::ExecuteFirstRequestInArray()
00200         {
00201         TInt r = KErrNone;
00202         if (iMessageArray.Count() != 0)
00203                 {
00204                 const RMessage2& message = iMessageArray[0].Message();
00205                 switch (message.Function())
00206                         {
00207                         case EDummyLDDSendData:
00208                                 {
00209                                 TBuf8<KMaxMessageLen> sendObject;
00210                                 r= message.Read(0, sendObject);
00211                                 if (r == KErrNone)
00212                                         {
00213                                         r = iServer.SendDataToDevice(iStatus, sendObject);
00214                                         if (r==KErrNone)
00215                                                 SetActive();
00216                                         }
00217                                 }
00218                                 break;
00219                         
00220                         default:
00221                                 break;
00222                         }
00223                 
00224                 }
00225         return r; 
00226         }
00230 void CProcessServerSession::CAsyncHandler::Complete(TInt aResult)
00231         {
00232         iMessageArray[0].Message().Complete(aResult);
00233         iMessageArray.Remove(0);
00234         iServer.UpdateDriverState(CProcessServer::ELogicalChannelOpened);
00235         }
00236 
00237 /***************TMessage****************/
00241 const RMessage2& CProcessServerSession::CAsyncHandler::TMessage::Message() const
00242         {
00243         return iMessage;
00244         }
00245         
00246 RMessage2& CProcessServerSession::CAsyncHandler::TMessage::Message()
00247         {
00248         return iMessage; 
00249         }
00250 
00251 //EOF

Generated by  doxygen 1.6.2