1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SALOME_ParallelContainer : implementation of container and engine for ParallelKernel
23 // File : SALOME_ParallelContainer_i.cxx
24 // Author : André RIBES, EDF
26 #include "SALOME_ParallelContainer_i.hxx"
27 #include "SALOME_Component_i.hxx"
28 #include "SALOME_FileRef_i.hxx"
29 #include "SALOME_FileTransfer_i.hxx"
30 #include "SALOME_NamingService.hxx"
32 #include "utilities.h"
33 #include "Basics_Utils.hxx"
48 #include <paco_omni.h>
51 #include "Container_init_python.hxx"
55 bool _Sleeping = false ;
57 extern "C" {void ActSigIntHandler() ; }
59 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
61 extern "C" {void SigIntHandler( int ) ; }
64 /*! \class Engines_Parallel_Container_i
65 * \brief C++ implementation of Engines::Container interface for parallel
66 * container implemented with PaCO++
69 //=============================================================================
73 //=============================================================================
75 Engines_Parallel_Container_i::Engines_Parallel_Container_i (CORBA::ORB_ptr orb,
78 PortableServer::POA_ptr poa,
79 std::string containerName,
80 bool isServantAloneInProcess) :
81 InterfaceParallel_impl(orb,ior,rank),
82 Engines::PACO_Container_serv(orb,ior,rank),
83 Engines::PACO_Container_base_serv(orb,ior,rank),
84 Engines::Container_serv(orb,ior,rank),
85 Engines::Container_base_serv(orb,ior,rank),
86 _numInstance(0),_isServantAloneInProcess(isServantAloneInProcess)
90 _hostname = Kernel_Utils::GetHostname();
91 _orb = CORBA::ORB::_duplicate(orb);
92 _poa = PortableServer::POA::_duplicate(poa);
94 // Add CORBA object to the poa
95 _id = _poa->activate_object(this);
97 CORBA::Object_var container_node = _poa->id_to_reference(*_id);
99 // Adding this servant to SALOME
100 _NS = new SALOME_NamingService();
102 _containerName = _NS->BuildContainerNameForNS(containerName.c_str(), _hostname.c_str());
104 // Ajout du numero de noeud
105 char node_number[12];
106 sprintf(node_number, "%d", getMyRank());
107 _containerName = _containerName + node_number;
109 // Init Python container part
110 CORBA::String_var sior = _orb->object_to_string(container_node);
111 std::string myCommand="pyCont = SALOME_Container.SALOME_Container_i('";
112 myCommand += _containerName + "','";
115 Py_ACQUIRE_NEW_THREAD;
116 PyRun_SimpleString("import SALOME_Container\n");
117 PyRun_SimpleString((char*)myCommand.c_str());
118 Py_RELEASE_NEW_THREAD;
120 // Init FileTransfer service
121 fileTransfer_i* aFileTransfer = new fileTransfer_i();
122 _fileTransfer = aFileTransfer->_this();
123 aFileTransfer->_remove_ref();
125 // Some signal handlers
129 //=============================================================================
133 //=============================================================================
135 Engines_Parallel_Container_i::~Engines_Parallel_Container_i()
137 MESSAGE("Container_i::~Container_i()");
144 //=============================================================================
145 //! Get container name
147 * CORBA attribute: Container name (see constructor)
149 //=============================================================================
151 char* Engines_Parallel_Container_i::name()
153 return CORBA::string_dup(_containerName.c_str()) ;
156 //=============================================================================
157 //! Get container working directory
159 * CORBA attribute: Container working directory
161 //=============================================================================
164 Engines_Parallel_Container_i::workingdir()
168 return CORBA::string_dup(wd) ;
171 //=============================================================================
172 //! Get container log file name
174 * CORBA attribute: Container log file name
176 //=============================================================================
179 Engines_Parallel_Container_i::logfilename()
181 return CORBA::string_dup(_logfilename.c_str()) ;
184 //! Set container log file name
186 Engines_Parallel_Container_i::logfilename(const char* name)
191 //=============================================================================
192 //! Get container host name
194 * CORBA method: Get the hostName of the Container (without domain extensions)
196 //=============================================================================
198 char* Engines_Parallel_Container_i::getHostName()
200 MESSAGE("Warning: getHostName of a parallel container returns the hostname of the first servant node");
201 return CORBA::string_dup(_hostname.c_str()) ;
204 //=============================================================================
205 //! Get container PID
207 * CORBA method: Get the PID (process identification) of the Container
209 //=============================================================================
211 CORBA::Long Engines_Parallel_Container_i::getPID()
213 MESSAGE("Warning: getPID of a parallel container returns the PID of the first servant node");
217 //=============================================================================
218 //! Ping the servant to check it is still alive
220 * CORBA method: check if servant is still alive
222 //=============================================================================
224 void Engines_Parallel_Container_i::ping()
226 MESSAGE("Engines_Parallel_Container_i::ping() my pid is "<< _pid);
229 //=============================================================================
230 //! Shutdown the container
232 * CORBA method, oneway: Server shutdown.
233 * - Container name removed from naming service,
234 * - servant deactivation,
235 * - orb shutdown if no other servants in the process
237 //=============================================================================
239 void Engines_Parallel_Container_i::Shutdown()
241 MESSAGE("Engines_Parallel_Container_i::Shutdown()");
243 /* For each seq component contained in this container
244 * tell it to self-destroy
246 std::map<std::string, Engines::Component_var>::iterator itm;
247 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
251 itm->second->destroy();
253 catch(const CORBA::Exception& e)
255 // ignore this entry and continue
259 // ignore this entry and continue
263 // Destroy each parallel component node...
264 std::map<std::string, PortableServer::ObjectId *>::iterator i;
265 for (i = _par_obj_inst_map.begin(); i != _par_obj_inst_map.end(); i++)
266 _poa->deactivate_object(*(i->second));
268 _NS->Destroy_FullDirectory(_containerName.c_str());
269 _NS->Destroy_Name(_containerName.c_str());
271 if(_isServantAloneInProcess)
273 MESSAGE("Effective Shutdown of container Begins...");
274 if(!CORBA::is_nil(_orb))
280 //=============================================================================
281 //! load a new component class
283 * CORBA method: load a new component class (Python or C++ implementation)
284 * \param componentName like COMPONENT
285 * try to make a Python import of COMPONENT,
286 * then a lib open of libCOMPONENTEngine.so
287 * \return true if dlopen successfull or already done, false otherwise
289 //=============================================================================
292 Engines_Parallel_Container_i::load_component_Library(const char* componentName)
294 MESSAGE("Begin of load_component_Library : " << componentName)
296 std::string aCompName = componentName;
298 string impl_name = string ("lib") + aCompName + string("Engine.so");
300 string impl_name = aCompName + string("Engine.dll");
303 _numInstanceMutex.lock(); // lock to be alone
305 // Check if already loaded or imported in the container
306 if (_toRemove_map.count(impl_name) != 0) _toRemove_map.erase(impl_name);
307 if (_library_map.count(impl_name) != 0)
309 MESSAGE("Library " << impl_name << " already loaded");
312 if (_library_map.count(aCompName) != 0)
314 MESSAGE("Python component already imported");
318 // --- try dlopen C++ component
321 MESSAGE("Try to load C++ component");
324 handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
326 handle = dlopen( impl_name.c_str() , 0 ) ;
330 _library_map[impl_name] = handle;
331 MESSAGE("Library " << impl_name << " loaded");
336 std::cerr << "Can't load shared library : " << impl_name << std::endl;
337 std::cerr << "error of dlopen: " << dlerror() << std::endl;
341 // --- try import Python component
344 MESSAGE("Try to import Python component "<<componentName);
345 Py_ACQUIRE_NEW_THREAD;
346 PyObject *mainmod = PyImport_AddModule("__main__");
347 PyObject *globals = PyModule_GetDict(mainmod);
348 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
349 PyObject *result = PyObject_CallMethod(pyCont,
350 (char*)"import_component",
351 (char*)"s",componentName);
352 std::string ret_p= PyString_AsString(result);
354 Py_RELEASE_NEW_THREAD;
356 if (ret_p=="") // import possible: Python component
358 _library_map[aCompName] = (void *)pyCont; // any non O value OK
359 MESSAGE("import Python: " << aCompName <<" OK");
364 std::cerr << "Error in importing Python component : " << aCompName << std::endl;
368 _numInstanceMutex.unlock();
372 //=============================================================================
373 //! Create a new component instance
375 * CORBA method: Creates a new servant instance of a component.
376 * The servant registers itself to naming service and Registry.
377 * \param genericRegisterName Name of the component instance to register
378 * in Registry & Name Service (without _inst_n suffix)
379 * \param studyId 0 for multiStudy instance,
380 * study Id (>0) otherwise
381 * \return a loaded component
383 //=============================================================================
385 Engines::Component_ptr
386 Engines_Parallel_Container_i::create_component_instance(const char*genericRegisterName,
389 MESSAGE("Begin of create_component_instance in node : " << getMyRank());
393 INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
394 return Engines::Component::_nil() ;
397 std::string aCompName = genericRegisterName;
399 string impl_name = string ("lib") + aCompName +string("Engine.so");
401 string impl_name = aCompName +string("Engine.dll");
404 _numInstanceMutex.lock();
407 // Test if the component lib is loaded
408 std::string type_of_lib("Not Loaded");
409 void* handle = _library_map[impl_name];
412 if (_library_map.count(aCompName) != 0 and !handle)
413 type_of_lib = "python";
415 if (type_of_lib == "Not Loaded")
417 std::cerr << "Component library is not loaded or imported ! lib was : " << aCompName << std::endl;
418 _numInstanceMutex.unlock();
419 return Engines::Component::_nil();
422 Engines::Component_var iobject = Engines::Component::_nil();
423 if (type_of_lib == "cpp")
424 iobject = createCPPInstance(aCompName, handle, studyId);
426 iobject = createPythonInstance(aCompName, studyId);
428 _numInstanceMutex.unlock();
429 return iobject._retn();
432 //=============================================================================
433 //! Find an existing (in the container) component instance
435 * CORBA method: Finds a servant instance of a component
436 * \param registeredName Name of the component in Registry or Name Service,
437 * without instance suffix number
438 * \param studyId 0 if instance is not associated to a study,
439 * >0 otherwise (== study id)
440 * \return the first instance found with same studyId
442 //=============================================================================
444 Engines::Component_ptr Engines_Parallel_Container_i::find_component_instance( const char* registeredName,
447 Engines::Component_var anEngine = Engines::Component::_nil();
448 map<string,Engines::Component_var>::iterator itm =_listInstances_map.begin();
449 while (itm != _listInstances_map.end())
451 string instance = (*itm).first;
453 if (instance.find(registeredName) == 0)
455 anEngine = (*itm).second;
456 if (studyId == anEngine->getStudyId())
458 return anEngine._retn();
463 return anEngine._retn();
466 //=============================================================================
467 //! Find or create a new component instance
469 * CORBA method: find or create an instance of the component (servant),
470 * load a new component class (dynamic library) if required,
471 * ---- FOR COMPATIBILITY WITH 2.2 ----
472 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
473 * The servant registers itself to naming service and Registry.
474 * \param genericRegisterName Name of the component to register
475 * in Registry & Name Service
476 * \param componentName Name of the constructed library of the component
477 * \return a loaded component
479 //=============================================================================
481 Engines::Component_ptr Engines_Parallel_Container_i::load_impl( const char* genericRegisterName,
482 const char* componentName )
484 Engines::Component_var iobject = Engines::Component::_nil();
485 if (load_component_Library(genericRegisterName))
486 iobject = find_or_create_instance(genericRegisterName);
487 return iobject._retn();
491 //=============================================================================
492 //! Remove the component instance from container
494 * CORBA method: Stops the component servant, and deletes all related objects
495 * \param component_i Component to be removed
497 //=============================================================================
499 void Engines_Parallel_Container_i::remove_impl(Engines::Component_ptr component_i)
501 ASSERT(!CORBA::is_nil(component_i));
502 string instanceName = component_i->instanceName();
503 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
504 // Test if the component is in this container
505 std::map<std::string, Engines::Component_var>::iterator itm;
506 itm = _listInstances_map.find(instanceName);
507 if (itm != _listInstances_map.end())
509 MESSAGE("Unloading component " << instanceName);
510 _listInstances_map.erase(instanceName);
511 component_i->destroy() ;
512 _NS->Destroy_Name(instanceName.c_str());
515 std::cerr << "WARNING !!!! component instance was not in this container !!!" << std::endl;
516 _numInstanceMutex.unlock() ;
519 //=============================================================================
520 //! Unload component libraries from the container
522 * CORBA method: Discharges unused libraries from the container.
524 //=============================================================================
526 void Engines_Parallel_Container_i::finalize_removal()
528 MESSAGE("Finalize removal : dlclose");
529 MESSAGE("WARNING FINALIZE DOES CURRENTLY NOTHING !!!");
531 // (see decInstanceCnt, load_component_Library)
532 //map<string, void *>::iterator ith;
533 //for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
535 // void *handle = (*ith).second;
536 // string impl_name= (*ith).first;
540 // SCRUTE(impl_name);
541 // dlclose(handle); // SALOME unstable after ...
542 // _library_map.erase(impl_name);
546 _numInstanceMutex.lock(); // lock to be alone
547 _toRemove_map.clear();
548 _numInstanceMutex.unlock();
551 //=============================================================================
552 //! Kill the container
554 * CORBA method: Kill the container process with exit(0).
555 * To remove : never returns !
557 //=============================================================================
559 bool Engines_Parallel_Container_i::Kill_impl()
561 MESSAGE("Engines_Parallel_Container_i::Kill() my pid is "<< _pid
562 << " my containerName is " << _containerName.c_str()
563 << " my machineName is " << _hostname.c_str());
564 INFOS("===============================================================");
565 INFOS("= REMOVE calls to Kill_impl in C++ container =");
566 INFOS("===============================================================");
572 //=============================================================================
573 //! Get or create a file reference object associated to a local file (to transfer it)
575 * CORBA method: get or create a fileRef object associated to a local file
576 * (a file on the computer on which runs the container server), which stores
577 * a list of (machine, localFileName) corresponding to copies already done.
579 * \param origFileName absolute path for a local file to copy on other
581 * \return a fileRef object associated to the file.
583 //=============================================================================
586 Engines_Parallel_Container_i::createFileRef(const char* origFileName)
588 string origName(origFileName);
589 Engines::fileRef_var theFileRef = Engines::fileRef::_nil();
591 if (origName[0] != '/')
593 INFOS("path of file to copy must be an absolute path begining with '/'");
594 return Engines::fileRef::_nil();
597 if (CORBA::is_nil(_fileRef_map[origName]))
599 CORBA::Object_var obj=_poa->id_to_reference(*_id);
600 Engines::Container_var pCont = Engines::Container::_narrow(obj);
601 fileRef_i* aFileRef = new fileRef_i(pCont, origFileName);
602 theFileRef = Engines::fileRef::_narrow(aFileRef->_this());
603 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
604 _fileRef_map[origName] = theFileRef;
605 _numInstanceMutex.unlock() ;
608 theFileRef = Engines::fileRef::_duplicate(_fileRef_map[origName]);
609 ASSERT(! CORBA::is_nil(theFileRef));
610 return theFileRef._retn();
613 //=============================================================================
616 * \return a reference to the fileTransfer object
618 //=============================================================================
620 Engines::fileTransfer_ptr
621 Engines_Parallel_Container_i::getFileTransfer()
623 Engines::fileTransfer_var aFileTransfer
624 = Engines::fileTransfer::_duplicate(_fileTransfer);
625 return aFileTransfer._retn();
629 Engines::Salome_file_ptr
630 Engines_Parallel_Container_i::createSalome_file(const char* origFileName)
632 string origName(origFileName);
633 if (CORBA::is_nil(_Salome_file_map[origName]))
635 Salome_file_i* aSalome_file = new Salome_file_i();
638 aSalome_file->setLocalFile(origFileName);
639 aSalome_file->recvFiles();
641 catch (const SALOME::SALOME_Exception& e)
643 return Engines::Salome_file::_nil();
646 Engines::Salome_file_var theSalome_file = Engines::Salome_file::_nil();
647 theSalome_file = Engines::Salome_file::_narrow(aSalome_file->_this());
648 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
649 _Salome_file_map[origName] = theSalome_file;
650 _numInstanceMutex.unlock() ;
653 Engines::Salome_file_ptr theSalome_file =
654 Engines::Salome_file::_duplicate(_Salome_file_map[origName]);
655 ASSERT(!CORBA::is_nil(theSalome_file));
656 return theSalome_file;
660 //=============================================================================
661 //! Finds an already existing component instance or create a new instance
663 * C++ method: Finds an already existing servant instance of a component, or
664 * create an instance.
665 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
666 * \param genericRegisterName Name of the component instance to register
667 * in Registry & Name Service,
668 * (without _inst_n suffix, like "COMPONENT")
669 * \return a loaded component
671 * example with names:
672 * aGenRegisterName = COMPONENT (= first argument)
673 * impl_name = libCOMPONENTEngine.so (= second argument)
674 * _containerName = /Containers/cli76ce/FactoryServer
675 * factoryName = COMPONENTEngine_factory
676 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
678 * instanceName = COMPONENT_inst_1
679 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
681 //=============================================================================
683 Engines::Component_ptr
684 Engines_Parallel_Container_i::find_or_create_instance(string genericRegisterName)
686 Engines::Component_var iobject = Engines::Component::_nil();
689 string aGenRegisterName = genericRegisterName;
690 // --- find a registered instance in naming service, or create
691 string component_registerBase = _containerName + "/" + aGenRegisterName;
692 CORBA::Object_var obj = _NS->ResolveFirst(component_registerBase.c_str());
693 if (CORBA::is_nil( obj ))
695 iobject = create_component_instance(genericRegisterName.c_str(),
696 0); // force multiStudy instance here !
700 iobject = Engines::Component::_narrow(obj) ;
701 Engines_Component_i *servant = dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
703 int studyId = servant->getStudyId();
704 ASSERT (studyId >= 0);
705 if (studyId != 0) // monoStudy instance: NOK
707 iobject = Engines::Component::_nil();
708 INFOS("load_impl & find_component_instance methods "
709 << "NOT SUITABLE for mono study components");
715 INFOS( "Container_i::load_impl catched" ) ;
717 return iobject._retn();
720 //=============================================================================
721 //! Create a new Python component instance
723 * C++ method: create a servant instance of a component.
724 * \param genericRegisterName Name of the component instance to register
725 * in Registry & Name Service,
726 * (without _inst_n suffix, like "COMPONENT")
727 * \param handle loaded library handle
728 * \param studyId 0 for multiStudy instance,
729 * study Id (>0) otherwise
730 * \return a loaded component
732 * example with names:
733 * aGenRegisterName = COMPONENT (= first argument)
734 * _containerName = /Containers/cli76ce/FactoryServer
735 * factoryName = COMPONENTEngine_factory
736 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
737 * instanceName = COMPONENT_inst_1
738 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
740 //=============================================================================
741 Engines::Component_ptr
742 Engines_Parallel_Container_i::createPythonInstance(string genericRegisterName, int studyId)
745 Engines::Component_var iobject = Engines::Component::_nil();
747 int numInstance = _numInstance;
749 sprintf( aNumI , "%d" , numInstance ) ;
750 string instanceName = genericRegisterName + "_inst_" + aNumI ;
751 string component_registerName = _containerName + "/" + instanceName;
753 Py_ACQUIRE_NEW_THREAD;
754 PyObject *mainmod = PyImport_AddModule("__main__");
755 PyObject *globals = PyModule_GetDict(mainmod);
756 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
757 PyObject *result = PyObject_CallMethod(pyCont,
758 (char*)"create_component_instance",
760 genericRegisterName.c_str(),
761 instanceName.c_str(),
763 std::string iors = PyString_AsString(result);
765 Py_RELEASE_NEW_THREAD;
769 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
770 iobject = Engines::Component::_narrow(obj);
771 _listInstances_map[instanceName] = iobject;
774 std::cerr << "createPythonInstance ior is empty ! Error in creation" << std::endl;
776 return iobject._retn();
779 //=============================================================================
780 //! Create a new CPP component instance
782 * C++ method: create a servant instance of a component.
783 * \param genericRegisterName Name of the component instance to register
784 * in Registry & Name Service,
785 * (without _inst_n suffix, like "COMPONENT")
786 * \param handle loaded library handle
787 * \param studyId 0 for multiStudy instance,
788 * study Id (>0) otherwise
789 * \return a loaded component
791 * example with names:
792 * aGenRegisterName = COMPONENT (= first argument)
793 * _containerName = /Containers/cli76ce/FactoryServer
794 * factoryName = COMPONENTEngine_factory
795 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
796 * instanceName = COMPONENT_inst_1
797 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
799 //=============================================================================
800 Engines::Component_ptr
801 Engines_Parallel_Container_i::createCPPInstance(string genericRegisterName,
805 MESSAGE("Entering Engines_Parallel_Container_i::createCPPInstance");
807 // --- find the factory
809 string aGenRegisterName = genericRegisterName;
810 string factory_name = aGenRegisterName + string("Engine_factory");
812 typedef PortableServer::ObjectId * (*FACTORY_FUNCTION_2)
814 PortableServer::POA_ptr,
815 PortableServer::ObjectId *,
819 FACTORY_FUNCTION_2 Component_factory = NULL;
821 Component_factory = (FACTORY_FUNCTION_2)dlsym( handle, factory_name.c_str() );
823 Component_factory = (FACTORY_FUNCTION_2)GetProcAddress( (HINSTANCE)handle, factory_name.c_str() );
826 if (!Component_factory)
828 INFOS("Can't resolve symbol: " + factory_name);
830 INFOS("dlerror() result is : " << dlerror());
832 return Engines::Component::_nil() ;
835 // --- create instance
836 Engines::Component_var iobject = Engines::Component::_nil() ;
839 int numInstance = _numInstance;
841 sprintf( aNumI , "%d" , numInstance );
842 string instanceName = aGenRegisterName + "_inst_" + aNumI;
843 string component_registerName =
844 _containerName + "/" + instanceName;
846 // --- Instanciate required CORBA object
848 PortableServer::ObjectId *id; //not owner, do not delete (nore use var)
849 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(),
850 aGenRegisterName.c_str() );
853 INFOS("Factory function returns NULL !");
854 return iobject._retn();
857 // --- get reference & servant from id
858 CORBA::Object_var obj = _poa->id_to_reference(*id);
859 iobject = Engines::Component::_narrow(obj);
861 Engines_Component_i *servant =
862 dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
864 servant->_remove_ref(); // compensate previous id_to_reference
865 _listInstances_map[instanceName] = iobject;
866 _cntInstances_map[aGenRegisterName] += 1;
867 #if defined(_DEBUG_) || defined(_DEBUG)
868 bool ret_studyId = servant->setStudyId(studyId);
871 servant->setStudyId(studyId);
874 // --- register the engine under the name
875 // containerName(.dir)/instanceName(.object)
876 _NS->Register(iobject , component_registerName.c_str());
877 MESSAGE( component_registerName.c_str() << " bound" );
881 INFOS( "Container_i::createInstance exception catched" );
883 return iobject._retn();
887 Engines_Parallel_Container_i::create_paco_component_node_instance(const char* componentName,
888 const char* proxy_containerName,
891 // Init de la méthode
893 Engines::Component_PaCO_var work_node;
894 std::string aCompName = componentName;
895 std::string _proxy_containerName = proxy_containerName;
898 string impl_name = string ("lib") + aCompName +string("Engine.so");
900 string impl_name = aCompName +string("Engine.dll");
902 void* handle = _library_map[impl_name];
903 _numInstanceMutex.lock() ; // lock on the instance number
905 int numInstance = _numInstance ;
906 _numInstanceMutex.unlock() ;
908 sprintf( aNumI , "%d" , numInstance ) ;
909 string instanceName = aCompName + "_inst_" + aNumI ;
911 // Step 1 : Get proxy !
912 string component_registerName = _proxy_containerName + "/" + instanceName;
913 CORBA::Object_var temp = _NS->Resolve(component_registerName.c_str());
914 Engines::Component_var obj_proxy = Engines::Component::_narrow(temp);
915 if (CORBA::is_nil(obj_proxy))
917 INFOS("Proxy reference from NamingService is nil !");
918 INFOS("Proxy name was : " << component_registerName);
919 SALOME::ExceptionStruct es;
920 es.type = SALOME::INTERNAL_ERROR;
921 es.text = "Proxy reference from NamingService is nil !";
922 throw SALOME::SALOME_Exception(es);
924 proxy_ior = _orb->object_to_string(obj_proxy);
927 string factory_name = aCompName + string("Engine_factory");
928 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
929 if (!Component_factory)
931 INFOS("Can't resolve symbol : " + factory_name);
933 INFOS("dlerror() result is : " << dlerror());
935 std::string ex_text = "Can't resolve symbol : " + factory_name;
936 SALOME::ExceptionStruct es;
937 es.type = SALOME::INTERNAL_ERROR;
938 es.text = CORBA::string_dup(ex_text.c_str());
939 throw SALOME::SALOME_Exception(es);
945 sprintf(aNumI2 , "%d" , getMyRank()) ;
946 string instanceName = aCompName + "_inst_" + aNumI + "_work_node_" + aNumI2;
947 string component_registerName = _containerName + "/" + instanceName;
949 // --- Instanciate work node
950 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
951 id = (Component_factory) (_orb, proxy_ior, getMyRank(), _poa, _id, instanceName.c_str(), componentName);
952 CORBA::string_free(proxy_ior);
954 // --- get reference & servant from id
955 CORBA::Object_var obj = _poa->id_to_reference(*id);
956 work_node = Engines::Component_PaCO::_narrow(obj) ;
957 if (CORBA::is_nil(work_node))
959 INFOS("work_node reference from factory is nil !");
960 SALOME::ExceptionStruct es;
961 es.type = SALOME::INTERNAL_ERROR;
962 es.text = "work_node reference from factory is nil !";
963 throw SALOME::SALOME_Exception(es);
966 _NS->Register(work_node, component_registerName.c_str());
967 _par_obj_inst_map[instanceName] = id;
968 MESSAGE(component_registerName.c_str() << " bound" );
972 INFOS("Container_i::create_paco_component_node_instance exception catched");
973 SALOME::ExceptionStruct es;
974 es.type = SALOME::INTERNAL_ERROR;
975 es.text = "Container_i::create_paco_component_node_instance exception catched";
976 throw SALOME::SALOME_Exception(es);
980 //=============================================================================
981 //! Decrement component instance reference count
985 //=============================================================================
987 void Engines_Parallel_Container_i::decInstanceCnt(string genericRegisterName)
989 if(_cntInstances_map.count(genericRegisterName) !=0 )
991 string aGenRegisterName =genericRegisterName;
992 MESSAGE("Engines_Parallel_Container_i::decInstanceCnt " << aGenRegisterName);
993 ASSERT(_cntInstances_map[aGenRegisterName] > 0);
994 _numInstanceMutex.lock(); // lock to be alone
995 // (see finalize_removal, load_component_Library)
996 _cntInstances_map[aGenRegisterName] -= 1;
997 SCRUTE(_cntInstances_map[aGenRegisterName]);
998 if (_cntInstances_map[aGenRegisterName] == 0)
1001 Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str());
1003 void* handle = _library_map[impl_name];
1005 _toRemove_map[impl_name] = handle;
1007 _numInstanceMutex.unlock();
1011 //=============================================================================
1012 //! Indicate if container is a python one
1014 * Retrieves only with container naming convention if it is a python container
1016 //=============================================================================
1018 bool Engines_Parallel_Container_i::isPythonContainer(const char* ContainerName)
1025 // Cette méthode permet de tenir à jour le compteur des
1026 // instances pour le container parallèle.
1027 // En effet losrque l'on charge un composant séquentielle seul
1028 // le compteur du noeud 0 est augmenté, il faut donc tenir les autres
1031 Engines_Parallel_Container_i::updateInstanceNumber()
1033 if (getMyRank() != 0)
1035 _numInstanceMutex.lock();
1037 _numInstanceMutex.unlock();
1041 /*! \brief copy a file from a remote host (container) to the local host
1042 * \param container the remote container
1043 * \param remoteFile the file to copy locally from the remote host into localFile
1044 * \param localFile the local file
1047 Engines_Parallel_Container_i::copyFile(Engines::Container_ptr container, const char* remoteFile, const char* localFile)
1049 Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
1052 if ((fp = fopen(localFile,"wb")) == NULL)
1054 INFOS("file " << localFile << " cannot be open for writing");
1058 CORBA::Long fileId = fileTransfer->open(remoteFile);
1061 Engines::fileBlock* aBlock;
1068 aBlock = fileTransfer->getBlock(fileId);
1069 toFollow = aBlock->length();
1071 CORBA::Octet *buf = aBlock->get_buffer();
1072 fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
1076 MESSAGE("end of transfer");
1077 fileTransfer->close(fileId);
1081 INFOS("open reference file for copy impossible");
1085 /*! \brief create a PyNode object to execute remote python code
1086 * \param nodeName the name of the node
1087 * \param code the python code to load
1088 * \return the PyNode
1091 Engines_Parallel_Container_i::createPyNode(const char* nodeName, const char* code)
1093 INFOS("Python component not yet implemented");
1094 Engines::PyNode_var node= Engines::PyNode::_nil();
1095 return node._retn();
1098 //=============================================================================
1102 //=============================================================================
1104 void ActSigIntHandler()
1107 struct sigaction SigIntAct ;
1108 SigIntAct.sa_sigaction = &SigIntHandler ;
1109 SigIntAct.sa_flags = SA_SIGINFO ;
1112 // DEBUG 03.02.2005 : the first parameter of sigaction is not a mask of signals
1113 // (SIGINT | SIGUSR1) :
1114 // it must be only one signal ===> one call for SIGINT
1115 // and an other one for SIGUSR1
1118 if ( sigaction( SIGINT , &SigIntAct, NULL ) ) {
1119 perror("SALOME_Container main ") ;
1122 if ( sigaction( SIGUSR1 , &SigIntAct, NULL ) ) {
1123 perror("SALOME_Container main ") ;
1126 if ( sigaction( SIGUSR2 , &SigIntAct, NULL ) )
1128 perror("SALOME_Container main ") ;
1132 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1133 // use of streams (and so on) should never be used because :
1134 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1135 // A stream operation may be interrupted by a signal and if the Handler use stream we
1136 // may have a "Dead-Lock" ===HangUp
1137 //==INFOS is commented
1138 // INFOS(pthread_self() << "SigIntHandler activated") ;
1140 signal( SIGINT, SigIntHandler );
1141 signal( SIGUSR1, SigIntHandler );
1147 void CallCancelThread();
1150 void SigIntHandler(int what , siginfo_t * siginfo ,
1152 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1153 // use of streams (and so on) should never be used because :
1154 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1155 // A stream operation may be interrupted by a signal and if the Handler use stream we
1156 // may have a "Dead-Lock" ===HangUp
1157 //==MESSAGE is commented
1158 // MESSAGE(pthread_self() << "SigIntHandler what " << what << endl
1159 // << " si_signo " << siginfo->si_signo << endl
1160 // << " si_code " << siginfo->si_code << endl
1161 // << " si_pid " << siginfo->si_pid) ;
1164 // MESSAGE("SigIntHandler END sleeping.") ;
1168 ActSigIntHandler() ;
1169 if ( siginfo->si_signo == SIGUSR1 ) {
1172 else if ( siginfo->si_signo == SIGUSR2 )
1174 CallCancelThread() ;
1178 // MESSAGE("SigIntHandler BEGIN sleeping.") ;
1180 while( _Sleeping ) {
1184 // MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1190 void SigIntHandler( int what ) {
1191 MESSAGE( pthread_self() << "SigIntHandler what " << what << endl );
1194 MESSAGE("SigIntHandler END sleeping.") ;
1198 ActSigIntHandler() ;
1199 if ( what == SIGUSR1 ) {
1204 MESSAGE("SigIntHandler BEGIN sleeping.") ;
1206 while( _Sleeping ) {
1210 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;