1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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, or (at your option) any later version.
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
23 // SALOME Session : implementation of Session_ServerThread.cxx
24 // File : Session_ServerThread.cxx
25 // Author : Paul RASCLE, EDF
27 #include "Session_ServerThread.hxx"
29 #include <SALOME_NamingService.hxx>
30 #include <SALOME_Container_i.hxx>
31 #include <SALOME_Launcher.hxx>
32 #include <SALOMEDSClient_ClientFactory.hxx>
33 #include <SALOME_ModuleCatalog_impl.hxx>
34 #include <RegistryService.hxx>
36 #include "Session_Session_i.hxx"
38 #include <Utils_ORB_INIT.hxx>
39 #include <Utils_SINGLETON.hxx>
40 #include <Utils_SALOME_Exception.hxx>
41 #include <Basics_Utils.hxx>
42 #include <NamingService_WaitForServerReadiness.hxx>
43 #include <utilities.h>
49 #include <QWaitCondition>
51 const int Session_ServerThread::NB_SRV_TYP = 6;
52 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
60 default constructor not for use
62 Session_ServerThread::Session_ServerThread()
64 ASSERT(0); // must not be called
70 Session_ServerThread::Session_ServerThread(int argc,
73 PortableServer::POA_ptr poa)
75 //MESSAGE("Session_ServerThread Constructor " << argv[0]);
77 _argv = new char*[ _argc + 1 ];
79 for (int i = 0; i < _argc; i++ )
80 _argv[i] = strdup( argv[i] );
82 _orb = CORBA::ORB::_duplicate(orb);
83 _root_poa = PortableServer::POA::_duplicate(poa);
85 _NS = new SALOME_NamingService(_orb); // one instance per server to limit
86 // multi thread coherence problems
87 _container = 0; // embedded container
93 Session_ServerThread::~Session_ServerThread()
95 //MESSAGE("~Session_ServerThread "<< _argv[0]);
97 for (int i = 0; i <_argc ; i++ )
103 run the thread : activate one servant, the servant type is given by
106 void Session_ServerThread::Init()
108 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
111 for (i=0; i<_argc; i++) SCRUTE(_argv[i]);
113 for (i=0; i<NB_SRV_TYP; i++) {
114 if (strcmp(_argv[0],_serverTypes[i])==0) {
116 MESSAGE("Server Thread type : "<<_serverTypes[i]);
120 NamingService_WaitForServerReadiness(_NS,"/Registry");
121 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
122 ActivateContainer(_argc, _argv);
125 case 1: // ModuleCatalog
127 NamingService_WaitForServerReadiness(_NS,"/Registry");
128 ActivateModuleCatalog(_argc, _argv);
133 NamingService_WaitForServerReadiness(_NS,"");
134 ActivateRegistry(_argc, _argv);
139 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
140 ActivateSALOMEDS(_argc, _argv);
145 NamingService_WaitForServerReadiness(_NS,"/Study");
146 std::string containerName = "/Containers/";
147 containerName = containerName + Kernel_Utils::GetHostname();
148 containerName = containerName + "/FactoryServer";
149 NamingService_WaitForServerReadiness(_NS,containerName);
150 ActivateSession(_argc, _argv);
153 case 5: // Container Manager
155 NamingService_WaitForServerReadiness(_NS,"");
156 ActivateContainerManager(_argc, _argv);
169 void Session_ServerThread::Shutdown()
171 if ( _container ) _container->Shutdown();
174 void Session_ServerThread::ActivateModuleCatalog(int argc,
178 MESSAGE("ModuleCatalog thread started");
179 // allocation on heap to allow destruction by POA
181 SALOME_ModuleCatalogImpl* Catalogue_i
182 = new SALOME_ModuleCatalogImpl(argc, argv);
184 // Tell the POA that the objects are ready to accept requests.
186 PortableServer::ObjectId_var id = _root_poa->activate_object (Catalogue_i);
187 Catalogue_i->_remove_ref();
189 CORBA::Object_var myCata = Catalogue_i->_this();
190 _NS->Register(myCata ,"/Kernel/ModulCatalog");
192 catch(CORBA::SystemException&) {
193 INFOS( "Caught CORBA::SystemException." );
195 catch(CORBA::Exception&) {
196 INFOS( "Caught CORBA::Exception." );
198 catch(omniORB::fatalException& fe) {
199 INFOS( "Caught omniORB::fatalException:" );
200 INFOS( " file: " << fe.file() );
201 INFOS( " line: " << fe.line() );
202 INFOS( " mesg: " << fe.errmsg() );
205 INFOS( "Caught unknown exception." );
209 void Session_ServerThread::ActivateSALOMEDS(int /*argc*/, char** /*argv*/)
212 MESSAGE("SALOMEDS thread started");
213 // We allocate the objects on the heap. Since these are reference
214 // counted objects, they will be deleted by the POA when they are no
217 ClientFactory::createStudy(_orb,_root_poa);
219 catch(CORBA::SystemException&) {
220 INFOS( "Caught CORBA::SystemException." );
222 catch(CORBA::Exception&) {
223 INFOS( "Caught CORBA::Exception." );
225 catch(omniORB::fatalException& fe) {
226 INFOS( "Caught omniORB::fatalException:" );
227 INFOS( " file: " << fe.file() );
228 INFOS( " line: " << fe.line() );
229 INFOS( " mesg: " << fe.errmsg() );
232 INFOS( "Caught unknown exception." );
236 void Session_ServerThread::ActivateRegistry(int argc,
239 MESSAGE("Registry thread started");
242 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
243 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
245 const char *ptrSessionName=0;
248 for ( k=1 ; k<argc ; k++ ) {
249 if ( strcmp(argv[k],"--salome_session")==0 ) {
250 ptrSessionName=argv[k+1];
254 ASSERT(ptrSessionName) ;
255 ASSERT(strlen( ptrSessionName )>0);
256 const char *registryName = "Registry";
257 Registry::Components_var varComponents;
259 RegistryService *ptrRegistry = new RegistryService;
260 ptrRegistry->SessionName( ptrSessionName );
261 ptrRegistry->SetOrb(_orb);
263 CORBA::PolicyList policies;
265 PortableServer::ThreadPolicy_var threadPol(_root_poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
266 policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol);
267 PortableServer::POAManager_var manager = _root_poa->the_POAManager();
268 PortableServer::POA_var poa2(_root_poa->create_POA("SingleThreadPOA4RegistryEmbedded",manager,policies));
269 threadPol->destroy();
271 PortableServer::ObjectId_var id(poa2->activate_object(ptrRegistry));
272 CORBA::Object_var pipo=poa2->id_to_reference(id);
273 varComponents = Registry::Components::_narrow(pipo) ;
274 ptrRegistry->_remove_ref(); //let poa manage registryservice deletion
277 CORBA::Object_var pipo = _NS->Resolve( registryName );
278 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
279 INFOS("RegistryService servant already existing" );
282 catch( const ServiceUnreachable &/*ex*/ ) {
284 catch( const CORBA::Exception &/*exx*/ ) {
286 std::string absoluteName = std::string("/") + registryName;
287 _NS->Register( varComponents , absoluteName.c_str() );
289 catch( const SALOME_Exception &ex ) {
290 INFOS( "Communication Error : " << ex.what() );
295 void Session_ServerThread::ActivateContainerManager(int /*argc*/, char** /*argv*/)
298 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
299 std::cout << "Activate SalomeLauncher ......!!!! " << std::endl;
300 new SALOME_Launcher(_orb,root_poa);
302 catch(CORBA::SystemException&) {
303 INFOS("Caught CORBA::SystemException.");
305 catch(PortableServer::POA::WrongPolicy&) {
306 INFOS("Caught CORBA::WrongPolicyException.");
308 catch(PortableServer::POA::ServantAlreadyActive&) {
309 INFOS("Caught CORBA::ServantAlreadyActiveException");
311 catch(CORBA::Exception&) {
312 INFOS("Caught CORBA::Exception.");
315 INFOS("Caught unknown exception.");
319 void Session_ServerThread::ActivateContainer(int argc, char** argv)
322 MESSAGE("Container thread started");
324 // get or create the child POA
326 PortableServer::POA_var factory_poa;
328 factory_poa = _root_poa->find_POA("factory_poa",0);
329 // 0 = no activation (already done if exists)
331 catch (PortableServer::POA::AdapterNonExistent&) {
332 MESSAGE("factory_poa does not exists, create...");
333 // define policy objects
334 PortableServer::ImplicitActivationPolicy_var implicitActivation =
335 _root_poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);
336 // default = NO_IMPLICIT_ACTIVATION
337 PortableServer::ThreadPolicy_var threadPolicy =
338 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
339 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
341 // create policy list
342 CORBA::PolicyList policyList;
343 policyList.length(2);
344 policyList[0] = PortableServer::ImplicitActivationPolicy::
345 _duplicate(implicitActivation);
346 policyList[1] = PortableServer::ThreadPolicy::
347 _duplicate(threadPolicy);
349 PortableServer::POAManager_var nil_mgr
350 = PortableServer::POAManager::_nil();
351 factory_poa = _root_poa->create_POA("factory_poa",
354 //with nil_mgr instead of pman,
355 //a new POA manager is created with the new POA
357 // destroy policy objects
358 implicitActivation->destroy();
359 threadPolicy->destroy();
361 // obtain the factory poa manager
362 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
364 MESSAGE("pmanfac->activate()");
367 char *containerName = (char*)"";
369 containerName = argv[1];
372 _container = new Engines_Container_i(_orb, _root_poa, containerName, argc, argv, true, false);
374 catch(CORBA::SystemException&) {
375 INFOS("Caught CORBA::SystemException.");
377 catch(PortableServer::POA::WrongPolicy&) {
378 INFOS("Caught CORBA::WrongPolicyException.");
380 catch(PortableServer::POA::ServantAlreadyActive&) {
381 INFOS("Caught CORBA::ServantAlreadyActiveException");
383 catch(CORBA::Exception&) {
384 INFOS("Caught CORBA::Exception.");
387 INFOS("Caught unknown exception.");
391 void Session_ServerThread::ActivateSession(int /*argc*/, char** /*argv*/)
393 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
399 Session_SessionThread::Session_SessionThread(int argc,
402 PortableServer::POA_ptr poa,
404 QWaitCondition* GUILauncher)
405 : Session_ServerThread(argc, argv, orb, poa),
406 _GUIMutex( GUIMutex ),
407 _GUILauncher( GUILauncher )
414 Session_SessionThread::~Session_SessionThread()
418 void Session_SessionThread::ActivateSession(int argc,
422 MESSAGE("Session thread started");
423 SALOME_Session_i * mySALOME_Session
424 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
425 PortableServer::ObjectId_var mySALOME_Sessionid
426 = _root_poa->activate_object(mySALOME_Session);
427 MESSAGE("poa->activate_object(mySALOME_Session)");
429 CORBA::Object_var obj = mySALOME_Session->_this();
430 CORBA::String_var sior(_orb->object_to_string(obj));
431 mySALOME_Session->_remove_ref();
433 mySALOME_Session->NSregister();
435 catch (CORBA::SystemException&) {
436 INFOS("Caught CORBA::SystemException.");
438 catch (CORBA::Exception&) {
439 INFOS("Caught CORBA::Exception.");
442 INFOS("Caught unknown exception.");