examples/Base/IPC/ClientServer/Complex/ComplexServer.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 */
00029 
00030 
00031 #include "ComplexClientAndServer.h"
00032 #include "ComplexServer.h"
00033 #include <e32svr.h>
00034 #include <e32uid.h>
00035 
00036 //
00037 //NOTE**: The example does not demonstrate any security features - its purpose is simply
00038 //        to demonstrate the basic principles of client/server interaction.
00039 //
00040 
00041 
00045 TInt CCountServer::ThreadFunction(TAny* )
00046         {
00047           // Useful TInt variable
00048         TInt err;
00049         
00050           // create cleanup stack
00051         CTrapCleanup* cleanup = CTrapCleanup::New();
00052         if (cleanup == NULL)
00053             {
00054                 PanicServer(ECreateTrapCleanup);
00055             }
00056                 
00057           // Create an active scheduler.
00058         CActiveScheduler* pScheduler=new CActiveScheduler;
00059         __ASSERT_ALWAYS(pScheduler,PanicServer(EMainSchedulerError));
00060           // Install the active scheduler.
00061         CActiveScheduler::Install(pScheduler);
00062         
00063           // Create the server object.
00064         CCountServer* pServer = NULL;
00065         TRAP(err,pServer = CCountServer::NewL(EPriorityStandard));
00066         __ASSERT_ALWAYS(!err,CCountServer::PanicServer(ESvrCreateServer));
00067         
00068           // Start the server
00069         err = pServer->Start(KCountServerName);
00070         if (err != KErrNone)
00071             {
00072                 CCountServer::PanicServer(ESvrStartServer);
00073             }
00074             
00075       // Let everyone know that we are ready to
00076       // deal with requests.
00077     RThread::Rendezvous(KErrNone);
00078     
00079       // And start fielding requests from client(s).
00080         CActiveScheduler::Start();
00081         
00082       // Tidy up...
00083     delete pServer;
00084     delete pScheduler;
00085     delete cleanup; 
00086 
00087           // ...although we should never get here!
00088         return KErrNone;
00089         }
00090 
00091 
00099 EXPORT_C TInt StartThread(RThread& aServerThread)
00100         {
00101         TInt res=KErrNone;
00102         
00103       // Create the server, if one with this name does not already exist.
00104         
00105         TFindServer findCountServer(KCountServerName);
00106         TFullName   name;
00107         
00108           // Need to check that the server exists.
00109         if (findCountServer.Next(name)!=KErrNone)
00110             {
00111               // Create the thread for the server.
00112                 res=aServerThread.Create(KCountServerName,
00113                         CCountServer::ThreadFunction,
00114                         KDefaultStackSize,
00115                         KDefaultHeapSize,
00116                         KDefaultHeapSize,
00117                         NULL
00118                         );
00119                   // The thread has been created OK so get it started - however
00120           // we need to make sure that it has started before we continue.
00121                 if (res==KErrNone)
00122                         {
00123                         TRequestStatus rendezvousStatus;
00124                         
00125                         aServerThread.SetPriority(EPriorityNormal);
00126                         aServerThread.Rendezvous(rendezvousStatus);
00127                         aServerThread.Resume();
00128                         User::WaitForRequest(rendezvousStatus);
00129                         }
00130                         
00131                   // The thread has not been created - clearly there's been a problem.
00132                 else
00133                         {
00134                         aServerThread.Close();
00135                         }
00136                 }
00137     return res;
00138         }

Generated by  doxygen 1.6.2