examples/Base/IPC/ClientServer/Complex/ComplexServerCCountSession.cpp

00001 // Copyright (c) 2000-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 // ComplexServer.cpp
00015 //
00016 
00017 #include "ComplexClientAndServer.h"
00018 #include "ComplexServer.h"
00019 #include <e32svr.h>
00020 #include <e32uid.h>
00021 
00022 
00023 //*******************************
00024 //CCountSession - implementations
00025 //*******************************
00026 
00030 CCountSession::CCountSession()
00031         {
00032         }
00033 
00034 
00048 void CCountSession::CreateL()
00049         {
00050           // Create new object index
00051         iCountersObjectIndex=CObjectIx::NewL();
00052         
00053           // Initialize the object container
00054           // using the object container index in the server.
00055         iContainer=((CCountServer*)Server())->NewContainerL();
00056         }
00057 
00058 
00066 void CCountSession::CloseSession() 
00067         {
00068           // Deletes the object index.
00069         delete iCountersObjectIndex;
00070         iCountersObjectIndex = NULL;
00071         
00072           // Removes object container from the container list    
00073         ((CCountServer*)Server())->RemoveContainer(iContainer);
00074         iContainer = NULL;
00075         }
00076 
00077 
00083 CCountSubSession* CCountSession::CounterFromHandle(const RMessage2& aMessage,TInt aHandle)
00084     {
00085         CCountSubSession* counter = (CCountSubSession*)iCountersObjectIndex->At(aHandle);
00086         if (counter == NULL)
00087             {
00088                 PanicClient(aMessage, EBadSubsessionHandle); 
00089             }
00090         return counter;
00091     }
00092 
00093 
00094 
00104 void CCountSession::ServiceL(const RMessage2& aMessage)
00105         {
00106         TRAPD(err,DispatchMessageL(aMessage));
00107         aMessage.Complete(err);
00108         }
00109 
00110 
00117 void CCountSession::DispatchMessageL(const RMessage2& aMessage)
00118         {
00119           // First check for session-relative requests
00120         switch (aMessage.Function())
00121                 {
00122         case ECountServCreateSubSession:
00123                 NewCounterL(aMessage);
00124                 return;
00125         case ECountServCloseSession:
00126                 CloseSession();
00127                 return;
00128         case ECountServResourceCount:
00129                 NumResourcesL(aMessage);
00130                 return; 
00131                 }
00132                 
00133           // All other function codes must be subsession relative.
00134           // We need to find the appropriate server side subsession
00135           // i.e. the CCountSubSession object. 
00136           // The handle value is passed as the 4th aregument.
00137     CCountSubSession* counter=CounterFromHandle(aMessage,aMessage.Int3());
00138         switch (aMessage.Function())
00139         {
00140         case ECountServInitSubSession:
00141                 counter->SetFromStringL(aMessage);
00142                 return; 
00143         case ECountServCloseSubSession:
00144              DeleteCounter(aMessage.Int3());
00145              return;
00146         case ECountServIncrease:
00147                 counter->Increase();
00148                 return;
00149         case ECountServIncreaseBy:
00150                 counter->IncreaseBy(aMessage);
00151                 return;
00152         case ECountServDecrease:
00153                 counter->Decrease();
00154                 return;
00155         case ECountServDecreaseBy:
00156                 counter->DecreaseBy(aMessage);
00157                 return;
00158         case ECountServReset:
00159                 counter->Reset();
00160                 return;
00161         case ECountServValue:
00162                 counter->CounterValueL(aMessage);
00163                 return;
00164         default:
00165                 PanicClient(aMessage,EBadRequest);
00166                 return;
00167         }
00168         }
00169 
00170 
00178 void CCountSession::NewCounterL(const RMessage2& aMessage)
00179         {
00180           // make a new counter object
00181         CCountSubSession* counter= new (ELeave) CCountSubSession(this);
00182          
00183           // add the CCountSubSession object to 
00184           // this subsession's object container
00185           // to gererate a unique id
00186         iContainer->AddL(counter);
00187         
00188           // Add the object to object index; this returns
00189           // a unique handle so that we can find the object
00190           // again laterit later.
00191         TInt handle=iCountersObjectIndex->AddL(counter);
00192 
00193       // Write the handle value back to the client.
00194       // NB It's not obvious but the handle value must be passed
00195       // back as the 4th parameter (i.e. parameter number 3 on
00196       // a scale of 0 to 3). 
00197       // The arguments that are passed across are actually
00198       // set up by RSubSessionBase::DoCreateSubSession().
00199       // If you pass your own arguments into a call
00200       // to RSubSessionBase::CreateSubSession(), which calls DoCreateSubSession, 
00201       // then only the first three are picked up - the 4th is reserved for the
00202       // the subsession handle.
00203         TPckgBuf<TInt> handlePckg(handle);
00204         TRAPD(res,aMessage.WriteL(3,handlePckg));
00205         if (res!=KErrNone)
00206                 {
00207                 iCountersObjectIndex->Remove(handle);
00208                 PanicClient(aMessage,EBadDescriptor);
00209                 return;
00210                 }
00211                 
00212           // We now have another "resource"
00213         iResourceCount++;
00214         }
00215 
00216 
00217 
00218 
00222 void CCountSession::DeleteCounter(TInt aHandle)
00223         {
00224         
00225           // This will delete the CCountSubSession object; the object is
00226           // reference counted, and removing the handle causes the object to be closed
00227           // [closing reduces the access count - the object is deleted if the access
00228           //  count reaches zero etc].
00229         iCountersObjectIndex->Remove(aHandle); 
00230           // decrement resource count
00231         iResourceCount--;
00232         }
00233 
00234 
00235 // return the number of resources owned by the session
00236 // required by CSession if derived class implements resource
00237 // mark-start and mark-end protocol
00238 TInt CCountSession::CountResources()
00239         {
00240         return iResourceCount;
00241         }
00242 
00243 
00248 void CCountSession::NumResourcesL(const RMessage2& aMessage)
00249         {
00250         TPckgBuf<TInt> resourcesPckg(iResourceCount);
00251         aMessage.WriteL(0,resourcesPckg);
00252         }
00253 
00254 
00258 void CCountSession::PanicClient(const RMessage2& aMessage,TInt aPanic) const
00259         {
00260         _LIT(KTxtServer,"CountServ server");
00261         aMessage.Panic(KTxtServer,aPanic);
00262         }
00263 

Generated by  doxygen 1.6.2