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