1 // SALOME Container : implementation of container and engine for Kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : Container_i.cxx
25 // Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
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"
40 #include "utilities.h"
42 bool _Sleeping = false ;
44 // Needed by multi-threaded Python
49 // Containers with name FactoryServer are started via rsh in LifeCycleCORBA
50 // Other Containers are started via start_impl of FactoryServer
52 extern "C" {void ActSigIntHandler() ; }
53 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
55 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb,
56 PortableServer::POA_ptr poa,
58 int argc , char* argv[] ) :
69 int i = strlen( _argv[ 0 ] ) - 1 ;
71 if ( _argv[ 0 ][ i ] == '/' ) {
72 _argv[ 0 ][ i+1 ] = '\0' ;
77 string hostname = GetHostname();
78 MESSAGE(hostname << " " << getpid() << " Engines_Container_i starting argc "
79 << _argc << " Thread " << pthread_self() ) ;
81 while ( _argv[ i ] ) {
82 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
86 MESSAGE("SALOME_Container usage : SALOME_Container ServerName -ORBInitRef NameService=corbaname::hostname:tcpipPortNumber") ;
92 _containerName = "/Containers/";
93 if (strlen(containerName)== 0)
95 _containerName += hostname;
99 _containerName += hostname;
100 _containerName += "/" ;
101 _containerName += containerName;
104 _orb = CORBA::ORB::_duplicate(orb) ;
105 _poa = PortableServer::POA::_duplicate(poa) ;
106 MESSAGE("activate object");
107 _id = _poa->activate_object(this);
109 // _NS = new SALOME_NamingService(_orb);
110 _NS = SINGLETON_<SALOME_NamingService>::Instance() ;
111 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
112 _NS->init_orb( orb ) ;
114 Engines::Container_ptr pCont
115 = Engines::Container::_narrow(_this());
116 SCRUTE(_containerName);
117 _NS->Register(pCont, _containerName.c_str());
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,
127 string hostname = GetHostname();
130 _containerName = "/Containers/";
131 if (strlen(containerName)== 0)
133 _containerName += hostname;
137 _containerName += containerName;
140 _orb = CORBA::ORB::_duplicate(orb) ;
141 _poa = PortableServer::POA::_duplicate(poa) ;
145 Engines_Container_i::~Engines_Container_i()
147 MESSAGE("Container_i::~Container_i()");
150 char* Engines_Container_i::name()
152 return CORBA::string_dup(_containerName.c_str()) ;
155 char* Engines_Container_i::machineName()
157 string s = GetHostname();
158 MESSAGE("Engines_Container_i::machineName " << s);
159 return CORBA::string_dup(s.c_str()) ;
162 void Engines_Container_i::ping()
164 MESSAGE("Engines_Container_i::ping() pid "<< getpid());
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());
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
180 CORBA::Object_var obj = Engines::Container::_nil() ;
183 string cont("/Containers/");
184 cont += machineName() ;
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 ) ;
192 INFOS(machineName() << " start_impl unknown container "
196 catch (ServiceUnreachable&) {
197 INFOS(machineName() << "Caught exception: Naming Service Unreachable");
200 INFOS(machineName() << "Caught unknown exception.");
203 _numInstanceMutex.unlock() ;
204 MESSAGE("start_impl container found without runSession") ;
205 return Engines::Container::_narrow(obj);
208 while ( _argv[ i ] ) {
209 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
212 // string shstr( "rsh -n " ) ;
213 // shstr += machineName() ;
215 // shstr += _argv[ 0 ] ;
216 // string shstr( _argv[ 0 ] ) ;
217 string shstr( "./runSession SALOME_Container " ) ;
218 shstr += ContainerName ;
221 shstr += _argv[ 2 ] ;
223 shstr += _argv[ 3 ] ;
225 shstr += " > /tmp/" ;
226 shstr += ContainerName ;
227 shstr += ".log 2>&1 &" ;
228 MESSAGE("system(" << shstr << ")") ;
229 int status = system( shstr.c_str() ) ;
231 INFOS("Engines_Container_i::start_impl runSession(SALOME_Container) failed (system command status -1)") ;
233 else if (status == 217) {
234 INFOS("Engines_Container_i::start_impl runSession(SALOME_Container) failed (system command status 217)") ;
236 INFOS(machineName() << " Engines_Container_i::start_impl runSession(SALOME_Container) done");
240 string anExe( _argv[ 0 ] ) ;
241 anExe += "runSession" ;
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 ] ) ;
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 ) ;
254 INFOS("Engines_Container_i::start_impl execl failed (system command status -1)") ;
255 perror( "Engines_Container_i::start_impl execl error ") ;
258 INFOS(machineName() << " Engines_Container_i::start_impl execl done");
264 obj = Engines::Container::_nil() ;
266 string cont("/Containers/");
267 cont += machineName() ;
269 cont += ContainerName;
272 while ( nilvar && count >= 0) {
274 obj = _NS->Resolve(cont.c_str());
275 nilvar = CORBA::is_nil( obj ) ;
277 INFOS(count << ". " << machineName()
278 << " start_impl unknown container " << cont.c_str());
282 _numInstanceMutex.unlock() ;
284 MESSAGE("start_impl container found after runSession(SALOME_Container)") ;
286 return Engines::Container::_narrow(obj);
288 catch (ServiceUnreachable&) {
289 INFOS(machineName() << "Caught exception: Naming Service Unreachable");
292 INFOS(machineName() << "Caught unknown exception.");
294 _numInstanceMutex.unlock() ;
295 MESSAGE("start_impl container not found after runSession(SALOME_Container)") ;
296 return Engines::Container::_nil() ;
299 Engines::Component_ptr Engines_Container_i::load_impl( const char* nameToRegister,
300 const char* componentName ) {
302 _numInstanceMutex.lock() ; // lock on the instance number
303 BEGIN_OF( "Container_i::load_impl " << componentName ) ;
306 sprintf( _aNumI , "%d" , _numInstance ) ;
308 string _impl_name = componentName;
309 string _nameToRegister = nameToRegister;
310 string instanceName = _nameToRegister + "_inst_" + _aNumI ;
311 //SCRUTE(instanceName);
313 //string absolute_impl_name = _library_path + "lib" + _impl_name + ".so";
314 string absolute_impl_name( _impl_name ) ;
315 SCRUTE(absolute_impl_name);
317 handle = dlopen( absolute_impl_name.c_str() , RTLD_LAZY ) ;
319 INFOS("Can't load shared library : " << absolute_impl_name);
320 INFOS("error dlopen: " << dlerror());
321 _numInstanceMutex.unlock() ;
322 return Engines::Component::_nil() ;
325 string factory_name = _nameToRegister + string("Engine_factory");
326 // SCRUTE(factory_name) ;
328 typedef PortableServer::ObjectId * (*FACTORY_FUNCTION)
330 PortableServer::POA_ptr,
331 PortableServer::ObjectId *,
334 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
337 if ( (error = dlerror() ) != NULL) {
338 INFOS("Can't resolve symbol: " + factory_name);
340 _numInstanceMutex.unlock() ;
341 return Engines::Component::_nil() ;
344 string component_registerName = _containerName + "/" + _nameToRegister;
345 Engines::Component_var iobject = Engines::Component::_nil() ;
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 ) ;
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" ) ;
362 else { // JR : No ReBind !!!
363 MESSAGE( "Container_i::load_impl " << component_registerName.c_str() << " already bound" ) ;
364 iobject = Engines::Component::_narrow( obj ) ;
368 MESSAGE( "Container_i::load_impl catched" ) ;
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);
378 void Engines_Container_i::remove_impl(Engines::Component_ptr component_i)
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
388 remove_map[instanceName] = handle_map[instanceName] ;
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 ++)
397 MESSAGE("reste " << (*im).first);
401 void Engines_Container_i::finalize_removal()
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 ++)
408 void * handle = (*im).second ;
410 MESSAGE("dlclose " << (*im).first);
413 _numInstanceMutex.unlock() ;
414 MESSAGE("remove_map.clear()");
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 ") ;
426 INFOS(pthread_self() << "SigIntHandler activated") ;
432 void SigIntHandler(int what , siginfo_t * siginfo ,
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) ;
440 INFOS("SigIntHandler END sleeping.")
441 MESSAGE("SigIntHandler END sleeping.") ;
446 if ( siginfo->si_signo == SIGUSR1 ) {
451 INFOS("SigIntHandler BEGIN sleeping.")
452 MESSAGE("SigIntHandler BEGIN sleeping.") ;
458 INFOS("SigIntHandler LEAVE sleeping after " << count << " s.")
459 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;