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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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_ContainerManager.hxx"
37 #include "SALOMEDS_StudyManager_i.hxx"
38 #include "SALOME_ModuleCatalog_impl.hxx"
39 #include "RegistryService.hxx"
40 #include "SALOME_Session_i.hxx"
42 #include "Utils_ORB_INIT.hxx"
43 #include "Utils_SINGLETON.hxx"
44 #include "Utils_SALOME_Exception.hxx"
46 #include "NamingService_WaitForServerReadiness.hxx"
47 #include "utilities.h"
54 const int Session_ServerThread::NB_SRV_TYP = 6;
55 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
62 //=============================================================================
64 * default constructor not for use
66 //=============================================================================
68 Session_ServerThread::Session_ServerThread()
70 ASSERT(0); // must not be called
73 //=============================================================================
77 //=============================================================================
79 Session_ServerThread::Session_ServerThread(int argc,
82 PortableServer::POA_ptr poa,
85 //MESSAGE("Session_ServerThread Constructor " << argv[0]);
88 _orb = CORBA::ORB::_duplicate(orb);
89 _root_poa = PortableServer::POA::_duplicate(poa);
92 _NS = new SALOME_NamingService(_orb); // one instance per server to limit
93 // multi thread coherence problems
96 //=============================================================================
100 //=============================================================================
102 Session_ServerThread::~Session_ServerThread()
104 //MESSAGE("~Session_ServerThread "<< _argv[0]);
107 //=============================================================================
109 * run the thread : activate one servant, the servant type is given by
112 //=============================================================================
114 void Session_ServerThread::Init()
116 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
118 for (int i=0; i<_argc; i++) SCRUTE(_argv[i]);
119 for (int i=0; i<NB_SRV_TYP; i++)
120 if (strcmp(_argv[0],_serverTypes[i])==0)
123 MESSAGE("Server Thread type : "<<_serverTypes[i]);
128 NamingService_WaitForServerReadiness(_NS,"/Registry");
129 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
130 ActivateContainer(_argc, _argv);
133 case 1: // ModuleCatalog
135 NamingService_WaitForServerReadiness(_NS,"/Registry");
136 ActivateModuleCatalog(_argc, _argv);
141 NamingService_WaitForServerReadiness(_NS,"");
142 ActivateRegistry(_argc, _argv);
147 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
148 ActivateSALOMEDS(_argc, _argv);
153 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
154 string containerName = "/Containers/";
155 containerName = containerName + GetHostname();
156 containerName = containerName + "/FactoryServer";
157 NamingService_WaitForServerReadiness(_NS,containerName);
158 ActivateSession(_argc, _argv);
161 case 5: // Container Manager
163 NamingService_WaitForServerReadiness(_NS,"");
164 ActivateContainerManager(_argc, _argv);
176 //=============================================================================
180 //=============================================================================
182 void Session_ServerThread::ActivateModuleCatalog(int argc,
187 INFOS("ModuleCatalog thread started");
188 // allocation on heap to allow destruction by POA
190 SALOME_ModuleCatalogImpl* Catalogue_i
191 = new SALOME_ModuleCatalogImpl(argc, argv);
193 // Tell the POA that the objects are ready to accept requests.
195 _root_poa->activate_object (Catalogue_i);
197 CORBA::Object_ptr myCata = Catalogue_i->_this();
198 _NS->Register(myCata ,"/Kernel/ModulCatalog");
200 catch(CORBA::SystemException&)
202 INFOS( "Caught CORBA::SystemException." );
204 catch(CORBA::Exception&)
206 INFOS( "Caught CORBA::Exception." );
208 catch(omniORB::fatalException& fe)
210 INFOS( "Caught omniORB::fatalException:" );
211 INFOS( " file: " << fe.file() );
212 INFOS( " line: " << fe.line() );
213 INFOS( " mesg: " << fe.errmsg() );
217 INFOS( "Caught unknown exception." );
221 //=============================================================================
225 //=============================================================================
227 void Session_ServerThread::ActivateSALOMEDS(int argc,
232 INFOS("SALOMEDS thread started");
233 // We allocate the objects on the heap. Since these are reference
234 // counted objects, they will be deleted by the POA when they are no
237 SALOMEDS_StudyManager_i * myStudyManager_i
238 = new SALOMEDS_StudyManager_i(_orb,_root_poa);
240 // Activate the objects. This tells the POA that the objects are
241 // ready to accept requests.
243 PortableServer::ObjectId_var myStudyManager_iid
244 = _root_poa->activate_object(myStudyManager_i);
245 myStudyManager_i->register_name("/myStudyManager");
247 catch(CORBA::SystemException&)
249 INFOS( "Caught CORBA::SystemException." );
251 catch(CORBA::Exception&)
253 INFOS( "Caught CORBA::Exception." );
255 catch(omniORB::fatalException& fe)
257 INFOS( "Caught omniORB::fatalException:" );
258 INFOS( " file: " << fe.file() );
259 INFOS( " line: " << fe.line() );
260 INFOS( " mesg: " << fe.errmsg() );
264 INFOS( "Caught unknown exception." );
268 //=============================================================================
272 //=============================================================================
274 void Session_ServerThread::ActivateRegistry(int argc,
277 INFOS("Registry thread started");
281 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
282 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
284 const char *ptrSessionName=0;
287 for ( k=1 ; k<argc ; k++ )
289 if( strcmp(argv[k],"--salome_session")==0 )
291 ptrSessionName=argv[k+1];
295 ASSERT(ptrSessionName) ;
296 ASSERT(strlen( ptrSessionName )>0);
297 const char *registryName = "Registry";
298 Registry::Components_var varComponents;
301 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
302 ptrRegistry->SessionName( ptrSessionName );
303 varComponents = ptrRegistry->_this();
304 // The RegistryService must not already exist.
308 CORBA::Object_var pipo = _NS->Resolve( registryName );
309 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
310 INFOS("RegistryService servant already existing" );
313 catch( const ServiceUnreachable &ex )
316 catch( const CORBA::Exception &exx )
319 string absoluteName = string("/") + registryName;
320 _NS->Register( varComponents , absoluteName.c_str() );
321 MESSAGE("On attend les requetes des clients");
323 catch( const SALOME_Exception &ex )
325 INFOS( "Communication Error : " << ex.what() );
330 //=============================================================================
334 //=============================================================================
336 void Session_ServerThread::ActivateContainerManager(int argc,
341 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
342 cout << "ActivateContainerManager ......!!!! " << endl;
343 SALOME_ContainerManager * myContainer
344 = new SALOME_ContainerManager(_orb);
346 catch(CORBA::SystemException&)
348 INFOS("Caught CORBA::SystemException.");
350 catch(PortableServer::POA::WrongPolicy&)
352 INFOS("Caught CORBA::WrongPolicyException.");
354 catch(PortableServer::POA::ServantAlreadyActive&)
356 INFOS("Caught CORBA::ServantAlreadyActiveException");
358 catch(CORBA::Exception&)
360 INFOS("Caught CORBA::Exception.");
364 INFOS("Caught unknown exception.");
368 //=============================================================================
372 //=============================================================================
374 void Session_ServerThread::ActivateContainer(int argc,
379 INFOS("Container thread started");
381 // get or create the child POA
383 PortableServer::POA_var factory_poa;
386 factory_poa = _root_poa->find_POA("factory_poa",0);
387 // 0 = no activation (already done if exists)
389 catch (PortableServer::POA::AdapterNonExistent&)
391 INFOS("factory_poa does not exists, create...");
392 // define policy objects
393 PortableServer::ImplicitActivationPolicy_var implicitActivation =
394 _root_poa->create_implicit_activation_policy(
395 PortableServer::NO_IMPLICIT_ACTIVATION);
396 // default = NO_IMPLICIT_ACTIVATION
397 PortableServer::ThreadPolicy_var threadPolicy =
398 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
399 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
401 // create policy list
402 CORBA::PolicyList policyList;
403 policyList.length(2);
404 policyList[0] = PortableServer::ImplicitActivationPolicy::
405 _duplicate(implicitActivation);
406 policyList[1] = PortableServer::ThreadPolicy::
407 _duplicate(threadPolicy);
409 PortableServer::POAManager_var nil_mgr
410 = PortableServer::POAManager::_nil();
411 factory_poa = _root_poa->create_POA("factory_poa",
414 //with nil_mgr instead of pman,
415 //a new POA manager is created with the new POA
417 // destroy policy objects
418 implicitActivation->destroy();
419 threadPolicy->destroy();
421 // obtain the factory poa manager
422 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
424 MESSAGE("pmanfac->activate()");
427 char *containerName = "";
430 containerName = argv[1];
433 Engines_Container_i * myContainer
434 = new Engines_Container_i(_orb, factory_poa, containerName , argc , argv );
436 catch(CORBA::SystemException&)
438 INFOS("Caught CORBA::SystemException.");
440 catch(PortableServer::POA::WrongPolicy&)
442 INFOS("Caught CORBA::WrongPolicyException.");
444 catch(PortableServer::POA::ServantAlreadyActive&)
446 INFOS("Caught CORBA::ServantAlreadyActiveException");
448 catch(CORBA::Exception&)
450 INFOS("Caught CORBA::Exception.");
454 INFOS("Caught unknown exception.");
458 //=============================================================================
462 //=============================================================================
464 void Session_ServerThread::ActivateSession(int argc,
467 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
470 Session_SessionThread::Session_SessionThread(int argc,
473 PortableServer::POA_ptr poa,
475 QWaitCondition* GUILauncher)
476 : Session_ServerThread(argc, argv, orb, poa, GUIMutex),
477 _GUILauncher( GUILauncher )
481 Session_SessionThread::~Session_SessionThread()
485 void Session_SessionThread::ActivateSession(int argc,
490 INFOS("Session thread started");
491 SALOME_Session_i * mySALOME_Session
492 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
493 PortableServer::ObjectId_var mySALOME_Sessionid
494 = _root_poa->activate_object(mySALOME_Session);
495 INFOS("poa->activate_object(mySALOME_Session)");
497 CORBA::Object_var obj = mySALOME_Session->_this();
498 CORBA::String_var sior(_orb->object_to_string(obj));
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.");