examples/Base/IPC/AdvancedClientServerExample/ProcessServer/src/processserver.cpp

00001 // Copyright (c) 2007-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 "processserver.h"
00017 #include "processserverstart.h"
00018 #include "processserversession.h"
00019 #include<e32debug.h>
00020 
00021 const TInt KProcessServerShutDownDelay = 50000000; //50 sec
00022 
00023 //******CProcessServer******//
00024 void CProcessServer::NewLC()
00025         {
00026         CProcessServer* s = new(ELeave) CProcessServer();
00027         CleanupStack::PushL(s);
00028         s->ConstructL();
00029         }
00033 CProcessServer::CProcessServer()
00034         :CServer2(EPriorityStandard, ESharableSessions),iDriverState(EStateUnknown)
00035         {
00036         }
00040 void CProcessServer::ConstructL()
00041         {
00042         // Call CServer2::StartL() before any other functions inside ConstructL()
00043         // to avoid an error other than KErrAlreadyExists when the duplicate server starts
00044         StartL(KProcessServerName);
00045         iDelayProcessServerShutDown = CDelayServerShutDown::NewL();
00046         }
00050 CProcessServer::~CProcessServer()
00051         {
00052         if (iDelayProcessServerShutDown)
00053                 {
00054                 iDelayProcessServerShutDown->Cancel();
00055                 delete iDelayProcessServerShutDown;
00056                 }
00057                 
00058         // unload device driver when server terminated
00059         UnloadDevice();
00060         }
00067 CSession2* CProcessServer::NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const
00068         {
00069         //check whether version is compatible
00070         TVersion v(KProcessServerVersion,KProcessServerMinorVersionNumber,KProcessServerBuildVersionNumber);
00071         if(!User::QueryVersionSupported(v, aVersion))
00072                 User::Leave(KErrNotSupported);
00073         
00074         // check client process has the required capability
00075         if(!aMessage.HasCapability(ECapabilityMultimediaDD))
00076                 User::Leave(KErrPermissionDenied);              
00077 
00078         // construct and return the server side client session object
00079         CProcessServer& ncThis = const_cast<CProcessServer&>(*this);
00080         CProcessServerSession* serverSession = CProcessServerSession::NewL(ncThis);
00081         return serverSession;
00082         }
00086 void CProcessServer::IncrementRefCount()
00087         {
00088         iRefCount++;
00089         iDelayProcessServerShutDown->Cancel(); // Cancel shutdown if it has started due to no clients being connected 
00090         }
00091         
00095 void CProcessServer::DecrementRefCount() 
00096         {
00097         iRefCount--;
00098         if ( iRefCount == 0 )
00099                 {
00100                 iDelayProcessServerShutDown->SetDelay(TTimeIntervalMicroSeconds32(KProcessServerShutDownDelay));
00101                 }
00102         }
00107 TInt CProcessServer::LoadDevice()
00108         {
00109         if (iDriverState>=EDriverLoaded && iDriverState<EDriverUnloaded)
00110                 return KErrNone; //Device has been loaded, return immediately
00111                 
00112         TInt r=User::LoadPhysicalDevice(KDriver1PddFileName);
00113         if (r!=KErrNone && r!=KErrAlreadyExists)
00114                 {
00115                 return r; //some error occurred
00116                 }
00117 
00118         r = User::LoadLogicalDevice(KDriver1LddFileName);
00119         if (r!=KErrNone && r!=KErrAlreadyExists)
00120                 {
00121                 return r; //some error occurred 
00122                 }
00123 
00124         //both PDD and LDD have been loaded
00125         UpdateDriverState(EDriverLoaded);
00126         return KErrNone; 
00127         }
00132 TInt CProcessServer::UnloadDevice()
00133         {
00134         if (iDriverState==EDriverUnloaded || iDriverState == EStateUnknown)
00135                 return KErrNone; //no device is loaded, return immediately
00136                 
00137         // close device
00138         if (iDriver.Handle())
00139                 iDriver.Close();
00140         // Unload Logical Device
00141         TInt r = User::FreeLogicalDevice(RDriver1::Name());
00142         if (r!=KErrNone)
00143                 return r;
00144         // Unload Physical Device
00145         TName pddName(RDriver1::Name());
00146         _LIT(KVariantExtension,".template");
00147         pddName.Append(KVariantExtension);
00148         r=User::FreePhysicalDevice(pddName);
00149         
00150         if (KErrNone==r)
00151                 UpdateDriverState(EDriverUnloaded);     
00152         return r;
00153         }
00158 TInt CProcessServer::OpenLogicalChannel()
00159         {
00160         if (iDriverState>=ELogicalChannelOpened && iDriverState<ELogicalChannelClosed)
00161                 return KErrNone;
00162         
00163         TInt r = iDriver.Open();
00164         if (KErrNone==r)
00165                 UpdateDriverState(ELogicalChannelOpened);
00166         
00167         return r;
00168         }
00172 void CProcessServer::CloseLogicalChannel()
00173         {
00174         if (iDriver.Handle())
00175                 {
00176                 iDriver.Close();
00177                 UpdateDriverState(ELogicalChannelClosed);
00178                 }
00179         }
00186 TInt CProcessServer::SendDataToDevice(TRequestStatus& aStatus, const TDesC8& aData)
00187         {
00188         TInt r = KErrNone;
00189         if (iDriverState>=ELogicalChannelOpened && iDriverState<ELogicalChannelClosed)
00190                 {
00191                 iDriver.SendData(aStatus, aData);
00192                 UpdateDriverState(ESendingData);
00193                 }
00194         else
00195                 {
00196                 r = KErrArgument;
00197                 }
00198         return r;
00199         }
00203 void CProcessServer::CancelSendData()
00204         {
00205         iDriver.SendDataCancel();
00206         UpdateDriverState(ELogicalChannelOpened);
00207         }
00212 void CProcessServer::UpdateDriverState(TDriverState aState)
00213         {
00214         iDriverState = aState;
00215         }
00216 
00217 //EOF

Generated by  doxygen 1.6.2