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.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : Container_i.cxx
25 // Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
29 //#define private public
43 #include "utilities.h"
44 #include <SALOMEconfig.h>
46 #include CORBA_SERVER_HEADER(SALOME_Component)
47 #include CORBA_SERVER_HEADER(SALOME_Exception)
49 //#include <SALOME_Component.hh>
51 #include <pthread.h> // must be before Python.h !
52 #include "SALOME_Container_i.hxx"
53 #include "SALOME_Component_i.hxx"
54 #include "SALOME_FileRef_i.hxx"
55 #include "SALOME_FileTransfer_i.hxx"
56 #include "Salome_file_i.hxx"
57 #include "SALOME_NamingService.hxx"
61 #include "Container_init_python.hxx"
65 bool _Sleeping = false ;
67 // // Needed by multi-threaded Python --- Supervision
72 // Containers with name FactoryServer are started via rsh in LifeCycleCORBA
73 // Other Containers are started via start_impl of FactoryServer
75 extern "C" {void ActSigIntHandler() ; }
77 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
79 extern "C" {void SigIntHandler( int ) ; }
83 map<std::string, int> Engines_Container_i::_cntInstances_map;
84 map<std::string, void *> Engines_Container_i::_library_map;
85 map<std::string, void *> Engines_Container_i::_toRemove_map;
86 omni_mutex Engines_Container_i::_numInstanceMutex ;
88 //=============================================================================
90 * Default constructor, not for use
92 //=============================================================================
94 Engines_Container_i::Engines_Container_i () :
99 //=============================================================================
103 //=============================================================================
105 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb,
106 PortableServer::POA_ptr poa,
107 char *containerName ,
108 int argc , char* argv[],
110 bool isServantAloneInProcess
112 _numInstance(0),_isServantAloneInProcess(isServantAloneInProcess)
114 _pid = (long)getpid();
122 string hostname = GetHostname();
124 MESSAGE(hostname << " " << getpid() <<
125 " Engines_Container_i starting argc " <<
126 _argc << " Thread " << pthread_self() ) ;
128 MESSAGE(hostname << " " << _getpid() <<
129 " Engines_Container_i starting argc " << _argc<< " Thread " << pthread_self().p ) ;
135 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
141 INFOS("SALOME_Container usage : SALOME_Container ServerName");
145 _isSupervContainer = false;
146 if (strcmp(argv[1],"SuperVisionContainer") == 0) _isSupervContainer = true;
148 if (_isSupervContainer)
154 _orb = CORBA::ORB::_duplicate(orb) ;
155 _poa = PortableServer::POA::_duplicate(poa) ;
157 // Pour les containers paralleles: il ne faut pas enregistrer et activer
158 // le container generique, mais le container specialise
162 _id = _poa->activate_object(this);
163 _NS = new SALOME_NamingService();
164 _NS->init_orb( _orb ) ;
165 CORBA::Object_var obj=_poa->id_to_reference(*_id);
166 Engines::Container_var pCont
167 = Engines::Container::_narrow(obj);
170 _containerName = _NS->BuildContainerNameForNS(containerName,
172 SCRUTE(_containerName);
173 _NS->Register(pCont, _containerName.c_str());
174 MESSAGE("Engines_Container_i::Engines_Container_i : Container name "
178 // import SALOME_Container
179 // pycont = SALOME_Container.SALOME_Container_i(containerIORStr)
181 CORBA::String_var sior = _orb->object_to_string(pCont);
182 string myCommand="pyCont = SALOME_Container.SALOME_Container_i('";
183 myCommand += _containerName + "','";
188 if (!_isSupervContainer)
192 PyEval_AcquireLock();
193 PyThreadState *myTstate = PyThreadState_New(KERNEL_PYTHON::_interp);
194 PyThreadState *myoldTstate = PyThreadState_Swap(myTstate);
196 Py_ACQUIRE_NEW_THREAD;
200 // mpv: this is temporary solution: there is a unregular crash if not
203 // first element is the path to Registry.dll, but it's wrong
204 PyRun_SimpleString("import sys\n");
205 PyRun_SimpleString("sys.path = sys.path[1:]\n");
207 PyRun_SimpleString("import SALOME_Container\n");
208 PyRun_SimpleString((char*)myCommand.c_str());
209 Py_RELEASE_NEW_THREAD;
212 fileTransfer_i* aFileTransfer = new fileTransfer_i();
213 CORBA::Object_var obref=aFileTransfer->_this();
214 _fileTransfer = Engines::fileTransfer::_narrow(obref);
215 aFileTransfer->_remove_ref();
219 //=============================================================================
223 //=============================================================================
225 Engines_Container_i::~Engines_Container_i()
227 MESSAGE("Container_i::~Container_i()");
233 //=============================================================================
235 * CORBA attribute: Container name (see constructor)
237 //=============================================================================
239 char* Engines_Container_i::name()
241 return CORBA::string_dup(_containerName.c_str()) ;
244 //=============================================================================
246 * CORBA attribute: Container working directory
248 //=============================================================================
250 char* Engines_Container_i::workingdir()
254 return CORBA::string_dup(wd) ;
257 //=============================================================================
259 * CORBA attribute: Container log file name
261 //=============================================================================
263 char* Engines_Container_i::logfilename()
265 return CORBA::string_dup(_logfilename.c_str()) ;
268 void Engines_Container_i::logfilename(const char* name)
273 //=============================================================================
275 * CORBA method: Get the hostName of the Container (without domain extensions)
277 //=============================================================================
279 char* Engines_Container_i::getHostName()
281 string s = GetHostname();
282 // MESSAGE("Engines_Container_i::getHostName " << s);
283 return CORBA::string_dup(s.c_str()) ;
286 //=============================================================================
288 * CORBA method: Get the PID (process identification) of the Container
290 //=============================================================================
292 CORBA::Long Engines_Container_i::getPID()
294 return (CORBA::Long)getpid();
297 //=============================================================================
299 * CORBA method: check if servant is still alive
301 //=============================================================================
303 void Engines_Container_i::ping()
305 MESSAGE("Engines_Container_i::ping() pid "<< getpid());
308 //=============================================================================
310 * CORBA method, oneway: Server shutdown.
311 * - Container name removed from naming service,
312 * - servant deactivation,
313 * - orb shutdown if no other servants in the process
315 //=============================================================================
317 void Engines_Container_i::Shutdown()
319 MESSAGE("Engines_Container_i::Shutdown()");
321 /* For each component contained in this container
322 * tell it to self-destroy
324 std::map<std::string, Engines::Component_var>::iterator itm;
325 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
326 itm->second->destroy();
328 _NS->Destroy_FullDirectory(_containerName.c_str());
329 _NS->Destroy_Name(_containerName.c_str());
331 //_poa->deactivate_object(*_id);
332 if(_isServantAloneInProcess)
334 MESSAGE("Effective Shutdown of container Begins...");
335 if(!CORBA::is_nil(_orb))
341 //=============================================================================
343 * CORBA method: load a new component class (Python or C++ implementation)
344 * \param componentName like COMPONENT
345 * try to make a Python import of COMPONENT,
346 * then a lib open of libCOMPONENTEngine.so
347 * \return true if dlopen successfull or already done, false otherwise
349 //=============================================================================
352 Engines_Container_i::load_component_Library(const char* componentName)
355 string aCompName = componentName;
357 // --- try dlopen C++ component
360 string impl_name = string ("lib") + aCompName + string("Engine.so");
362 string impl_name = aCompName + string("Engine.dll");
366 _numInstanceMutex.lock(); // lock to be alone
367 // (see decInstanceCnt, finalize_removal))
368 if (_toRemove_map.count(impl_name) != 0) _toRemove_map.erase(impl_name);
369 if (_library_map.count(impl_name) != 0)
371 MESSAGE("Library " << impl_name << " already loaded");
372 _numInstanceMutex.unlock();
378 handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
381 handle = LoadLibrary( impl_name.c_str() );
386 _library_map[impl_name] = handle;
387 _numInstanceMutex.unlock();
390 _numInstanceMutex.unlock();
392 // --- try import Python component
394 INFOS("try import Python component "<<componentName);
395 if (_isSupervContainer)
397 INFOS("Supervision Container does not support Python Component Engines");
400 if (_library_map.count(aCompName) != 0)
402 return true; // Python Component, already imported
406 Py_ACQUIRE_NEW_THREAD;
407 PyObject *mainmod = PyImport_AddModule("__main__");
408 PyObject *globals = PyModule_GetDict(mainmod);
409 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
410 PyObject *result = PyObject_CallMethod(pyCont,
413 int ret= PyInt_AsLong(result);
416 Py_RELEASE_NEW_THREAD;
418 if (ret) // import possible: Python component
420 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
421 _library_map[aCompName] = (void *)pyCont; // any non O value OK
422 _numInstanceMutex.unlock() ;
423 MESSAGE("import Python: "<<aCompName<<" OK");
427 INFOS( "Impossible to load component: " << componentName );
428 INFOS( "Can't load shared library: " << impl_name );
429 INFOS( "Can't import Python module: " << componentName );
433 //=============================================================================
435 * CORBA method: Creates a new servant instance of a component.
436 * The servant registers itself to naming service and Registry.
437 * \param genericRegisterName Name of the component instance to register
438 * in Registry & Name Service (without _inst_n suffix)
439 * \param studyId 0 for multiStudy instance,
440 * study Id (>0) otherwise
441 * \return a loaded component
443 //=============================================================================
445 Engines::Component_ptr
446 Engines_Container_i::create_component_instance(const char*genericRegisterName,
451 INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
452 return Engines::Component::_nil() ;
455 Engines::Component_var iobject = Engines::Component::_nil() ;
457 string aCompName = genericRegisterName;
458 if (_library_map.count(aCompName) != 0) // Python component
460 if (_isSupervContainer)
462 INFOS("Supervision Container does not support Python Component Engines");
463 return Engines::Component::_nil();
465 _numInstanceMutex.lock() ; // lock on the instance number
467 int numInstance = _numInstance ;
468 _numInstanceMutex.unlock() ;
471 sprintf( aNumI , "%d" , numInstance ) ;
472 string instanceName = aCompName + "_inst_" + aNumI ;
473 string component_registerName =
474 _containerName + "/" + instanceName;
476 Py_ACQUIRE_NEW_THREAD;
477 PyObject *mainmod = PyImport_AddModule("__main__");
478 PyObject *globals = PyModule_GetDict(mainmod);
479 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
480 PyObject *result = PyObject_CallMethod(pyCont,
481 "create_component_instance",
484 instanceName.c_str(),
486 string iors = PyString_AsString(result);
489 Py_RELEASE_NEW_THREAD;
493 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
494 iobject = Engines::Component::_narrow( obj ) ;
495 _listInstances_map[instanceName] = iobject;
497 return iobject._retn();
503 string impl_name = string ("lib") + genericRegisterName +string("Engine.so");
505 string impl_name = genericRegisterName +string("Engine.dll");
507 if (_library_map.count(impl_name) == 0)
509 INFOS("shared library " << impl_name <<" must be loaded before creating instance");
510 return Engines::Component::_nil() ;
514 void* handle = _library_map[impl_name];
515 iobject = createInstance(genericRegisterName,
518 return iobject._retn();
522 //=============================================================================
524 * CORBA method: Finds a servant instance of a component
525 * \param registeredName Name of the component in Registry or Name Service,
526 * without instance suffix number
527 * \param studyId 0 if instance is not associated to a study,
528 * >0 otherwise (== study id)
529 * \return the first instance found with same studyId
531 //=============================================================================
533 Engines::Component_ptr
534 Engines_Container_i::find_component_instance( const char* registeredName,
537 Engines::Component_var anEngine = Engines::Component::_nil();
538 map<string,Engines::Component_var>::iterator itm =_listInstances_map.begin();
539 while (itm != _listInstances_map.end())
541 string instance = (*itm).first;
543 if (instance.find(registeredName) == 0)
545 anEngine = (*itm).second;
546 if (studyId == anEngine->getStudyId())
548 return anEngine._retn();
553 return anEngine._retn();
556 //=============================================================================
558 * CORBA method: find or create an instance of the component (servant),
559 * load a new component class (dynamic library) if required,
560 * ---- FOR COMPATIBILITY WITH 2.2 ----
561 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
562 * The servant registers itself to naming service and Registry.
563 * \param genericRegisterName Name of the component to register
564 * in Registry & Name Service
565 * \param componentName Name of the constructed library of the component
566 * \return a loaded component
568 //=============================================================================
570 Engines::Component_ptr
571 Engines_Container_i::load_impl( const char* genericRegisterName,
572 const char* componentName )
574 string impl_name = string ("lib") + genericRegisterName +string("Engine.so");
575 Engines::Component_var iobject = Engines::Component::_nil() ;
576 if (load_component_Library(genericRegisterName))
577 iobject = find_or_create_instance(genericRegisterName, impl_name);
578 return iobject._retn();
582 //=============================================================================
584 * CORBA method: Stops the component servant, and deletes all related objects
585 * \param component_i Component to be removed
587 //=============================================================================
589 void Engines_Container_i::remove_impl(Engines::Component_ptr component_i)
591 ASSERT(! CORBA::is_nil(component_i));
592 string instanceName = component_i->instanceName() ;
593 MESSAGE("unload component " << instanceName);
594 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
595 _listInstances_map.erase(instanceName);
596 _numInstanceMutex.unlock() ;
597 component_i->destroy() ;
598 _NS->Destroy_Name(instanceName.c_str());
601 //=============================================================================
603 * CORBA method: Discharges unused libraries from the container.
605 //=============================================================================
607 void Engines_Container_i::finalize_removal()
609 MESSAGE("finalize unload : dlclose");
610 _numInstanceMutex.lock(); // lock to be alone
611 // (see decInstanceCnt, load_component_Library)
612 map<string, void *>::iterator ith;
613 for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
615 void *handle = (*ith).second;
616 string impl_name= (*ith).first;
621 // dlclose(handle); // SALOME unstable after ...
622 // _library_map.erase(impl_name);
625 _toRemove_map.clear();
626 _numInstanceMutex.unlock();
629 //=============================================================================
631 * CORBA method: Kill the container process with exit(0).
632 * To remove : never returns !
634 //=============================================================================
636 bool Engines_Container_i::Kill_impl()
638 MESSAGE("Engines_Container_i::Kill() pid "<< getpid() << " containerName "
639 << _containerName.c_str() << " machineName "
640 << GetHostname().c_str());
641 INFOS("===============================================================");
642 INFOS("= REMOVE calls to Kill_impl in C++ container =");
643 INFOS("===============================================================");
649 //=============================================================================
651 * CORBA method: get or create a fileRef object associated to a local file
652 * (a file on the computer on which runs the container server), which stores
653 * a list of (machine, localFileName) corresponding to copies already done.
655 * \param origFileName absolute path for a local file to copy on other
657 * \return a fileRef object associated to the file.
659 //=============================================================================
662 Engines_Container_i::createFileRef(const char* origFileName)
664 string origName(origFileName);
665 Engines::fileRef_var theFileRef = Engines::fileRef::_nil();
667 if (origName[0] != '/')
669 INFOS("path of file to copy must be an absolute path begining with '/'");
670 return Engines::fileRef::_nil();
673 if (CORBA::is_nil(_fileRef_map[origName]))
675 CORBA::Object_var obj=_poa->id_to_reference(*_id);
676 Engines::Container_var pCont = Engines::Container::_narrow(obj);
677 fileRef_i* aFileRef = new fileRef_i(pCont, origFileName);
678 theFileRef = Engines::fileRef::_narrow(aFileRef->_this());
679 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
680 _fileRef_map[origName] = theFileRef;
681 _numInstanceMutex.unlock() ;
684 theFileRef = Engines::fileRef::_duplicate(_fileRef_map[origName]);
685 ASSERT(! CORBA::is_nil(theFileRef));
686 return theFileRef._retn();
689 //=============================================================================
692 * \return a reference to the fileTransfer object
694 //=============================================================================
696 Engines::fileTransfer_ptr
697 Engines_Container_i::getFileTransfer()
699 Engines::fileTransfer_var aFileTransfer
700 = Engines::fileTransfer::_duplicate(_fileTransfer);
701 return aFileTransfer._retn();
705 Engines::Salome_file_ptr
706 Engines_Container_i::createSalome_file(const char* origFileName)
708 string origName(origFileName);
709 if (CORBA::is_nil(_Salome_file_map[origName]))
711 Salome_file_i* aSalome_file = new Salome_file_i();
712 aSalome_file->setContainer(Engines::Container::_duplicate(this->_this()));
715 aSalome_file->setLocalFile(origFileName);
716 aSalome_file->recvFiles();
718 catch (const SALOME::SALOME_Exception& e)
720 return Engines::Salome_file::_nil();
723 Engines::Salome_file_var theSalome_file = Engines::Salome_file::_nil();
724 theSalome_file = Engines::Salome_file::_narrow(aSalome_file->_this());
725 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
726 _Salome_file_map[origName] = theSalome_file;
727 _numInstanceMutex.unlock() ;
730 Engines::Salome_file_ptr theSalome_file =
731 Engines::Salome_file::_duplicate(_Salome_file_map[origName]);
732 ASSERT(!CORBA::is_nil(theSalome_file));
733 return theSalome_file;
735 //=============================================================================
737 * C++ method: Finds an already existing servant instance of a component, or
738 * create an instance.
739 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
740 * \param genericRegisterName Name of the component instance to register
741 * in Registry & Name Service,
742 * (without _inst_n suffix, like "COMPONENT")
743 * \param componentLibraryName like "libCOMPONENTEngine.so"
744 * \return a loaded component
746 * example with names:
747 * aGenRegisterName = COMPONENT (= first argument)
748 * impl_name = libCOMPONENTEngine.so (= second argument)
749 * _containerName = /Containers/cli76ce/FactoryServer
750 * factoryName = COMPONENTEngine_factory
751 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
753 * instanceName = COMPONENT_inst_1
754 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
756 //=============================================================================
758 Engines::Component_ptr
759 Engines_Container_i::find_or_create_instance(string genericRegisterName,
760 string componentLibraryName)
762 string aGenRegisterName = genericRegisterName;
763 string impl_name = componentLibraryName;
764 if (_library_map.count(impl_name) == 0)
766 INFOS("shared library " << impl_name <<" must be loaded before creating instance");
767 return Engines::Component::_nil() ;
771 // --- find a registered instance in naming service, or create
773 void* handle = _library_map[impl_name];
774 string component_registerBase =
775 _containerName + "/" + aGenRegisterName;
776 Engines::Component_var iobject = Engines::Component::_nil() ;
779 CORBA::Object_var obj =
780 _NS->ResolveFirst( component_registerBase.c_str());
781 if ( CORBA::is_nil( obj ) )
783 iobject = createInstance(genericRegisterName,
785 0); // force multiStudy instance here !
789 iobject = Engines::Component::_narrow( obj ) ;
790 Engines_Component_i *servant =
791 dynamic_cast<Engines_Component_i*>
792 (_poa->reference_to_servant(iobject));
794 int studyId = servant->getStudyId();
795 ASSERT (studyId >= 0);
796 if (studyId == 0) // multiStudy instance, OK
799 MESSAGE(component_registerBase.c_str()<<" already bound");
801 else // monoStudy instance: NOK
803 iobject = Engines::Component::_nil();
804 INFOS("load_impl & find_component_instance methods "
805 << "NOT SUITABLE for mono study components");
811 INFOS( "Container_i::load_impl catched" ) ;
813 return iobject._retn();
817 //=============================================================================
819 * C++ method: create a servant instance of a component.
820 * \param genericRegisterName Name of the component instance to register
821 * in Registry & Name Service,
822 * (without _inst_n suffix, like "COMPONENT")
823 * \param handle loaded library handle
824 * \param studyId 0 for multiStudy instance,
825 * study Id (>0) otherwise
826 * \return a loaded component
828 * example with names:
829 * aGenRegisterName = COMPONENT (= first argument)
830 * _containerName = /Containers/cli76ce/FactoryServer
831 * factoryName = COMPONENTEngine_factory
832 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
833 * instanceName = COMPONENT_inst_1
834 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
836 //=============================================================================
838 Engines::Component_ptr
839 Engines_Container_i::createInstance(string genericRegisterName,
843 // --- find the factory
845 string aGenRegisterName = genericRegisterName;
846 string factory_name = aGenRegisterName + string("Engine_factory");
847 SCRUTE(factory_name) ;
849 typedef PortableServer::ObjectId * (*FACTORY_FUNCTION)
851 PortableServer::POA_ptr,
852 PortableServer::ObjectId *,
857 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION)dlsym( handle, factory_name.c_str() );
859 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION)GetProcAddress( (HINSTANCE)handle, factory_name.c_str() );
862 if ( !Component_factory )
864 INFOS( "Can't resolve symbol: " + factory_name );
868 return Engines::Component::_nil() ;
871 // --- create instance
873 Engines::Component_var iobject = Engines::Component::_nil() ;
877 _numInstanceMutex.lock() ; // lock on the instance number
879 int numInstance = _numInstance ;
880 _numInstanceMutex.unlock() ;
883 sprintf( aNumI , "%d" , numInstance ) ;
884 string instanceName = aGenRegisterName + "_inst_" + aNumI ;
885 string component_registerName =
886 _containerName + "/" + instanceName;
888 // --- Instanciate required CORBA object
890 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
891 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(),
892 aGenRegisterName.c_str() ) ;
894 return iobject._retn();
896 // --- get reference & servant from id
898 CORBA::Object_var obj = _poa->id_to_reference(*id);
899 iobject = Engines::Component::_narrow( obj ) ;
901 Engines_Component_i *servant =
902 dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
904 //SCRUTE(servant->pd_refCount);
905 servant->_remove_ref(); // compensate previous id_to_reference
906 //SCRUTE(servant->pd_refCount);
907 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
908 _listInstances_map[instanceName] = iobject;
909 _cntInstances_map[aGenRegisterName] += 1;
910 _numInstanceMutex.unlock() ;
911 SCRUTE(aGenRegisterName);
912 SCRUTE(_cntInstances_map[aGenRegisterName]);
913 //SCRUTE(servant->pd_refCount);
914 bool ret_studyId = servant->setStudyId(studyId);
917 // --- register the engine under the name
918 // containerName(.dir)/instanceName(.object)
920 _NS->Register( iobject , component_registerName.c_str() ) ;
921 MESSAGE( component_registerName.c_str() << " bound" ) ;
925 INFOS( "Container_i::createInstance exception catched" ) ;
927 return iobject._retn();
930 //=============================================================================
934 //=============================================================================
936 void Engines_Container_i::decInstanceCnt(string genericRegisterName)
938 if(_cntInstances_map.count(genericRegisterName)==0)
940 string aGenRegisterName =genericRegisterName;
941 MESSAGE("Engines_Container_i::decInstanceCnt " << aGenRegisterName);
942 ASSERT(_cntInstances_map[aGenRegisterName] > 0);
943 _numInstanceMutex.lock(); // lock to be alone
944 // (see finalize_removal, load_component_Library)
945 _cntInstances_map[aGenRegisterName] -= 1;
946 SCRUTE(_cntInstances_map[aGenRegisterName]);
947 if (_cntInstances_map[aGenRegisterName] == 0)
950 Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str());
952 void* handle = _library_map[impl_name];
954 _toRemove_map[impl_name] = handle;
956 _numInstanceMutex.unlock();
959 //=============================================================================
961 * Retrieves only with container naming convention if it is a python container
963 //=============================================================================
965 bool Engines_Container_i::isPythonContainer(const char* ContainerName)
968 int len=strlen(ContainerName);
970 if(strcmp(ContainerName+len-2,"Py")==0)
975 //=============================================================================
979 //=============================================================================
981 void ActSigIntHandler()
984 struct sigaction SigIntAct ;
985 SigIntAct.sa_sigaction = &SigIntHandler ;
986 SigIntAct.sa_flags = SA_SIGINFO ;
989 // DEBUG 03.02.2005 : the first parameter of sigaction is not a mask of signals
990 // (SIGINT | SIGUSR1) :
991 // it must be only one signal ===> one call for SIGINT
992 // and an other one for SIGUSR1
995 if ( sigaction( SIGINT , &SigIntAct, NULL ) )
997 perror("SALOME_Container main ") ;
1000 if ( sigaction( SIGUSR1 , &SigIntAct, NULL ) )
1002 perror("SALOME_Container main ") ;
1005 if ( sigaction( SIGUSR2 , &SigIntAct, NULL ) )
1007 perror("SALOME_Container main ") ;
1011 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1012 // use of streams (and so on) should never be used because :
1013 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1014 // A stream operation may be interrupted by a signal and if the Handler use stream we
1015 // may have a "Dead-Lock" ===HangUp
1016 //==INFOS is commented
1017 // INFOS(pthread_self() << "SigIntHandler activated") ;
1020 signal( SIGINT, SigIntHandler );
1021 signal( SIGUSR1, SigIntHandler );
1027 void CallCancelThread() ;
1030 void SigIntHandler(int what ,
1031 siginfo_t * siginfo ,
1034 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1035 // use of streams (and so on) should never be used because :
1036 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1037 // A stream operation may be interrupted by a signal and if the Handler use stream we
1038 // may have a "Dead-Lock" ===HangUp
1039 //==MESSAGE is commented
1040 // MESSAGE(pthread_self() << "SigIntHandler what " << what << endl
1041 // << " si_signo " << siginfo->si_signo << endl
1042 // << " si_code " << siginfo->si_code << endl
1043 // << " si_pid " << siginfo->si_pid) ;
1048 // MESSAGE("SigIntHandler END sleeping.") ;
1053 ActSigIntHandler() ;
1054 if ( siginfo->si_signo == SIGUSR1 )
1058 else if ( siginfo->si_signo == SIGUSR2 )
1060 CallCancelThread() ;
1065 // MESSAGE("SigIntHandler BEGIN sleeping.") ;
1072 // MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1078 void SigIntHandler( int what )
1081 MESSAGE( pthread_self() << "SigIntHandler what " << what << endl );
1083 MESSAGE( "SigIntHandler what " << what << endl );
1088 MESSAGE("SigIntHandler END sleeping.") ;
1093 ActSigIntHandler() ;
1094 if ( what == SIGUSR1 )
1101 MESSAGE("SigIntHandler BEGIN sleeping.") ;
1108 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;