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_ContainerManager.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]);
84 _orb = CORBA::ORB::_duplicate(orb);
85 _root_poa = PortableServer::POA::_duplicate(poa);
87 _NS = new SALOME_NamingService(_orb); // one instance per server to limit
88 // multi thread coherence problems
94 Session_ServerThread::~Session_ServerThread()
96 //MESSAGE("~Session_ServerThread "<< _argv[0]);
100 run the thread : activate one servant, the servant type is given by
103 void Session_ServerThread::Init()
105 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
107 for (int i=0; i<_argc; i++) SCRUTE(_argv[i]);
109 for (int i=0; i<NB_SRV_TYP; i++)
111 for (i=0; i<NB_SRV_TYP; i++)
113 if (strcmp(_argv[0],_serverTypes[i])==0)
116 MESSAGE("Server Thread type : "<<_serverTypes[i]);
121 NamingService_WaitForServerReadiness(_NS,"/Registry");
122 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
123 ActivateContainer(_argc, _argv);
126 case 1: // ModuleCatalog
128 NamingService_WaitForServerReadiness(_NS,"/Registry");
129 ActivateModuleCatalog(_argc, _argv);
134 NamingService_WaitForServerReadiness(_NS,"");
135 ActivateRegistry(_argc, _argv);
140 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
141 ActivateSALOMEDS(_argc, _argv);
146 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
147 string containerName = "/Containers/";
148 containerName = containerName + GetHostname();
149 containerName = containerName + "/FactoryServer";
150 NamingService_WaitForServerReadiness(_NS,containerName);
151 ActivateSession(_argc, _argv);
154 case 5: // SalomeApp_Engine
156 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
157 ActivateEngine(_argc, _argv);
160 case 6: // Container Manager
162 NamingService_WaitForServerReadiness(_NS,"");
163 ActivateContainerManager(_argc, _argv);
175 void Session_ServerThread::ActivateModuleCatalog(int argc,
180 INFOS("ModuleCatalog thread started");
181 // allocation on heap to allow destruction by POA
183 SALOME_ModuleCatalogImpl* Catalogue_i
184 = new SALOME_ModuleCatalogImpl(argc, argv);
186 // Tell the POA that the objects are ready to accept requests.
188 _root_poa->activate_object (Catalogue_i);
190 CORBA::Object_ptr myCata = Catalogue_i->_this();
191 _NS->Register(myCata ,"/Kernel/ModulCatalog");
193 catch(CORBA::SystemException&)
195 INFOS( "Caught CORBA::SystemException." );
197 catch(CORBA::Exception&)
199 INFOS( "Caught CORBA::Exception." );
201 catch(omniORB::fatalException& fe)
203 INFOS( "Caught omniORB::fatalException:" );
204 INFOS( " file: " << fe.file() );
205 INFOS( " line: " << fe.line() );
206 INFOS( " mesg: " << fe.errmsg() );
210 INFOS( "Caught unknown exception." );
214 void Session_ServerThread::ActivateSALOMEDS(int argc,
219 INFOS("SALOMEDS thread started");
220 // We allocate the objects on the heap. Since these are reference
221 // counted objects, they will be deleted by the POA when they are no
224 ClientFactory::createStudyManager(_orb,_root_poa);
226 catch(CORBA::SystemException&)
228 INFOS( "Caught CORBA::SystemException." );
230 catch(CORBA::Exception&)
232 INFOS( "Caught CORBA::Exception." );
234 catch(omniORB::fatalException& fe)
236 INFOS( "Caught omniORB::fatalException:" );
237 INFOS( " file: " << fe.file() );
238 INFOS( " line: " << fe.line() );
239 INFOS( " mesg: " << fe.errmsg() );
243 INFOS( "Caught unknown exception." );
247 void Session_ServerThread::ActivateRegistry(int argc,
250 INFOS("Registry thread started");
254 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
255 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
257 const char *ptrSessionName=0;
260 for ( k=1 ; k<argc ; k++ )
262 if( strcmp(argv[k],"--salome_session")==0 )
264 ptrSessionName=argv[k+1];
268 ASSERT(ptrSessionName) ;
269 ASSERT(strlen( ptrSessionName )>0);
270 const char *registryName = "Registry";
271 Registry::Components_var varComponents;
274 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
275 ptrRegistry->SessionName( ptrSessionName );
276 varComponents = ptrRegistry->_this();
277 // The RegistryService must not already exist.
281 CORBA::Object_var pipo = _NS->Resolve( registryName );
282 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
283 INFOS("RegistryService servant already existing" );
286 catch( const ServiceUnreachable &ex )
289 catch( const CORBA::Exception &exx )
292 string absoluteName = string("/") + registryName;
293 _NS->Register( varComponents , absoluteName.c_str() );
294 MESSAGE("On attend les requetes des clients");
296 catch( const SALOME_Exception &ex )
298 INFOS( "Communication Error : " << ex.what() );
303 void Session_ServerThread::ActivateContainerManager(int argc,
308 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
309 cout << "ActivateContainerManager ......!!!! " << endl;
310 SALOME_ContainerManager * myContainer
311 = new SALOME_ContainerManager(_orb);
313 catch(CORBA::SystemException&)
315 INFOS("Caught CORBA::SystemException.");
317 catch(PortableServer::POA::WrongPolicy&)
319 INFOS("Caught CORBA::WrongPolicyException.");
321 catch(PortableServer::POA::ServantAlreadyActive&)
323 INFOS("Caught CORBA::ServantAlreadyActiveException");
325 catch(CORBA::Exception&)
327 INFOS("Caught CORBA::Exception.");
331 INFOS("Caught unknown exception.");
335 void Session_ServerThread::ActivateContainer(int argc,
340 INFOS("Container thread started");
342 // get or create the child POA
344 PortableServer::POA_var factory_poa;
347 factory_poa = _root_poa->find_POA("factory_poa",0);
348 // 0 = no activation (already done if exists)
350 catch (PortableServer::POA::AdapterNonExistent&)
352 INFOS("factory_poa does not exists, create...");
353 // define policy objects
354 PortableServer::ImplicitActivationPolicy_var implicitActivation =
355 _root_poa->create_implicit_activation_policy(
356 PortableServer::NO_IMPLICIT_ACTIVATION);
357 // default = NO_IMPLICIT_ACTIVATION
358 PortableServer::ThreadPolicy_var threadPolicy =
359 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
360 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
362 // create policy list
363 CORBA::PolicyList policyList;
364 policyList.length(2);
365 policyList[0] = PortableServer::ImplicitActivationPolicy::
366 _duplicate(implicitActivation);
367 policyList[1] = PortableServer::ThreadPolicy::
368 _duplicate(threadPolicy);
370 PortableServer::POAManager_var nil_mgr
371 = PortableServer::POAManager::_nil();
372 factory_poa = _root_poa->create_POA("factory_poa",
375 //with nil_mgr instead of pman,
376 //a new POA manager is created with the new POA
378 // destroy policy objects
379 implicitActivation->destroy();
380 threadPolicy->destroy();
382 // obtain the factory poa manager
383 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
385 MESSAGE("pmanfac->activate()");
388 char *containerName = "";
391 containerName = argv[1];
394 Engines_Container_i * myContainer
395 = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
397 catch(CORBA::SystemException&)
399 INFOS("Caught CORBA::SystemException.");
401 catch(PortableServer::POA::WrongPolicy&)
403 INFOS("Caught CORBA::WrongPolicyException.");
405 catch(PortableServer::POA::ServantAlreadyActive&)
407 INFOS("Caught CORBA::ServantAlreadyActiveException");
409 catch(CORBA::Exception&)
411 INFOS("Caught CORBA::Exception.");
415 INFOS("Caught unknown exception.");
419 void Session_ServerThread::ActivateEngine(int /*argc*/, char ** /*argv*/)
423 INFOS("SalomeApp_Engine thread started");
424 SalomeApp_Engine_i* anEngine = new SalomeApp_Engine_i();
425 /*PortableServer::ObjectId_var id = */_root_poa->activate_object( anEngine );
426 INFOS("poa->activate_object( SalomeApp_Engine )");
428 CORBA::Object_ptr obj = anEngine->_this();
429 _NS->Register( obj ,"/SalomeAppEngine");
432 catch (CORBA::SystemException&)
434 INFOS("Caught CORBA::SystemException.");
436 catch (CORBA::Exception&)
438 INFOS("Caught CORBA::Exception.");
442 INFOS("Caught unknown exception.");
446 void Session_ServerThread::ActivateSession(int argc,
449 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
455 Session_SessionThread::Session_SessionThread(int argc,
458 PortableServer::POA_ptr poa,
460 QWaitCondition* GUILauncher)
461 : Session_ServerThread(argc, argv, orb, poa),
462 _GUIMutex( GUIMutex ),
463 _GUILauncher( GUILauncher )
470 Session_SessionThread::~Session_SessionThread()
474 void Session_SessionThread::ActivateSession(int argc,
479 INFOS("Session thread started");
480 SALOME_Session_i * mySALOME_Session
481 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
482 PortableServer::ObjectId_var mySALOME_Sessionid
483 = _root_poa->activate_object(mySALOME_Session);
484 INFOS("poa->activate_object(mySALOME_Session)");
486 CORBA::Object_var obj = mySALOME_Session->_this();
487 CORBA::String_var sior(_orb->object_to_string(obj));
489 mySALOME_Session->NSregister();
491 catch (CORBA::SystemException&)
493 INFOS("Caught CORBA::SystemException.");
495 catch (CORBA::Exception&)
497 INFOS("Caught CORBA::Exception.");
501 INFOS("Caught unknown exception.");