1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SALOME Session : implementation of Session_ServerThread.cxx
23 // File : Session_ServerThread.cxx
24 // Author : Paul RASCLE, EDF
27 // #include <SALOMEconfig.h>
28 // #include CORBA_SERVER_HEADER(SALOME_Session)
29 // #include CORBA_SERVER_HEADER(SALOMEDS)
31 #include "Session_ServerThread.hxx"
33 #include <SALOME_NamingService.hxx>
34 #include <SALOME_Container_i.hxx>
35 #include <SALOME_Launcher.hxx>
36 #include <SALOMEDSClient_ClientFactory.hxx>
37 #include <SALOME_ModuleCatalog_impl.hxx>
38 #include <RegistryService.hxx>
40 #include "Session_Session_i.hxx"
41 #include "SalomeApp_Engine_i.hxx"
43 #include <Utils_ORB_INIT.hxx>
44 #include <Utils_SINGLETON.hxx>
45 #include <Utils_SALOME_Exception.hxx>
46 #include <Basics_Utils.hxx>
47 #include <NamingService_WaitForServerReadiness.hxx>
48 #include <utilities.h>
54 #include <QWaitCondition>
58 const int Session_ServerThread::NB_SRV_TYP = 7;
59 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
68 default constructor not for use
70 Session_ServerThread::Session_ServerThread()
72 ASSERT(0); // must not be called
78 Session_ServerThread::Session_ServerThread(int argc,
81 PortableServer::POA_ptr poa)
83 //MESSAGE("Session_ServerThread Constructor " << argv[0]);
85 _argv = new char*[ _argc + 1 ];
87 for (int i = 0; i < _argc; i++ )
88 _argv[i] = strdup( argv[i] );
90 _orb = CORBA::ORB::_duplicate(orb);
91 _root_poa = PortableServer::POA::_duplicate(poa);
93 _NS = new SALOME_NamingService(_orb); // one instance per server to limit
94 // multi thread coherence problems
100 Session_ServerThread::~Session_ServerThread()
102 //MESSAGE("~Session_ServerThread "<< _argv[0]);
104 for (int i = 0; i <_argc ; i++ )
110 run the thread : activate one servant, the servant type is given by
113 void Session_ServerThread::Init()
115 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
118 for (i=0; i<_argc; i++) SCRUTE(_argv[i]);
120 for (i=0; i<NB_SRV_TYP; i++) {
121 if (strcmp(_argv[0],_serverTypes[i])==0) {
123 MESSAGE("Server Thread type : "<<_serverTypes[i]);
127 NamingService_WaitForServerReadiness(_NS,"/Registry");
128 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
129 ActivateContainer(_argc, _argv);
132 case 1: // ModuleCatalog
134 NamingService_WaitForServerReadiness(_NS,"/Registry");
135 ActivateModuleCatalog(_argc, _argv);
140 NamingService_WaitForServerReadiness(_NS,"");
141 ActivateRegistry(_argc, _argv);
146 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
147 ActivateSALOMEDS(_argc, _argv);
152 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
153 string containerName = "/Containers/";
154 containerName = containerName + Kernel_Utils::GetHostname();
155 containerName = containerName + "/FactoryServer";
156 NamingService_WaitForServerReadiness(_NS,containerName);
157 ActivateSession(_argc, _argv);
160 case 5: // SalomeApp_Engine
162 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
163 ActivateEngine(_argc, _argv);
166 case 6: // Container Manager
168 NamingService_WaitForServerReadiness(_NS,"");
169 ActivateContainerManager(_argc, _argv);
182 void Session_ServerThread::ActivateModuleCatalog(int argc,
186 MESSAGE("ModuleCatalog thread started");
187 // allocation on heap to allow destruction by POA
189 SALOME_ModuleCatalogImpl* Catalogue_i
190 = new SALOME_ModuleCatalogImpl(argc, argv);
192 // Tell the POA that the objects are ready to accept requests.
194 PortableServer::ObjectId_var id = _root_poa->activate_object (Catalogue_i);
195 Catalogue_i->_remove_ref();
197 CORBA::Object_var myCata = Catalogue_i->_this();
198 _NS->Register(myCata ,"/Kernel/ModulCatalog");
200 catch(CORBA::SystemException&) {
201 INFOS( "Caught CORBA::SystemException." );
203 catch(CORBA::Exception&) {
204 INFOS( "Caught CORBA::Exception." );
206 catch(omniORB::fatalException& fe) {
207 INFOS( "Caught omniORB::fatalException:" );
208 INFOS( " file: " << fe.file() );
209 INFOS( " line: " << fe.line() );
210 INFOS( " mesg: " << fe.errmsg() );
213 INFOS( "Caught unknown exception." );
217 void Session_ServerThread::ActivateSALOMEDS(int argc,
221 MESSAGE("SALOMEDS thread started");
222 // We allocate the objects on the heap. Since these are reference
223 // counted objects, they will be deleted by the POA when they are no
226 ClientFactory::createStudyManager(_orb,_root_poa);
228 catch(CORBA::SystemException&) {
229 INFOS( "Caught CORBA::SystemException." );
231 catch(CORBA::Exception&) {
232 INFOS( "Caught CORBA::Exception." );
234 catch(omniORB::fatalException& fe) {
235 INFOS( "Caught omniORB::fatalException:" );
236 INFOS( " file: " << fe.file() );
237 INFOS( " line: " << fe.line() );
238 INFOS( " mesg: " << fe.errmsg() );
241 INFOS( "Caught unknown exception." );
245 void Session_ServerThread::ActivateRegistry(int argc,
248 MESSAGE("Registry thread started");
251 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
252 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
254 const char *ptrSessionName=0;
257 for ( k=1 ; k<argc ; k++ ) {
258 if ( strcmp(argv[k],"--salome_session")==0 ) {
259 ptrSessionName=argv[k+1];
263 ASSERT(ptrSessionName) ;
264 ASSERT(strlen( ptrSessionName )>0);
265 const char *registryName = "Registry";
266 Registry::Components_var varComponents;
268 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
269 ptrRegistry->SessionName( ptrSessionName );
270 ptrRegistry->SetOrb(_orb);
271 varComponents = ptrRegistry->_this();
272 // The RegistryService must not already exist.
275 CORBA::Object_var pipo = _NS->Resolve( registryName );
276 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
277 INFOS("RegistryService servant already existing" );
280 catch( const ServiceUnreachable &/*ex*/ ) {
282 catch( const CORBA::Exception &/*exx*/ ) {
284 string absoluteName = string("/") + registryName;
285 _NS->Register( varComponents , absoluteName.c_str() );
286 MESSAGE("On attend les requetes des clients");
288 catch( const SALOME_Exception &ex ) {
289 INFOS( "Communication Error : " << ex.what() );
294 void Session_ServerThread::ActivateContainerManager(int argc,
298 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
299 cout << "Activate SalomeLauncher ......!!!! " << endl;
300 SALOME_Launcher * myContainer = new SALOME_Launcher(_orb,root_poa);
302 catch(CORBA::SystemException&) {
303 INFOS("Caught CORBA::SystemException.");
305 catch(PortableServer::POA::WrongPolicy&) {
306 INFOS("Caught CORBA::WrongPolicyException.");
308 catch(PortableServer::POA::ServantAlreadyActive&) {
309 INFOS("Caught CORBA::ServantAlreadyActiveException");
311 catch(CORBA::Exception&) {
312 INFOS("Caught CORBA::Exception.");
315 INFOS("Caught unknown exception.");
319 void Session_ServerThread::ActivateContainer(int argc,
323 MESSAGE("Container thread started");
325 // get or create the child POA
327 PortableServer::POA_var factory_poa;
329 factory_poa = _root_poa->find_POA("factory_poa",0);
330 // 0 = no activation (already done if exists)
332 catch (PortableServer::POA::AdapterNonExistent&) {
333 MESSAGE("factory_poa does not exists, create...");
334 // define policy objects
335 PortableServer::ImplicitActivationPolicy_var implicitActivation =
336 _root_poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);
337 // default = NO_IMPLICIT_ACTIVATION
338 PortableServer::ThreadPolicy_var threadPolicy =
339 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
340 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
342 // create policy list
343 CORBA::PolicyList policyList;
344 policyList.length(2);
345 policyList[0] = PortableServer::ImplicitActivationPolicy::
346 _duplicate(implicitActivation);
347 policyList[1] = PortableServer::ThreadPolicy::
348 _duplicate(threadPolicy);
350 PortableServer::POAManager_var nil_mgr
351 = PortableServer::POAManager::_nil();
352 factory_poa = _root_poa->create_POA("factory_poa",
355 //with nil_mgr instead of pman,
356 //a new POA manager is created with the new POA
358 // destroy policy objects
359 implicitActivation->destroy();
360 threadPolicy->destroy();
362 // obtain the factory poa manager
363 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
365 MESSAGE("pmanfac->activate()");
368 char *containerName = "";
370 containerName = argv[1];
373 Engines_Container_i * myContainer
374 = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
376 catch(CORBA::SystemException&) {
377 INFOS("Caught CORBA::SystemException.");
379 catch(PortableServer::POA::WrongPolicy&) {
380 INFOS("Caught CORBA::WrongPolicyException.");
382 catch(PortableServer::POA::ServantAlreadyActive&) {
383 INFOS("Caught CORBA::ServantAlreadyActiveException");
385 catch(CORBA::Exception&) {
386 INFOS("Caught CORBA::Exception.");
389 INFOS("Caught unknown exception.");
393 void Session_ServerThread::ActivateEngine(int /*argc*/, char ** /*argv*/)
396 MESSAGE("SalomeApp_Engine thread started");
397 SalomeApp_Engine_i* anEngine = new SalomeApp_Engine_i();
398 PortableServer::ObjectId_var id =_root_poa->activate_object( anEngine );
399 MESSAGE("poa->activate_object( SalomeApp_Engine )");
401 CORBA::Object_var obj = anEngine->_this();
402 anEngine->_remove_ref();
403 _NS->Register( obj ,"/SalomeAppEngine");
405 catch (CORBA::SystemException&) {
406 INFOS("Caught CORBA::SystemException.");
408 catch (CORBA::Exception&) {
409 INFOS("Caught CORBA::Exception.");
412 INFOS("Caught unknown exception.");
416 void Session_ServerThread::ActivateSession(int argc,
419 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
425 Session_SessionThread::Session_SessionThread(int argc,
428 PortableServer::POA_ptr poa,
430 QWaitCondition* GUILauncher)
431 : Session_ServerThread(argc, argv, orb, poa),
432 _GUIMutex( GUIMutex ),
433 _GUILauncher( GUILauncher )
440 Session_SessionThread::~Session_SessionThread()
444 void Session_SessionThread::ActivateSession(int argc,
448 MESSAGE("Session thread started");
449 SALOME_Session_i * mySALOME_Session
450 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
451 PortableServer::ObjectId_var mySALOME_Sessionid
452 = _root_poa->activate_object(mySALOME_Session);
453 MESSAGE("poa->activate_object(mySALOME_Session)");
455 CORBA::Object_var obj = mySALOME_Session->_this();
456 CORBA::String_var sior(_orb->object_to_string(obj));
457 mySALOME_Session->_remove_ref();
459 mySALOME_Session->NSregister();
461 catch (CORBA::SystemException&) {
462 INFOS("Caught CORBA::SystemException.");
464 catch (CORBA::Exception&) {
465 INFOS("Caught CORBA::Exception.");
468 INFOS("Caught unknown exception.");