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