Salome HOME
#18963 Minimize compiler warnings
[modules/gui.git] / src / Session / Session_ServerThread.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME Session : implementation of Session_ServerThread.cxx
24 //  File   : Session_ServerThread.cxx
25 //  Author : Paul RASCLE, EDF
26
27 #include "Session_ServerThread.hxx"
28
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>
35
36 #include "Session_Session_i.hxx"
37
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>
44
45 #include <cstdlib>
46 #include <ctime>
47
48 #include <QMutex>
49 #include <QWaitCondition>
50
51 const int Session_ServerThread::NB_SRV_TYP = 6;
52 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
53                                                               "ModuleCatalog",
54                                                               "Registry",
55                                                               "SALOMEDS",
56                                                               "Session",
57                                                               "ContainerManager"};
58
59 /*! 
60   default constructor not for use
61 */
62 Session_ServerThread::Session_ServerThread()
63 {
64   ASSERT(0); // must not be called
65 }
66
67 /*! 
68   constructor
69 */
70 Session_ServerThread::Session_ServerThread(int argc,
71                                            char ** argv, 
72                                            CORBA::ORB_ptr orb, 
73                                            PortableServer::POA_ptr poa)
74 {
75   //MESSAGE("Session_ServerThread Constructor " << argv[0]);
76   _argc = argc;
77   _argv = new char*[ _argc + 1 ];
78   _argv[_argc] = 0;
79   for (int i = 0; i < _argc; i++ )
80     _argv[i] = strdup( argv[i] );
81
82   _orb = CORBA::ORB::_duplicate(orb);
83   _root_poa = PortableServer::POA::_duplicate(poa);
84   _servType =-1;
85   _NS = new SALOME_NamingService(_orb); // one instance per server to limit
86                                         // multi thread coherence problems
87   _container = 0;                       // embedded container
88 }
89
90 /*! 
91   destructor 
92 */
93 Session_ServerThread::~Session_ServerThread()
94 {
95   //MESSAGE("~Session_ServerThread "<< _argv[0]);
96   delete _NS;
97   for (int i = 0; i <_argc ; i++ )
98     free( _argv[i] );
99   delete[] _argv;
100 }
101
102 /*! 
103   run the thread : activate one servant, the servant type is given by
104   argument _argv[0]
105 */
106 void Session_ServerThread::Init()
107 {
108   MESSAGE("Session_ServerThread::Init "<< _argv[0]); 
109
110   int i;
111   for (i=0; i<_argc; i++) SCRUTE(_argv[i]);
112
113   for (i=0; i<NB_SRV_TYP; i++) {
114     if (strcmp(_argv[0],_serverTypes[i])==0) {
115       _servType = i;
116       MESSAGE("Server Thread type : "<<_serverTypes[i]);
117       switch (_servType) {
118       case 0:  // Container
119         {
120           NamingService_WaitForServerReadiness(_NS,"/Registry");
121           NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
122           ActivateContainer(_argc, _argv);
123           break;
124         }
125       case 1:  // ModuleCatalog
126         {
127           NamingService_WaitForServerReadiness(_NS,"/Registry");
128           ActivateModuleCatalog(_argc, _argv);
129           break;
130         }
131       case 2:  // Registry
132         {
133           NamingService_WaitForServerReadiness(_NS,"");
134           ActivateRegistry(_argc, _argv);
135           break;
136         }
137       case 3:  // SALOMEDS
138         {
139           NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
140           ActivateSALOMEDS(_argc, _argv);
141           break;
142         }
143       case 4:  // Session
144         {
145           NamingService_WaitForServerReadiness(_NS,"/Study");
146           std::string containerName = "/Containers/";
147           containerName = containerName + Kernel_Utils::GetHostname();
148           containerName = containerName + "/FactoryServer";
149           NamingService_WaitForServerReadiness(_NS,containerName);
150           ActivateSession(_argc, _argv);
151           break;
152         }
153       case 5: // Container Manager
154         {
155           NamingService_WaitForServerReadiness(_NS,"");
156           ActivateContainerManager(_argc, _argv);
157           break;
158         }
159       default:
160         {
161           ASSERT(0);
162           break;
163         }
164       }
165     }
166   }
167 }
168
169 void Session_ServerThread::Shutdown()
170 {
171   if ( _container ) _container->Shutdown();
172 }
173
174 void Session_ServerThread::ActivateModuleCatalog(int argc,
175                                                  char ** argv)
176 {
177   try {
178     MESSAGE("ModuleCatalog thread started");
179     // allocation on heap to allow destruction by POA
180     
181     SALOME_ModuleCatalogImpl* Catalogue_i
182       = new SALOME_ModuleCatalogImpl(argc, argv);
183     
184     // Tell the POA that the objects are ready to accept requests.
185     
186     PortableServer::ObjectId_var id = _root_poa->activate_object (Catalogue_i);
187     Catalogue_i->_remove_ref();
188     
189     CORBA::Object_var myCata = Catalogue_i->_this();
190     _NS->Register(myCata ,"/Kernel/ModulCatalog");
191   }
192   catch(CORBA::SystemException&) {
193     INFOS( "Caught CORBA::SystemException." );
194   }
195   catch(CORBA::Exception&) {
196     INFOS( "Caught CORBA::Exception." );
197   }
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() );
203   }
204   catch(...) {
205     INFOS( "Caught unknown exception." );
206   }
207 }
208
209 void Session_ServerThread::ActivateSALOMEDS(int /*argc*/, char** /*argv*/)
210 {
211   try {
212     MESSAGE("SALOMEDS thread started");
213     // We allocate the objects on the heap.  Since these are reference
214     // counted objects, they will be deleted by the POA when they are no
215     // longer needed.    
216     
217     ClientFactory::createStudy(_orb,_root_poa);
218   }
219   catch(CORBA::SystemException&) {
220     INFOS( "Caught CORBA::SystemException." );
221   }
222   catch(CORBA::Exception&) {
223     INFOS( "Caught CORBA::Exception." );
224   }
225   catch(omniORB::fatalException& fe) {
226     INFOS( "Caught omniORB::fatalException:" );
227     INFOS( "  file: " << fe.file() );
228     INFOS( "  line: " << fe.line() );
229     INFOS( "  mesg: " << fe.errmsg() );
230   }
231   catch(...) {
232     INFOS( "Caught unknown exception." );
233   }
234 }
235
236 void Session_ServerThread::ActivateRegistry(int argc,
237                                             char ** argv)
238 {
239   MESSAGE("Registry thread started");
240   SCRUTE(argc); 
241   if ( argc<3 ) {
242     INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
243     throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
244   }
245   const char *ptrSessionName=0;
246
247   int k=0 ;
248   for ( k=1 ; k<argc ; k++ ) {
249     if ( strcmp(argv[k],"--salome_session")==0 ) {
250       ptrSessionName=argv[k+1];
251       break;
252     }
253   }
254   ASSERT(ptrSessionName) ;
255   ASSERT(strlen( ptrSessionName )>0);
256   const char *registryName = "Registry";
257   Registry::Components_var varComponents;
258   try {
259     RegistryService *ptrRegistry = new RegistryService;
260     ptrRegistry->SessionName( ptrSessionName );
261     ptrRegistry->SetOrb(_orb);
262     //
263     CORBA::PolicyList policies;
264     policies.length(1);
265     PortableServer::ThreadPolicy_var threadPol(_root_poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
266     policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol);
267     PortableServer::POAManager_var manager = _root_poa->the_POAManager();
268     PortableServer::POA_var poa2(_root_poa->create_POA("SingleThreadPOA4RegistryEmbedded",manager,policies));
269     threadPol->destroy();
270     //
271     PortableServer::ObjectId_var id(poa2->activate_object(ptrRegistry));
272     CORBA::Object_var pipo=poa2->id_to_reference(id);
273     varComponents = Registry::Components::_narrow(pipo) ;
274     ptrRegistry->_remove_ref(); //let poa manage registryservice deletion
275     
276     try {
277       CORBA::Object_var pipo = _NS->Resolve( registryName );
278       if (CORBA::is_nil(pipo) )  throw ServiceUnreachable();
279       INFOS("RegistryService servant already existing" );
280       ASSERT(0);
281     }
282     catch( const ServiceUnreachable &/*ex*/ ) {
283     }
284     catch( const CORBA::Exception &/*exx*/ ) {
285     }
286     std::string absoluteName = std::string("/") + registryName;
287     _NS->Register( varComponents , absoluteName.c_str() );
288   }
289   catch( const SALOME_Exception &ex ) {
290     INFOS( "Communication Error : " << ex.what() );
291     ASSERT(0);
292   }
293 }
294
295 void Session_ServerThread::ActivateContainerManager(int /*argc*/, char** /*argv*/)
296 {
297   try {
298     PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
299     std::cout << "Activate SalomeLauncher ......!!!! " << std::endl;
300     new SALOME_Launcher(_orb,root_poa);
301   }
302   catch(CORBA::SystemException&) {
303     INFOS("Caught CORBA::SystemException.");
304   }
305   catch(PortableServer::POA::WrongPolicy&) {
306     INFOS("Caught CORBA::WrongPolicyException.");
307   }
308   catch(PortableServer::POA::ServantAlreadyActive&) {
309     INFOS("Caught CORBA::ServantAlreadyActiveException");
310   }
311   catch(CORBA::Exception&) {
312     INFOS("Caught CORBA::Exception.");
313   }
314   catch(...) {
315     INFOS("Caught unknown exception.");
316   }
317 }
318
319 void Session_ServerThread::ActivateContainer(int argc, char** argv)
320 {
321   try {
322     MESSAGE("Container thread started");
323     
324     // get or create the child POA
325     
326     PortableServer::POA_var factory_poa;
327     try {
328       factory_poa = _root_poa->find_POA("factory_poa",0);
329       // 0 = no activation (already done if exists)
330     }
331     catch (PortableServer::POA::AdapterNonExistent&) {
332       MESSAGE("factory_poa does not exists, create...");
333       // define policy objects     
334       PortableServer::ImplicitActivationPolicy_var implicitActivation =
335         _root_poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);
336       // default = NO_IMPLICIT_ACTIVATION
337       PortableServer::ThreadPolicy_var threadPolicy =
338         _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
339       // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
340       
341       // create policy list
342       CORBA::PolicyList policyList;
343       policyList.length(2);
344       policyList[0] = PortableServer::ImplicitActivationPolicy::
345         _duplicate(implicitActivation);
346       policyList[1] = PortableServer::ThreadPolicy::
347         _duplicate(threadPolicy);
348       
349       PortableServer::POAManager_var nil_mgr
350         = PortableServer::POAManager::_nil();
351       factory_poa = _root_poa->create_POA("factory_poa",
352                                           nil_mgr,
353                                           policyList);
354       //with nil_mgr instead of pman,
355       //a new POA manager is created with the new POA
356       
357       // destroy policy objects
358       implicitActivation->destroy();
359       threadPolicy->destroy();
360       
361       // obtain the factory poa manager
362       PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
363       pmanfac->activate();
364       MESSAGE("pmanfac->activate()");
365     }
366     
367     char *containerName = (char*)"";
368     if (argc >1) {
369       containerName = argv[1];
370     }
371     
372     _container = new Engines_Container_i(_orb, _root_poa, containerName, argc, argv, true, false);
373   }
374   catch(CORBA::SystemException&) {
375     INFOS("Caught CORBA::SystemException.");
376   }
377   catch(PortableServer::POA::WrongPolicy&) {
378     INFOS("Caught CORBA::WrongPolicyException.");
379   }
380   catch(PortableServer::POA::ServantAlreadyActive&) {
381     INFOS("Caught CORBA::ServantAlreadyActiveException");
382   }
383   catch(CORBA::Exception&) {
384     INFOS("Caught CORBA::Exception.");
385   }
386   catch(...) {
387     INFOS("Caught unknown exception.");
388   }
389 }
390
391 void Session_ServerThread::ActivateSession(int /*argc*/, char** /*argv*/)
392 {
393   MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
394 }
395
396 /*! 
397   constructor 
398 */
399 Session_SessionThread::Session_SessionThread(int argc,
400                                              char** argv, 
401                                              CORBA::ORB_ptr orb, 
402                                              PortableServer::POA_ptr poa,
403                                              QMutex* GUIMutex,
404                                              QWaitCondition* GUILauncher)
405 : Session_ServerThread(argc, argv, orb, poa),
406   _GUIMutex( GUIMutex ),
407   _GUILauncher( GUILauncher )
408 {
409 }
410
411 /*! 
412   destructor 
413 */
414 Session_SessionThread::~Session_SessionThread()
415 {
416 }
417
418 void Session_SessionThread::ActivateSession(int argc,
419                                             char ** argv)
420 {
421   try {
422     MESSAGE("Session thread started");
423     SALOME_Session_i * mySALOME_Session
424       = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
425     PortableServer::ObjectId_var mySALOME_Sessionid
426       = _root_poa->activate_object(mySALOME_Session);
427     MESSAGE("poa->activate_object(mySALOME_Session)");
428     
429     CORBA::Object_var obj = mySALOME_Session->_this();
430     CORBA::String_var sior(_orb->object_to_string(obj));
431     mySALOME_Session->_remove_ref();
432     
433     mySALOME_Session->NSregister();
434   }
435   catch (CORBA::SystemException&) {
436     INFOS("Caught CORBA::SystemException.");
437   }
438   catch (CORBA::Exception&) {
439     INFOS("Caught CORBA::Exception.");
440   }
441   catch (...) {
442     INFOS("Caught unknown exception.");
443   }
444 }