examples/Base/IPC/ClientServer/Simple/SimpleClient.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 // **NOTE**: The example does not demonstrate any security features - its purpose is simply
00015 // to demonstrate the basic principles of client/server interaction.
00016 //
00017 
00018 
00019 
00020 // needed for client interface
00021 #include "ClientServer.h"
00022 
00023 // needed for client (doExampleL)
00024 #include "SimpleClient.h"
00025 #include "CommonFramework.h"
00026 
00027 
00028 const TUint kDefaultMessageSlots=4;
00029 
00030 
00031 //**********************************
00032 //RCountServ
00033 //**********************************
00034 
00035 RCountServSession::RCountServSession()
00036         {
00037         }
00038 
00039 
00049 TInt RCountServSession::Connect()
00050         {
00051         TInt r=StartThread(iServerThread);
00052         if (r==KErrNone)
00053                 r=CreateSession(KCountServerName,Version(),kDefaultMessageSlots);
00054         return(r); 
00055         }
00056 
00057 
00061 TVersion RCountServSession::Version(void) const
00062         {
00063         return(TVersion(KCountServMajorVersionNumber,KCountServMinorVersionNumber,KCountServBuildVersionNumber));
00064         }
00065 
00066 
00071 TInt RCountServSession::SetFromString(const TDesC& aString)
00072         {
00073         TIpcArgs args(&aString);
00074     return SendReceive(ECountServSetFromString, args);
00075         }
00076 
00077 
00081 void RCountServSession::Increase()
00082         {
00083         SendReceive(ECountServIncrease);
00084         }
00085 
00086 
00091 void RCountServSession::IncreaseBy(TInt anInt)
00092         {
00093         TIpcArgs args(anInt);
00094         SendReceive(ECountServIncreaseBy, args);
00095         }
00096 
00097 
00101 void RCountServSession::Decrease()
00102         {
00103         SendReceive(ECountServDecrease);        
00104         }
00105 
00106 
00111 void RCountServSession::DecreaseBy(TInt anInt)
00112         {
00113         TIpcArgs args(anInt);
00114         SendReceive(ECountServDecreaseBy, args);
00115         }
00116 
00120 void RCountServSession::Reset()
00121         {
00122         SendReceive(ECountServReset);   
00123         }
00124 
00125 
00131 TInt RCountServSession::CounterValue()
00132         {
00133         TInt res=0;
00134         TPckgBuf<TInt> pckg;
00135         
00136           // Note that TPckgBuf is of type TDes8
00137         TIpcArgs args(&pckg);
00138         SendReceive(ECountServValue, args);
00139     
00140       // Extract the value returned from the server. 
00141         res = pckg();
00142         return res;
00143         }
00144 
00145 
00152 TInt RCountServSession::UnsupportedRequest()
00153         {
00154         return SendReceive(ECountServUnsupportedRequest);
00155         }
00156 
00160 void RCountServSession::BadRequest()
00161         {
00162         SendReceive(9999);
00163         }
00164 
00165 
00166 
00170 void RCountServSession::Close()
00171         {
00172         RSessionBase::Close();
00173     iServerThread.Close();      
00174         }
00175 
00176 
00177 
00178 
00186 LOCAL_C void doExampleL()
00187         {
00188         _LIT(KTxtTestingCountServer,"Testing the count server \n\n");
00189         _LIT(KTxtInitCounterWith,"\nInitialize the counter with : ");           
00190     _LIT(KTxtInitCounterFailed,"\nSetting the counter from string failed: non-numeric character detected\n");   
00191     _LIT(KTxtGetCounterValue,"\nGetting the counter value back from the server: %d \n");
00192         
00193         
00194           // Say that we are testing the count server
00195         console->Printf(KTxtTestingCountServer); 
00196         
00197           // This is our handle to the server
00198     RCountServSession ss;
00199 
00200           // Connect to the count server, starting it up if we need to 
00201           // (which in this example we do need to).
00202           // This creates a session with the server.
00203         User::LeaveIfError(ss.Connect());
00204 
00205       // At this point the server appears ready to accept requests.
00206         console->Printf(KTxtInitCounterWith);
00207 
00208       // Initialise the counter by passing an illegal string - this should
00209       // fail - but we will check anyway before reporting a failure.
00210     _LIT(KTxtIllegalString,"22h4");
00211         console->Printf(KTxtIllegalString);
00212         TInt ret = ss.SetFromString(KTxtIllegalString);
00213         if (ret==ENonNumericString)
00214             {
00215                 console->Printf(KTxtInitCounterFailed);
00216             }
00217         
00218           // Now try and initialise the counter using a legal string.
00219         console->Printf(KTxtInitCounterWith);
00220         _LIT(KTxtLegalString,"224");
00221         console->Printf(KTxtLegalString);
00222         ret = ss.SetFromString(KTxtLegalString);
00223         if (ret==ENonNumericString)
00224             {
00225                 console->Printf(KTxtInitCounterFailed);
00226             }
00227         
00228           // Now get the value back from the server, just to see what it believes
00229           // it has. We are expecting the value 224 (of course!!!).
00230         ret = ss.CounterValue();
00231         console->Printf(KTxtGetCounterValue, ret);
00232         
00233           // Now increase the counter value by the default value.
00234         _LIT(KTxt1,"\nIncrease counter (default 1)..");         
00235         console->Printf(KTxt1);
00236         ss.Increase();
00237         ret = ss.CounterValue();
00238         console->Printf(KTxtGetCounterValue, ret);
00239 
00240           // Now increase the counter value by 2.
00241         _LIT(KTxt2,"\nIncrease counter by 2..");                
00242         console->Printf(KTxt2);
00243         ss.IncreaseBy(2);
00244         ret = ss.CounterValue();
00245         console->Printf(KTxtGetCounterValue, ret);
00246         
00247           // Now decrease the counter value by the default value.       
00248         _LIT(KTxt3,"\nDecrease counter(default 1)..");          
00249         console->Printf(KTxt3);
00250         ss.Decrease();
00251     ret = ss.CounterValue();
00252         console->Printf(KTxtGetCounterValue, ret);
00253         
00254       // Now increase the counter value by 7.
00255         _LIT(KTxt4,"\nIncrease counter by 7..");                
00256         console->Printf(KTxt4);
00257         ss.IncreaseBy(7);
00258         ret = ss.CounterValue();
00259         console->Printf(KTxtGetCounterValue, ret);
00260                 
00261       // Now increase the counter value again by the default value.             
00262         _LIT(KTxt5,"\nIncrease counter(default 1)..");  
00263         console->Printf(KTxt5);
00264         ss.Increase();
00265         ret = ss.CounterValue();
00266         console->Printf(KTxtGetCounterValue, ret);
00267 
00268       // Now decrease the counter value by 3.           
00269         _LIT(KTxt6,"\nDecrease counter by 3..");        
00270         console->Printf(KTxt6);
00271         ss.DecreaseBy(3);
00272         ret = ss.CounterValue();
00273         console->Printf(KTxtGetCounterValue, ret);
00274         
00275           // Reset counter value
00276         _LIT(KTxt7,"\nReseting counter value to 0..");
00277         console->Printf(KTxt7);
00278         ss.Reset();
00279         ret = ss.CounterValue();
00280         console->Printf(KTxtGetCounterValue, ret);
00281 
00282         
00283           // Call API function which is not implemented in the server
00284         _LIT(KTxt8,"\nAbout to call the unsupported function Stop()..");
00285         console->Printf(KTxt8);
00286         ret = ss.UnsupportedRequest();
00287         _LIT(KTxt9,"\nSorry, UnsupportedRequest() is not supported\n");
00288         if (ret==KErrNotSupported)
00289             {
00290                 console->Printf(KTxt9);
00291             }
00292                 
00293      
00294       // This request will panic this client so do not remove the
00295       // following comment unless you want to try it.
00296 //      ss.BadRequest();
00297    
00298    
00299           // Close the sesssion with the count server.
00300         ss.Close();
00301         }

Generated by  doxygen 1.6.2