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 "SALOMEDS_StudyManager_i.hxx"
37 #include "SALOME_ModuleCatalog_impl.hxx"
38 #include "RegistryService.hxx"
39 #include "SALOME_Session_i.hxx"
41 #include "Utils_ORB_INIT.hxx"
42 #include "Utils_SINGLETON.hxx"
43 #include "Utils_SALOME_Exception.hxx"
45 #include "NamingService_WaitForServerReadiness.hxx"
46 #include "utilities.h"
53 const int Session_ServerThread::NB_SRV_TYP = 5;
54 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
60 //=============================================================================
62 * default constructor not for use
64 //=============================================================================
66 Session_ServerThread::Session_ServerThread()
68 ASSERT(0); // must not be called
71 //=============================================================================
75 //=============================================================================
77 Session_ServerThread::Session_ServerThread(int argc,
80 PortableServer::POA_ptr poa,
83 //MESSAGE("Session_ServerThread Constructor " << argv[0]);
86 _orb = CORBA::ORB::_duplicate(orb);
87 _root_poa = PortableServer::POA::_duplicate(poa);
90 _NS = new SALOME_NamingService(_orb); // one instance per server to limit
91 // multi thread coherence problems
94 //=============================================================================
98 //=============================================================================
100 Session_ServerThread::~Session_ServerThread()
102 //MESSAGE("~Session_ServerThread "<< _argv[0]);
105 //=============================================================================
107 * run the thread : activate one servant, the servant type is given by
110 //=============================================================================
112 void Session_ServerThread::Init()
114 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
116 for (int i=0; i<_argc; i++) SCRUTE(_argv[i]);
117 for (int i=0; i<NB_SRV_TYP; i++)
118 if (strcmp(_argv[0],_serverTypes[i])==0)
121 MESSAGE("Server Thread type : "<<_serverTypes[i]);
126 NamingService_WaitForServerReadiness(_NS,"/Registry");
127 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
128 ActivateContainer(_argc, _argv);
131 case 1: // ModuleCatalog
133 NamingService_WaitForServerReadiness(_NS,"/Registry");
134 ActivateModuleCatalog(_argc, _argv);
139 NamingService_WaitForServerReadiness(_NS,"");
140 ActivateRegistry(_argc, _argv);
145 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
146 ActivateSALOMEDS(_argc, _argv);
151 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
152 string containerName = "/Containers/";
153 containerName = containerName + GetHostname();
154 containerName = containerName + "/FactoryServer";
155 NamingService_WaitForServerReadiness(_NS,containerName);
156 ActivateSession(_argc, _argv);
168 //=============================================================================
172 //=============================================================================
174 void Session_ServerThread::ActivateModuleCatalog(int argc,
179 INFOS("ModuleCatalog thread started");
180 // allocation on heap to allow destruction by POA
182 SALOME_ModuleCatalogImpl* Catalogue_i
183 = new SALOME_ModuleCatalogImpl(argc, argv);
185 // Tell the POA that the objects are ready to accept requests.
187 _root_poa->activate_object (Catalogue_i);
189 CORBA::Object_ptr myCata = Catalogue_i->_this();
190 _NS->Register(myCata ,"/Kernel/ModulCatalog");
192 catch(CORBA::SystemException&)
194 INFOS( "Caught CORBA::SystemException." );
196 catch(CORBA::Exception&)
198 INFOS( "Caught CORBA::Exception." );
200 catch(omniORB::fatalException& fe)
202 INFOS( "Caught omniORB::fatalException:" );
203 INFOS( " file: " << fe.file() );
204 INFOS( " line: " << fe.line() );
205 INFOS( " mesg: " << fe.errmsg() );
209 INFOS( "Caught unknown exception." );
213 //=============================================================================
217 //=============================================================================
219 void Session_ServerThread::ActivateSALOMEDS(int argc,
224 INFOS("SALOMEDS thread started");
225 // We allocate the objects on the heap. Since these are reference
226 // counted objects, they will be deleted by the POA when they are no
229 SALOMEDS_StudyManager_i * myStudyManager_i
230 = new SALOMEDS_StudyManager_i(_orb,_root_poa);
232 // Activate the objects. This tells the POA that the objects are
233 // ready to accept requests.
235 PortableServer::ObjectId_var myStudyManager_iid
236 = _root_poa->activate_object(myStudyManager_i);
237 myStudyManager_i->register_name("/myStudyManager");
239 catch(CORBA::SystemException&)
241 INFOS( "Caught CORBA::SystemException." );
243 catch(CORBA::Exception&)
245 INFOS( "Caught CORBA::Exception." );
247 catch(omniORB::fatalException& fe)
249 INFOS( "Caught omniORB::fatalException:" );
250 INFOS( " file: " << fe.file() );
251 INFOS( " line: " << fe.line() );
252 INFOS( " mesg: " << fe.errmsg() );
256 INFOS( "Caught unknown exception." );
260 //=============================================================================
264 //=============================================================================
266 void Session_ServerThread::ActivateRegistry(int argc,
269 INFOS("Registry thread started");
273 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
274 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
276 const char *ptrSessionName=0;
279 for ( k=1 ; k<argc ; k++ )
281 if( strcmp(argv[k],"--salome_session")==0 )
283 ptrSessionName=argv[k+1];
287 ASSERT(ptrSessionName) ;
288 ASSERT(strlen( ptrSessionName )>0);
289 const char *registryName = "Registry";
290 Registry::Components_var varComponents;
293 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
294 ptrRegistry->SessionName( ptrSessionName );
295 varComponents = ptrRegistry->_this();
296 // The RegistryService must not already exist.
300 CORBA::Object_var pipo = _NS->Resolve( registryName );
301 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
302 INFOS("RegistryService servant already existing" );
305 catch( const ServiceUnreachable &ex )
308 catch( const CORBA::Exception &exx )
311 string absoluteName = string("/") + registryName;
312 _NS->Register( varComponents , absoluteName.c_str() );
313 MESSAGE("On attend les requetes des clients");
315 catch( const SALOME_Exception &ex )
317 INFOS( "Communication Error : " << ex.what() );
322 //=============================================================================
326 //=============================================================================
328 void Session_ServerThread::ActivateContainer(int argc,
333 INFOS("Container thread started");
335 // get or create the child POA
337 PortableServer::POA_var factory_poa;
340 factory_poa = _root_poa->find_POA("factory_poa",0);
341 // 0 = no activation (already done if exists)
343 catch (PortableServer::POA::AdapterNonExistent&)
345 INFOS("factory_poa does not exists, create...");
346 // define policy objects
347 PortableServer::ImplicitActivationPolicy_var implicitActivation =
348 _root_poa->create_implicit_activation_policy(
349 PortableServer::NO_IMPLICIT_ACTIVATION);
350 // default = NO_IMPLICIT_ACTIVATION
351 PortableServer::ThreadPolicy_var threadPolicy =
352 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
353 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
355 // create policy list
356 CORBA::PolicyList policyList;
357 policyList.length(2);
358 policyList[0] = PortableServer::ImplicitActivationPolicy::
359 _duplicate(implicitActivation);
360 policyList[1] = PortableServer::ThreadPolicy::
361 _duplicate(threadPolicy);
363 PortableServer::POAManager_var nil_mgr
364 = PortableServer::POAManager::_nil();
365 factory_poa = _root_poa->create_POA("factory_poa",
368 //with nil_mgr instead of pman,
369 //a new POA manager is created with the new POA
371 // destroy policy objects
372 implicitActivation->destroy();
373 threadPolicy->destroy();
375 // obtain the factory poa manager
376 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
378 MESSAGE("pmanfac->activate()");
381 char *containerName = "";
384 containerName = argv[1];
387 Engines_Container_i * myContainer
388 = new Engines_Container_i(_orb, factory_poa, containerName , argc , argv , true , false);
390 catch(CORBA::SystemException&)
392 INFOS("Caught CORBA::SystemException.");
394 catch(PortableServer::POA::WrongPolicy&)
396 INFOS("Caught CORBA::WrongPolicyException.");
398 catch(PortableServer::POA::ServantAlreadyActive&)
400 INFOS("Caught CORBA::ServantAlreadyActiveException");
402 catch(CORBA::Exception&)
404 INFOS("Caught CORBA::Exception.");
408 INFOS("Caught unknown exception.");
412 //=============================================================================
416 //=============================================================================
418 void Session_ServerThread::ActivateSession(int argc,
421 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
424 Session_SessionThread::Session_SessionThread(int argc,
427 PortableServer::POA_ptr poa,
429 QWaitCondition* GUILauncher)
430 : Session_ServerThread(argc, argv, orb, poa, GUIMutex),
431 _GUILauncher( GUILauncher )
435 Session_SessionThread::~Session_SessionThread()
439 void Session_SessionThread::ActivateSession(int argc,
444 INFOS("Session thread started");
445 SALOME_Session_i * mySALOME_Session
446 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
447 PortableServer::ObjectId_var mySALOME_Sessionid
448 = _root_poa->activate_object(mySALOME_Session);
449 INFOS("poa->activate_object(mySALOME_Session)");
451 CORBA::Object_var obj = mySALOME_Session->_this();
452 CORBA::String_var sior(_orb->object_to_string(obj));
454 mySALOME_Session->NSregister();
456 catch (CORBA::SystemException&)
458 INFOS("Caught CORBA::SystemException.");
460 catch (CORBA::Exception&)
462 INFOS("Caught CORBA::Exception.");
466 INFOS("Caught unknown exception.");