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[] ) :
61 _pid = (long)getpid();
70 int i = strlen( _argv[ 0 ] ) - 1 ;
72 if ( _argv[ 0 ][ i ] == '/' ) {
73 _argv[ 0 ][ i+1 ] = '\0' ;
78 string hostname = GetHostname();
79 MESSAGE(hostname << " " << getpid() << " Engines_Container_i starting argc "
80 << _argc << " Thread " << pthread_self() ) ;
82 while ( _argv[ i ] ) {
83 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
87 MESSAGE("SALOME_Container usage : SALOME_Container ServerName -ORBInitRef NameService=corbaname::hostname:tcpipPortNumber") ;
93 _containerName = "/Containers/";
94 if (strlen(containerName)== 0)
96 _containerName += hostname;
100 _containerName += hostname;
101 _containerName += "/" ;
102 _containerName += containerName;
105 _orb = CORBA::ORB::_duplicate(orb) ;
106 _poa = PortableServer::POA::_duplicate(poa) ;
107 MESSAGE("activate object");
108 _id = _poa->activate_object(this);
110 // _NS = new SALOME_NamingService(_orb);
111 _NS = SINGLETON_<SALOME_NamingService>::Instance() ;
112 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
113 _NS->init_orb( orb ) ;
115 Engines::Container_ptr pCont
116 = Engines::Container::_narrow(_this());
117 SCRUTE(_containerName);
118 _NS->Register(pCont, _containerName.c_str());
121 // Constructeur pour composant parallele : ne pas faire appel au naming service
122 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb,
123 PortableServer::POA_ptr poa,
128 _pid = (long)getpid();
129 string hostname = GetHostname();
132 _containerName = "/Containers/";
133 if (strlen(containerName)== 0)
135 _containerName += hostname;
139 _containerName += containerName;
142 _orb = CORBA::ORB::_duplicate(orb) ;
143 _poa = PortableServer::POA::_duplicate(poa) ;
147 Engines_Container_i::~Engines_Container_i()
149 MESSAGE("Container_i::~Container_i()");
152 char* Engines_Container_i::name()
154 return CORBA::string_dup(_containerName.c_str()) ;
157 char* Engines_Container_i::machineName()
159 string s = GetHostname();
160 MESSAGE("Engines_Container_i::machineName " << s);
161 return CORBA::string_dup(s.c_str()) ;
164 void Engines_Container_i::ping()
166 MESSAGE("Engines_Container_i::ping() pid "<< getpid());
169 bool Engines_Container_i::Kill_impl() {
170 MESSAGE("Engines_Container_i::Kill() pid "<< getpid() << " containerName "
171 << _containerName.c_str() << " machineName "
172 << GetHostname().c_str());
176 Engines::Container_ptr Engines_Container_i::start_impl(
177 const char* ContainerName ) {
178 MESSAGE("start_impl argc " << _argc << " ContainerName " << ContainerName
179 << hex << this << dec) ;
180 _numInstanceMutex.lock() ; // lock on the instance number
182 CORBA::Object_var obj = Engines::Container::_nil() ;
185 string cont("/Containers/");
186 cont += machineName() ;
188 cont += ContainerName;
189 INFOS(machineName() << " start_impl unknown container " << cont.c_str()
190 << " try to Resolve" );
191 obj = _NS->Resolve( cont.c_str() );
192 nilvar = CORBA::is_nil( obj ) ;
194 INFOS(machineName() << " start_impl unknown container "
198 catch (ServiceUnreachable&) {
199 INFOS(machineName() << "Caught exception: Naming Service Unreachable");
202 INFOS(machineName() << "Caught unknown exception.");
205 _numInstanceMutex.unlock() ;
206 MESSAGE("start_impl container found without runSession") ;
207 return Engines::Container::_narrow(obj);
210 while ( _argv[ i ] ) {
211 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
214 // string shstr( "rsh -n " ) ;
215 // shstr += machineName() ;
217 // shstr += _argv[ 0 ] ;
218 // string shstr( _argv[ 0 ] ) ;
219 string shstr( "./runSession SALOME_Container " ) ;
220 shstr += ContainerName ;
223 shstr += _argv[ 2 ] ;
225 shstr += _argv[ 3 ] ;
227 shstr += " > /tmp/" ;
228 shstr += ContainerName ;
229 shstr += ".log 2>&1 &" ;
230 MESSAGE("system(" << shstr << ")") ;
231 int status = system( shstr.c_str() ) ;
233 INFOS("Engines_Container_i::start_impl runSession(SALOME_Container) failed (system command status -1)") ;
235 else if (status == 217) {
236 INFOS("Engines_Container_i::start_impl runSession(SALOME_Container) failed (system command status 217)") ;
238 INFOS(machineName() << " Engines_Container_i::start_impl runSession(SALOME_Container) done");
242 string anExe( _argv[ 0 ] ) ;
243 anExe += "runSession" ;
245 args[ 0 ] = "runSession" ;
246 args[ 1 ] = "SALOME_Container" ;
247 args[ 2 ] = strdup( ContainerName ) ;
248 args[ 3 ] = strdup( _argv[ 2 ] ) ;
249 args[ 4 ] = strdup( _argv[ 3 ] ) ;
251 MESSAGE("execl(" << anExe.c_str() << " , " << args[ 0 ] << " , "
252 << args[ 1 ] << " , " << args[ 2 ] << " , " << args[ 3 ]
253 << " , " << args[ 4 ] << ")") ;
254 int status = execv( anExe.c_str() , args ) ;
256 INFOS("Engines_Container_i::start_impl execl failed (system command status -1)") ;
257 perror( "Engines_Container_i::start_impl execl error ") ;
260 INFOS(machineName() << " Engines_Container_i::start_impl execl done");
266 obj = Engines::Container::_nil() ;
268 string cont("/Containers/");
269 cont += machineName() ;
271 cont += ContainerName;
274 while ( nilvar && count >= 0) {
276 obj = _NS->Resolve(cont.c_str());
277 nilvar = CORBA::is_nil( obj ) ;
279 INFOS(count << ". " << machineName()
280 << " start_impl unknown container " << cont.c_str());
284 _numInstanceMutex.unlock() ;
286 MESSAGE("start_impl container found after runSession(SALOME_Container)") ;
288 return Engines::Container::_narrow(obj);
290 catch (ServiceUnreachable&) {
291 INFOS(machineName() << "Caught exception: Naming Service Unreachable");
294 INFOS(machineName() << "Caught unknown exception.");
296 _numInstanceMutex.unlock() ;
297 MESSAGE("start_impl container not found after runSession(SALOME_Container)") ;
298 return Engines::Container::_nil() ;
301 Engines::Component_ptr Engines_Container_i::load_impl( const char* nameToRegister,
302 const char* componentName ) {
304 _numInstanceMutex.lock() ; // lock on the instance number
305 BEGIN_OF( "Container_i::load_impl " << componentName ) ;
308 sprintf( _aNumI , "%d" , _numInstance ) ;
310 string _impl_name = componentName;
311 string _nameToRegister = nameToRegister;
312 string instanceName = _nameToRegister + "_inst_" + _aNumI ;
313 //SCRUTE(instanceName);
315 //string absolute_impl_name = _library_path + "lib" + _impl_name + ".so";
316 string absolute_impl_name( _impl_name ) ;
317 SCRUTE(absolute_impl_name);
319 handle = dlopen( absolute_impl_name.c_str() , RTLD_LAZY ) ;
321 INFOS("Can't load shared library : " << absolute_impl_name);
322 INFOS("error dlopen: " << dlerror());
323 _numInstanceMutex.unlock() ;
324 return Engines::Component::_nil() ;
327 string factory_name = _nameToRegister + string("Engine_factory");
328 // SCRUTE(factory_name) ;
330 typedef PortableServer::ObjectId * (*FACTORY_FUNCTION)
332 PortableServer::POA_ptr,
333 PortableServer::ObjectId *,
336 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
339 if ( (error = dlerror() ) != NULL) {
340 INFOS("Can't resolve symbol: " + factory_name);
342 _numInstanceMutex.unlock() ;
343 return Engines::Component::_nil() ;
346 string component_registerName = _containerName + "/" + _nameToRegister;
347 Engines::Component_var iobject = Engines::Component::_nil() ;
349 CORBA::Object_var obj = _NS->Resolve( component_registerName.c_str() ) ;
350 if ( CORBA::is_nil( obj ) ) {
351 // Instanciate required CORBA object
352 PortableServer::ObjectId * id ;
353 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str() ,
354 _nameToRegister.c_str() ) ;
355 // get reference from id
356 obj = _poa->id_to_reference(*id);
357 iobject = Engines::Component::_narrow( obj ) ;
359 // _numInstanceMutex.lock() ; // lock on the add on handle_map (necessary ?)
360 // register the engine under the name containerName.dir/nameToRegister.object
361 _NS->Register( iobject , component_registerName.c_str() ) ;
362 MESSAGE( "Container_i::load_impl " << component_registerName.c_str() << " bound" ) ;
364 else { // JR : No ReBind !!!
365 MESSAGE( "Container_i::load_impl " << component_registerName.c_str() << " already bound" ) ;
366 iobject = Engines::Component::_narrow( obj ) ;
370 MESSAGE( "Container_i::load_impl catched" ) ;
373 //Jr _numInstanceMutex.lock() ; // lock on the add on handle_map (necessary ?)
374 handle_map[instanceName] = handle;
375 END_OF("Container_i::load_impl");
376 _numInstanceMutex.unlock() ;
377 return Engines::Component::_duplicate(iobject);
380 void Engines_Container_i::remove_impl(Engines::Component_ptr component_i)
382 ASSERT(! CORBA::is_nil(component_i));
383 string instanceName = component_i->instanceName() ;
384 MESSAGE("unload component " << instanceName);
385 component_i->destroy() ;
386 MESSAGE("test key handle_map");
387 _numInstanceMutex.lock() ; // lock on the remove on handle_map
388 if (handle_map[instanceName]) // if key does not exist, created & initialized null
390 remove_map[instanceName] = handle_map[instanceName] ;
392 else MESSAGE("pas d'entree handle_map");
393 handle_map.erase(instanceName) ;
394 _numInstanceMutex.unlock() ;
395 MESSAGE("contenu handle_map");
396 map<string, void *>::iterator im ;
397 for (im = handle_map.begin() ; im != handle_map.end() ; im ++)
399 MESSAGE("reste " << (*im).first);
403 void Engines_Container_i::finalize_removal()
405 MESSAGE("finalize unload : dlclose");
406 map<string, void *>::iterator im ;
407 _numInstanceMutex.lock() ; // lock on the explore remove_map & dlclose
408 for (im = remove_map.begin() ; im != remove_map.end() ; im ++)
410 void * handle = (*im).second ;
412 MESSAGE("dlclose " << (*im).first);
415 _numInstanceMutex.unlock() ;
416 MESSAGE("remove_map.clear()");
419 void ActSigIntHandler() {
420 struct sigaction SigIntAct ;
421 SigIntAct.sa_sigaction = &SigIntHandler ;
422 SigIntAct.sa_flags = SA_SIGINFO ;
423 if ( sigaction( SIGINT | SIGUSR1 , &SigIntAct, NULL ) ) {
424 perror("SALOME_Container main ") ;
428 INFOS(pthread_self() << "SigIntHandler activated") ;
434 void SigIntHandler(int what , siginfo_t * siginfo ,
436 MESSAGE(pthread_self() << "SigIntHandler what " << what << endl
437 << " si_signo " << siginfo->si_signo << endl
438 << " si_code " << siginfo->si_code << endl
439 << " si_pid " << siginfo->si_pid) ;
442 INFOS("SigIntHandler END sleeping.")
443 MESSAGE("SigIntHandler END sleeping.") ;
448 if ( siginfo->si_signo == SIGUSR1 ) {
453 INFOS("SigIntHandler BEGIN sleeping.")
454 MESSAGE("SigIntHandler BEGIN sleeping.") ;
460 INFOS("SigIntHandler LEAVE sleeping after " << count << " s.")
461 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
467 // Get the PID of the Container
469 long Engines_Container_i::getPID() {
473 // Get the hostName of the Container
475 char* Engines_Container_i::getHostName() {
476 return((char*)(GetHostname().c_str()));