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 <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",
65 //=============================================================================
67 * default constructor not for use
69 //=============================================================================
71 Session_ServerThread::Session_ServerThread()
73 ASSERT(0); // must not be called
76 //=============================================================================
80 //=============================================================================
82 Session_ServerThread::Session_ServerThread(int argc,
85 PortableServer::POA_ptr poa,
88 //MESSAGE("Session_ServerThread Constructor " << argv[0]);
91 _orb = CORBA::ORB::_duplicate(orb);
92 _root_poa = PortableServer::POA::_duplicate(poa);
95 _NS = new SALOME_NamingService(_orb); // one instance per server to limit
96 // multi thread coherence problems
99 //=============================================================================
103 //=============================================================================
105 Session_ServerThread::~Session_ServerThread()
107 //MESSAGE("~Session_ServerThread "<< _argv[0]);
110 //=============================================================================
112 * run the thread : activate one servant, the servant type is given by
115 //=============================================================================
117 void Session_ServerThread::Init()
119 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
121 for (int i=0; i<_argc; i++) SCRUTE(_argv[i]);
123 for (int i=0; i<NB_SRV_TYP; i++)
125 for (i=0; i<NB_SRV_TYP; i++)
127 if (strcmp(_argv[0],_serverTypes[i])==0)
130 MESSAGE("Server Thread type : "<<_serverTypes[i]);
135 NamingService_WaitForServerReadiness(_NS,"/Registry");
136 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
137 ActivateContainer(_argc, _argv);
140 case 1: // ModuleCatalog
142 NamingService_WaitForServerReadiness(_NS,"/Registry");
143 ActivateModuleCatalog(_argc, _argv);
148 NamingService_WaitForServerReadiness(_NS,"");
149 ActivateRegistry(_argc, _argv);
154 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
155 ActivateSALOMEDS(_argc, _argv);
160 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
161 string containerName = "/Containers/";
162 containerName = containerName + GetHostname();
163 containerName = containerName + "/FactoryServer";
164 NamingService_WaitForServerReadiness(_NS,containerName);
165 ActivateSession(_argc, _argv);
168 case 5: // SalomeApp_Engine
170 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
171 ActivateEngine(_argc, _argv);
174 case 6: // Container Manager
176 NamingService_WaitForServerReadiness(_NS,"");
177 ActivateContainerManager(_argc, _argv);
189 //=============================================================================
193 //=============================================================================
195 void Session_ServerThread::ActivateModuleCatalog(int argc,
200 INFOS("ModuleCatalog thread started");
201 // allocation on heap to allow destruction by POA
203 SALOME_ModuleCatalogImpl* Catalogue_i
204 = new SALOME_ModuleCatalogImpl(argc, argv);
206 // Tell the POA that the objects are ready to accept requests.
208 _root_poa->activate_object (Catalogue_i);
210 CORBA::Object_ptr myCata = Catalogue_i->_this();
211 _NS->Register(myCata ,"/Kernel/ModulCatalog");
213 catch(CORBA::SystemException&)
215 INFOS( "Caught CORBA::SystemException." );
217 catch(CORBA::Exception&)
219 INFOS( "Caught CORBA::Exception." );
221 catch(omniORB::fatalException& fe)
223 INFOS( "Caught omniORB::fatalException:" );
224 INFOS( " file: " << fe.file() );
225 INFOS( " line: " << fe.line() );
226 INFOS( " mesg: " << fe.errmsg() );
230 INFOS( "Caught unknown exception." );
234 //=============================================================================
238 //=============================================================================
240 void Session_ServerThread::ActivateSALOMEDS(int argc,
245 INFOS("SALOMEDS thread started");
246 // We allocate the objects on the heap. Since these are reference
247 // counted objects, they will be deleted by the POA when they are no
250 ClientFactory::createStudyManager(_orb,_root_poa);
252 catch(CORBA::SystemException&)
254 INFOS( "Caught CORBA::SystemException." );
256 catch(CORBA::Exception&)
258 INFOS( "Caught CORBA::Exception." );
260 catch(omniORB::fatalException& fe)
262 INFOS( "Caught omniORB::fatalException:" );
263 INFOS( " file: " << fe.file() );
264 INFOS( " line: " << fe.line() );
265 INFOS( " mesg: " << fe.errmsg() );
269 INFOS( "Caught unknown exception." );
273 //=============================================================================
277 //=============================================================================
279 void Session_ServerThread::ActivateRegistry(int argc,
282 INFOS("Registry thread started");
286 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
287 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
289 const char *ptrSessionName=0;
292 for ( k=1 ; k<argc ; k++ )
294 if( strcmp(argv[k],"--salome_session")==0 )
296 ptrSessionName=argv[k+1];
300 ASSERT(ptrSessionName) ;
301 ASSERT(strlen( ptrSessionName )>0);
302 const char *registryName = "Registry";
303 Registry::Components_var varComponents;
306 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
307 ptrRegistry->SessionName( ptrSessionName );
308 varComponents = ptrRegistry->_this();
309 // The RegistryService must not already exist.
313 CORBA::Object_var pipo = _NS->Resolve( registryName );
314 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
315 INFOS("RegistryService servant already existing" );
318 catch( const ServiceUnreachable &ex )
321 catch( const CORBA::Exception &exx )
324 string absoluteName = string("/") + registryName;
325 _NS->Register( varComponents , absoluteName.c_str() );
326 MESSAGE("On attend les requetes des clients");
328 catch( const SALOME_Exception &ex )
330 INFOS( "Communication Error : " << ex.what() );
335 //=============================================================================
339 //=============================================================================
341 void Session_ServerThread::ActivateContainerManager(int argc,
346 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
347 cout << "ActivateContainerManager ......!!!! " << endl;
348 SALOME_ContainerManager * myContainer
349 = new SALOME_ContainerManager(_orb);
351 catch(CORBA::SystemException&)
353 INFOS("Caught CORBA::SystemException.");
355 catch(PortableServer::POA::WrongPolicy&)
357 INFOS("Caught CORBA::WrongPolicyException.");
359 catch(PortableServer::POA::ServantAlreadyActive&)
361 INFOS("Caught CORBA::ServantAlreadyActiveException");
363 catch(CORBA::Exception&)
365 INFOS("Caught CORBA::Exception.");
369 INFOS("Caught unknown exception.");
373 //=============================================================================
377 //=============================================================================
379 void Session_ServerThread::ActivateContainer(int argc,
384 INFOS("Container thread started");
386 // get or create the child POA
388 PortableServer::POA_var factory_poa;
391 factory_poa = _root_poa->find_POA("factory_poa",0);
392 // 0 = no activation (already done if exists)
394 catch (PortableServer::POA::AdapterNonExistent&)
396 INFOS("factory_poa does not exists, create...");
397 // define policy objects
398 PortableServer::ImplicitActivationPolicy_var implicitActivation =
399 _root_poa->create_implicit_activation_policy(
400 PortableServer::NO_IMPLICIT_ACTIVATION);
401 // default = NO_IMPLICIT_ACTIVATION
402 PortableServer::ThreadPolicy_var threadPolicy =
403 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
404 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
406 // create policy list
407 CORBA::PolicyList policyList;
408 policyList.length(2);
409 policyList[0] = PortableServer::ImplicitActivationPolicy::
410 _duplicate(implicitActivation);
411 policyList[1] = PortableServer::ThreadPolicy::
412 _duplicate(threadPolicy);
414 PortableServer::POAManager_var nil_mgr
415 = PortableServer::POAManager::_nil();
416 factory_poa = _root_poa->create_POA("factory_poa",
419 //with nil_mgr instead of pman,
420 //a new POA manager is created with the new POA
422 // destroy policy objects
423 implicitActivation->destroy();
424 threadPolicy->destroy();
426 // obtain the factory poa manager
427 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
429 MESSAGE("pmanfac->activate()");
432 char *containerName = "";
435 containerName = argv[1];
438 Engines_Container_i * myContainer
439 = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
441 catch(CORBA::SystemException&)
443 INFOS("Caught CORBA::SystemException.");
445 catch(PortableServer::POA::WrongPolicy&)
447 INFOS("Caught CORBA::WrongPolicyException.");
449 catch(PortableServer::POA::ServantAlreadyActive&)
451 INFOS("Caught CORBA::ServantAlreadyActiveException");
453 catch(CORBA::Exception&)
455 INFOS("Caught CORBA::Exception.");
459 INFOS("Caught unknown exception.");
463 //=============================================================================
467 //=============================================================================
469 void Session_ServerThread::ActivateEngine(int /*argc*/, char ** /*argv*/)
473 INFOS("SalomeApp_Engine thread started");
474 SalomeApp_Engine_i* anEngine = new SalomeApp_Engine_i();
475 /*PortableServer::ObjectId_var id = */_root_poa->activate_object( anEngine );
476 INFOS("poa->activate_object( SalomeApp_Engine )");
478 CORBA::Object_ptr obj = anEngine->_this();
479 _NS->Register( obj ,"/SalomeAppEngine");
482 catch (CORBA::SystemException&)
484 INFOS("Caught CORBA::SystemException.");
486 catch (CORBA::Exception&)
488 INFOS("Caught CORBA::Exception.");
492 INFOS("Caught unknown exception.");
496 //=============================================================================
500 //=============================================================================
502 void Session_ServerThread::ActivateSession(int argc,
505 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
508 Session_SessionThread::Session_SessionThread(int argc,
511 PortableServer::POA_ptr poa,
513 QWaitCondition* GUILauncher)
514 : Session_ServerThread(argc, argv, orb, poa, GUIMutex),
515 _GUILauncher( GUILauncher )
519 Session_SessionThread::~Session_SessionThread()
523 void Session_SessionThread::ActivateSession(int argc,
528 INFOS("Session thread started");
529 SALOME_Session_i * mySALOME_Session
530 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
531 PortableServer::ObjectId_var mySALOME_Sessionid
532 = _root_poa->activate_object(mySALOME_Session);
533 INFOS("poa->activate_object(mySALOME_Session)");
535 CORBA::Object_var obj = mySALOME_Session->_this();
536 CORBA::String_var sior(_orb->object_to_string(obj));
538 mySALOME_Session->NSregister();
540 catch (CORBA::SystemException&)
542 INFOS("Caught CORBA::SystemException.");
544 catch (CORBA::Exception&)
546 INFOS("Caught CORBA::Exception.");
550 INFOS("Caught unknown exception.");