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

Generated by  doxygen 1.6.2