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 "utilities.h"
52 const int Session_ServerThread::NB_SRV_TYP = 5;
53 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
59 //=============================================================================
61 * Wait until the given server is ready i.e. is name is found in namingService.
62 * Try 40 times, with 250 ms sleep between each try.
63 * If Logger is used for traces, it must be ready before this call, because
64 * SALOME_NamingService client uses SALOME traces. So, Logger readiness must be
65 * checked in Launch script before execution of WaitForServerReadiness.
67 //=============================================================================
69 void WaitForServerReadiness(SALOME_NamingService* NS, string serverName)
71 long TIMESleep = 250000000; // 250 ms.
72 int NumberOfTries = 40; // total wait = 10 s.
76 ts_req.tv_nsec=TIMESleep;
82 for (int itry=0; itry < NumberOfTries; itry++)
86 if (serverName.length() == 0)
88 string curdir = NS->Current_Directory(); // to wait for naming service
90 break; // naming service found
94 CORBA::Object_ptr obj = NS->Resolve(serverName.c_str());
95 if (! CORBA::is_nil(obj))
98 break; // server found, no more try to do
100 MESSAGE("Server "<< serverName <<" not yet ready, waiting...");
101 int a = nanosleep(&ts_req,&ts_rem); // wait before retry
104 catch( ServiceUnreachable& )
106 MESSAGE("CORBA::COMM_FAILURE: Naming Service not yet ready, waiting...");
107 int a = nanosleep(&ts_req,&ts_rem); // wait before retry
112 INFOS("Server "<< serverName <<" not found, abort...");
117 //=============================================================================
119 * default constructor not for use
121 //=============================================================================
123 Session_ServerThread::Session_ServerThread()
125 ASSERT(0); // must not be called
128 //=============================================================================
132 //=============================================================================
134 Session_ServerThread::Session_ServerThread(int argc,
137 PortableServer::POA_ptr poa,
140 //MESSAGE("Session_ServerThread Constructor " << argv[0]);
143 _orb = CORBA::ORB::_duplicate(orb);
144 _root_poa = PortableServer::POA::_duplicate(poa);
145 _GUIMutex = GUIMutex;
147 _NS = new SALOME_NamingService(_orb); // one instance per server to limit
148 // multi thread coherence problems
151 //=============================================================================
155 //=============================================================================
157 Session_ServerThread::~Session_ServerThread()
159 //MESSAGE("~Session_ServerThread "<< _argv[0]);
162 //=============================================================================
164 * run the thread : activate one servant, the servant type is given by
167 //=============================================================================
169 void Session_ServerThread::Init()
171 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
173 for (int i=0; i<_argc; i++) SCRUTE(_argv[i]);
174 for (int i=0; i<NB_SRV_TYP; i++)
175 if (strcmp(_argv[0],_serverTypes[i])==0)
178 MESSAGE("Server Thread type : "<<_serverTypes[i]);
183 WaitForServerReadiness(_NS,"/Registry");
184 ActivateContainer(_argc, _argv);
187 case 1: // ModuleCatalog
189 WaitForServerReadiness(_NS,"/Registry");
190 ActivateModuleCatalog(_argc, _argv);
195 WaitForServerReadiness(_NS,"");
196 ActivateRegistry(_argc, _argv);
201 WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
202 ActivateSALOMEDS(_argc, _argv);
207 WaitForServerReadiness(_NS,"/myStudyManager");
208 string containerName = "/Containers/";
209 containerName = containerName + GetHostname();
210 containerName = containerName + "/FactoryServer";
211 WaitForServerReadiness(_NS,containerName);
212 ActivateSession(_argc, _argv);
224 //=============================================================================
228 //=============================================================================
230 void Session_ServerThread::ActivateModuleCatalog(int argc,
235 INFOS("ModuleCatalog thread started");
236 // allocation on heap to allow destruction by POA
238 SALOME_ModuleCatalogImpl* Catalogue_i
239 = new SALOME_ModuleCatalogImpl(argc, argv);
241 // Tell the POA that the objects are ready to accept requests.
243 _root_poa->activate_object (Catalogue_i);
245 CORBA::Object_ptr myCata = Catalogue_i->_this();
246 _NS->Register(myCata ,"/Kernel/ModulCatalog");
248 catch(CORBA::SystemException&)
250 INFOS( "Caught CORBA::SystemException." );
252 catch(CORBA::Exception&)
254 INFOS( "Caught CORBA::Exception." );
256 catch(omniORB::fatalException& fe)
258 INFOS( "Caught omniORB::fatalException:" );
259 INFOS( " file: " << fe.file() );
260 INFOS( " line: " << fe.line() );
261 INFOS( " mesg: " << fe.errmsg() );
265 INFOS( "Caught unknown exception." );
269 //=============================================================================
273 //=============================================================================
275 void Session_ServerThread::ActivateSALOMEDS(int argc,
280 INFOS("SALOMEDS thread started");
281 // We allocate the objects on the heap. Since these are reference
282 // counted objects, they will be deleted by the POA when they are no
285 SALOMEDS_StudyManager_i * myStudyManager_i
286 = new SALOMEDS_StudyManager_i(_orb);
288 // Activate the objects. This tells the POA that the objects are
289 // ready to accept requests.
291 PortableServer::ObjectId_var myStudyManager_iid
292 = _root_poa->activate_object(myStudyManager_i);
293 myStudyManager_i->register_name("/myStudyManager");
295 catch(CORBA::SystemException&)
297 INFOS( "Caught CORBA::SystemException." );
299 catch(CORBA::Exception&)
301 INFOS( "Caught CORBA::Exception." );
303 catch(omniORB::fatalException& fe)
305 INFOS( "Caught omniORB::fatalException:" );
306 INFOS( " file: " << fe.file() );
307 INFOS( " line: " << fe.line() );
308 INFOS( " mesg: " << fe.errmsg() );
312 INFOS( "Caught unknown exception." );
316 //=============================================================================
320 //=============================================================================
322 void Session_ServerThread::ActivateRegistry(int argc,
325 INFOS("Registry thread started");
329 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
330 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
332 const char *ptrSessionName=0;
335 for ( k=1 ; k<argc ; k++ )
337 if( strcmp(argv[k],"--salome_session")==0 )
339 ptrSessionName=argv[k+1];
343 ASSERT(ptrSessionName) ;
344 ASSERT(strlen( ptrSessionName )>0);
345 const char *registryName = "Registry";
346 Registry::Components_var varComponents;
349 RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
350 ptrRegistry->SessionName( ptrSessionName );
351 varComponents = ptrRegistry->_this();
352 // The RegistryService must not already exist.
356 CORBA::Object_var pipo = _NS->Resolve( registryName );
357 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
358 INFOS("RegistryService servant already existing" );
361 catch( const ServiceUnreachable &ex )
364 catch( const CORBA::Exception &exx )
367 string absoluteName = string("/") + registryName;
368 _NS->Register( varComponents , absoluteName.c_str() );
369 MESSAGE("On attend les requetes des clients");
371 catch( const SALOME_Exception &ex )
373 INFOS( "Communication Error : " << ex.what() );
378 //=============================================================================
382 //=============================================================================
384 void Session_ServerThread::ActivateContainer(int argc,
389 INFOS("Container thread started");
391 // get or create the child POA
393 PortableServer::POA_var factory_poa;
396 factory_poa = _root_poa->find_POA("factory_poa",0);
397 // 0 = no activation (already done if exists)
399 catch (PortableServer::POA::AdapterNonExistent&)
401 INFOS("factory_poa does not exists, create...");
402 // define policy objects
403 PortableServer::ImplicitActivationPolicy_var implicitActivation =
404 _root_poa->create_implicit_activation_policy(
405 PortableServer::NO_IMPLICIT_ACTIVATION);
406 // default = NO_IMPLICIT_ACTIVATION
407 PortableServer::ThreadPolicy_var threadPolicy =
408 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
409 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
411 // create policy list
412 CORBA::PolicyList policyList;
413 policyList.length(2);
414 policyList[0] = PortableServer::ImplicitActivationPolicy::
415 _duplicate(implicitActivation);
416 policyList[1] = PortableServer::ThreadPolicy::
417 _duplicate(threadPolicy);
419 PortableServer::POAManager_var nil_mgr
420 = PortableServer::POAManager::_nil();
421 factory_poa = _root_poa->create_POA("factory_poa",
424 //with nil_mgr instead of pman,
425 //a new POA manager is created with the new POA
427 // destroy policy objects
428 implicitActivation->destroy();
429 threadPolicy->destroy();
431 // obtain the factory poa manager
432 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
434 MESSAGE("pmanfac->activate()");
437 char *containerName = "";
440 containerName = argv[1];
443 Engines_Container_i * myContainer
444 = new Engines_Container_i(_orb, factory_poa, containerName , argc , argv );
446 catch(CORBA::SystemException&)
448 INFOS("Caught CORBA::SystemException.");
450 catch(PortableServer::POA::WrongPolicy&)
452 INFOS("Caught CORBA::WrongPolicyException.");
454 catch(PortableServer::POA::ServantAlreadyActive&)
456 INFOS("Caught CORBA::ServantAlreadyActiveException");
458 catch(CORBA::Exception&)
460 INFOS("Caught CORBA::Exception.");
464 INFOS("Caught unknown exception.");
468 //=============================================================================
472 //=============================================================================
474 void Session_ServerThread::ActivateSession(int argc,
477 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
480 Session_SessionThread::Session_SessionThread(int argc,
483 PortableServer::POA_ptr poa,
485 QWaitCondition* GUILauncher)
486 : Session_ServerThread(argc, argv, orb, poa, GUIMutex),
487 _GUILauncher( GUILauncher )
491 Session_SessionThread::~Session_SessionThread()
495 void Session_SessionThread::ActivateSession(int argc,
500 INFOS("Session thread started");
501 SALOME_Session_i * mySALOME_Session
502 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
503 PortableServer::ObjectId_var mySALOME_Sessionid
504 = _root_poa->activate_object(mySALOME_Session);
505 INFOS("poa->activate_object(mySALOME_Session)");
507 CORBA::Object_var obj = mySALOME_Session->_this();
508 CORBA::String_var sior(_orb->object_to_string(obj));
510 mySALOME_Session->NSregister();
512 catch (CORBA::SystemException&)
514 INFOS("Caught CORBA::SystemException.");
516 catch (CORBA::Exception&)
518 INFOS("Caught CORBA::Exception.");
522 INFOS("Caught unknown exception.");