1 // SALOME Session : implementation of Session_ServerThread.cxx
3 // Copyright (C) 2003 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
24 // File : Session_ServerThread.cxx
25 // Author : Paul RASCLE, EDF
29 // #include <SALOMEconfig.h>
30 // #include CORBA_SERVER_HEADER(SALOME_Session)
31 // #include CORBA_SERVER_HEADER(SALOMEDS)
33 #include "Session_ServerThread.hxx"
35 #include "SALOME_Container_i.hxx"
36 #include "SALOME_Launcher.hxx"
37 #include <SALOMEDSClient.hxx>
38 #include <SALOMEDSClient_ClientFactory.hxx>
39 #include "SALOME_ModuleCatalog_impl.hxx"
40 #include "RegistryService.hxx"
41 #include "Session_Session_i.hxx"
42 #include "SalomeApp_Engine_i.hxx"
44 #include "Utils_ORB_INIT.hxx"
45 #include "Utils_SINGLETON.hxx"
46 #include "Utils_SALOME_Exception.hxx"
48 #include "NamingService_WaitForServerReadiness.hxx"
49 #include "utilities.h"
56 const int Session_ServerThread::NB_SRV_TYP = 7;
57 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
66 default constructor not for use
68 Session_ServerThread::Session_ServerThread()
70 ASSERT(0); // must not be called
76 Session_ServerThread::Session_ServerThread(int argc,
79 PortableServer::POA_ptr poa)
81 //MESSAGE("Session_ServerThread Constructor " << argv[0]);
83 _argv = new char*[ _argc + 1 ];
85 for (int i = 0; i < _argc; i++ )
86 _argv[i] = strdup( argv[i] );
88 _orb = CORBA::ORB::_duplicate(orb);
89 _root_poa = PortableServer::POA::_duplicate(poa);
91 _NS = new SALOME_NamingService(_orb); // one instance per server to limit
92 // multi thread coherence problems
98 Session_ServerThread::~Session_ServerThread()
100 //MESSAGE("~Session_ServerThread "<< _argv[0]);
102 for (int i = 0; i <_argc ; i++ )
108 run the thread : activate one servant, the servant type is given by
111 void Session_ServerThread::Init()
113 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
115 for (int i=0; i<_argc; i++) SCRUTE(_argv[i]);
117 for (int i=0; i<NB_SRV_TYP; i++)
119 for (i=0; i<NB_SRV_TYP; i++)
121 if (strcmp(_argv[0],_serverTypes[i])==0)
124 MESSAGE("Server Thread type : "<<_serverTypes[i]);
129 NamingService_WaitForServerReadiness(_NS,"/Registry");
130 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
131 ActivateContainer(_argc, _argv);
134 case 1: // ModuleCatalog
136 NamingService_WaitForServerReadiness(_NS,"/Registry");
137 ActivateModuleCatalog(_argc, _argv);
142 NamingService_WaitForServerReadiness(_NS,"");
143 ActivateRegistry(_argc, _argv);
148 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
149 ActivateSALOMEDS(_argc, _argv);
154 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
155 string containerName = "/Containers/";
156 containerName = containerName + GetHostname();
157 containerName = containerName + "/FactoryServer";
158 NamingService_WaitForServerReadiness(_NS,containerName);
159 ActivateSession(_argc, _argv);
162 case 5: // SalomeApp_Engine
164 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
165 ActivateEngine(_argc, _argv);
168 case 6: // Container Manager
170 NamingService_WaitForServerReadiness(_NS,"");
171 ActivateContainerManager(_argc, _argv);
183 void Session_ServerThread::ActivateModuleCatalog(int argc,
188 MESSAGE("ModuleCatalog thread started");
189 // allocation on heap to allow destruction by POA
191 SALOME_ModuleCatalogImpl* Catalogue_i
192 = new SALOME_ModuleCatalogImpl(argc, argv);
194 // Tell the POA that the objects are ready to accept requests.
196 PortableServer::ObjectId_var id= _root_poa->activate_object (Catalogue_i);
197 Catalogue_i->_remove_ref();
199 CORBA::Object_var myCata = Catalogue_i->_this();
200 _NS->Register(myCata ,"/Kernel/ModulCatalog");
202 catch(CORBA::SystemException&)
204 INFOS( "Caught CORBA::SystemException." );
206 catch(CORBA::Exception&)
208 INFOS( "Caught CORBA::Exception." );
210 catch(omniORB::fatalException& fe)
212 INFOS( "Caught omniORB::fatalException:" );
213 INFOS( " file: " << fe.file() );
214 INFOS( " line: " << fe.line() );
215 INFOS( " mesg: " << fe.errmsg() );
219 INFOS( "Caught unknown exception." );
223 void Session_ServerThread::ActivateSALOMEDS(int argc,
228 MESSAGE("SALOMEDS thread started");
229 // We allocate the objects on the heap. Since these are reference
230 // counted objects, they will be deleted by the POA when they are no
233 ClientFactory::createStudyManager(_orb,_root_poa);
235 catch(CORBA::SystemException&)
237 INFOS( "Caught CORBA::SystemException." );
239 catch(CORBA::Exception&)
241 INFOS( "Caught CORBA::Exception." );
243 catch(omniORB::fatalException& fe)
245 INFOS( "Caught omniORB::fatalException:" );
246 INFOS( " file: " << fe.file() );
247 INFOS( " line: " << fe.line() );
248 INFOS( " mesg: " << fe.errmsg() );
252 INFOS( "Caught unknown exception." );
256 void Session_ServerThread::ActivateRegistry(int argc,
259 MESSAGE("Registry thread started");
263 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
264 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
266 const char *ptrSessionName=0;
269 for ( k=1 ; k<argc ; k++ )
271 if( strcmp(argv[k],"--salome_session")==0 )
273 ptrSessionName=argv[k+1];
277 ASSERT(ptrSessionName) ;
278 ASSERT(strlen( ptrSessionName )>0);
279 const char *registryName = "Registry";
280 Registry::Components_var varComponents;
283 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
284 ptrRegistry->SessionName( ptrSessionName );
285 ptrRegistry->SetOrb(_orb);
286 varComponents = ptrRegistry->_this();
287 // The RegistryService must not already exist.
291 CORBA::Object_var pipo = _NS->Resolve( registryName );
292 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
293 INFOS("RegistryService servant already existing" );
296 catch( const ServiceUnreachable &/*ex*/ )
299 catch( const CORBA::Exception &/*exx*/ )
302 string absoluteName = string("/") + registryName;
303 _NS->Register( varComponents , absoluteName.c_str() );
304 MESSAGE("On attend les requetes des clients");
306 catch( const SALOME_Exception &ex )
308 INFOS( "Communication Error : " << ex.what() );
313 void Session_ServerThread::ActivateContainerManager(int argc,
318 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
319 cout << "ActivateSalomeLauncher ......!!!! " << endl;
320 SALOME_Launcher * myContainer = new SALOME_Launcher(_orb,root_poa);
322 catch(CORBA::SystemException&)
324 INFOS("Caught CORBA::SystemException.");
326 catch(PortableServer::POA::WrongPolicy&)
328 INFOS("Caught CORBA::WrongPolicyException.");
330 catch(PortableServer::POA::ServantAlreadyActive&)
332 INFOS("Caught CORBA::ServantAlreadyActiveException");
334 catch(CORBA::Exception&)
336 INFOS("Caught CORBA::Exception.");
340 INFOS("Caught unknown exception.");
344 void Session_ServerThread::ActivateContainer(int argc,
349 MESSAGE("Container thread started");
351 // get or create the child POA
353 PortableServer::POA_var factory_poa;
356 factory_poa = _root_poa->find_POA("factory_poa",0);
357 // 0 = no activation (already done if exists)
359 catch (PortableServer::POA::AdapterNonExistent&)
361 MESSAGE("factory_poa does not exists, create...");
362 // define policy objects
363 PortableServer::ImplicitActivationPolicy_var implicitActivation =
364 _root_poa->create_implicit_activation_policy(
365 PortableServer::NO_IMPLICIT_ACTIVATION);
366 // default = NO_IMPLICIT_ACTIVATION
367 PortableServer::ThreadPolicy_var threadPolicy =
368 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
369 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
371 // create policy list
372 CORBA::PolicyList policyList;
373 policyList.length(2);
374 policyList[0] = PortableServer::ImplicitActivationPolicy::
375 _duplicate(implicitActivation);
376 policyList[1] = PortableServer::ThreadPolicy::
377 _duplicate(threadPolicy);
379 PortableServer::POAManager_var nil_mgr
380 = PortableServer::POAManager::_nil();
381 factory_poa = _root_poa->create_POA("factory_poa",
384 //with nil_mgr instead of pman,
385 //a new POA manager is created with the new POA
387 // destroy policy objects
388 implicitActivation->destroy();
389 threadPolicy->destroy();
391 // obtain the factory poa manager
392 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
394 MESSAGE("pmanfac->activate()");
397 char *containerName = "";
400 containerName = argv[1];
403 Engines_Container_i * myContainer
404 = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
406 catch(CORBA::SystemException&)
408 INFOS("Caught CORBA::SystemException.");
410 catch(PortableServer::POA::WrongPolicy&)
412 INFOS("Caught CORBA::WrongPolicyException.");
414 catch(PortableServer::POA::ServantAlreadyActive&)
416 INFOS("Caught CORBA::ServantAlreadyActiveException");
418 catch(CORBA::Exception&)
420 INFOS("Caught CORBA::Exception.");
424 INFOS("Caught unknown exception.");
428 void Session_ServerThread::ActivateEngine(int /*argc*/, char ** /*argv*/)
432 MESSAGE("SalomeApp_Engine thread started");
433 SalomeApp_Engine_i* anEngine = new SalomeApp_Engine_i();
434 PortableServer::ObjectId_var id =_root_poa->activate_object( anEngine );
435 MESSAGE("poa->activate_object( SalomeApp_Engine )");
437 CORBA::Object_var obj = anEngine->_this();
438 anEngine->_remove_ref();
439 _NS->Register( obj ,"/SalomeAppEngine");
442 catch (CORBA::SystemException&)
444 INFOS("Caught CORBA::SystemException.");
446 catch (CORBA::Exception&)
448 INFOS("Caught CORBA::Exception.");
452 INFOS("Caught unknown exception.");
456 void Session_ServerThread::ActivateSession(int argc,
459 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
465 Session_SessionThread::Session_SessionThread(int argc,
468 PortableServer::POA_ptr poa,
470 QWaitCondition* GUILauncher)
471 : Session_ServerThread(argc, argv, orb, poa),
472 _GUIMutex( GUIMutex ),
473 _GUILauncher( GUILauncher )
480 Session_SessionThread::~Session_SessionThread()
484 void Session_SessionThread::ActivateSession(int argc,
489 MESSAGE("Session thread started");
490 SALOME_Session_i * mySALOME_Session
491 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
492 PortableServer::ObjectId_var mySALOME_Sessionid
493 = _root_poa->activate_object(mySALOME_Session);
494 MESSAGE("poa->activate_object(mySALOME_Session)");
496 CORBA::Object_var obj = mySALOME_Session->_this();
497 CORBA::String_var sior(_orb->object_to_string(obj));
498 mySALOME_Session->_remove_ref();
500 mySALOME_Session->NSregister();
502 catch (CORBA::SystemException&)
504 INFOS("Caught CORBA::SystemException.");
506 catch (CORBA::Exception&)
508 INFOS("Caught CORBA::Exception.");
512 INFOS("Caught unknown exception.");