Salome HOME
Merge Python 3 porting.
[modules/gui.git] / src / Session / Session_ServerThread.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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   _container = 0;                       // embedded container
88 }
89
90 /*! 
91   destructor 
92 */
93 Session_ServerThread::~Session_ServerThread()
94 {
95   //MESSAGE("~Session_ServerThread "<< _argv[0]);
96   delete _NS;
97   for (int i = 0; i <_argc ; i++ )
98     free( _argv[i] );
99   delete[] _argv;
100 }
101
102 /*! 
103   run the thread : activate one servant, the servant type is given by
104   argument _argv[0]
105 */
106 void Session_ServerThread::Init()
107 {
108   MESSAGE("Session_ServerThread::Init "<< _argv[0]); 
109
110   int i;
111   for (i=0; i<_argc; i++) SCRUTE(_argv[i]);
112
113   for (i=0; i<NB_SRV_TYP; i++) {
114     if (strcmp(_argv[0],_serverTypes[i])==0) {
115       _servType = i;
116       MESSAGE("Server Thread type : "<<_serverTypes[i]);
117       switch (_servType) {
118       case 0:  // Container
119         {
120           NamingService_WaitForServerReadiness(_NS,"/Registry");
121           NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
122           ActivateContainer(_argc, _argv);
123           break;
124         }
125       case 1:  // ModuleCatalog
126         {
127           NamingService_WaitForServerReadiness(_NS,"/Registry");
128           ActivateModuleCatalog(_argc, _argv);
129           break;
130         }
131       case 2:  // Registry
132         {
133           NamingService_WaitForServerReadiness(_NS,"");
134           ActivateRegistry(_argc, _argv);
135           break;
136         }
137       case 3:  // SALOMEDS
138         {
139           NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
140           ActivateSALOMEDS(_argc, _argv);
141           break;
142         }
143       case 4:  // Session
144         {
145           NamingService_WaitForServerReadiness(_NS,"/Study");
146           std::string containerName = "/Containers/";
147           containerName = containerName + Kernel_Utils::GetHostname();
148           containerName = containerName + "/FactoryServer";
149           NamingService_WaitForServerReadiness(_NS,containerName);
150           ActivateSession(_argc, _argv);
151           break;
152         }
153       case 5: // Container Manager
154         {
155           NamingService_WaitForServerReadiness(_NS,"");
156           ActivateContainerManager(_argc, _argv);
157           break;
158         }
159       default:
160         {
161           ASSERT(0);
162           break;
163         }
164       }
165     }
166   }
167 }
168
169 void Session_ServerThread::Shutdown()
170 {
171   if ( _container ) _container->Shutdown();
172 }
173
174 void Session_ServerThread::ActivateModuleCatalog(int argc,
175                                                  char ** argv)
176 {
177   try {
178     MESSAGE("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     PortableServer::ObjectId_var id = _root_poa->activate_object (Catalogue_i);
187     Catalogue_i->_remove_ref();
188     
189     CORBA::Object_var myCata = Catalogue_i->_this();
190     _NS->Register(myCata ,"/Kernel/ModulCatalog");
191   }
192   catch(CORBA::SystemException&) {
193     INFOS( "Caught CORBA::SystemException." );
194   }
195   catch(CORBA::Exception&) {
196     INFOS( "Caught CORBA::Exception." );
197   }
198   catch(omniORB::fatalException& fe) {
199     INFOS( "Caught omniORB::fatalException:" );
200     INFOS( "  file: " << fe.file() );
201     INFOS( "  line: " << fe.line() );
202     INFOS( "  mesg: " << fe.errmsg() );
203   }
204   catch(...) {
205     INFOS( "Caught unknown exception." );
206   }
207 }
208
209 void Session_ServerThread::ActivateSALOMEDS(int argc,
210                                             char ** argv)
211 {
212   try {
213     MESSAGE("SALOMEDS thread started");
214     // We allocate the objects on the heap.  Since these are reference
215     // counted objects, they will be deleted by the POA when they are no
216     // longer needed.    
217     
218     ClientFactory::createStudy(_orb,_root_poa);
219   }
220   catch(CORBA::SystemException&) {
221     INFOS( "Caught CORBA::SystemException." );
222   }
223   catch(CORBA::Exception&) {
224     INFOS( "Caught CORBA::Exception." );
225   }
226   catch(omniORB::fatalException& fe) {
227     INFOS( "Caught omniORB::fatalException:" );
228     INFOS( "  file: " << fe.file() );
229     INFOS( "  line: " << fe.line() );
230     INFOS( "  mesg: " << fe.errmsg() );
231   }
232   catch(...) {
233     INFOS( "Caught unknown exception." );
234   }
235 }
236
237 void Session_ServerThread::ActivateRegistry(int argc,
238                                             char ** argv)
239 {
240   MESSAGE("Registry thread started");
241   SCRUTE(argc); 
242   if ( argc<3 ) {
243     INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
244     throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
245   }
246   const char *ptrSessionName=0;
247
248   int k=0 ;
249   for ( k=1 ; k<argc ; k++ ) {
250     if ( strcmp(argv[k],"--salome_session")==0 ) {
251       ptrSessionName=argv[k+1];
252       break;
253     }
254   }
255   ASSERT(ptrSessionName) ;
256   ASSERT(strlen( ptrSessionName )>0);
257   const char *registryName = "Registry";
258   Registry::Components_var varComponents;
259   try {
260     RegistryService *ptrRegistry = new RegistryService;
261     ptrRegistry->SessionName( ptrSessionName );
262     ptrRegistry->SetOrb(_orb);
263     varComponents = ptrRegistry->_this();
264     ptrRegistry->_remove_ref(); //let poa manage registry service deletion
265     // The RegistryService must not already exist.
266     
267     try {
268       CORBA::Object_var pipo = _NS->Resolve( registryName );
269       if (CORBA::is_nil(pipo) )  throw ServiceUnreachable();
270       INFOS("RegistryService servant already existing" );
271       ASSERT(0);
272     }
273     catch( const ServiceUnreachable &/*ex*/ ) {
274     }
275     catch( const CORBA::Exception &/*exx*/ ) {
276     }
277     std::string absoluteName = std::string("/") + registryName;
278     _NS->Register( varComponents , absoluteName.c_str() );
279   }
280   catch( const SALOME_Exception &ex ) {
281     INFOS( "Communication Error : " << ex.what() );
282     ASSERT(0);
283   }
284 }
285
286 void Session_ServerThread::ActivateContainerManager(int argc,
287                                              char ** argv)
288 {
289   try {
290     PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
291     std::cout << "Activate SalomeLauncher ......!!!! " << std::endl;
292     new SALOME_Launcher(_orb,root_poa);
293   }
294   catch(CORBA::SystemException&) {
295     INFOS("Caught CORBA::SystemException.");
296   }
297   catch(PortableServer::POA::WrongPolicy&) {
298     INFOS("Caught CORBA::WrongPolicyException.");
299   }
300   catch(PortableServer::POA::ServantAlreadyActive&) {
301     INFOS("Caught CORBA::ServantAlreadyActiveException");
302   }
303   catch(CORBA::Exception&) {
304     INFOS("Caught CORBA::Exception.");
305   }
306   catch(...) {
307     INFOS("Caught unknown exception.");
308   }
309 }
310
311 void Session_ServerThread::ActivateContainer(int argc,
312                                              char ** argv)
313 {
314   try {
315     MESSAGE("Container thread started");
316     
317     // get or create the child POA
318     
319     PortableServer::POA_var factory_poa;
320     try {
321       factory_poa = _root_poa->find_POA("factory_poa",0);
322       // 0 = no activation (already done if exists)
323     }
324     catch (PortableServer::POA::AdapterNonExistent&) {
325       MESSAGE("factory_poa does not exists, create...");
326       // define policy objects     
327       PortableServer::ImplicitActivationPolicy_var implicitActivation =
328         _root_poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);
329       // default = NO_IMPLICIT_ACTIVATION
330       PortableServer::ThreadPolicy_var threadPolicy =
331         _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
332       // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
333       
334       // create policy list
335       CORBA::PolicyList policyList;
336       policyList.length(2);
337       policyList[0] = PortableServer::ImplicitActivationPolicy::
338         _duplicate(implicitActivation);
339       policyList[1] = PortableServer::ThreadPolicy::
340         _duplicate(threadPolicy);
341       
342       PortableServer::POAManager_var nil_mgr
343         = PortableServer::POAManager::_nil();
344       factory_poa = _root_poa->create_POA("factory_poa",
345                                           nil_mgr,
346                                           policyList);
347       //with nil_mgr instead of pman,
348       //a new POA manager is created with the new POA
349       
350       // destroy policy objects
351       implicitActivation->destroy();
352       threadPolicy->destroy();
353       
354       // obtain the factory poa manager
355       PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
356       pmanfac->activate();
357       MESSAGE("pmanfac->activate()");
358     }
359     
360     char *containerName = (char*)"";
361     if (argc >1) {
362       containerName = argv[1];
363     }
364     
365     _container = new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
366   }
367   catch(CORBA::SystemException&) {
368     INFOS("Caught CORBA::SystemException.");
369   }
370   catch(PortableServer::POA::WrongPolicy&) {
371     INFOS("Caught CORBA::WrongPolicyException.");
372   }
373   catch(PortableServer::POA::ServantAlreadyActive&) {
374     INFOS("Caught CORBA::ServantAlreadyActiveException");
375   }
376   catch(CORBA::Exception&) {
377     INFOS("Caught CORBA::Exception.");
378   }
379   catch(...) {
380     INFOS("Caught unknown exception.");
381   }
382 }
383
384 void Session_ServerThread::ActivateSession(int argc,
385                                            char ** argv)
386 {
387   MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
388 }
389
390 /*! 
391   constructor 
392 */
393 Session_SessionThread::Session_SessionThread(int argc,
394                                              char** argv, 
395                                              CORBA::ORB_ptr orb, 
396                                              PortableServer::POA_ptr poa,
397                                              QMutex* GUIMutex,
398                                              QWaitCondition* GUILauncher)
399 : Session_ServerThread(argc, argv, orb, poa),
400   _GUIMutex( GUIMutex ),
401   _GUILauncher( GUILauncher )
402 {
403 }
404
405 /*! 
406   destructor 
407 */
408 Session_SessionThread::~Session_SessionThread()
409 {
410 }
411
412 void Session_SessionThread::ActivateSession(int argc,
413                                             char ** argv)
414 {
415   try {
416     MESSAGE("Session thread started");
417     SALOME_Session_i * mySALOME_Session
418       = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
419     PortableServer::ObjectId_var mySALOME_Sessionid
420       = _root_poa->activate_object(mySALOME_Session);
421     MESSAGE("poa->activate_object(mySALOME_Session)");
422     
423     CORBA::Object_var obj = mySALOME_Session->_this();
424     CORBA::String_var sior(_orb->object_to_string(obj));
425     mySALOME_Session->_remove_ref();
426     
427     mySALOME_Session->NSregister();
428   }
429   catch (CORBA::SystemException&) {
430     INFOS("Caught CORBA::SystemException.");
431   }
432   catch (CORBA::Exception&) {
433     INFOS("Caught CORBA::Exception.");
434   }
435   catch (...) {
436     INFOS("Caught unknown exception.");
437   }
438 }