examples/Base/IPC/ClientServer/Simple/SimpleClient.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:
00028 **NOTE**: The example does not demonstrate any security features - its purpose is simply
00029 to demonstrate the basic principles of client/server interaction.  
00030 */
00031 
00032 
00033 
00034 
00035 // needed for client interface
00036 #include "ClientServer.h"
00037 
00038 // needed for client (doExampleL)
00039 #include "SimpleClient.h"
00040 #include "CommonFramework.h"
00041 
00042 
00043 const TUint kDefaultMessageSlots=4;
00044 
00045 
00046 //**********************************
00047 //RCountServ
00048 //**********************************
00049 
00050 RCountServSession::RCountServSession()
00051         {
00052         }
00053 
00054 
00064 TInt RCountServSession::Connect()
00065         {
00066         TInt r=StartThread(iServerThread);
00067         if (r==KErrNone)
00068                 r=CreateSession(KCountServerName,Version(),kDefaultMessageSlots);
00069         return(r); 
00070         }
00071 
00072 
00076 TVersion RCountServSession::Version(void) const
00077         {
00078         return(TVersion(KCountServMajorVersionNumber,KCountServMinorVersionNumber,KCountServBuildVersionNumber));
00079         }
00080 
00081 
00086 TInt RCountServSession::SetFromString(const TDesC& aString)
00087         {
00088         TIpcArgs args(&aString);
00089     return SendReceive(ECountServSetFromString, args);
00090         }
00091 
00092 
00096 void RCountServSession::Increase()
00097         {
00098         SendReceive(ECountServIncrease);
00099         }
00100 
00101 
00106 void RCountServSession::IncreaseBy(TInt anInt)
00107         {
00108         TIpcArgs args(anInt);
00109         SendReceive(ECountServIncreaseBy, args);
00110         }
00111 
00112 
00116 void RCountServSession::Decrease()
00117         {
00118         SendReceive(ECountServDecrease);        
00119         }
00120 
00121 
00126 void RCountServSession::DecreaseBy(TInt anInt)
00127         {
00128         TIpcArgs args(anInt);
00129         SendReceive(ECountServDecreaseBy, args);
00130         }
00131 
00135 void RCountServSession::Reset()
00136         {
00137         SendReceive(ECountServReset);   
00138         }
00139 
00140 
00146 TInt RCountServSession::CounterValue()
00147         {
00148         TInt res=0;
00149         TPckgBuf<TInt> pckg;
00150         
00151           // Note that TPckgBuf is of type TDes8
00152         TIpcArgs args(&pckg);
00153         SendReceive(ECountServValue, args);
00154     
00155       // Extract the value returned from the server. 
00156         res = pckg();
00157         return res;
00158         }
00159 
00160 
00167 TInt RCountServSession::UnsupportedRequest()
00168         {
00169         return SendReceive(ECountServUnsupportedRequest);
00170         }
00171 
00175 void RCountServSession::BadRequest()
00176         {
00177         SendReceive(9999);
00178         }
00179 
00180 
00181 
00185 void RCountServSession::Close()
00186         {
00187         RSessionBase::Close();
00188     iServerThread.Close();      
00189         }
00190 
00191 
00192 
00193 
00201 LOCAL_C void doExampleL()
00202         {
00203         _LIT(KTxtTestingCountServer,"Testing the count server \n\n");
00204         _LIT(KTxtInitCounterWith,"\nInitialize the counter with : ");           
00205     _LIT(KTxtInitCounterFailed,"\nSetting the counter from string failed: non-numeric character detected\n");   
00206     _LIT(KTxtGetCounterValue,"\nGetting the counter value back from the server: %d \n");
00207         
00208         
00209           // Say that we are testing the count server
00210         console->Printf(KTxtTestingCountServer); 
00211         
00212           // This is our handle to the server
00213     RCountServSession ss;
00214 
00215           // Connect to the count server, starting it up if we need to 
00216           // (which in this example we do need to).
00217           // This creates a session with the server.
00218         User::LeaveIfError(ss.Connect());
00219 
00220       // At this point the server appears ready to accept requests.
00221         console->Printf(KTxtInitCounterWith);
00222 
00223       // Initialise the counter by passing an illegal string - this should
00224       // fail - but we will check anyway before reporting a failure.
00225     _LIT(KTxtIllegalString,"22h4");
00226         console->Printf(KTxtIllegalString);
00227         TInt ret = ss.SetFromString(KTxtIllegalString);
00228         if (ret==ENonNumericString)
00229             {
00230                 console->Printf(KTxtInitCounterFailed);
00231             }
00232         
00233           // Now try and initialise the counter using a legal string.
00234         console->Printf(KTxtInitCounterWith);
00235         _LIT(KTxtLegalString,"224");
00236         console->Printf(KTxtLegalString);
00237         ret = ss.SetFromString(KTxtLegalString);
00238         if (ret==ENonNumericString)
00239             {
00240                 console->Printf(KTxtInitCounterFailed);
00241             }
00242         
00243           // Now get the value back from the server, just to see what it believes
00244           // it has. We are expecting the value 224 (of course!!!).
00245         ret = ss.CounterValue();
00246         console->Printf(KTxtGetCounterValue, ret);
00247         
00248           // Now increase the counter value by the default value.
00249         _LIT(KTxt1,"\nIncrease counter (default 1)..");         
00250         console->Printf(KTxt1);
00251         ss.Increase();
00252         ret = ss.CounterValue();
00253         console->Printf(KTxtGetCounterValue, ret);
00254 
00255           // Now increase the counter value by 2.
00256         _LIT(KTxt2,"\nIncrease counter by 2..");                
00257         console->Printf(KTxt2);
00258         ss.IncreaseBy(2);
00259         ret = ss.CounterValue();
00260         console->Printf(KTxtGetCounterValue, ret);
00261         
00262           // Now decrease the counter value by the default value.       
00263         _LIT(KTxt3,"\nDecrease counter(default 1)..");          
00264         console->Printf(KTxt3);
00265         ss.Decrease();
00266     ret = ss.CounterValue();
00267         console->Printf(KTxtGetCounterValue, ret);
00268         
00269       // Now increase the counter value by 7.
00270         _LIT(KTxt4,"\nIncrease counter by 7..");                
00271         console->Printf(KTxt4);
00272         ss.IncreaseBy(7);
00273         ret = ss.CounterValue();
00274         console->Printf(KTxtGetCounterValue, ret);
00275                 
00276       // Now increase the counter value again by the default value.             
00277         _LIT(KTxt5,"\nIncrease counter(default 1)..");  
00278         console->Printf(KTxt5);
00279         ss.Increase();
00280         ret = ss.CounterValue();
00281         console->Printf(KTxtGetCounterValue, ret);
00282 
00283       // Now decrease the counter value by 3.           
00284         _LIT(KTxt6,"\nDecrease counter by 3..");        
00285         console->Printf(KTxt6);
00286         ss.DecreaseBy(3);
00287         ret = ss.CounterValue();
00288         console->Printf(KTxtGetCounterValue, ret);
00289         
00290           // Reset counter value
00291         _LIT(KTxt7,"\nReseting counter value to 0..");
00292         console->Printf(KTxt7);
00293         ss.Reset();
00294         ret = ss.CounterValue();
00295         console->Printf(KTxtGetCounterValue, ret);
00296 
00297         
00298           // Call API function which is not implemented in the server
00299         _LIT(KTxt8,"\nAbout to call the unsupported function Stop()..");
00300         console->Printf(KTxt8);
00301         ret = ss.UnsupportedRequest();
00302         _LIT(KTxt9,"\nSorry, UnsupportedRequest() is not supported\n");
00303         if (ret==KErrNotSupported)
00304             {
00305                 console->Printf(KTxt9);
00306             }
00307                 
00308      
00309       // This request will panic this client so do not remove the
00310       // following comment unless you want to try it.
00311 //      ss.BadRequest();
00312    
00313    
00314           // Close the sesssion with the count server.
00315         ss.Close();
00316         }

Generated by  doxygen 1.6.2