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
29 #define private public
30 #include <SALOMEconfig.h>
31 #include CORBA_SERVER_HEADER(SALOME_Component)
32 #include "SALOME_Container_i.hxx"
33 #include "SALOME_Component_i.hxx"
34 #include "SALOME_NamingService.hxx"
41 #include "utilities.h"
44 bool _Sleeping = false ;
46 // Needed by multi-threaded Python
51 // Containers with name FactoryServer are started via rsh in LifeCycleCORBA
52 // Other Containers are started via start_impl of FactoryServer
54 extern "C" {void ActSigIntHandler() ; }
55 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
57 const char *Engines_Container_i::_defaultContainerName="FactoryServer";
59 //=============================================================================
61 * Default constructor, not for use
63 //=============================================================================
65 Engines_Container_i::Engines_Container_i () :
70 //=============================================================================
74 //=============================================================================
76 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb,
77 PortableServer::POA_ptr poa,
79 int argc , char* argv[],
81 bool isServantAloneInProcess
83 _numInstance(0),_isServantAloneInProcess(isServantAloneInProcess)
85 _pid = (long)getpid();
95 int i = strlen( _argv[ 0 ] ) - 1 ;
98 if ( _argv[ 0 ][ i ] == '/' )
100 _argv[ 0 ][ i+1 ] = '\0' ;
105 string hostname = GetHostname();
106 MESSAGE(hostname << " " << getpid() << " Engines_Container_i starting argc "
107 << _argc << " Thread " << pthread_self() ) ;
111 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
116 MESSAGE("SALOME_Container usage : SALOME_Container ServerName " <<
117 "-ORBInitRef NameService=corbaname::hostname:tcpipPortNumber") ;
121 _containerName = BuildContainerNameForNS(containerName,hostname.c_str());
123 _orb = CORBA::ORB::_duplicate(orb) ;
124 _poa = PortableServer::POA::_duplicate(poa) ;
126 // Pour les containers paralleles: il ne faut pas enregistrer et activer
127 // le container generique, mais le container specialise
131 _id = _poa->activate_object(this);
132 _NS = new SALOME_NamingService();
133 _NS->init_orb( CORBA::ORB::_duplicate(_orb) ) ;
134 CORBA::Object_var obj=_poa->id_to_reference(*_id);
135 Engines::Container_var pCont
136 = Engines::Container::_narrow(obj);
137 SCRUTE(_containerName);
138 _NS->Register(pCont, _containerName.c_str());
142 //=============================================================================
146 //=============================================================================
148 Engines_Container_i::~Engines_Container_i()
150 MESSAGE("Container_i::~Container_i()");
154 //=============================================================================
156 * CORBA attribute: Container name (see constructor)
158 //=============================================================================
160 char* Engines_Container_i::name()
162 return CORBA::string_dup(_containerName.c_str()) ;
165 //=============================================================================
167 * CORBA method: Get the hostName of the Container (without domain extensions)
169 //=============================================================================
171 char* Engines_Container_i::getHostName()
173 string s = GetHostname();
174 MESSAGE("Engines_Container_i::getHostName " << s);
175 return CORBA::string_dup(s.c_str()) ;
178 //=============================================================================
180 * CORBA method: Get the PID (process identification) of the Container
182 //=============================================================================
184 CORBA::Long Engines_Container_i::getPID()
186 return (CORBA::Long)getpid();
189 //=============================================================================
191 * CORBA method: check if servant is still alive
193 //=============================================================================
195 void Engines_Container_i::ping()
197 MESSAGE("Engines_Container_i::ping() pid "<< getpid());
200 //=============================================================================
202 * CORBA method, oneway: Server shutdown.
203 * - Container name removed from naming service,
204 * - servant deactivation,
205 * - orb shutdown if no other servants in the process
207 //=============================================================================
209 void Engines_Container_i::Shutdown()
211 MESSAGE("Engines_Container_i::Shutdown()");
212 _NS->Destroy_Name(_containerName.c_str());
214 //_poa->deactivate_object(*_id);
215 if(_isServantAloneInProcess)
220 //=============================================================================
222 * CORBA method: load a new component class (dynamic library)
223 * \param componentLibraryName like "libCOMPONENTEngine.so"
224 * \return true if dlopen successfull or already done, false otherwise
226 //=============================================================================
229 Engines_Container_i::load_component_Library(const char* componentLibraryName)
231 string impl_name = componentLibraryName;
234 if (_library_map[impl_name])
236 MESSAGE("Library " << impl_name << " already loaded");
240 handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
243 INFOS("Can't load shared library : " << impl_name);
244 INFOS("error dlopen: " << dlerror());
249 _library_map[impl_name] = handle;
254 //=============================================================================
256 * CORBA method: Creates a new servant instance of a component.
257 * The servant registers itself to naming service and Registry.
258 * \param genericRegisterName Name of the component instance to register
259 * in Registry & Name Service (without _inst_n suffix)
260 * \param componentName Name of the constructed library of the component
261 * \param studyId 0 for multiStudy instance,
262 * study Id (>0) otherwise
263 * \return a loaded component
265 //=============================================================================
267 Engines::Component_ptr
268 Engines_Container_i::create_component_instance(const char*genericRegisterName,
269 const char*componentLibraryName,
274 INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
275 return Engines::Component::_nil() ;
278 string impl_name = componentLibraryName;
279 void* handle = _library_map[impl_name];
282 INFOS("shared library " << impl_name <<"must be loaded before instance");
283 return Engines::Component::_nil() ;
287 Engines::Component_var iobject = Engines::Component::_nil() ;
288 iobject = createInstance(genericRegisterName,
291 return iobject._retn();
295 //=============================================================================
297 * CORBA method: Finds a servant instance of a component
298 * \param registeredName Name of the component in Registry or Name Service,
299 * without instance suffix number
300 * \param studyId 0 if instance is not associated to a study,
301 * >0 otherwise (== study id)
302 * \return the first instance found with same studyId
304 //=============================================================================
306 Engines::Component_ptr
307 Engines_Container_i::find_component_instance( const char* registeredName,
313 //=============================================================================
315 * CORBA method: find or create an instance of the component (servant),
316 * load a new component class (dynamic library) if required,
317 * ---- FOR COMPATIBILITY WITH 2.2 ----
318 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
319 * The servant registers itself to naming service and Registry.
320 * \param genericRegisterName Name of the component to register
321 * in Registry & Name Service
322 * \param componentName Name of the constructed library of the component
323 * \return a loaded component
325 //=============================================================================
327 Engines::Component_ptr
328 Engines_Container_i::load_impl( const char* genericRegisterName,
329 const char* componentName )
331 Engines::Component_var iobject = Engines::Component::_nil() ;
332 if (load_component_Library(componentName))
333 iobject = find_or_create_instance(genericRegisterName, componentName);
334 return iobject._retn();
338 //=============================================================================
340 * CORBA method: Stops the component servant, and deletes all related objects
341 * \param component_i Component to be removed
343 //=============================================================================
345 void Engines_Container_i::remove_impl(Engines::Component_ptr component_i)
347 ASSERT(! CORBA::is_nil(component_i));
348 string instanceName = component_i->instanceName() ;
349 MESSAGE("unload component " << instanceName);
350 _listInstances_map.erase(instanceName);
351 component_i->destroy() ;
354 //=============================================================================
356 * CORBA method: Discharges all components from the container.
358 //=============================================================================
360 void Engines_Container_i::finalize_removal()
362 MESSAGE("finalize unload : dlclose");
363 map<string, void *>::iterator im ;
364 _numInstanceMutex.lock() ; // lock on the explore _remove_map & dlclose
365 for (im = _remove_map.begin() ; im != _remove_map.end() ; im ++)
367 void * handle = (*im).second ;
369 MESSAGE("dlclose " << (*im).first);
371 _remove_map.clear() ;
372 _numInstanceMutex.unlock() ;
373 MESSAGE("_remove_map.clear()");
376 //=============================================================================
378 * CORBA method: Kill the container process with exit(0).
379 * To remove : never returns !
381 //=============================================================================
383 bool Engines_Container_i::Kill_impl()
385 MESSAGE("Engines_Container_i::Kill() pid "<< getpid() << " containerName "
386 << _containerName.c_str() << " machineName "
387 << GetHostname().c_str());
388 INFOS("===============================================================");
389 INFOS("= REMOVE calls to Kill_impl in C++ container =");
390 INFOS("===============================================================");
395 //=============================================================================
397 * C++ method: Finds an already existing servant instance of a component, or
398 * create an instance.
399 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
400 * \param genericRegisterName Name of the component instance to register
401 * in Registry & Name Service,
402 * (without _inst_n suffix, like "COMPONENT")
403 * \param componentLibraryName like "libCOMPONENTEngine.so"
404 * \return a loaded component
406 * example with names:
407 * aGenRegisterName = COMPONENT (= first argument)
408 * impl_name = libCOMPONENTEngine.so (= second argument)
409 * _containerName = /Containers/cli76ce/FactoryServer
410 * factoryName = COMPONENTEngine_factory
411 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
413 * instanceName = COMPONENT_inst_1
414 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
416 //=============================================================================
418 Engines::Component_ptr
419 Engines_Container_i::find_or_create_instance(string genericRegisterName,
420 string componentLibraryName)
422 string aGenRegisterName = genericRegisterName;
423 string impl_name = componentLibraryName;
424 void* handle = _library_map[impl_name];
427 INFOS("shared library " << impl_name <<"must be loaded before instance");
428 return Engines::Component::_nil() ;
432 // --- find a registered instance in naming service, or create
434 string component_registerBase =
435 _containerName + "/" + aGenRegisterName;
436 Engines::Component_var iobject = Engines::Component::_nil() ;
439 CORBA::Object_var obj =
440 _NS->ResolveFirst( component_registerBase.c_str());
441 if ( CORBA::is_nil( obj ) )
443 iobject = createInstance(genericRegisterName,
445 0); // force multiStudy instance here !
449 iobject = Engines::Component::_narrow( obj ) ;
450 Engines_Component_i *servant =
451 dynamic_cast<Engines_Component_i*>
452 (_poa->reference_to_servant(iobject));
454 int studyId = servant->getStudyId();
455 ASSERT (studyId >= 0);
456 if (studyId == 0) // multiStudy instance, OK
459 MESSAGE(component_registerBase.c_str()<<" already bound");
461 else // monoStudy instance: NOK
463 iobject = Engines::Component::_nil();
464 INFOS("load_impl & find_component_instance methods "
465 << "NOT SUITABLE for mono study components");
471 INFOS( "Container_i::load_impl catched" ) ;
473 return iobject._retn();
477 //=============================================================================
479 * C++ method: create a servant instance of a component.
480 * \param genericRegisterName Name of the component instance to register
481 * in Registry & Name Service,
482 * (without _inst_n suffix, like "COMPONENT")
483 * \param handle loaded library handle
484 * \param studyId 0 for multiStudy instance,
485 * study Id (>0) otherwise
486 * \return a loaded component
488 * example with names:
489 * aGenRegisterName = COMPONENT (= first argument)
490 * _containerName = /Containers/cli76ce/FactoryServer
491 * factoryName = COMPONENTEngine_factory
492 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
493 * instanceName = COMPONENT_inst_1
494 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
496 //=============================================================================
498 Engines::Component_ptr
499 Engines_Container_i::createInstance(string genericRegisterName,
503 // --- find the factory
505 string aGenRegisterName = genericRegisterName;
506 string factory_name = aGenRegisterName + string("Engine_factory");
507 SCRUTE(factory_name) ;
509 typedef PortableServer::ObjectId * (*FACTORY_FUNCTION)
511 PortableServer::POA_ptr,
512 PortableServer::ObjectId *,
516 FACTORY_FUNCTION Component_factory
517 = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
520 if ( (error = dlerror() ) != NULL)
522 INFOS("Can't resolve symbol: " + factory_name);
524 return Engines::Component::_nil() ;
527 // --- create instance
529 Engines::Component_var iobject = Engines::Component::_nil() ;
533 _numInstanceMutex.lock() ; // lock on the instance number
535 int numInstance = _numInstance ;
536 _numInstanceMutex.unlock() ;
539 sprintf( aNumI , "%d" , numInstance ) ;
540 string instanceName = aGenRegisterName + "_inst_" + aNumI ;
541 string component_registerName =
542 _containerName + "/" + instanceName;
544 // --- Instanciate required CORBA object
546 PortableServer::ObjectId * id ;
547 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(),
548 aGenRegisterName.c_str() ) ;
550 // --- get reference & servant from id
552 CORBA::Object_var obj = _poa->id_to_reference(*id);
553 iobject = Engines::Component::_narrow( obj ) ;
555 Engines_Component_i *servant =
556 dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
558 SCRUTE(servant->pd_refCount);
559 servant->_remove_ref(); // compensate previous id_to_reference
560 SCRUTE(servant->pd_refCount);
561 _listInstances_map[instanceName] = iobject;
562 SCRUTE(servant->pd_refCount);
563 ASSERT(servant->setStudyId(studyId));
565 // --- register the engine under the name
566 // containerName(.dir)/instanceName(.object)
568 _NS->Register( iobject , component_registerName.c_str() ) ;
569 MESSAGE( component_registerName.c_str() << " bound" ) ;
573 INFOS( "Container_i::createInstance exception catched" ) ;
575 return iobject._retn();
579 //=============================================================================
581 * Retrieves only with container naming convention if it is a python container
583 //=============================================================================
585 bool Engines_Container_i::isPythonContainer(const char* ContainerName)
588 int len=strlen(ContainerName);
590 if(strcmp(ContainerName+len-2,"Py")==0)
595 //=============================================================================
597 * Returns string = container path + name, to use in Naming service
599 //=============================================================================
601 string Engines_Container_i::BuildContainerNameForNS(const char *ContainerName,
602 const char *hostname)
604 string ret="/Containers/";
607 if (strlen(ContainerName)== 0)
608 ret+=_defaultContainerName;
610 ret += ContainerName;
614 //=============================================================================
618 //=============================================================================
620 void ActSigIntHandler()
622 struct sigaction SigIntAct ;
623 SigIntAct.sa_sigaction = &SigIntHandler ;
624 SigIntAct.sa_flags = SA_SIGINFO ;
625 // DEBUG 03.02.2005 : the first parameter of sigaction is not a mask of signals (SIGINT | SIGUSR1) :
626 // it must be only one signal ===> one call for SIGINT and an other one for SIGUSR1
627 if ( sigaction( SIGINT , &SigIntAct, NULL ) ) {
628 perror("SALOME_Container main ") ;
631 if ( sigaction( SIGUSR1 , &SigIntAct, NULL ) ) {
632 perror("SALOME_Container main ") ;
635 INFOS(pthread_self() << "SigIntHandler activated") ;
640 void SigIntHandler(int what , siginfo_t * siginfo ,
643 MESSAGE(pthread_self() << "SigIntHandler what " << what << endl
644 << " si_signo " << siginfo->si_signo << endl
645 << " si_code " << siginfo->si_code << endl
646 << " si_pid " << siginfo->si_pid) ;
649 MESSAGE("SigIntHandler END sleeping.") ;
654 if ( siginfo->si_signo == SIGUSR1 ) {
659 MESSAGE("SigIntHandler BEGIN sleeping.") ;
665 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
671 //=============================================================================
673 * CORBA method: Create one instance of componentName component
674 * and register it as genericRegisterName in naming service
676 //=============================================================================
678 // Engines::Component_ptr Engines_Container_i::instance( const char* genericRegisterName,
679 // const char* componentName )
681 // _numInstanceMutex.lock() ; // lock on the instance number
682 // BEGIN_OF( "Container_i::instance " << componentName ) ;
684 // string _genericRegisterName = genericRegisterName;
685 // string component_registerName = _containerName + "/" + _genericRegisterName;
687 // Engines::Component_var iobject = Engines::Component::_nil() ;
691 // CORBA::Object_var obj = _NS->Resolve( component_registerName.c_str() ) ;
692 // if (! CORBA::is_nil( obj ) )
694 // MESSAGE( "Container_i::instance " << component_registerName.c_str() << " already registered" ) ;
695 // iobject = Engines::Component::_narrow( obj ) ;
699 // string _compo_name = componentName;
700 // string _impl_name = "lib" + _compo_name + "Engine.so";
701 // SCRUTE(_impl_name);
704 // handle = dlopen( _impl_name.c_str() , RTLD_LAZY ) ;
708 // string factory_name = _compo_name + "Engine_factory";
709 // SCRUTE(factory_name) ;
711 // typedef PortableServer::ObjectId * (*FACTORY_FUNCTION)
713 // PortableServer::POA_ptr,
714 // PortableServer::ObjectId *,
717 // FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
720 // if ( (error = dlerror() ) == NULL)
722 // // Instanciate required CORBA object
725 // sprintf( _aNumI , "%d" , _numInstance ) ;
726 // string instanceName = _compo_name + "_inst_" + _aNumI ;
727 // SCRUTE(instanceName);
729 // PortableServer::ObjectId * id ;
730 // id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str() ,
731 // _genericRegisterName.c_str() ) ;
732 // // get reference from id
733 // obj = _poa->id_to_reference(*id);
734 // iobject = Engines::Component::_narrow( obj ) ;
736 // // register the engine under the name containerName.dir/genericRegisterName.object
737 // _NS->Register( iobject , component_registerName.c_str() ) ;
738 // MESSAGE( "Container_i::instance " << component_registerName.c_str() << " registered" ) ;
739 // _handle_map[instanceName] = handle;
743 // INFOS("Can't resolve symbol: " + factory_name);
749 // INFOS("Can't load shared library : " << _impl_name);
750 // INFOS("error dlopen: " << dlerror());
756 // INFOS( "Container_i::instance exception caught" ) ;
758 // END_OF("Container_i::instance");
759 // _numInstanceMutex.unlock() ;
760 // return Engines::Component::_duplicate(iobject);
763 //=============================================================================
765 * CORBA attribute: Machine Name (hostname without domain extensions)
767 //=============================================================================
769 // char* Engines_Container_i::machineName()
771 // string s = GetHostname();
772 // MESSAGE("Engines_Container_i::machineName " << s);
773 // return CORBA::string_dup(s.c_str()) ;