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]);
121 for (int i=0; i<NB_SRV_TYP; i++)
122 if (strcmp(_argv[0],_serverTypes[i])==0)
125 MESSAGE("Server Thread type : "<<_serverTypes[i]);
130 NamingService_WaitForServerReadiness(_NS,"/Registry");
131 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
132 ActivateContainer(_argc, _argv);
135 case 1: // ModuleCatalog
137 NamingService_WaitForServerReadiness(_NS,"/Registry");
138 ActivateModuleCatalog(_argc, _argv);
143 NamingService_WaitForServerReadiness(_NS,"");
144 ActivateRegistry(_argc, _argv);
149 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
150 ActivateSALOMEDS(_argc, _argv);
155 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
156 string containerName = "/Containers/";
157 containerName = containerName + GetHostname();
158 containerName = containerName + "/FactoryServer";
159 NamingService_WaitForServerReadiness(_NS,containerName);
160 ActivateSession(_argc, _argv);
163 case 5: // SalomeApp_Engine
165 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
166 ActivateEngine(_argc, _argv);
168 case 6: // Container Manager
170 NamingService_WaitForServerReadiness(_NS,"");
171 ActivateContainerManager(_argc, _argv);
183 //=============================================================================
187 //=============================================================================
189 void Session_ServerThread::ActivateModuleCatalog(int argc,
194 INFOS("ModuleCatalog thread started");
195 // allocation on heap to allow destruction by POA
197 SALOME_ModuleCatalogImpl* Catalogue_i
198 = new SALOME_ModuleCatalogImpl(argc, argv);
200 // Tell the POA that the objects are ready to accept requests.
202 _root_poa->activate_object (Catalogue_i);
204 CORBA::Object_ptr myCata = Catalogue_i->_this();
205 _NS->Register(myCata ,"/Kernel/ModulCatalog");
207 catch(CORBA::SystemException&)
209 INFOS( "Caught CORBA::SystemException." );
211 catch(CORBA::Exception&)
213 INFOS( "Caught CORBA::Exception." );
215 catch(omniORB::fatalException& fe)
217 INFOS( "Caught omniORB::fatalException:" );
218 INFOS( " file: " << fe.file() );
219 INFOS( " line: " << fe.line() );
220 INFOS( " mesg: " << fe.errmsg() );
224 INFOS( "Caught unknown exception." );
228 //=============================================================================
232 //=============================================================================
234 void Session_ServerThread::ActivateSALOMEDS(int argc,
239 INFOS("SALOMEDS thread started");
240 // We allocate the objects on the heap. Since these are reference
241 // counted objects, they will be deleted by the POA when they are no
244 SALOMEDS_StudyManager_i * myStudyManager_i
245 = new SALOMEDS_StudyManager_i(_orb,_root_poa);
247 // Activate the objects. This tells the POA that the objects are
248 // ready to accept requests.
250 PortableServer::ObjectId_var myStudyManager_iid
251 = _root_poa->activate_object(myStudyManager_i);
252 myStudyManager_i->register_name("/myStudyManager");
254 catch(CORBA::SystemException&)
256 INFOS( "Caught CORBA::SystemException." );
258 catch(CORBA::Exception&)
260 INFOS( "Caught CORBA::Exception." );
262 catch(omniORB::fatalException& fe)
264 INFOS( "Caught omniORB::fatalException:" );
265 INFOS( " file: " << fe.file() );
266 INFOS( " line: " << fe.line() );
267 INFOS( " mesg: " << fe.errmsg() );
271 INFOS( "Caught unknown exception." );
275 //=============================================================================
279 //=============================================================================
281 void Session_ServerThread::ActivateRegistry(int argc,
284 INFOS("Registry thread started");
288 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
289 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
291 const char *ptrSessionName=0;
294 for ( k=1 ; k<argc ; k++ )
296 if( strcmp(argv[k],"--salome_session")==0 )
298 ptrSessionName=argv[k+1];
302 ASSERT(ptrSessionName) ;
303 ASSERT(strlen( ptrSessionName )>0);
304 const char *registryName = "Registry";
305 Registry::Components_var varComponents;
308 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
309 ptrRegistry->SessionName( ptrSessionName );
310 varComponents = ptrRegistry->_this();
311 // The RegistryService must not already exist.
315 CORBA::Object_var pipo = _NS->Resolve( registryName );
316 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
317 INFOS("RegistryService servant already existing" );
320 catch( const ServiceUnreachable &ex )
323 catch( const CORBA::Exception &exx )
326 string absoluteName = string("/") + registryName;
327 _NS->Register( varComponents , absoluteName.c_str() );
328 MESSAGE("On attend les requetes des clients");
330 catch( const SALOME_Exception &ex )
332 INFOS( "Communication Error : " << ex.what() );
337 //=============================================================================
341 //=============================================================================
343 void Session_ServerThread::ActivateContainerManager(int argc,
348 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
349 cout << "ActivateContainerManager ......!!!! " << endl;
350 SALOME_ContainerManager * myContainer
351 = new SALOME_ContainerManager(_orb);
353 catch(CORBA::SystemException&)
355 INFOS("Caught CORBA::SystemException.");
357 catch(PortableServer::POA::WrongPolicy&)
359 INFOS("Caught CORBA::WrongPolicyException.");
361 catch(PortableServer::POA::ServantAlreadyActive&)
363 INFOS("Caught CORBA::ServantAlreadyActiveException");
365 catch(CORBA::Exception&)
367 INFOS("Caught CORBA::Exception.");
371 INFOS("Caught unknown exception.");
375 //=============================================================================
379 //=============================================================================
381 void Session_ServerThread::ActivateContainer(int argc,
386 INFOS("Container thread started");
388 // get or create the child POA
390 PortableServer::POA_var factory_poa;
393 factory_poa = _root_poa->find_POA("factory_poa",0);
394 // 0 = no activation (already done if exists)
396 catch (PortableServer::POA::AdapterNonExistent&)
398 INFOS("factory_poa does not exists, create...");
399 // define policy objects
400 PortableServer::ImplicitActivationPolicy_var implicitActivation =
401 _root_poa->create_implicit_activation_policy(
402 PortableServer::NO_IMPLICIT_ACTIVATION);
403 // default = NO_IMPLICIT_ACTIVATION
404 PortableServer::ThreadPolicy_var threadPolicy =
405 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
406 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
408 // create policy list
409 CORBA::PolicyList policyList;
410 policyList.length(2);
411 policyList[0] = PortableServer::ImplicitActivationPolicy::
412 _duplicate(implicitActivation);
413 policyList[1] = PortableServer::ThreadPolicy::
414 _duplicate(threadPolicy);
416 PortableServer::POAManager_var nil_mgr
417 = PortableServer::POAManager::_nil();
418 factory_poa = _root_poa->create_POA("factory_poa",
421 //with nil_mgr instead of pman,
422 //a new POA manager is created with the new POA
424 // destroy policy objects
425 implicitActivation->destroy();
426 threadPolicy->destroy();
428 // obtain the factory poa manager
429 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
431 MESSAGE("pmanfac->activate()");
434 char *containerName = "";
437 containerName = argv[1];
440 Engines_Container_i * myContainer
441 = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
443 catch(CORBA::SystemException&)
445 INFOS("Caught CORBA::SystemException.");
447 catch(PortableServer::POA::WrongPolicy&)
449 INFOS("Caught CORBA::WrongPolicyException.");
451 catch(PortableServer::POA::ServantAlreadyActive&)
453 INFOS("Caught CORBA::ServantAlreadyActiveException");
455 catch(CORBA::Exception&)
457 INFOS("Caught CORBA::Exception.");
461 INFOS("Caught unknown exception.");
465 //=============================================================================
469 //=============================================================================
471 void Session_ServerThread::ActivateEngine(int /*argc*/, char ** /*argv*/)
475 INFOS("SalomeApp_Engine thread started");
476 SalomeApp_Engine_i* anEngine = new SalomeApp_Engine_i();
477 /*PortableServer::ObjectId_var id = */_root_poa->activate_object( anEngine );
478 INFOS("poa->activate_object( SalomeApp_Engine )");
480 CORBA::Object_ptr obj = anEngine->_this();
481 _NS->Register( obj ,"/SalomeAppEngine");
484 catch (CORBA::SystemException&)
486 INFOS("Caught CORBA::SystemException.");
488 catch (CORBA::Exception&)
490 INFOS("Caught CORBA::Exception.");
494 INFOS("Caught unknown exception.");
498 //=============================================================================
502 //=============================================================================
504 void Session_ServerThread::ActivateSession(int argc,
507 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
510 Session_SessionThread::Session_SessionThread(int argc,
513 PortableServer::POA_ptr poa,
515 QWaitCondition* GUILauncher)
516 : Session_ServerThread(argc, argv, orb, poa, GUIMutex),
517 _GUILauncher( GUILauncher )
521 Session_SessionThread::~Session_SessionThread()
525 void Session_SessionThread::ActivateSession(int argc,
530 INFOS("Session thread started");
531 SALOME_Session_i * mySALOME_Session
532 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
533 PortableServer::ObjectId_var mySALOME_Sessionid
534 = _root_poa->activate_object(mySALOME_Session);
535 INFOS("poa->activate_object(mySALOME_Session)");
537 CORBA::Object_var obj = mySALOME_Session->_this();
538 CORBA::String_var sior(_orb->object_to_string(obj));
540 mySALOME_Session->NSregister();
542 catch (CORBA::SystemException&)
544 INFOS("Caught CORBA::SystemException.");
546 catch (CORBA::Exception&)
548 INFOS("Caught CORBA::Exception.");
552 INFOS("Caught unknown exception.");