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);
173 case 6: // Container Manager
175 NamingService_WaitForServerReadiness(_NS,"");
176 ActivateContainerManager(_argc, _argv);
188 //=============================================================================
192 //=============================================================================
194 void Session_ServerThread::ActivateModuleCatalog(int argc,
199 INFOS("ModuleCatalog thread started");
200 // allocation on heap to allow destruction by POA
202 SALOME_ModuleCatalogImpl* Catalogue_i
203 = new SALOME_ModuleCatalogImpl(argc, argv);
205 // Tell the POA that the objects are ready to accept requests.
207 _root_poa->activate_object (Catalogue_i);
209 CORBA::Object_ptr myCata = Catalogue_i->_this();
210 _NS->Register(myCata ,"/Kernel/ModulCatalog");
212 catch(CORBA::SystemException&)
214 INFOS( "Caught CORBA::SystemException." );
216 catch(CORBA::Exception&)
218 INFOS( "Caught CORBA::Exception." );
220 catch(omniORB::fatalException& fe)
222 INFOS( "Caught omniORB::fatalException:" );
223 INFOS( " file: " << fe.file() );
224 INFOS( " line: " << fe.line() );
225 INFOS( " mesg: " << fe.errmsg() );
229 INFOS( "Caught unknown exception." );
233 //=============================================================================
237 //=============================================================================
239 void Session_ServerThread::ActivateSALOMEDS(int argc,
244 INFOS("SALOMEDS thread started");
245 // We allocate the objects on the heap. Since these are reference
246 // counted objects, they will be deleted by the POA when they are no
249 SALOMEDS_StudyManager_i * myStudyManager_i
250 = new SALOMEDS_StudyManager_i(_orb,_root_poa);
252 // Activate the objects. This tells the POA that the objects are
253 // ready to accept requests.
255 PortableServer::ObjectId_var myStudyManager_iid
256 = _root_poa->activate_object(myStudyManager_i);
257 myStudyManager_i->register_name("/myStudyManager");
259 catch(CORBA::SystemException&)
261 INFOS( "Caught CORBA::SystemException." );
263 catch(CORBA::Exception&)
265 INFOS( "Caught CORBA::Exception." );
267 catch(omniORB::fatalException& fe)
269 INFOS( "Caught omniORB::fatalException:" );
270 INFOS( " file: " << fe.file() );
271 INFOS( " line: " << fe.line() );
272 INFOS( " mesg: " << fe.errmsg() );
276 INFOS( "Caught unknown exception." );
280 //=============================================================================
284 //=============================================================================
286 void Session_ServerThread::ActivateRegistry(int argc,
289 INFOS("Registry thread started");
293 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
294 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
296 const char *ptrSessionName=0;
299 for ( k=1 ; k<argc ; k++ )
301 if( strcmp(argv[k],"--salome_session")==0 )
303 ptrSessionName=argv[k+1];
307 ASSERT(ptrSessionName) ;
308 ASSERT(strlen( ptrSessionName )>0);
309 const char *registryName = "Registry";
310 Registry::Components_var varComponents;
313 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
314 ptrRegistry->SessionName( ptrSessionName );
315 varComponents = ptrRegistry->_this();
316 // The RegistryService must not already exist.
320 CORBA::Object_var pipo = _NS->Resolve( registryName );
321 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
322 INFOS("RegistryService servant already existing" );
325 catch( const ServiceUnreachable &ex )
328 catch( const CORBA::Exception &exx )
331 string absoluteName = string("/") + registryName;
332 _NS->Register( varComponents , absoluteName.c_str() );
333 MESSAGE("On attend les requetes des clients");
335 catch( const SALOME_Exception &ex )
337 INFOS( "Communication Error : " << ex.what() );
342 //=============================================================================
346 //=============================================================================
348 void Session_ServerThread::ActivateContainerManager(int argc,
353 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
354 cout << "ActivateContainerManager ......!!!! " << endl;
355 SALOME_ContainerManager * myContainer
356 = new SALOME_ContainerManager(_orb);
358 catch(CORBA::SystemException&)
360 INFOS("Caught CORBA::SystemException.");
362 catch(PortableServer::POA::WrongPolicy&)
364 INFOS("Caught CORBA::WrongPolicyException.");
366 catch(PortableServer::POA::ServantAlreadyActive&)
368 INFOS("Caught CORBA::ServantAlreadyActiveException");
370 catch(CORBA::Exception&)
372 INFOS("Caught CORBA::Exception.");
376 INFOS("Caught unknown exception.");
380 //=============================================================================
384 //=============================================================================
386 void Session_ServerThread::ActivateContainer(int argc,
391 INFOS("Container thread started");
393 // get or create the child POA
395 PortableServer::POA_var factory_poa;
398 factory_poa = _root_poa->find_POA("factory_poa",0);
399 // 0 = no activation (already done if exists)
401 catch (PortableServer::POA::AdapterNonExistent&)
403 INFOS("factory_poa does not exists, create...");
404 // define policy objects
405 PortableServer::ImplicitActivationPolicy_var implicitActivation =
406 _root_poa->create_implicit_activation_policy(
407 PortableServer::NO_IMPLICIT_ACTIVATION);
408 // default = NO_IMPLICIT_ACTIVATION
409 PortableServer::ThreadPolicy_var threadPolicy =
410 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
411 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
413 // create policy list
414 CORBA::PolicyList policyList;
415 policyList.length(2);
416 policyList[0] = PortableServer::ImplicitActivationPolicy::
417 _duplicate(implicitActivation);
418 policyList[1] = PortableServer::ThreadPolicy::
419 _duplicate(threadPolicy);
421 PortableServer::POAManager_var nil_mgr
422 = PortableServer::POAManager::_nil();
423 factory_poa = _root_poa->create_POA("factory_poa",
426 //with nil_mgr instead of pman,
427 //a new POA manager is created with the new POA
429 // destroy policy objects
430 implicitActivation->destroy();
431 threadPolicy->destroy();
433 // obtain the factory poa manager
434 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
436 MESSAGE("pmanfac->activate()");
439 char *containerName = "";
442 containerName = argv[1];
445 Engines_Container_i * myContainer
446 = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
448 catch(CORBA::SystemException&)
450 INFOS("Caught CORBA::SystemException.");
452 catch(PortableServer::POA::WrongPolicy&)
454 INFOS("Caught CORBA::WrongPolicyException.");
456 catch(PortableServer::POA::ServantAlreadyActive&)
458 INFOS("Caught CORBA::ServantAlreadyActiveException");
460 catch(CORBA::Exception&)
462 INFOS("Caught CORBA::Exception.");
466 INFOS("Caught unknown exception.");
470 //=============================================================================
474 //=============================================================================
476 void Session_ServerThread::ActivateEngine(int /*argc*/, char ** /*argv*/)
480 INFOS("SalomeApp_Engine thread started");
481 SalomeApp_Engine_i* anEngine = new SalomeApp_Engine_i();
482 /*PortableServer::ObjectId_var id = */_root_poa->activate_object( anEngine );
483 INFOS("poa->activate_object( SalomeApp_Engine )");
485 CORBA::Object_ptr obj = anEngine->_this();
486 _NS->Register( obj ,"/SalomeAppEngine");
489 catch (CORBA::SystemException&)
491 INFOS("Caught CORBA::SystemException.");
493 catch (CORBA::Exception&)
495 INFOS("Caught CORBA::Exception.");
499 INFOS("Caught unknown exception.");
503 //=============================================================================
507 //=============================================================================
509 void Session_ServerThread::ActivateSession(int argc,
512 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
515 Session_SessionThread::Session_SessionThread(int argc,
518 PortableServer::POA_ptr poa,
520 QWaitCondition* GUILauncher)
521 : Session_ServerThread(argc, argv, orb, poa, GUIMutex),
522 _GUILauncher( GUILauncher )
526 Session_SessionThread::~Session_SessionThread()
530 void Session_SessionThread::ActivateSession(int argc,
535 INFOS("Session thread started");
536 SALOME_Session_i * mySALOME_Session
537 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
538 PortableServer::ObjectId_var mySALOME_Sessionid
539 = _root_poa->activate_object(mySALOME_Session);
540 INFOS("poa->activate_object(mySALOME_Session)");
542 CORBA::Object_var obj = mySALOME_Session->_this();
543 CORBA::String_var sior(_orb->object_to_string(obj));
545 mySALOME_Session->NSregister();
547 catch (CORBA::SystemException&)
549 INFOS("Caught CORBA::SystemException.");
551 catch (CORBA::Exception&)
553 INFOS("Caught CORBA::Exception.");
557 INFOS("Caught unknown exception.");