00001 /* 00002 * cobainserver.cpp,v 1.20 2003/11/20 17:50:18 mattid Exp 00003 * 00004 * COBAIN - Communications API for EPOC Environments 00005 * Copyright (C) 2002-2003 Matti Dahlbom, Matti Kokkola 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 * Contact: Matti Dahlbom <matti@777-team.org> 00022 * Matti Kokkola <matti.kokkola@iki.fi> 00023 */ 00024 00025 #include <e32svr.h> 00026 #include <flogger.h> 00027 00028 #include "cobainserver.h" 00029 #include "serversession.h" 00030 #include "Log.h" 00031 00032 CCobainServer::CCobainServer(TInt aPriority) 00033 : CServer(aPriority, ESharableSessions) 00034 { 00035 #ifndef __WINS__ 00036 Log::Init(); 00037 #endif 00038 } 00039 00040 CCobainServer::~CCobainServer() 00041 { 00042 Log::Print(_L("** server dying")); 00043 } 00044 00045 CCobainServer* CCobainServer::NewL() 00046 { 00047 CCobainServer *server = new (ELeave)CCobainServer(CServer::EPriorityStandard); 00048 CleanupStack::PushL(server); 00049 server->ConstructL(); 00050 CleanupStack::Pop(); // server 00051 00052 return server; 00053 } 00054 00055 void CCobainServer::ConstructL() 00056 { 00057 // start the server 00058 StartL(KCobainServerName); 00059 00060 Log::Print(_L("** server constructed")); 00061 } 00062 00063 CSharableSession* CCobainServer::NewSessionL(const TVersion &aVersion) const 00064 { 00065 // verify version 00066 if( !User::QueryVersionSupported(TVersion(KCobainServerMajorVersion, 00067 KCobainServerMinorVersion, 00068 KCobainServerBuildVersion), 00069 aVersion) ) { 00070 User::Leave(KErrNotSupported); 00071 } 00072 00073 RThread client = Message().Client(); 00074 CServerSession *session = 00075 CServerSession::NewL(client, 00076 const_cast<CCobainServer*>(this)/*, 00077 iSubsessionContainerIndex->CreateL()*/); 00078 00079 return session; 00080 } 00081 00082 void CCobainServer::IncrementSession() 00083 { 00084 iSessionCount++; 00085 } 00086 00087 void CCobainServer::DecrementSession() 00088 { 00089 iSessionCount--; 00090 00091 if (iSessionCount < 1) 00092 { 00093 // stopping server will delete server 00094 CActiveScheduler::Stop(); 00095 } 00096 } 00097 00098 void CCobainServer::PanicServer(CCobainServer::TServerPanic aPanic) 00099 { 00100 User::Panic(KServerPanic, aPanic); 00101 } 00102 00103 void CCobainServer::ThreadFunctionL() 00104 { 00105 CActiveScheduler* activeScheduler = CActiveScheduler::Current(); 00106 00107 if( activeScheduler == NULL ) 00108 { 00109 activeScheduler = new (ELeave) CActiveScheduler(); 00110 CleanupStack::PushL(activeScheduler) ; 00111 00112 CActiveScheduler::Install(activeScheduler); 00113 } 00114 00115 CCobainServer *server = CCobainServer::NewL(); 00116 CleanupStack::PushL(server); 00117 00118 RSemaphore semaphore; 00119 User::LeaveIfError(semaphore.OpenGlobal(KCobainServerSemaphoreName)); 00120 00121 semaphore.Signal(); 00122 semaphore.Close(); 00123 00124 // blocks until CActiveScheduler::Stop() is invoked 00125 CActiveScheduler::Start(); 00126 00127 CleanupStack::PopAndDestroy(2); // server, activeScheduler 00128 } 00129 00130 TInt CCobainServer::ThreadFunction(TAny* /*aNone*/) 00131 { 00132 CTrapCleanup* cleanupStack = CTrapCleanup::New(); 00133 if( cleanupStack == NULL ) 00134 { 00135 PanicServer(CCobainServer::EServerStartFailed); 00136 } 00137 00138 TRAPD(err, ThreadFunctionL()); 00139 if( err != KErrNone ) 00140 { 00141 RDebug::Print(_L("The server thread has left the building! err = %d"), err); 00142 PanicServer(CCobainServer::EServerStartFailed); 00143 } 00144 00145 delete cleanupStack; 00146 00147 return KErrNone; 00148 } 00149 00150 #ifdef __WINS__ // simulator environment builds 00151 00152 IMPORT_C TInt WinsMain(); 00153 EXPORT_C TInt WinsMain() 00154 { 00155 return reinterpret_cast<TInt>(&CCobainServer::ThreadFunction); 00156 } 00157 00158 GLDEF_C TInt E32Dll(TDllReason) 00159 { 00160 return KErrNone; 00161 } 00162 00163 #else // target machine builds 00164 00165 TInt E32Main() 00166 { 00167 return CCobainServer::ThreadFunction(NULL); 00168 } 00169 00170 #endif
1.3.5