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