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 "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"
47 #include "NamingService_WaitForServerReadiness.hxx"
48 #include "utilities.h"
55 const int Session_ServerThread::NB_SRV_TYP = 7;
56 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
64 //=============================================================================
66 * default constructor not for use
68 //=============================================================================
70 Session_ServerThread::Session_ServerThread()
72 ASSERT(0); // must not be called
75 //=============================================================================
79 //=============================================================================
81 Session_ServerThread::Session_ServerThread(int argc,
84 PortableServer::POA_ptr poa,
87 //MESSAGE("Session_ServerThread Constructor " << argv[0]);
90 _orb = CORBA::ORB::_duplicate(orb);
91 _root_poa = PortableServer::POA::_duplicate(poa);
94 _NS = new SALOME_NamingService(_orb); // one instance per server to limit
95 // multi thread coherence problems
98 //=============================================================================
102 //=============================================================================
104 Session_ServerThread::~Session_ServerThread()
106 //MESSAGE("~Session_ServerThread "<< _argv[0]);
109 //=============================================================================
111 * run the thread : activate one servant, the servant type is given by
114 //=============================================================================
116 void Session_ServerThread::Init()
118 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
120 for (int i=0; i<_argc; i++) SCRUTE(_argv[i]);
122 for (int i=0; i<NB_SRV_TYP; i++)
124 for (i=0; i<NB_SRV_TYP; i++)
126 if (strcmp(_argv[0],_serverTypes[i])==0)
129 MESSAGE("Server Thread type : "<<_serverTypes[i]);
134 NamingService_WaitForServerReadiness(_NS,"/Registry");
135 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
136 ActivateContainer(_argc, _argv);
139 case 1: // ModuleCatalog
141 NamingService_WaitForServerReadiness(_NS,"/Registry");
142 ActivateModuleCatalog(_argc, _argv);
147 NamingService_WaitForServerReadiness(_NS,"");
148 ActivateRegistry(_argc, _argv);
153 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
154 ActivateSALOMEDS(_argc, _argv);
159 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
160 string containerName = "/Containers/";
161 containerName = containerName + GetHostname();
162 containerName = containerName + "/FactoryServer";
163 NamingService_WaitForServerReadiness(_NS,containerName);
164 ActivateSession(_argc, _argv);
167 case 5: // SalomeApp_Engine
169 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
170 ActivateEngine(_argc, _argv);
172 case 6: // Container Manager
174 NamingService_WaitForServerReadiness(_NS,"");
175 ActivateContainerManager(_argc, _argv);
187 //=============================================================================
191 //=============================================================================
193 void Session_ServerThread::ActivateModuleCatalog(int argc,
198 INFOS("ModuleCatalog thread started");
199 // allocation on heap to allow destruction by POA
201 SALOME_ModuleCatalogImpl* Catalogue_i
202 = new SALOME_ModuleCatalogImpl(argc, argv);
204 // Tell the POA that the objects are ready to accept requests.
206 _root_poa->activate_object (Catalogue_i);
208 CORBA::Object_ptr myCata = Catalogue_i->_this();
209 _NS->Register(myCata ,"/Kernel/ModulCatalog");
211 catch(CORBA::SystemException&)
213 INFOS( "Caught CORBA::SystemException." );
215 catch(CORBA::Exception&)
217 INFOS( "Caught CORBA::Exception." );
219 catch(omniORB::fatalException& fe)
221 INFOS( "Caught omniORB::fatalException:" );
222 INFOS( " file: " << fe.file() );
223 INFOS( " line: " << fe.line() );
224 INFOS( " mesg: " << fe.errmsg() );
228 INFOS( "Caught unknown exception." );
232 //=============================================================================
236 //=============================================================================
238 void Session_ServerThread::ActivateSALOMEDS(int argc,
243 INFOS("SALOMEDS thread started");
244 // We allocate the objects on the heap. Since these are reference
245 // counted objects, they will be deleted by the POA when they are no
248 SALOMEDS_StudyManager_i * myStudyManager_i
249 = new SALOMEDS_StudyManager_i(_orb,_root_poa);
251 // Activate the objects. This tells the POA that the objects are
252 // ready to accept requests.
254 PortableServer::ObjectId_var myStudyManager_iid
255 = _root_poa->activate_object(myStudyManager_i);
256 myStudyManager_i->register_name("/myStudyManager");
258 catch(CORBA::SystemException&)
260 INFOS( "Caught CORBA::SystemException." );
262 catch(CORBA::Exception&)
264 INFOS( "Caught CORBA::Exception." );
266 catch(omniORB::fatalException& fe)
268 INFOS( "Caught omniORB::fatalException:" );
269 INFOS( " file: " << fe.file() );
270 INFOS( " line: " << fe.line() );
271 INFOS( " mesg: " << fe.errmsg() );
275 INFOS( "Caught unknown exception." );
279 //=============================================================================
283 //=============================================================================
285 void Session_ServerThread::ActivateRegistry(int argc,
288 INFOS("Registry thread started");
292 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
293 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
295 const char *ptrSessionName=0;
298 for ( k=1 ; k<argc ; k++ )
300 if( strcmp(argv[k],"--salome_session")==0 )
302 ptrSessionName=argv[k+1];
306 ASSERT(ptrSessionName) ;
307 ASSERT(strlen( ptrSessionName )>0);
308 const char *registryName = "Registry";
309 Registry::Components_var varComponents;
312 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
313 ptrRegistry->SessionName( ptrSessionName );
314 varComponents = ptrRegistry->_this();
315 // The RegistryService must not already exist.
319 CORBA::Object_var pipo = _NS->Resolve( registryName );
320 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
321 INFOS("RegistryService servant already existing" );
324 catch( const ServiceUnreachable &ex )
327 catch( const CORBA::Exception &exx )
330 string absoluteName = string("/") + registryName;
331 _NS->Register( varComponents , absoluteName.c_str() );
332 MESSAGE("On attend les requetes des clients");
334 catch( const SALOME_Exception &ex )
336 INFOS( "Communication Error : " << ex.what() );
341 //=============================================================================
345 //=============================================================================
347 void Session_ServerThread::ActivateContainerManager(int argc,
352 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
353 cout << "ActivateContainerManager ......!!!! " << endl;
354 SALOME_ContainerManager * myContainer
355 = new SALOME_ContainerManager(_orb);
357 catch(CORBA::SystemException&)
359 INFOS("Caught CORBA::SystemException.");
361 catch(PortableServer::POA::WrongPolicy&)
363 INFOS("Caught CORBA::WrongPolicyException.");
365 catch(PortableServer::POA::ServantAlreadyActive&)
367 INFOS("Caught CORBA::ServantAlreadyActiveException");
369 catch(CORBA::Exception&)
371 INFOS("Caught CORBA::Exception.");
375 INFOS("Caught unknown exception.");
379 //=============================================================================
383 //=============================================================================
385 void Session_ServerThread::ActivateContainer(int argc,
390 INFOS("Container thread started");
392 // get or create the child POA
394 PortableServer::POA_var factory_poa;
397 factory_poa = _root_poa->find_POA("factory_poa",0);
398 // 0 = no activation (already done if exists)
400 catch (PortableServer::POA::AdapterNonExistent&)
402 INFOS("factory_poa does not exists, create...");
403 // define policy objects
404 PortableServer::ImplicitActivationPolicy_var implicitActivation =
405 _root_poa->create_implicit_activation_policy(
406 PortableServer::NO_IMPLICIT_ACTIVATION);
407 // default = NO_IMPLICIT_ACTIVATION
408 PortableServer::ThreadPolicy_var threadPolicy =
409 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
410 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
412 // create policy list
413 CORBA::PolicyList policyList;
414 policyList.length(2);
415 policyList[0] = PortableServer::ImplicitActivationPolicy::
416 _duplicate(implicitActivation);
417 policyList[1] = PortableServer::ThreadPolicy::
418 _duplicate(threadPolicy);
420 PortableServer::POAManager_var nil_mgr
421 = PortableServer::POAManager::_nil();
422 factory_poa = _root_poa->create_POA("factory_poa",
425 //with nil_mgr instead of pman,
426 //a new POA manager is created with the new POA
428 // destroy policy objects
429 implicitActivation->destroy();
430 threadPolicy->destroy();
432 // obtain the factory poa manager
433 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
435 MESSAGE("pmanfac->activate()");
438 char *containerName = "";
441 containerName = argv[1];
444 Engines_Container_i * myContainer
445 = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
447 catch(CORBA::SystemException&)
449 INFOS("Caught CORBA::SystemException.");
451 catch(PortableServer::POA::WrongPolicy&)
453 INFOS("Caught CORBA::WrongPolicyException.");
455 catch(PortableServer::POA::ServantAlreadyActive&)
457 INFOS("Caught CORBA::ServantAlreadyActiveException");
459 catch(CORBA::Exception&)
461 INFOS("Caught CORBA::Exception.");
465 INFOS("Caught unknown exception.");
469 //=============================================================================
473 //=============================================================================
475 void Session_ServerThread::ActivateEngine(int /*argc*/, char ** /*argv*/)
479 INFOS("SalomeApp_Engine thread started");
480 SalomeApp_Engine_i* anEngine = new SalomeApp_Engine_i();
481 /*PortableServer::ObjectId_var id = */_root_poa->activate_object( anEngine );
482 INFOS("poa->activate_object( SalomeApp_Engine )");
484 CORBA::Object_ptr obj = anEngine->_this();
485 _NS->Register( obj ,"/SalomeAppEngine");
488 catch (CORBA::SystemException&)
490 INFOS("Caught CORBA::SystemException.");
492 catch (CORBA::Exception&)
494 INFOS("Caught CORBA::Exception.");
498 INFOS("Caught unknown exception.");
502 //=============================================================================
506 //=============================================================================
508 void Session_ServerThread::ActivateSession(int argc,
511 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
514 Session_SessionThread::Session_SessionThread(int argc,
517 PortableServer::POA_ptr poa,
519 QWaitCondition* GUILauncher)
520 : Session_ServerThread(argc, argv, orb, poa, GUIMutex),
521 _GUILauncher( GUILauncher )
525 Session_SessionThread::~Session_SessionThread()
529 void Session_SessionThread::ActivateSession(int argc,
534 INFOS("Session thread started");
535 SALOME_Session_i * mySALOME_Session
536 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
537 PortableServer::ObjectId_var mySALOME_Sessionid
538 = _root_poa->activate_object(mySALOME_Session);
539 INFOS("poa->activate_object(mySALOME_Session)");
541 CORBA::Object_var obj = mySALOME_Session->_this();
542 CORBA::String_var sior(_orb->object_to_string(obj));
544 mySALOME_Session->NSregister();
546 catch (CORBA::SystemException&)
548 INFOS("Caught CORBA::SystemException.");
550 catch (CORBA::Exception&)
552 INFOS("Caught CORBA::Exception.");
556 INFOS("Caught unknown exception.");