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