Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / Session / Session_ServerThread.cxx
1 //  SALOME Session : implementation of Session_ServerThread.cxx
2 //
3 //  Copyright (C) 2003  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.
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 //
24 //  File   : Session_ServerThread.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 // #include <SALOMEconfig.h>
30 // #include CORBA_SERVER_HEADER(SALOME_Session)
31 // #include CORBA_SERVER_HEADER(SALOMEDS)
32
33 #include "Session_ServerThread.hxx"
34
35 #include "SALOME_Container_i.hxx"
36 #include "SALOME_Launcher.hxx"
37 #include <SALOMEDSClient.hxx>
38 #include <SALOMEDSClient_ClientFactory.hxx>
39 #include "SALOME_ModuleCatalog_impl.hxx"
40 #include "RegistryService.hxx"
41 #include "Session_Session_i.hxx"
42 #include "SalomeApp_Engine_i.hxx"
43
44 #include "Utils_ORB_INIT.hxx"
45 #include "Utils_SINGLETON.hxx"
46 #include "Utils_SALOME_Exception.hxx"
47 #include "OpUtil.hxx"
48 #include "NamingService_WaitForServerReadiness.hxx"
49 #include "utilities.h"
50
51 #include <cstdlib>
52 #include <ctime>
53
54 using namespace std;
55
56 const int Session_ServerThread::NB_SRV_TYP = 7;
57 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
58                                                               "ModuleCatalog",
59                                                               "Registry",
60                                                               "SALOMEDS",
61                                                               "Session",
62                                                               "SalomeAppEngine",
63                                                               "ContainerManager"};
64
65 /*!
66   default constructor not for use
67 */
68 Session_ServerThread::Session_ServerThread()
69 {
70   ASSERT(0); // must not be called
71 }
72
73 /*!
74   constructor
75 */
76 Session_ServerThread::Session_ServerThread(int argc,
77                                            char ** argv,
78                                            CORBA::ORB_ptr orb,
79                                            PortableServer::POA_ptr poa)
80 {
81   //MESSAGE("Session_ServerThread Constructor " << argv[0]);
82   _argc = argc;
83   _argv = new char*[ _argc + 1 ];
84   _argv[_argc] = 0;
85   for (int i = 0; i < _argc; i++ )
86     _argv[i] = strdup( argv[i] );
87
88   _orb = CORBA::ORB::_duplicate(orb);
89   _root_poa = PortableServer::POA::_duplicate(poa);
90   _servType =-1;
91   _NS = new SALOME_NamingService(_orb); // one instance per server to limit
92                                         // multi thread coherence problems
93 }
94
95 /*!
96   destructor
97 */
98 Session_ServerThread::~Session_ServerThread()
99 {
100   //MESSAGE("~Session_ServerThread "<< _argv[0]);
101   delete _NS;
102   for (int i = 0; i <_argc ; i++ )
103     free( _argv[i] );
104   delete[] _argv;
105 }
106
107 /*!
108   run the thread : activate one servant, the servant type is given by
109   argument _argv[0]
110 */
111 void Session_ServerThread::Init()
112 {
113   MESSAGE("Session_ServerThread::Init "<< _argv[0]);
114
115   for (int i=0; i<_argc; i++) SCRUTE(_argv[i]);
116 #ifndef WNT
117   for (int i=0; i<NB_SRV_TYP; i++)
118 #else
119   for (i=0; i<NB_SRV_TYP; i++)
120 #endif
121     if (strcmp(_argv[0],_serverTypes[i])==0)
122       {
123         _servType = i;
124         MESSAGE("Server Thread type : "<<_serverTypes[i]);
125         switch (_servType)
126           {
127           case 0:  // Container
128             {
129               NamingService_WaitForServerReadiness(_NS,"/Registry");
130               NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
131               ActivateContainer(_argc, _argv);
132               break;
133             }
134           case 1:  // ModuleCatalog
135             {
136               NamingService_WaitForServerReadiness(_NS,"/Registry");
137               ActivateModuleCatalog(_argc, _argv);
138               break;
139             }
140           case 2:  // Registry
141             {
142               NamingService_WaitForServerReadiness(_NS,"");
143               ActivateRegistry(_argc, _argv);
144               break;
145             }
146           case 3:  // SALOMEDS
147             {
148               NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
149               ActivateSALOMEDS(_argc, _argv);
150               break;
151             }
152           case 4:  // Session
153             {
154               NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
155               string containerName = "/Containers/";
156               containerName = containerName + GetHostname();
157               containerName = containerName + "/FactoryServer";
158               NamingService_WaitForServerReadiness(_NS,containerName);
159               ActivateSession(_argc, _argv);
160               break;
161             }
162           case 5: // SalomeApp_Engine
163             {
164               NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
165               ActivateEngine(_argc, _argv);
166               break;
167             }
168           case 6: // Container Manager
169             {
170               NamingService_WaitForServerReadiness(_NS,"");
171               ActivateContainerManager(_argc, _argv);
172               break;
173             }
174           default:
175             {
176               ASSERT(0);
177               break;
178             }
179           }
180       }
181 }
182
183 void Session_ServerThread::ActivateModuleCatalog(int argc,
184                                                  char ** argv)
185 {
186   try
187     {
188       MESSAGE("ModuleCatalog thread started");
189       // allocation on heap to allow destruction by POA
190
191       SALOME_ModuleCatalogImpl* Catalogue_i
192         = new SALOME_ModuleCatalogImpl(argc, argv);
193
194       // Tell the POA that the objects are ready to accept requests.
195
196       PortableServer::ObjectId_var id= _root_poa->activate_object (Catalogue_i);
197       Catalogue_i->_remove_ref();
198
199       CORBA::Object_var myCata = Catalogue_i->_this();
200       _NS->Register(myCata ,"/Kernel/ModulCatalog");
201     }
202   catch(CORBA::SystemException&)
203     {
204       INFOS( "Caught CORBA::SystemException." );
205     }
206   catch(CORBA::Exception&)
207     {
208       INFOS( "Caught CORBA::Exception." );
209     }
210   catch(omniORB::fatalException& fe)
211     {
212       INFOS( "Caught omniORB::fatalException:" );
213       INFOS( "  file: " << fe.file() );
214       INFOS( "  line: " << fe.line() );
215       INFOS( "  mesg: " << fe.errmsg() );
216     }
217   catch(...)
218     {
219       INFOS( "Caught unknown exception." );
220     }
221 }
222
223 void Session_ServerThread::ActivateSALOMEDS(int argc,
224                                             char ** argv)
225 {
226   try
227     {
228       MESSAGE("SALOMEDS thread started");
229       // We allocate the objects on the heap.  Since these are reference
230       // counted objects, they will be deleted by the POA when they are no
231       // longer needed.
232
233       ClientFactory::createStudyManager(_orb,_root_poa);
234     }
235   catch(CORBA::SystemException&)
236     {
237       INFOS( "Caught CORBA::SystemException." );
238     }
239   catch(CORBA::Exception&)
240     {
241       INFOS( "Caught CORBA::Exception." );
242     }
243   catch(omniORB::fatalException& fe)
244     {
245       INFOS( "Caught omniORB::fatalException:" );
246       INFOS( "  file: " << fe.file() );
247       INFOS( "  line: " << fe.line() );
248       INFOS( "  mesg: " << fe.errmsg() );
249     }
250   catch(...)
251     {
252       INFOS( "Caught unknown exception." );
253     }
254 }
255
256 void Session_ServerThread::ActivateRegistry(int argc,
257                                             char ** argv)
258 {
259   MESSAGE("Registry thread started");
260   SCRUTE(argc);
261   if( argc<3 )
262     {
263       INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
264       throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
265     }
266   const char *ptrSessionName=0;
267
268   int k=0 ;
269   for ( k=1 ; k<argc ; k++ )
270     {
271       if( strcmp(argv[k],"--salome_session")==0 )
272         {
273           ptrSessionName=argv[k+1];
274           break;
275         }
276     }
277   ASSERT(ptrSessionName) ;
278   ASSERT(strlen( ptrSessionName )>0);
279   const char *registryName = "Registry";
280   Registry::Components_var varComponents;
281   try
282     {
283       RegistryService *ptrRegistry = SINGLETON_<RegistryService>::Instance();
284       ptrRegistry->SessionName( ptrSessionName );
285       ptrRegistry->SetOrb(_orb);
286       varComponents = ptrRegistry->_this();
287       // The RegistryService must not already exist.
288
289       try
290         {
291           CORBA::Object_var pipo = _NS->Resolve( registryName );
292           if (CORBA::is_nil(pipo) )  throw ServiceUnreachable();
293           INFOS("RegistryService servant already existing" );
294           ASSERT(0);
295         }
296       catch( const ServiceUnreachable &/*ex*/ )
297         {
298         }
299       catch( const CORBA::Exception &/*exx*/ )
300         {
301         }
302       string absoluteName = string("/") + registryName;
303       _NS->Register( varComponents , absoluteName.c_str() );
304       MESSAGE("On attend les requetes des clients");
305     }
306   catch( const SALOME_Exception &ex )
307     {
308       INFOS( "Communication Error : " << ex.what() );
309       ASSERT(0);
310     }
311 }
312
313 void Session_ServerThread::ActivateContainerManager(int argc,
314                                                     char ** argv)
315 {
316   try
317     {
318       PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
319       cout << "ActivateSalomeLauncher ......!!!! " << endl;
320       SALOME_Launcher * myContainer = new SALOME_Launcher(_orb,root_poa);
321     }
322   catch(CORBA::SystemException&)
323     {
324       INFOS("Caught CORBA::SystemException.");
325     }
326   catch(PortableServer::POA::WrongPolicy&)
327     {
328       INFOS("Caught CORBA::WrongPolicyException.");
329     }
330   catch(PortableServer::POA::ServantAlreadyActive&)
331     {
332       INFOS("Caught CORBA::ServantAlreadyActiveException");
333     }
334   catch(CORBA::Exception&)
335     {
336       INFOS("Caught CORBA::Exception.");
337     }
338   catch(...)
339     {
340       INFOS("Caught unknown exception.");
341     }
342 }
343
344 void Session_ServerThread::ActivateContainer(int argc,
345                                              char ** argv)
346 {
347   try
348     {
349       MESSAGE("Container thread started");
350
351       // get or create the child POA
352
353       PortableServer::POA_var factory_poa;
354       try
355         {
356           factory_poa = _root_poa->find_POA("factory_poa",0);
357           // 0 = no activation (already done if exists)
358         }
359       catch (PortableServer::POA::AdapterNonExistent&)
360         {
361           MESSAGE("factory_poa does not exists, create...");
362           // define policy objects
363           PortableServer::ImplicitActivationPolicy_var implicitActivation =
364             _root_poa->create_implicit_activation_policy(
365                                                          PortableServer::NO_IMPLICIT_ACTIVATION);
366           // default = NO_IMPLICIT_ACTIVATION
367           PortableServer::ThreadPolicy_var threadPolicy =
368             _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
369           // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
370
371           // create policy list
372           CORBA::PolicyList policyList;
373           policyList.length(2);
374           policyList[0] = PortableServer::ImplicitActivationPolicy::
375             _duplicate(implicitActivation);
376           policyList[1] = PortableServer::ThreadPolicy::
377             _duplicate(threadPolicy);
378
379           PortableServer::POAManager_var nil_mgr
380             = PortableServer::POAManager::_nil();
381           factory_poa = _root_poa->create_POA("factory_poa",
382                                               nil_mgr,
383                                               policyList);
384           //with nil_mgr instead of pman,
385           //a new POA manager is created with the new POA
386
387           // destroy policy objects
388           implicitActivation->destroy();
389           threadPolicy->destroy();
390
391           // obtain the factory poa manager
392           PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
393           pmanfac->activate();
394           MESSAGE("pmanfac->activate()");
395         }
396
397       char *containerName = "";
398       if (argc >1)
399         {
400           containerName = argv[1];
401         }
402
403       Engines_Container_i * myContainer
404         = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
405     }
406   catch(CORBA::SystemException&)
407     {
408       INFOS("Caught CORBA::SystemException.");
409     }
410   catch(PortableServer::POA::WrongPolicy&)
411     {
412       INFOS("Caught CORBA::WrongPolicyException.");
413     }
414   catch(PortableServer::POA::ServantAlreadyActive&)
415     {
416       INFOS("Caught CORBA::ServantAlreadyActiveException");
417     }
418   catch(CORBA::Exception&)
419     {
420       INFOS("Caught CORBA::Exception.");
421     }
422   catch(...)
423     {
424       INFOS("Caught unknown exception.");
425     }
426 }
427
428 void Session_ServerThread::ActivateEngine(int /*argc*/, char ** /*argv*/)
429 {
430   try
431     {
432       MESSAGE("SalomeApp_Engine thread started");
433       SalomeApp_Engine_i* anEngine = new SalomeApp_Engine_i();
434       PortableServer::ObjectId_var id =_root_poa->activate_object( anEngine );
435       MESSAGE("poa->activate_object( SalomeApp_Engine )");
436
437       CORBA::Object_var obj = anEngine->_this();
438       anEngine->_remove_ref();
439       _NS->Register( obj ,"/SalomeAppEngine");
440
441     }
442   catch (CORBA::SystemException&)
443     {
444       INFOS("Caught CORBA::SystemException.");
445     }
446   catch (CORBA::Exception&)
447     {
448       INFOS("Caught CORBA::Exception.");
449     }
450   catch (...)
451     {
452       INFOS("Caught unknown exception.");
453     }
454 }
455
456 void Session_ServerThread::ActivateSession(int argc,
457                                            char ** argv)
458 {
459   MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
460 }
461
462 /*!
463   constructor
464 */
465 Session_SessionThread::Session_SessionThread(int argc,
466                                              char** argv,
467                                              CORBA::ORB_ptr orb,
468                                              PortableServer::POA_ptr poa,
469                                              QMutex* GUIMutex,
470                                              QWaitCondition* GUILauncher)
471   : Session_ServerThread(argc, argv, orb, poa),
472     _GUIMutex( GUIMutex ),
473     _GUILauncher( GUILauncher )
474 {
475 }
476
477 /*!
478   destructor
479 */
480 Session_SessionThread::~Session_SessionThread()
481 {
482 }
483
484 void Session_SessionThread::ActivateSession(int argc,
485                                             char ** argv)
486 {
487   try
488     {
489       MESSAGE("Session thread started");
490       SALOME_Session_i * mySALOME_Session
491         = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
492       PortableServer::ObjectId_var mySALOME_Sessionid
493         = _root_poa->activate_object(mySALOME_Session);
494       MESSAGE("poa->activate_object(mySALOME_Session)");
495
496       CORBA::Object_var obj = mySALOME_Session->_this();
497       CORBA::String_var sior(_orb->object_to_string(obj));
498       mySALOME_Session->_remove_ref();
499
500       mySALOME_Session->NSregister();
501     }
502   catch (CORBA::SystemException&)
503     {
504       INFOS("Caught CORBA::SystemException.");
505     }
506   catch (CORBA::Exception&)
507     {
508       INFOS("Caught CORBA::Exception.");
509     }
510   catch (...)
511     {
512       INFOS("Caught unknown exception.");
513     }
514 }