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

00001 /*
00002 Copyright (c) 2000-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: ComplexServer.cpp  
00028 */
00029 
00030 
00031 #include "ComplexClientAndServer.h"
00032 #include "ComplexServer.h"
00033 #include <e32svr.h>
00034 #include <e32uid.h>
00035 
00036 
00037 //*******************************
00038 //CCountSession - implementations
00039 //*******************************
00040 
00044 CCountSession::CCountSession()
00045         {
00046         }
00047 
00048 
00062 void CCountSession::CreateL()
00063         {
00064           // Create new object index
00065         iCountersObjectIndex=CObjectIx::NewL();
00066         
00067           // Initialize the object container
00068           // using the object container index in the server.
00069         iContainer=((CCountServer*)Server())->NewContainerL();
00070         }
00071 
00072 
00080 void CCountSession::CloseSession() 
00081         {
00082           // Deletes the object index.
00083         delete iCountersObjectIndex;
00084         iCountersObjectIndex = NULL;
00085         
00086           // Removes object container from the container list    
00087         ((CCountServer*)Server())->RemoveContainer(iContainer);
00088         iContainer = NULL;
00089         }
00090 
00091 
00097 CCountSubSession* CCountSession::CounterFromHandle(const RMessage2& aMessage,TInt aHandle)
00098     {
00099         CCountSubSession* counter = (CCountSubSession*)iCountersObjectIndex->At(aHandle);
00100         if (counter == NULL)
00101             {
00102                 PanicClient(aMessage, EBadSubsessionHandle); 
00103             }
00104         return counter;
00105     }
00106 
00107 
00108 
00118 void CCountSession::ServiceL(const RMessage2& aMessage)
00119         {
00120         TRAPD(err,DispatchMessageL(aMessage));
00121         aMessage.Complete(err);
00122         }
00123 
00124 
00131 void CCountSession::DispatchMessageL(const RMessage2& aMessage)
00132         {
00133           // First check for session-relative requests
00134         switch (aMessage.Function())
00135                 {
00136         case ECountServCreateSubSession:
00137                 NewCounterL(aMessage);
00138                 return;
00139         case ECountServCloseSession:
00140                 CloseSession();
00141                 return;
00142         case ECountServResourceCount:
00143                 NumResourcesL(aMessage);
00144                 return; 
00145                 }
00146                 
00147           // All other function codes must be subsession relative.
00148           // We need to find the appropriate server side subsession
00149           // i.e. the CCountSubSession object. 
00150           // The handle value is passed as the 4th aregument.
00151     CCountSubSession* counter=CounterFromHandle(aMessage,aMessage.Int3());
00152         switch (aMessage.Function())
00153         {
00154         case ECountServInitSubSession:
00155                 counter->SetFromStringL(aMessage);
00156                 return; 
00157         case ECountServCloseSubSession:
00158              DeleteCounter(aMessage.Int3());
00159              return;
00160         case ECountServIncrease:
00161                 counter->Increase();
00162                 return;
00163         case ECountServIncreaseBy:
00164                 counter->IncreaseBy(aMessage);
00165                 return;
00166         case ECountServDecrease:
00167                 counter->Decrease();
00168                 return;
00169         case ECountServDecreaseBy:
00170                 counter->DecreaseBy(aMessage);
00171                 return;
00172         case ECountServReset:
00173                 counter->Reset();
00174                 return;
00175         case ECountServValue:
00176                 counter->CounterValueL(aMessage);
00177                 return;
00178         default:
00179                 PanicClient(aMessage,EBadRequest);
00180                 return;
00181         }
00182         }
00183 
00184 
00192 void CCountSession::NewCounterL(const RMessage2& aMessage)
00193         {
00194           // make a new counter object
00195         CCountSubSession* counter= new (ELeave) CCountSubSession(this);
00196          
00197           // add the CCountSubSession object to 
00198           // this subsession's object container
00199           // to gererate a unique id
00200         iContainer->AddL(counter);
00201         
00202           // Add the object to object index; this returns
00203           // a unique handle so that we can find the object
00204           // again laterit later.
00205         TInt handle=iCountersObjectIndex->AddL(counter);
00206 
00207       // Write the handle value back to the client.
00208       // NB It's not obvious but the handle value must be passed
00209       // back as the 4th parameter (i.e. parameter number 3 on
00210       // a scale of 0 to 3). 
00211       // The arguments that are passed across are actually
00212       // set up by RSubSessionBase::DoCreateSubSession().
00213       // If you pass your own arguments into a call
00214       // to RSubSessionBase::CreateSubSession(), which calls DoCreateSubSession, 
00215       // then only the first three are picked up - the 4th is reserved for the
00216       // the subsession handle.
00217         TPckgBuf<TInt> handlePckg(handle);
00218         TRAPD(res,aMessage.WriteL(3,handlePckg));
00219         if (res!=KErrNone)
00220                 {
00221                 iCountersObjectIndex->Remove(handle);
00222                 PanicClient(aMessage,EBadDescriptor);
00223                 return;
00224                 }
00225                 
00226           // We now have another "resource"
00227         iResourceCount++;
00228         }
00229 
00230 
00231 
00232 
00236 void CCountSession::DeleteCounter(TInt aHandle)
00237         {
00238         
00239           // This will delete the CCountSubSession object; the object is
00240           // reference counted, and removing the handle causes the object to be closed
00241           // [closing reduces the access count - the object is deleted if the access
00242           //  count reaches zero etc].
00243         iCountersObjectIndex->Remove(aHandle); 
00244           // decrement resource count
00245         iResourceCount--;
00246         }
00247 
00248 
00249 // return the number of resources owned by the session
00250 // required by CSession if derived class implements resource
00251 // mark-start and mark-end protocol
00252 TInt CCountSession::CountResources()
00253         {
00254         return iResourceCount;
00255         }
00256 
00257 
00262 void CCountSession::NumResourcesL(const RMessage2& aMessage)
00263         {
00264         TPckgBuf<TInt> resourcesPckg(iResourceCount);
00265         aMessage.WriteL(0,resourcesPckg);
00266         }
00267 
00268 
00272 void CCountSession::PanicClient(const RMessage2& aMessage,TInt aPanic) const
00273         {
00274         _LIT(KTxtServer,"CountServ server");
00275         aMessage.Panic(KTxtServer,aPanic);
00276         }
00277 

Generated by  doxygen 1.6.2