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