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