1 // Copyright (C) 2007-2016 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,"/myStudyManager");
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,
213 MESSAGE("SALOMEDS thread started");
214 // We allocate the objects on the heap. Since these are reference
215 // counted objects, they will be deleted by the POA when they are no
218 ClientFactory::createStudyManager(_orb,_root_poa);
220 catch(CORBA::SystemException&) {
221 INFOS( "Caught CORBA::SystemException." );
223 catch(CORBA::Exception&) {
224 INFOS( "Caught CORBA::Exception." );
226 catch(omniORB::fatalException& fe) {
227 INFOS( "Caught omniORB::fatalException:" );
228 INFOS( " file: " << fe.file() );
229 INFOS( " line: " << fe.line() );
230 INFOS( " mesg: " << fe.errmsg() );
233 INFOS( "Caught unknown exception." );
237 void Session_ServerThread::ActivateRegistry(int argc,
240 MESSAGE("Registry thread started");
243 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
244 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
246 const char *ptrSessionName=0;
249 for ( k=1 ; k<argc ; k++ ) {
250 if ( strcmp(argv[k],"--salome_session")==0 ) {
251 ptrSessionName=argv[k+1];
255 ASSERT(ptrSessionName) ;
256 ASSERT(strlen( ptrSessionName )>0);
257 const char *registryName = "Registry";
258 Registry::Components_var varComponents;
260 RegistryService *ptrRegistry = new RegistryService;
261 ptrRegistry->SessionName( ptrSessionName );
262 ptrRegistry->SetOrb(_orb);
263 varComponents = ptrRegistry->_this();
264 ptrRegistry->_remove_ref(); //let poa manage registry service deletion
265 // The RegistryService must not already exist.
268 CORBA::Object_var pipo = _NS->Resolve( registryName );
269 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
270 INFOS("RegistryService servant already existing" );
273 catch( const ServiceUnreachable &/*ex*/ ) {
275 catch( const CORBA::Exception &/*exx*/ ) {
277 std::string absoluteName = std::string("/") + registryName;
278 _NS->Register( varComponents , absoluteName.c_str() );
280 catch( const SALOME_Exception &ex ) {
281 INFOS( "Communication Error : " << ex.what() );
286 void Session_ServerThread::ActivateContainerManager(int argc,
290 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
291 std::cout << "Activate SalomeLauncher ......!!!! " << std::endl;
292 new SALOME_Launcher(_orb,root_poa);
294 catch(CORBA::SystemException&) {
295 INFOS("Caught CORBA::SystemException.");
297 catch(PortableServer::POA::WrongPolicy&) {
298 INFOS("Caught CORBA::WrongPolicyException.");
300 catch(PortableServer::POA::ServantAlreadyActive&) {
301 INFOS("Caught CORBA::ServantAlreadyActiveException");
303 catch(CORBA::Exception&) {
304 INFOS("Caught CORBA::Exception.");
307 INFOS("Caught unknown exception.");
311 void Session_ServerThread::ActivateContainer(int argc,
315 MESSAGE("Container thread started");
317 // get or create the child POA
319 PortableServer::POA_var factory_poa;
321 factory_poa = _root_poa->find_POA("factory_poa",0);
322 // 0 = no activation (already done if exists)
324 catch (PortableServer::POA::AdapterNonExistent&) {
325 MESSAGE("factory_poa does not exists, create...");
326 // define policy objects
327 PortableServer::ImplicitActivationPolicy_var implicitActivation =
328 _root_poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);
329 // default = NO_IMPLICIT_ACTIVATION
330 PortableServer::ThreadPolicy_var threadPolicy =
331 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
332 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
334 // create policy list
335 CORBA::PolicyList policyList;
336 policyList.length(2);
337 policyList[0] = PortableServer::ImplicitActivationPolicy::
338 _duplicate(implicitActivation);
339 policyList[1] = PortableServer::ThreadPolicy::
340 _duplicate(threadPolicy);
342 PortableServer::POAManager_var nil_mgr
343 = PortableServer::POAManager::_nil();
344 factory_poa = _root_poa->create_POA("factory_poa",
347 //with nil_mgr instead of pman,
348 //a new POA manager is created with the new POA
350 // destroy policy objects
351 implicitActivation->destroy();
352 threadPolicy->destroy();
354 // obtain the factory poa manager
355 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
357 MESSAGE("pmanfac->activate()");
360 char *containerName = (char*)"";
362 containerName = argv[1];
365 _container = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
367 catch(CORBA::SystemException&) {
368 INFOS("Caught CORBA::SystemException.");
370 catch(PortableServer::POA::WrongPolicy&) {
371 INFOS("Caught CORBA::WrongPolicyException.");
373 catch(PortableServer::POA::ServantAlreadyActive&) {
374 INFOS("Caught CORBA::ServantAlreadyActiveException");
376 catch(CORBA::Exception&) {
377 INFOS("Caught CORBA::Exception.");
380 INFOS("Caught unknown exception.");
384 void Session_ServerThread::ActivateSession(int argc,
387 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
393 Session_SessionThread::Session_SessionThread(int argc,
396 PortableServer::POA_ptr poa,
398 QWaitCondition* GUILauncher)
399 : Session_ServerThread(argc, argv, orb, poa),
400 _GUIMutex( GUIMutex ),
401 _GUILauncher( GUILauncher )
408 Session_SessionThread::~Session_SessionThread()
412 void Session_SessionThread::ActivateSession(int argc,
416 MESSAGE("Session thread started");
417 SALOME_Session_i * mySALOME_Session
418 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
419 PortableServer::ObjectId_var mySALOME_Sessionid
420 = _root_poa->activate_object(mySALOME_Session);
421 MESSAGE("poa->activate_object(mySALOME_Session)");
423 CORBA::Object_var obj = mySALOME_Session->_this();
424 CORBA::String_var sior(_orb->object_to_string(obj));
425 mySALOME_Session->_remove_ref();
427 mySALOME_Session->NSregister();
429 catch (CORBA::SystemException&) {
430 INFOS("Caught CORBA::SystemException.");
432 catch (CORBA::Exception&) {
433 INFOS("Caught CORBA::Exception.");
436 INFOS("Caught unknown exception.");