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