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