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