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 ActivateContainer(_argc, _argv);
130 case 1: // ModuleCatalog
132 NamingService_WaitForServerReadiness(_NS,"/Registry");
133 ActivateModuleCatalog(_argc, _argv);
138 NamingService_WaitForServerReadiness(_NS,"");
139 ActivateRegistry(_argc, _argv);
144 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
145 ActivateSALOMEDS(_argc, _argv);
150 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
151 string containerName = "/Containers/";
152 containerName = containerName + GetHostname();
153 containerName = containerName + "/FactoryServer";
154 NamingService_WaitForServerReadiness(_NS,containerName);
155 ActivateSession(_argc, _argv);
167 //=============================================================================
171 //=============================================================================
173 void Session_ServerThread::ActivateModuleCatalog(int argc,
178 INFOS("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 _root_poa->activate_object (Catalogue_i);
188 CORBA::Object_ptr myCata = Catalogue_i->_this();
189 _NS->Register(myCata ,"/Kernel/ModulCatalog");
191 catch(CORBA::SystemException&)
193 INFOS( "Caught CORBA::SystemException." );
195 catch(CORBA::Exception&)
197 INFOS( "Caught CORBA::Exception." );
199 catch(omniORB::fatalException& fe)
201 INFOS( "Caught omniORB::fatalException:" );
202 INFOS( " file: " << fe.file() );
203 INFOS( " line: " << fe.line() );
204 INFOS( " mesg: " << fe.errmsg() );
208 INFOS( "Caught unknown exception." );
212 //=============================================================================
216 //=============================================================================
218 void Session_ServerThread::ActivateSALOMEDS(int argc,
223 INFOS("SALOMEDS thread started");
224 // We allocate the objects on the heap. Since these are reference
225 // counted objects, they will be deleted by the POA when they are no
228 SALOMEDS_StudyManager_i * myStudyManager_i
229 = new SALOMEDS_StudyManager_i(_orb,_root_poa);
231 // Activate the objects. This tells the POA that the objects are
232 // ready to accept requests.
234 PortableServer::ObjectId_var myStudyManager_iid
235 = _root_poa->activate_object(myStudyManager_i);
236 myStudyManager_i->register_name("/myStudyManager");
238 catch(CORBA::SystemException&)
240 INFOS( "Caught CORBA::SystemException." );
242 catch(CORBA::Exception&)
244 INFOS( "Caught CORBA::Exception." );
246 catch(omniORB::fatalException& fe)
248 INFOS( "Caught omniORB::fatalException:" );
249 INFOS( " file: " << fe.file() );
250 INFOS( " line: " << fe.line() );
251 INFOS( " mesg: " << fe.errmsg() );
255 INFOS( "Caught unknown exception." );
259 //=============================================================================
263 //=============================================================================
265 void Session_ServerThread::ActivateRegistry(int argc,
268 INFOS("Registry thread started");
272 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
273 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
275 const char *ptrSessionName=0;
278 for ( k=1 ; k<argc ; k++ )
280 if( strcmp(argv[k],"--salome_session")==0 )
282 ptrSessionName=argv[k+1];
286 ASSERT(ptrSessionName) ;
287 ASSERT(strlen( ptrSessionName )>0);
288 const char *registryName = "Registry";
289 Registry::Components_var varComponents;
292 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
293 ptrRegistry->SessionName( ptrSessionName );
294 varComponents = ptrRegistry->_this();
295 // The RegistryService must not already exist.
299 CORBA::Object_var pipo = _NS->Resolve( registryName );
300 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
301 INFOS("RegistryService servant already existing" );
304 catch( const ServiceUnreachable &ex )
307 catch( const CORBA::Exception &exx )
310 string absoluteName = string("/") + registryName;
311 _NS->Register( varComponents , absoluteName.c_str() );
312 MESSAGE("On attend les requetes des clients");
314 catch( const SALOME_Exception &ex )
316 INFOS( "Communication Error : " << ex.what() );
321 //=============================================================================
325 //=============================================================================
327 void Session_ServerThread::ActivateContainer(int argc,
332 INFOS("Container thread started");
334 // get or create the child POA
336 PortableServer::POA_var factory_poa;
339 factory_poa = _root_poa->find_POA("factory_poa",0);
340 // 0 = no activation (already done if exists)
342 catch (PortableServer::POA::AdapterNonExistent&)
344 INFOS("factory_poa does not exists, create...");
345 // define policy objects
346 PortableServer::ImplicitActivationPolicy_var implicitActivation =
347 _root_poa->create_implicit_activation_policy(
348 PortableServer::NO_IMPLICIT_ACTIVATION);
349 // default = NO_IMPLICIT_ACTIVATION
350 PortableServer::ThreadPolicy_var threadPolicy =
351 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
352 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
354 // create policy list
355 CORBA::PolicyList policyList;
356 policyList.length(2);
357 policyList[0] = PortableServer::ImplicitActivationPolicy::
358 _duplicate(implicitActivation);
359 policyList[1] = PortableServer::ThreadPolicy::
360 _duplicate(threadPolicy);
362 PortableServer::POAManager_var nil_mgr
363 = PortableServer::POAManager::_nil();
364 factory_poa = _root_poa->create_POA("factory_poa",
367 //with nil_mgr instead of pman,
368 //a new POA manager is created with the new POA
370 // destroy policy objects
371 implicitActivation->destroy();
372 threadPolicy->destroy();
374 // obtain the factory poa manager
375 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
377 MESSAGE("pmanfac->activate()");
380 char *containerName = "";
383 containerName = argv[1];
386 Engines_Container_i * myContainer
387 = new Engines_Container_i(_orb, factory_poa, containerName , argc , argv );
389 catch(CORBA::SystemException&)
391 INFOS("Caught CORBA::SystemException.");
393 catch(PortableServer::POA::WrongPolicy&)
395 INFOS("Caught CORBA::WrongPolicyException.");
397 catch(PortableServer::POA::ServantAlreadyActive&)
399 INFOS("Caught CORBA::ServantAlreadyActiveException");
401 catch(CORBA::Exception&)
403 INFOS("Caught CORBA::Exception.");
407 INFOS("Caught unknown exception.");
411 //=============================================================================
415 //=============================================================================
417 void Session_ServerThread::ActivateSession(int argc,
420 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
423 Session_SessionThread::Session_SessionThread(int argc,
426 PortableServer::POA_ptr poa,
428 QWaitCondition* GUILauncher)
429 : Session_ServerThread(argc, argv, orb, poa, GUIMutex),
430 _GUILauncher( GUILauncher )
434 Session_SessionThread::~Session_SessionThread()
438 void Session_SessionThread::ActivateSession(int argc,
443 INFOS("Session thread started");
444 SALOME_Session_i * mySALOME_Session
445 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
446 PortableServer::ObjectId_var mySALOME_Sessionid
447 = _root_poa->activate_object(mySALOME_Session);
448 INFOS("poa->activate_object(mySALOME_Session)");
450 CORBA::Object_var obj = mySALOME_Session->_this();
451 CORBA::String_var sior(_orb->object_to_string(obj));
453 mySALOME_Session->NSregister();
455 catch (CORBA::SystemException&)
457 INFOS("Caught CORBA::SystemException.");
459 catch (CORBA::Exception&)
461 INFOS("Caught CORBA::Exception.");
465 INFOS("Caught unknown exception.");