Salome HOME
PR : merge branch V1_2c dans branche principale pour V1_3_0_b1
[modules/kernel.git] / src / Container / Container_i.cxx
1 //  SALOME Container : implementation of container and engine for Kernel
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   : Container_i.cxx
25 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA 
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include <SALOMEconfig.h>
31 #include CORBA_SERVER_HEADER(SALOME_Component)
32 #include "SALOME_Container_i.hxx"
33 #include "SALOME_NamingService.hxx"
34 #include "Utils_SINGLETON.hxx"
35 #include "OpUtil.hxx"
36 #include <stdio.h>
37 #include <dlfcn.h>
38 #include <unistd.h>
39
40 #include "utilities.h"
41
42 bool _Sleeping = false ;
43
44 // Needed by multi-threaded Python
45 int _ArgC ;
46 char ** _ArgV ;
47
48
49 // Containers with name FactoryServer are started via rsh in LifeCycleCORBA
50 // Other Containers are started via start_impl of FactoryServer
51
52 extern "C" {void ActSigIntHandler() ; }
53 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
54
55 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, 
56                                           PortableServer::POA_ptr poa,
57                                           char *containerName ,
58                                           int argc , char* argv[] ) :
59  _numInstance(0)
60 {
61
62   ActSigIntHandler() ;
63
64   _ArgC = argc ;
65   _ArgV = argv ;
66
67   _argc = argc ;
68   _argv = argv ;
69   int i = strlen( _argv[ 0 ] ) - 1 ;
70   while ( i >= 0 ) {
71     if ( _argv[ 0 ][ i ] == '/' ) {
72       _argv[ 0 ][ i+1 ] = '\0' ;
73       break ;
74     }
75     i -= 1 ;
76   }
77   string hostname = GetHostname();
78   MESSAGE(hostname << " " << getpid() << " Engines_Container_i starting argc "
79           << _argc << " Thread " << pthread_self() ) ;
80   i = 0 ;
81   while ( _argv[ i ] ) {
82     MESSAGE("           argv" << i << " " << _argv[ i ]) ;
83     i++ ;
84   }
85   if ( argc != 4 ) {
86     MESSAGE("SALOME_Container usage : SALOME_Container ServerName -ORBInitRef NameService=corbaname::hostname:tcpipPortNumber") ;
87 //    exit(0) ;
88   }
89
90   SCRUTE(hostname);
91
92   _containerName = "/Containers/";
93   if (strlen(containerName)== 0)
94     {
95       _containerName += hostname;
96     }
97   else
98     {
99       _containerName += hostname;
100       _containerName += "/" ;
101       _containerName += containerName;
102     }
103
104   _orb = CORBA::ORB::_duplicate(orb) ;
105   _poa = PortableServer::POA::_duplicate(poa) ;
106   MESSAGE("activate object");
107   _id = _poa->activate_object(this);
108
109 //   _NS = new SALOME_NamingService(_orb);
110   _NS = SINGLETON_<SALOME_NamingService>::Instance() ;
111   ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
112   _NS->init_orb( orb ) ;
113
114   Engines::Container_ptr pCont 
115     = Engines::Container::_narrow(_this());
116   SCRUTE(_containerName);
117   _NS->Register(pCont, _containerName.c_str()); 
118 }
119
120 // Constructeur pour composant parallele : ne pas faire appel au naming service
121 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, 
122                                           PortableServer::POA_ptr poa,
123                                           char *containerName,
124                                           int flag ) 
125   : _numInstance(0)
126 {
127   string hostname = GetHostname();
128   SCRUTE(hostname);
129
130   _containerName = "/Containers/";
131   if (strlen(containerName)== 0)
132     {
133       _containerName += hostname;
134     }
135   else
136     {
137       _containerName += containerName;
138     }
139
140   _orb = CORBA::ORB::_duplicate(orb) ;
141   _poa = PortableServer::POA::_duplicate(poa) ;
142
143 }
144
145 Engines_Container_i::~Engines_Container_i()
146 {
147   MESSAGE("Container_i::~Container_i()");
148 }
149
150 char* Engines_Container_i::name()
151 {
152    return CORBA::string_dup(_containerName.c_str()) ;
153 }
154
155 char* Engines_Container_i::machineName()
156 {
157   string s = GetHostname();
158   MESSAGE("Engines_Container_i::machineName " << s);
159    return CORBA::string_dup(s.c_str()) ;
160 }
161
162 void Engines_Container_i::ping()
163 {
164   MESSAGE("Engines_Container_i::ping() pid "<< getpid());
165 }
166
167 bool Engines_Container_i::Kill_impl() {
168   MESSAGE("Engines_Container_i::Kill() pid "<< getpid() << " containerName "
169           << _containerName.c_str() << " machineName "
170           << GetHostname().c_str());
171   exit( 0 ) ;
172 }
173
174 Engines::Container_ptr Engines_Container_i::start_impl(
175                                       const char* ContainerName ) {
176   MESSAGE("start_impl argc " << _argc << " ContainerName " << ContainerName
177           << hex << this << dec) ;
178   _numInstanceMutex.lock() ; // lock on the instance number
179
180   CORBA::Object_var obj = Engines::Container::_nil() ;
181   bool nilvar = true ;
182   try {
183     string cont("/Containers/");
184     cont += machineName() ;
185     cont += "/" ;
186     cont += ContainerName;
187     INFOS(machineName() << " start_impl unknown container " << cont.c_str()
188           << " try to Resolve" );
189     obj = _NS->Resolve( cont.c_str() );
190     nilvar = CORBA::is_nil( obj ) ;
191     if ( nilvar ) {
192       INFOS(machineName() << " start_impl unknown container "
193             << ContainerName);
194     }
195   }
196   catch (ServiceUnreachable&) {
197     INFOS(machineName() << "Caught exception: Naming Service Unreachable");
198   }
199   catch (...) {
200     INFOS(machineName() << "Caught unknown exception.");
201   }
202   if ( !nilvar ) {
203     _numInstanceMutex.unlock() ;
204     MESSAGE("start_impl container found without runSession") ;
205     return Engines::Container::_narrow(obj);
206   }
207   int i = 0 ;
208   while ( _argv[ i ] ) {
209     MESSAGE("           argv" << i << " " << _argv[ i ]) ;
210     i++ ;
211   }
212 //  string shstr( "rsh -n " ) ;
213 //  shstr += machineName() ;
214 //  shstr += " " ;
215 //  shstr += _argv[ 0 ] ;
216 //  string shstr( _argv[ 0 ] ) ;
217   string shstr( "./runSession SALOME_Container " ) ;
218   shstr += ContainerName ;
219   if ( _argc == 4 ) {
220     shstr += " " ;
221     shstr += _argv[ 2 ] ;
222     shstr += " " ;
223     shstr += _argv[ 3 ] ;
224   }
225   shstr += " > /tmp/" ;
226   shstr += ContainerName ;
227   shstr += ".log 2>&1 &" ;
228   MESSAGE("system(" << shstr << ")") ;
229   int status = system( shstr.c_str() ) ;
230   if (status == -1) {
231     INFOS("Engines_Container_i::start_impl runSession(SALOME_Container) failed (system command status -1)") ;
232   }
233   else if (status == 217) {
234     INFOS("Engines_Container_i::start_impl runSession(SALOME_Container) failed (system command status 217)") ;
235   }
236   INFOS(machineName() << " Engines_Container_i::start_impl runSession(SALOME_Container) done");
237 #if 0
238   pid_t pid = fork() ;
239   if ( pid == 0 ) {
240     string anExe( _argv[ 0 ] ) ;
241     anExe += "runSession" ;
242     char * args[ 6 ] ;
243     args[ 0 ] = "runSession" ;
244     args[ 1 ] = "SALOME_Container" ;
245     args[ 2 ] = strdup( ContainerName ) ;
246     args[ 3 ] = strdup( _argv[ 2 ] ) ;
247     args[ 4 ] = strdup( _argv[ 3 ] ) ;
248     args[ 5 ] = NULL ;
249     MESSAGE("execl(" << anExe.c_str() << " , " << args[ 0 ] << " , "
250                      << args[ 1 ] << " , " << args[ 2 ] << " , " << args[ 3 ]
251                      << " , " << args[ 4 ] << ")") ;
252     int status = execv( anExe.c_str() , args ) ;
253     if (status == -1) {
254       INFOS("Engines_Container_i::start_impl execl failed (system command status -1)") ;
255       perror( "Engines_Container_i::start_impl execl error ") ;
256     }
257     else {
258       INFOS(machineName() << " Engines_Container_i::start_impl execl done");
259     }
260     exit(0) ;
261   }
262 #endif
263
264   obj = Engines::Container::_nil() ;
265   try {
266     string cont("/Containers/");
267     cont += machineName() ;
268     cont += "/" ;
269     cont += ContainerName;
270     nilvar = true ;
271     int count = 20 ;
272     while ( nilvar && count >= 0) {
273       sleep( 1 ) ;
274       obj = _NS->Resolve(cont.c_str());
275       nilvar = CORBA::is_nil( obj ) ;
276       if ( nilvar ) {
277         INFOS(count << ". " << machineName()
278               << " start_impl unknown container " << cont.c_str());
279         count -= 1 ;
280       }
281     }
282     _numInstanceMutex.unlock() ;
283     if ( !nilvar ) {
284       MESSAGE("start_impl container found after runSession(SALOME_Container)") ;
285     }
286     return Engines::Container::_narrow(obj);
287   }
288   catch (ServiceUnreachable&) {
289     INFOS(machineName() << "Caught exception: Naming Service Unreachable");
290   }
291   catch (...) {
292     INFOS(machineName() << "Caught unknown exception.");
293   }
294   _numInstanceMutex.unlock() ;
295   MESSAGE("start_impl container not found after runSession(SALOME_Container)") ;
296   return Engines::Container::_nil() ;
297 }
298
299 Engines::Component_ptr Engines_Container_i::load_impl( const char* nameToRegister,
300                                                        const char* componentName ) {
301
302   _numInstanceMutex.lock() ; // lock on the instance number
303   BEGIN_OF( "Container_i::load_impl " << componentName ) ;
304   _numInstance++ ;
305   char _aNumI[12];
306   sprintf( _aNumI , "%d" , _numInstance ) ;
307
308   string _impl_name = componentName;
309   string _nameToRegister = nameToRegister;
310   string instanceName = _nameToRegister + "_inst_" + _aNumI ;
311   //SCRUTE(instanceName);
312
313   //string absolute_impl_name = _library_path + "lib" + _impl_name + ".so";
314   string absolute_impl_name( _impl_name ) ;
315   SCRUTE(absolute_impl_name);
316   void* handle;
317   handle = dlopen( absolute_impl_name.c_str() , RTLD_LAZY ) ;
318   if ( !handle ) {
319     INFOS("Can't load shared library : " << absolute_impl_name);
320     INFOS("error dlopen: " << dlerror());
321     _numInstanceMutex.unlock() ;
322     return Engines::Component::_nil() ;
323   }
324   
325   string factory_name = _nameToRegister + string("Engine_factory");
326   //  SCRUTE(factory_name) ;
327
328   typedef  PortableServer::ObjectId * (*FACTORY_FUNCTION)
329                             (CORBA::ORB_ptr,
330                              PortableServer::POA_ptr, 
331                              PortableServer::ObjectId *, 
332                              const char *, 
333                              const char *) ; 
334   FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
335
336   char *error ;
337   if ( (error = dlerror() ) != NULL) {
338       INFOS("Can't resolve symbol: " + factory_name);
339       SCRUTE(error);
340       _numInstanceMutex.unlock() ;
341       return Engines::Component::_nil() ;
342     }
343
344   string component_registerName = _containerName + "/" + _nameToRegister;
345   Engines::Component_var iobject = Engines::Component::_nil() ;
346   try {
347     CORBA::Object_var obj = _NS->Resolve( component_registerName.c_str() ) ;
348     if ( CORBA::is_nil( obj ) ) {
349 // Instanciate required CORBA object
350       PortableServer::ObjectId * id ;
351       id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str() ,
352                                  _nameToRegister.c_str() ) ;
353   // get reference from id
354       obj = _poa->id_to_reference(*id);
355       iobject = Engines::Component::_narrow( obj ) ;
356
357 //  _numInstanceMutex.lock() ; // lock on the add on handle_map (necessary ?)
358   // register the engine under the name containerName.dir/nameToRegister.object
359       _NS->Register( iobject , component_registerName.c_str() ) ;
360       MESSAGE( "Container_i::load_impl " << component_registerName.c_str() << " bound" ) ;
361     }
362     else { // JR : No ReBind !!!
363       MESSAGE( "Container_i::load_impl " << component_registerName.c_str() << " already bound" ) ;
364       iobject = Engines::Component::_narrow( obj ) ;
365     }
366   }
367   catch (...) {
368     MESSAGE( "Container_i::load_impl catched" ) ;
369   }
370
371 //Jr  _numInstanceMutex.lock() ; // lock on the add on handle_map (necessary ?)
372   handle_map[instanceName] = handle;
373   END_OF("Container_i::load_impl");
374   _numInstanceMutex.unlock() ;
375   return Engines::Component::_duplicate(iobject);
376 }
377
378 void Engines_Container_i::remove_impl(Engines::Component_ptr component_i)
379 {
380   ASSERT(! CORBA::is_nil(component_i));
381   string instanceName = component_i->instanceName() ;
382   MESSAGE("unload component " << instanceName);
383   component_i->destroy() ;
384   MESSAGE("test key handle_map");
385   _numInstanceMutex.lock() ; // lock on the remove on handle_map
386   if (handle_map[instanceName]) // if key does not exist, created & initialized null
387     {
388       remove_map[instanceName] = handle_map[instanceName] ;
389     }
390   else MESSAGE("pas d'entree handle_map");
391   handle_map.erase(instanceName) ;   
392   _numInstanceMutex.unlock() ;
393   MESSAGE("contenu handle_map");
394   map<string, void *>::iterator im ;
395   for (im = handle_map.begin() ; im != handle_map.end() ; im ++)
396     {
397       MESSAGE("reste " << (*im).first);
398     }
399 }
400
401 void Engines_Container_i::finalize_removal()
402 {
403   MESSAGE("finalize unload : dlclose");
404   map<string, void *>::iterator im ;
405   _numInstanceMutex.lock() ; // lock on the explore remove_map & dlclose
406   for (im = remove_map.begin() ; im != remove_map.end() ; im ++)
407     {
408       void * handle = (*im).second ;
409       dlclose(handle) ;
410       MESSAGE("dlclose " << (*im).first);
411     }
412   remove_map.clear() ;  
413   _numInstanceMutex.unlock() ;
414   MESSAGE("remove_map.clear()");
415 }
416
417 void ActSigIntHandler() {
418   struct sigaction SigIntAct ;
419   SigIntAct.sa_sigaction = &SigIntHandler ;
420   SigIntAct.sa_flags = SA_SIGINFO ;
421   if ( sigaction( SIGINT | SIGUSR1 , &SigIntAct, NULL ) ) {
422     perror("SALOME_Container main ") ;
423     exit(0) ;
424   }
425   else {
426     INFOS(pthread_self() << "SigIntHandler activated") ;
427   }
428 }
429
430 void SetCpuUsed() ;
431
432 void SigIntHandler(int what , siginfo_t * siginfo ,
433                                         void * toto ) {
434   MESSAGE(pthread_self() << "SigIntHandler what     " << what << endl
435           << "              si_signo " << siginfo->si_signo << endl
436           << "              si_code  " << siginfo->si_code << endl
437           << "              si_pid   " << siginfo->si_pid) ;
438   if ( _Sleeping ) {
439     _Sleeping = false ;
440     INFOS("SigIntHandler END sleeping.")
441     MESSAGE("SigIntHandler END sleeping.") ;
442     return ;
443   }
444   else {
445     ActSigIntHandler() ;
446     if ( siginfo->si_signo == SIGUSR1 ) {
447       SetCpuUsed() ;
448     }
449     else {
450       _Sleeping = true ;
451       INFOS("SigIntHandler BEGIN sleeping.")
452       MESSAGE("SigIntHandler BEGIN sleeping.") ;
453       int count = 0 ;
454       while( _Sleeping ) {
455         sleep( 1 ) ;
456         count += 1 ;
457       }
458       INFOS("SigIntHandler LEAVE sleeping after " << count << " s.")
459       MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
460     }
461     return ;
462   }
463 }