1 // Copyright (C) 2007-2012 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.
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
92 Session_ServerThread::~Session_ServerThread()
94 //MESSAGE("~Session_ServerThread "<< _argv[0]);
96 for (int i = 0; i <_argc ; i++ )
102 run the thread : activate one servant, the servant type is given by
105 void Session_ServerThread::Init()
107 MESSAGE("Session_ServerThread::Init "<< _argv[0]);
110 for (i=0; i<_argc; i++) SCRUTE(_argv[i]);
112 for (i=0; i<NB_SRV_TYP; i++) {
113 if (strcmp(_argv[0],_serverTypes[i])==0) {
115 MESSAGE("Server Thread type : "<<_serverTypes[i]);
119 NamingService_WaitForServerReadiness(_NS,"/Registry");
120 NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
121 ActivateContainer(_argc, _argv);
124 case 1: // ModuleCatalog
126 NamingService_WaitForServerReadiness(_NS,"/Registry");
127 ActivateModuleCatalog(_argc, _argv);
132 NamingService_WaitForServerReadiness(_NS,"");
133 ActivateRegistry(_argc, _argv);
138 NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
139 ActivateSALOMEDS(_argc, _argv);
144 NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
145 std::string containerName = "/Containers/";
146 containerName = containerName + Kernel_Utils::GetHostname();
147 containerName = containerName + "/FactoryServer";
148 NamingService_WaitForServerReadiness(_NS,containerName);
149 ActivateSession(_argc, _argv);
152 case 5: // Container Manager
154 NamingService_WaitForServerReadiness(_NS,"");
155 ActivateContainerManager(_argc, _argv);
168 void Session_ServerThread::ActivateModuleCatalog(int argc,
172 MESSAGE("ModuleCatalog thread started");
173 // allocation on heap to allow destruction by POA
175 SALOME_ModuleCatalogImpl* Catalogue_i
176 = new SALOME_ModuleCatalogImpl(argc, argv);
178 // Tell the POA that the objects are ready to accept requests.
180 PortableServer::ObjectId_var id = _root_poa->activate_object (Catalogue_i);
181 Catalogue_i->_remove_ref();
183 CORBA::Object_var myCata = Catalogue_i->_this();
184 _NS->Register(myCata ,"/Kernel/ModulCatalog");
186 catch(CORBA::SystemException&) {
187 INFOS( "Caught CORBA::SystemException." );
189 catch(CORBA::Exception&) {
190 INFOS( "Caught CORBA::Exception." );
192 catch(omniORB::fatalException& fe) {
193 INFOS( "Caught omniORB::fatalException:" );
194 INFOS( " file: " << fe.file() );
195 INFOS( " line: " << fe.line() );
196 INFOS( " mesg: " << fe.errmsg() );
199 INFOS( "Caught unknown exception." );
203 void Session_ServerThread::ActivateSALOMEDS(int argc,
207 MESSAGE("SALOMEDS thread started");
208 // We allocate the objects on the heap. Since these are reference
209 // counted objects, they will be deleted by the POA when they are no
212 ClientFactory::createStudyManager(_orb,_root_poa);
214 catch(CORBA::SystemException&) {
215 INFOS( "Caught CORBA::SystemException." );
217 catch(CORBA::Exception&) {
218 INFOS( "Caught CORBA::Exception." );
220 catch(omniORB::fatalException& fe) {
221 INFOS( "Caught omniORB::fatalException:" );
222 INFOS( " file: " << fe.file() );
223 INFOS( " line: " << fe.line() );
224 INFOS( " mesg: " << fe.errmsg() );
227 INFOS( "Caught unknown exception." );
231 void Session_ServerThread::ActivateRegistry(int argc,
234 MESSAGE("Registry thread started");
237 INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
238 throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
240 const char *ptrSessionName=0;
243 for ( k=1 ; k<argc ; k++ ) {
244 if ( strcmp(argv[k],"--salome_session")==0 ) {
245 ptrSessionName=argv[k+1];
249 ASSERT(ptrSessionName) ;
250 ASSERT(strlen( ptrSessionName )>0);
251 const char *registryName = "Registry";
252 Registry::Components_var varComponents;
254 RegistryService *ptrRegistry = new RegistryService;
255 ptrRegistry->SessionName( ptrSessionName );
256 ptrRegistry->SetOrb(_orb);
257 varComponents = ptrRegistry->_this();
258 ptrRegistry->_remove_ref(); //let poa manage registry service deletion
259 // The RegistryService must not already exist.
262 CORBA::Object_var pipo = _NS->Resolve( registryName );
263 if (CORBA::is_nil(pipo) ) throw ServiceUnreachable();
264 INFOS("RegistryService servant already existing" );
267 catch( const ServiceUnreachable &/*ex*/ ) {
269 catch( const CORBA::Exception &/*exx*/ ) {
271 std::string absoluteName = std::string("/") + registryName;
272 _NS->Register( varComponents , absoluteName.c_str() );
274 catch( const SALOME_Exception &ex ) {
275 INFOS( "Communication Error : " << ex.what() );
280 void Session_ServerThread::ActivateContainerManager(int argc,
284 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
285 std::cout << "Activate SalomeLauncher ......!!!! " << std::endl;
286 new SALOME_Launcher(_orb,root_poa);
288 catch(CORBA::SystemException&) {
289 INFOS("Caught CORBA::SystemException.");
291 catch(PortableServer::POA::WrongPolicy&) {
292 INFOS("Caught CORBA::WrongPolicyException.");
294 catch(PortableServer::POA::ServantAlreadyActive&) {
295 INFOS("Caught CORBA::ServantAlreadyActiveException");
297 catch(CORBA::Exception&) {
298 INFOS("Caught CORBA::Exception.");
301 INFOS("Caught unknown exception.");
305 void Session_ServerThread::ActivateContainer(int argc,
309 MESSAGE("Container thread started");
311 // get or create the child POA
313 PortableServer::POA_var factory_poa;
315 factory_poa = _root_poa->find_POA("factory_poa",0);
316 // 0 = no activation (already done if exists)
318 catch (PortableServer::POA::AdapterNonExistent&) {
319 MESSAGE("factory_poa does not exists, create...");
320 // define policy objects
321 PortableServer::ImplicitActivationPolicy_var implicitActivation =
322 _root_poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);
323 // default = NO_IMPLICIT_ACTIVATION
324 PortableServer::ThreadPolicy_var threadPolicy =
325 _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
326 // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
328 // create policy list
329 CORBA::PolicyList policyList;
330 policyList.length(2);
331 policyList[0] = PortableServer::ImplicitActivationPolicy::
332 _duplicate(implicitActivation);
333 policyList[1] = PortableServer::ThreadPolicy::
334 _duplicate(threadPolicy);
336 PortableServer::POAManager_var nil_mgr
337 = PortableServer::POAManager::_nil();
338 factory_poa = _root_poa->create_POA("factory_poa",
341 //with nil_mgr instead of pman,
342 //a new POA manager is created with the new POA
344 // destroy policy objects
345 implicitActivation->destroy();
346 threadPolicy->destroy();
348 // obtain the factory poa manager
349 PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
351 MESSAGE("pmanfac->activate()");
354 char *containerName = (char*)"";
356 containerName = argv[1];
359 new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
361 catch(CORBA::SystemException&) {
362 INFOS("Caught CORBA::SystemException.");
364 catch(PortableServer::POA::WrongPolicy&) {
365 INFOS("Caught CORBA::WrongPolicyException.");
367 catch(PortableServer::POA::ServantAlreadyActive&) {
368 INFOS("Caught CORBA::ServantAlreadyActiveException");
370 catch(CORBA::Exception&) {
371 INFOS("Caught CORBA::Exception.");
374 INFOS("Caught unknown exception.");
378 void Session_ServerThread::ActivateSession(int argc,
381 MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
387 Session_SessionThread::Session_SessionThread(int argc,
390 PortableServer::POA_ptr poa,
392 QWaitCondition* GUILauncher)
393 : Session_ServerThread(argc, argv, orb, poa),
394 _GUIMutex( GUIMutex ),
395 _GUILauncher( GUILauncher )
402 Session_SessionThread::~Session_SessionThread()
406 void Session_SessionThread::ActivateSession(int argc,
410 MESSAGE("Session thread started");
411 SALOME_Session_i * mySALOME_Session
412 = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
413 PortableServer::ObjectId_var mySALOME_Sessionid
414 = _root_poa->activate_object(mySALOME_Session);
415 MESSAGE("poa->activate_object(mySALOME_Session)");
417 CORBA::Object_var obj = mySALOME_Session->_this();
418 CORBA::String_var sior(_orb->object_to_string(obj));
419 mySALOME_Session->_remove_ref();
421 mySALOME_Session->NSregister();
423 catch (CORBA::SystemException&) {
424 INFOS("Caught CORBA::SystemException.");
426 catch (CORBA::Exception&) {
427 INFOS("Caught CORBA::Exception.");
430 INFOS("Caught unknown exception.");