1 // Copyright (C) 2007-2024 CEA, EDF, 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, or (at your option) any later version.
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 // File : SALOME_ParallelContainer_i.cxx
23 // Author : André RIBES, EDF
25 #include "SALOME_ParallelContainer_i.hxx"
26 #include "SALOME_Component_i.hxx"
27 #include "SALOME_FileRef_i.hxx"
28 #include "SALOME_FileTransfer_i.hxx"
29 #include "SALOME_NamingService.hxx"
31 #include "utilities.h"
32 #include "Basics_Utils.hxx"
47 #include <paco_omni.h>
50 #include "Container_init_python.hxx"
53 bool _Sleeping = false ;
55 extern "C" {void ActSigIntHandler() ; }
57 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
59 extern "C" {void SigIntHandler( int ) ; }
62 /*! \class Engines_Parallel_Container_i
63 * \brief C++ implementation of Engines::Container interface for parallel
64 * container implemented with PaCO++
67 //=============================================================================
71 //=============================================================================
73 Engines_Parallel_Container_i::Engines_Parallel_Container_i (CORBA::ORB_ptr orb,
76 PortableServer::POA_ptr poa,
77 std::string containerName,
78 bool isServantAloneInProcess) :
79 InterfaceParallel_impl(orb,ior,rank),
80 Engines::PACO_Container_serv(orb,ior,rank),
81 Engines::PACO_Container_base_serv(orb,ior,rank),
82 Engines::Container_serv(orb,ior,rank),
83 Engines::Container_base_serv(orb,ior,rank),
84 _numInstance(0),_isServantAloneInProcess(isServantAloneInProcess)
88 _hostname = Kernel_Utils::GetHostname();
89 _orb = CORBA::ORB::_duplicate(orb);
90 _poa = PortableServer::POA::_duplicate(poa);
92 // Add CORBA object to the poa
93 _id = _poa->activate_object(this);
95 CORBA::Object_var container_node = _poa->id_to_reference(*_id);
97 // Adding this servant to SALOME
98 _NS = new SALOME_NamingService();
100 _containerName = _NS->BuildContainerNameForNS(containerName.c_str(), _hostname.c_str());
102 // Ajout du numero de noeud
103 char node_number[12];
104 sprintf(node_number, "%d", getMyRank());
105 _containerName = _containerName + node_number;
107 // Init Python container part
108 CORBA::String_var sior = _orb->object_to_string(container_node);
109 std::string myCommand="pyCont = SALOME_Container.SALOME_Container_i('";
110 myCommand += _containerName + "','";
115 PyRun_SimpleString("import SALOME_Container\n");
116 PyRun_SimpleString((char*)myCommand.c_str());
119 // Init FileTransfer service
120 fileTransfer_i* aFileTransfer = new fileTransfer_i();
121 _fileTransfer = aFileTransfer->_this();
122 aFileTransfer->_remove_ref();
124 // Some signal handlers
128 //=============================================================================
132 //=============================================================================
134 Engines_Parallel_Container_i::~Engines_Parallel_Container_i()
136 MESSAGE("Container_i::~Container_i()");
143 //=============================================================================
144 //! Get container name
146 * CORBA attribute: Container name (see constructor)
148 //=============================================================================
150 char* Engines_Parallel_Container_i::name()
152 return CORBA::string_dup(_containerName.c_str()) ;
155 //=============================================================================
156 //! Get container working directory
158 * CORBA attribute: Container working directory
160 //=============================================================================
163 Engines_Parallel_Container_i::workingdir()
167 return CORBA::string_dup(wd) ;
170 //=============================================================================
171 //! Get container log file name
173 * CORBA attribute: Container log file name
175 //=============================================================================
178 Engines_Parallel_Container_i::logfilename()
180 return CORBA::string_dup(_logfilename.c_str()) ;
183 //! Set container log file name
185 Engines_Parallel_Container_i::logfilename(const char* name)
190 //=============================================================================
191 //! Get container host name
193 * CORBA method: Get the hostName of the Container (without domain extensions)
195 //=============================================================================
197 char* Engines_Parallel_Container_i::getHostName()
199 MESSAGE("Warning: getHostName of a parallel container returns the hostname of the first servant node");
200 return CORBA::string_dup(_hostname.c_str()) ;
203 //=============================================================================
204 //! Get container PID
206 * CORBA method: Get the PID (process identification) of the Container
208 //=============================================================================
210 CORBA::Long Engines_Parallel_Container_i::getPID()
212 MESSAGE("Warning: getPID of a parallel container returns the PID of the first servant node");
216 //=============================================================================
217 //! Ping the servant to check it is still alive
219 * CORBA method: check if servant is still alive
221 //=============================================================================
223 void Engines_Parallel_Container_i::ping()
225 MESSAGE("Engines_Parallel_Container_i::ping() my pid is "<< _pid);
228 //=============================================================================
229 //! Shutdown the container
231 * CORBA method, oneway: Server shutdown.
232 * - Container name removed from naming service,
233 * - servant deactivation,
234 * - orb shutdown if no other servants in the process
236 //=============================================================================
238 void Engines_Parallel_Container_i::Shutdown()
240 MESSAGE("Engines_Parallel_Container_i::Shutdown()");
242 /* For each seq component contained in this container
243 * tell it to self-destroy
245 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
246 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
250 itm->second->destroy();
252 catch(const CORBA::Exception& e)
254 // ignore this entry and continue
258 // ignore this entry and continue
262 // Destroy each parallel component node...
263 std::map<std::string, PortableServer::ObjectId *>::iterator i;
264 for (i = _par_obj_inst_map.begin(); i != _par_obj_inst_map.end(); i++)
265 _poa->deactivate_object(*(i->second));
267 _NS->Destroy_FullDirectory(_containerName.c_str());
268 _NS->Destroy_Name(_containerName.c_str());
270 if(_isServantAloneInProcess)
272 MESSAGE("Effective Shutdown of container Begins...");
273 if(!CORBA::is_nil(_orb))
279 //=============================================================================
280 //! load a new component class
282 * CORBA method: load a new component class (Python or C++ implementation)
283 * \param componentName like COMPONENT
284 * try to make a Python import of COMPONENT,
285 * then a lib open of libCOMPONENTEngine.so
286 * \return true if dlopen successful or already done, false otherwise
288 //=============================================================================
291 Engines_Parallel_Container_i::load_component_Library(const char* componentName, CORBA::String_out reason)
293 reason=CORBA::string_dup("");
295 MESSAGE("Begin of load_component_Library : " << componentName)
297 std::string aCompName = componentName;
300 std::string impl_name = string ("lib") + aCompName + string("Engine.dylib");
302 std::string impl_name = string ("lib") + aCompName + string("Engine.so");
305 std::string impl_name = aCompName + string("Engine.dll");
308 _numInstanceMutex.lock(); // lock to be alone
310 // Check if already loaded or imported in the container
311 if (_toRemove_map.count(impl_name) != 0) _toRemove_map.erase(impl_name);
312 if (_library_map.count(impl_name) != 0)
314 MESSAGE("Library " << impl_name << " already loaded");
317 if (_library_map.count(aCompName) != 0)
319 MESSAGE("Python component already imported");
323 // --- try dlopen C++ component
326 MESSAGE("Try to load C++ component");
329 handle = dlopen( impl_name.c_str() , RTLD_LAZY | RTLD_GLOBAL ) ;
331 handle = dlopen( impl_name.c_str() , 0 ) ;
335 _library_map[impl_name] = handle;
336 MESSAGE("Library " << impl_name << " loaded");
341 std::cerr << "Can't load shared library : " << impl_name << std::endl;
342 std::cerr << "error of dlopen: " << dlerror() << std::endl;
346 // --- try import Python component
349 MESSAGE("Try to import Python component "<<componentName);
352 PyObject *mainmod = PyImport_AddModule("__main__");
353 PyObject *globals = PyModule_GetDict(mainmod);
354 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
355 PyObject *result = PyObject_CallMethod(pyCont,
356 (char*)"import_component",
357 (char*)"s",componentName);
358 std::string ret_p= PyUnicode_AsUTF8(result);
362 if (ret_p=="") // import possible: Python component
364 _library_map[aCompName] = (void *)pyCont; // any non O value OK
365 MESSAGE("import Python: " << aCompName <<" OK");
370 std::cerr << "Error in importing Python component : " << aCompName << std::endl;
374 _numInstanceMutex.unlock();
378 //=============================================================================
379 //! Create a new component instance
381 * CORBA method: Creates a new servant instance of a component.
382 * The servant registers itself to naming service and Registry.
383 * \param genericRegisterName Name of the component instance to register
384 * in Registry & Name Service (without _inst_n suffix)
385 * \return a loaded component
387 //=============================================================================
388 Engines::EngineComponent_ptr
389 Engines_Parallel_Container_i::create_component_instance(const char*genericRegisterName)
391 Engines::FieldsDict_var env = new Engines::FieldsDict;
393 Engines::EngineComponent_ptr compo = create_component_instance_env(genericRegisterName,env, reason);
394 CORBA::string_free(reason);
398 //=============================================================================
399 //! Create a new component instance
401 * CORBA method: Creates a new servant instance of a component.
402 * The servant registers itself to naming service and Registry.
403 * \param genericRegisterName Name of the component instance to register
404 * in Registry & Name Service (without _inst_n suffix)
405 * \param env dict of environment variables
406 * \return a loaded component
408 //=============================================================================
410 Engines::EngineComponent_ptr
411 Engines_Parallel_Container_i::create_component_instance_env(const char*genericRegisterName,
412 const Engines::FieldsDict& env,
413 CORBA::String_out reason)
415 MESSAGE("Begin of create_component_instance in node : " << getMyRank());
416 reason=CORBA::string_dup("");
418 std::string aCompName = genericRegisterName;
421 std::string impl_name = string ("lib") + aCompName + string("Engine.dylib");
423 std::string impl_name = string ("lib") + aCompName +string("Engine.so");
426 std::string impl_name = aCompName +string("Engine.dll");
429 _numInstanceMutex.lock();
432 // Test if the component lib is loaded
433 std::string type_of_lib("Not Loaded");
434 void* handle = _library_map[impl_name];
437 if (_library_map.count(aCompName) != 0 && !handle)
438 type_of_lib = "python";
440 if (type_of_lib == "Not Loaded")
442 std::cerr << "Component library is not loaded or imported ! lib was : " << aCompName << std::endl;
443 _numInstanceMutex.unlock();
444 return Engines::EngineComponent::_nil();
447 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
448 if (type_of_lib == "cpp")
449 iobject = createCPPInstance(aCompName, handle);
451 iobject = createPythonInstance(aCompName);
453 _numInstanceMutex.unlock();
454 return iobject._retn();
457 //=============================================================================
458 //! Find an existing (in the container) component instance
460 * CORBA method: Finds a servant instance of a component
461 * \param registeredName Name of the component in Registry or Name Service,
462 * without instance suffix number
463 * \return the first found instance
465 //=============================================================================
467 Engines::EngineComponent_ptr Engines_Parallel_Container_i::find_component_instance(const char* registeredName)
469 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_nil();
470 std::map<std::string,Engines::EngineComponent_var>::iterator itm =_listInstances_map.begin();
471 while (itm != _listInstances_map.end())
473 std::string instance = (*itm).first;
475 if (instance.find(registeredName) == 0)
477 anEngine = (*itm).second;
478 return anEngine._retn();
482 return anEngine._retn();
485 char* Engines_Parallel_Container_i::create_python_service_instance(const char* CompName,
486 CORBA::String_out reason)
489 reason=CORBA::string_dup("");
490 return CORBA::string_dup("");
493 //=============================================================================
494 //! Find or create a new component instance
496 * CORBA method: find or create an instance of the component (servant),
497 * load a new component class (dynamic library) if required,
498 * ---- FOR COMPATIBILITY WITH 2.2 ----
499 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
500 * The servant registers itself to naming service and Registry.
501 * \param genericRegisterName Name of the component to register
502 * in Registry & Name Service
503 * \param componentName Name of the constructed library of the component
504 * \return a loaded component
506 //=============================================================================
508 Engines::EngineComponent_ptr Engines_Parallel_Container_i::load_impl( const char* genericRegisterName,
509 const char* componentName )
511 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
513 if (load_component_Library(genericRegisterName,reason))
514 iobject = find_or_create_instance(genericRegisterName);
515 CORBA::string_free(reason);
516 return iobject._retn();
520 //=============================================================================
521 //! Remove the component instance from container
523 * CORBA method: Stops the component servant, and deletes all related objects
524 * \param component_i Component to be removed
526 //=============================================================================
528 void Engines_Parallel_Container_i::remove_impl(Engines::EngineComponent_ptr component_i)
530 ASSERT(!CORBA::is_nil(component_i));
531 std::string instanceName = component_i->instanceName();
532 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
533 // Test if the component is in this container
534 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
535 itm = _listInstances_map.find(instanceName);
536 if (itm != _listInstances_map.end())
538 MESSAGE("Unloading component " << instanceName);
539 _listInstances_map.erase(instanceName);
540 component_i->destroy() ;
541 _NS->Destroy_Name(instanceName.c_str());
544 std::cerr << "WARNING !!!! component instance was not in this container !!!" << std::endl;
545 _numInstanceMutex.unlock() ;
548 //=============================================================================
549 //! Unload component libraries from the container
551 * CORBA method: Discharges unused libraries from the container.
553 //=============================================================================
555 void Engines_Parallel_Container_i::finalize_removal()
557 MESSAGE("Finalize removal : dlclose");
558 MESSAGE("WARNING FINALIZE DOES CURRENTLY NOTHING !!!");
560 // (see decInstanceCnt, load_component_Library)
561 //std::map<std::string, void *>::iterator ith;
562 //for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
564 // void *handle = (*ith).second;
565 // std::string impl_name= (*ith).first;
569 // SCRUTE(impl_name);
570 // dlclose(handle); // SALOME unstable after ...
571 // _library_map.erase(impl_name);
575 _numInstanceMutex.lock(); // lock to be alone
576 _toRemove_map.clear();
577 _numInstanceMutex.unlock();
580 //=============================================================================
581 //! Kill the container
583 * CORBA method: Kill the container process with exit(0).
584 * To remove : never returns !
586 //=============================================================================
588 bool Engines_Parallel_Container_i::Kill_impl()
590 MESSAGE("Engines_Parallel_Container_i::Kill() my pid is "<< _pid
591 << " my containerName is " << _containerName.c_str()
592 << " my machineName is " << _hostname.c_str());
593 INFOS("===============================================================");
594 INFOS("= REMOVE calls to Kill_impl in C++ container =");
595 INFOS("===============================================================");
601 //=============================================================================
602 //! Get or create a file reference object associated to a local file (to transfer it)
604 * CORBA method: get or create a fileRef object associated to a local file
605 * (a file on the computer on which runs the container server), which stores
606 * a list of (machine, localFileName) corresponding to copies already done.
608 * \param origFileName absolute path for a local file to copy on other
610 * \return a fileRef object associated to the file.
612 //=============================================================================
615 Engines_Parallel_Container_i::createFileRef(const char* origFileName)
617 std::string origName(origFileName);
618 Engines::fileRef_var theFileRef = Engines::fileRef::_nil();
620 if (origName[0] != '/')
622 INFOS("path of file to copy must be an absolute path beginning with '/'");
623 return Engines::fileRef::_nil();
626 if (CORBA::is_nil(_fileRef_map[origName]))
628 CORBA::Object_var obj=_poa->id_to_reference(*_id);
629 Engines::Container_var pCont = Engines::Container::_narrow(obj);
630 fileRef_i* aFileRef = new fileRef_i(pCont, origFileName);
631 theFileRef = Engines::fileRef::_narrow(aFileRef->_this());
632 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
633 _fileRef_map[origName] = theFileRef;
634 _numInstanceMutex.unlock() ;
637 theFileRef = Engines::fileRef::_duplicate(_fileRef_map[origName]);
638 ASSERT(! CORBA::is_nil(theFileRef));
639 return theFileRef._retn();
642 //=============================================================================
645 * \return a reference to the fileTransfer object
647 //=============================================================================
649 Engines::fileTransfer_ptr
650 Engines_Parallel_Container_i::getFileTransfer()
652 Engines::fileTransfer_var aFileTransfer
653 = Engines::fileTransfer::_duplicate(_fileTransfer);
654 return aFileTransfer._retn();
658 Engines::Salome_file_ptr
659 Engines_Parallel_Container_i::createSalome_file(const char* origFileName)
661 string origName(origFileName);
662 if (CORBA::is_nil(_Salome_file_map[origName]))
664 Salome_file_i* aSalome_file = new Salome_file_i();
667 aSalome_file->setLocalFile(origFileName);
668 aSalome_file->recvFiles();
670 catch (const SALOME::SALOME_Exception& e)
672 return Engines::Salome_file::_nil();
675 Engines::Salome_file_var theSalome_file = Engines::Salome_file::_nil();
676 theSalome_file = Engines::Salome_file::_narrow(aSalome_file->_this());
677 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
678 _Salome_file_map[origName] = theSalome_file;
679 _numInstanceMutex.unlock() ;
682 Engines::Salome_file_ptr theSalome_file =
683 Engines::Salome_file::_duplicate(_Salome_file_map[origName]);
684 ASSERT(!CORBA::is_nil(theSalome_file));
685 return theSalome_file;
689 //=============================================================================
690 //! Finds an already existing component instance or create a new instance
692 * C++ method: Finds an already existing servant instance of a component, or
693 * create an instance.
694 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
695 * \param genericRegisterName Name of the component instance to register
696 * in Registry & Name Service,
697 * (without _inst_n suffix, like "COMPONENT")
698 * \return a loaded component
700 * example with names:
701 * aGenRegisterName = COMPONENT (= first argument)
702 * impl_name = libCOMPONENTEngine.so (= second argument)
703 * _containerName = /Containers/cli76ce/FactoryServer
704 * factoryName = COMPONENTEngine_factory
705 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
707 * instanceName = COMPONENT_inst_1
708 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
710 //=============================================================================
712 Engines::EngineComponent_ptr
713 Engines_Parallel_Container_i::find_or_create_instance(std::string genericRegisterName)
715 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
718 std::string aGenRegisterName = genericRegisterName;
719 // --- find a registered instance in naming service, or create
720 std::string component_registerBase = _containerName + "/" + aGenRegisterName;
721 CORBA::Object_var obj = _NS->ResolveFirst(component_registerBase.c_str());
722 if (CORBA::is_nil( obj ))
724 iobject = create_component_instance(genericRegisterName.c_str());
728 iobject = Engines::EngineComponent::_narrow(obj) ;
733 INFOS( "Container_i::load_impl caught" ) ;
735 return iobject._retn();
738 //=============================================================================
739 //! Create a new Python component instance
741 * C++ method: create a servant instance of a component.
742 * \param genericRegisterName Name of the component instance to register
743 * in Registry & Name Service,
744 * (without _inst_n suffix, like "COMPONENT")
745 * \param handle loaded library handle
746 * \return a loaded component
748 * example with names:
749 * aGenRegisterName = COMPONENT (= first argument)
750 * _containerName = /Containers/cli76ce/FactoryServer
751 * factoryName = COMPONENTEngine_factory
752 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
753 * instanceName = COMPONENT_inst_1
754 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
756 //=============================================================================
757 Engines::EngineComponent_ptr
758 Engines_Parallel_Container_i::createPythonInstance(std::string genericRegisterName)
761 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
763 int numInstance = _numInstance;
765 sprintf( aNumI , "%d" , numInstance ) ;
766 std::string instanceName = genericRegisterName + "_inst_" + aNumI ;
767 std::string component_registerName = _containerName + "/" + instanceName;
771 PyObject *mainmod = PyImport_AddModule("__main__");
772 PyObject *globals = PyModule_GetDict(mainmod);
773 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
774 PyObject *result = PyObject_CallMethod(pyCont,
775 (char*)"create_component_instance",
777 genericRegisterName.c_str(),
778 instanceName.c_str());
781 PyArg_ParseTuple(result,"ss", &ior, &error);
788 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
789 iobject = Engines::EngineComponent::_narrow(obj);
790 _listInstances_map[instanceName] = iobject;
793 std::cerr << "createPythonInstance ior is empty ! Error in creation" << std::endl;
795 return iobject._retn();
798 //=============================================================================
799 //! Create a new CPP component instance
801 * C++ method: create a servant instance of a component.
802 * \param genericRegisterName Name of the component instance to register
803 * in Registry & Name Service,
804 * (without _inst_n suffix, like "COMPONENT")
805 * \param handle loaded library handle
806 * \return a loaded component
808 * example with names:
809 * aGenRegisterName = COMPONENT (= first argument)
810 * _containerName = /Containers/cli76ce/FactoryServer
811 * factoryName = COMPONENTEngine_factory
812 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
813 * instanceName = COMPONENT_inst_1
814 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
816 //=============================================================================
817 Engines::EngineComponent_ptr
818 Engines_Parallel_Container_i::createCPPInstance(std::string genericRegisterName,
821 MESSAGE("Entering Engines_Parallel_Container_i::createCPPInstance");
823 // --- find the factory
825 std::string aGenRegisterName = genericRegisterName;
826 std::string factory_name = aGenRegisterName + string("Engine_factory");
828 typedef PortableServer::ObjectId * (*FACTORY_FUNCTION_2)
830 PortableServer::POA_ptr,
831 PortableServer::ObjectId *,
835 FACTORY_FUNCTION_2 Component_factory = NULL;
837 Component_factory = (FACTORY_FUNCTION_2)dlsym( handle, factory_name.c_str() );
839 Component_factory = (FACTORY_FUNCTION_2)GetProcAddress( (HINSTANCE)handle, factory_name.c_str() );
842 if (!Component_factory)
844 INFOS("Can't resolve symbol: " + factory_name);
846 INFOS("dlerror() result is : " << dlerror());
848 return Engines::EngineComponent::_nil() ;
851 // --- create instance
852 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
855 int numInstance = _numInstance;
857 sprintf( aNumI , "%d" , numInstance );
858 std::string instanceName = aGenRegisterName + "_inst_" + aNumI;
859 std::string component_registerName =
860 _containerName + "/" + instanceName;
862 // --- Instantiate required CORBA object
864 PortableServer::ObjectId *id; //not owner, do not delete (nore use var)
865 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(),
866 aGenRegisterName.c_str() );
869 INFOS("Factory function returns NULL !");
870 return iobject._retn();
873 // --- get reference from id
874 CORBA::Object_var obj = _poa->id_to_reference(*id);
875 iobject = Engines::EngineComponent::_narrow(obj);
877 _listInstances_map[instanceName] = iobject;
878 _cntInstances_map[aGenRegisterName] += 1;
880 // --- register the engine under the name
881 // containerName(.dir)/instanceName(.object)
882 _NS->Register(iobject , component_registerName.c_str());
883 MESSAGE( component_registerName.c_str() << " bound" );
887 INFOS( "Container_i::createInstance exception caught" );
889 return iobject._retn();
893 Engines_Parallel_Container_i::create_paco_component_node_instance(const char* componentName,
894 const char* proxy_containerName)
896 // Init de la méthode
898 Engines::EngineComponent_PaCO_var work_node;
899 std::string aCompName = componentName;
900 std::string _proxy_containerName = proxy_containerName;
904 string impl_name = string ("lib") + aCompName + string("Engine.dylib");
906 string impl_name = string ("lib") + aCompName +string("Engine.so");
909 string impl_name = aCompName +string("Engine.dll");
911 void* handle = _library_map[impl_name];
912 _numInstanceMutex.lock() ; // lock on the instance number
914 int numInstance = _numInstance ;
915 _numInstanceMutex.unlock() ;
917 sprintf( aNumI , "%d" , numInstance ) ;
918 string instanceName = aCompName + "_inst_" + aNumI ;
920 // Step 1 : Get proxy !
921 string component_registerName = _proxy_containerName + "/" + instanceName;
922 CORBA::Object_var temp = _NS->Resolve(component_registerName.c_str());
923 Engines::EngineComponent_var obj_proxy = Engines::EngineComponent::_narrow(temp);
924 if (CORBA::is_nil(obj_proxy))
926 INFOS("Proxy reference from NamingService is nil !");
927 INFOS("Proxy name was : " << component_registerName);
928 SALOME::ExceptionStruct es;
929 es.type = SALOME::INTERNAL_ERROR;
930 es.text = "Proxy reference from NamingService is nil !";
931 throw SALOME::SALOME_Exception(es);
933 proxy_ior = _orb->object_to_string(obj_proxy);
936 string factory_name = aCompName + string("Engine_factory");
937 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
938 if (!Component_factory)
940 INFOS("Can't resolve symbol : " + factory_name);
942 INFOS("dlerror() result is : " << dlerror());
944 std::string ex_text = "Can't resolve symbol : " + factory_name;
945 SALOME::ExceptionStruct es;
946 es.type = SALOME::INTERNAL_ERROR;
947 es.text = CORBA::string_dup(ex_text.c_str());
948 throw SALOME::SALOME_Exception(es);
954 sprintf(aNumI2 , "%d" , getMyRank()) ;
955 std::string instanceName = aCompName + "_inst_" + aNumI + "_work_node_" + aNumI2;
956 std::string component_registerName = _containerName + "/" + instanceName;
958 // --- Instantiate work node
959 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
960 id = (Component_factory) (_orb, proxy_ior, getMyRank(), _poa, _id, instanceName.c_str(), componentName);
961 CORBA::string_free(proxy_ior);
963 // --- get reference from id
964 CORBA::Object_var obj = _poa->id_to_reference(*id);
965 work_node = Engines::EngineComponent_PaCO::_narrow(obj) ;
966 if (CORBA::is_nil(work_node))
968 INFOS("work_node reference from factory is nil !");
969 SALOME::ExceptionStruct es;
970 es.type = SALOME::INTERNAL_ERROR;
971 es.text = "work_node reference from factory is nil !";
972 throw SALOME::SALOME_Exception(es);
975 _NS->Register(work_node, component_registerName.c_str());
976 _par_obj_inst_map[instanceName] = id;
977 MESSAGE(component_registerName.c_str() << " bound" );
981 INFOS("Container_i::create_paco_component_node_instance exception caught");
982 SALOME::ExceptionStruct es;
983 es.type = SALOME::INTERNAL_ERROR;
984 es.text = "Container_i::create_paco_component_node_instance exception caught";
985 throw SALOME::SALOME_Exception(es);
989 //=============================================================================
990 //! Decrement component instance reference count
994 //=============================================================================
996 void Engines_Parallel_Container_i::decInstanceCnt(std::string genericRegisterName)
998 if(_cntInstances_map.count(genericRegisterName) !=0 )
1000 std::string aGenRegisterName =genericRegisterName;
1001 MESSAGE("Engines_Parallel_Container_i::decInstanceCnt " << aGenRegisterName);
1002 ASSERT(_cntInstances_map[aGenRegisterName] > 0);
1003 _numInstanceMutex.lock(); // lock to be alone
1004 // (see finalize_removal, load_component_Library)
1005 _cntInstances_map[aGenRegisterName] -= 1;
1006 SCRUTE(_cntInstances_map[aGenRegisterName]);
1007 if (_cntInstances_map[aGenRegisterName] == 0)
1009 std::string impl_name =
1010 Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str());
1012 void* handle = _library_map[impl_name];
1014 _toRemove_map[impl_name] = handle;
1016 _numInstanceMutex.unlock();
1020 //=============================================================================
1021 //! Indicate if container is a python one
1023 * Retrieves only with container naming convention if it is a python container
1025 //=============================================================================
1027 bool Engines_Parallel_Container_i::isPythonContainer(const char* ContainerName)
1034 // Cette méthode permet de tenir à jour le compteur des
1035 // instances pour le container parallèle.
1036 // En effet losrque l'on charge un composant séquentielle seul
1037 // le compteur du noeud 0 est augmenté, il faut donc tenir les autres
1040 Engines_Parallel_Container_i::updateInstanceNumber()
1042 if (getMyRank() != 0)
1044 _numInstanceMutex.lock();
1046 _numInstanceMutex.unlock();
1050 /*! \brief copy a file from a remote host (container) to the local host
1051 * \param container the remote container
1052 * \param remoteFile the file to copy locally from the remote host into localFile
1053 * \param localFile the local file
1056 Engines_Parallel_Container_i::copyFile(Engines::Container_ptr container, const char* remoteFile, const char* localFile)
1058 Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
1061 if ((fp = fopen(localFile,"wb")) == NULL)
1063 INFOS("file " << localFile << " cannot be open for writing");
1067 CORBA::Long fileId = fileTransfer->open(remoteFile);
1070 Engines::fileBlock* aBlock;
1077 aBlock = fileTransfer->getBlock(fileId);
1078 toFollow = aBlock->length();
1080 CORBA::Octet *buf = aBlock->get_buffer();
1081 fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
1085 MESSAGE("end of transfer");
1086 fileTransfer->close(fileId);
1090 INFOS("open reference file for copy impossible");
1094 /*! \brief create a PyNode object to execute remote python code
1095 * \param nodeName the name of the node
1096 * \param code the python code to load
1097 * \return the PyNode
1100 Engines_Parallel_Container_i::createPyNode(const char* nodeName, const char* code)
1102 INFOS("Python component not yet implemented");
1103 Engines::PyNode_var node= Engines::PyNode::_nil();
1104 return node._retn();
1107 Engines::PyNode_ptr Engines_Parallel_Container_i::getDefaultPyNode(const char *nodeName)
1109 INFOS("Python component not yet implemented");
1110 return Engines::PyNode::_nil();
1113 Engines::PyScriptNode_ptr
1114 Engines_Parallel_Container_i::createPyScriptNode(const char* nodeName, const char* cod)
1116 INFOS("Python script node not yet implemented");
1117 Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil();
1118 return node._retn();
1121 Engines::PyScriptNode_ptr Engines_Parallel_Container_i::getDefaultPyScriptNode(const char *nodeName)
1123 INFOS("Python script node not yet implemented");
1124 return Engines::PyScriptNode::_nil();
1127 //=============================================================================
1131 //=============================================================================
1133 void ActSigIntHandler()
1136 struct sigaction SigIntAct ;
1137 SigIntAct.sa_sigaction = &SigIntHandler ;
1138 SigIntAct.sa_flags = SA_SIGINFO ;
1141 // DEBUG 03.02.2005 : the first parameter of sigaction is not a mask of signals
1142 // (SIGINT | SIGUSR1) :
1143 // it must be only one signal ===> one call for SIGINT
1144 // and an other one for SIGUSR1
1147 if ( sigaction( SIGINT , &SigIntAct, NULL ) ) {
1148 perror("SALOME_Container main ") ;
1151 if ( sigaction( SIGUSR1 , &SigIntAct, NULL ) ) {
1152 perror("SALOME_Container main ") ;
1155 if ( sigaction( SIGUSR2 , &SigIntAct, NULL ) )
1157 perror("SALOME_Container main ") ;
1161 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1162 // use of streams (and so on) should never be used because :
1163 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1164 // A stream operation may be interrupted by a signal and if the Handler use stream we
1165 // may have a "Dead-Lock" ===HangUp
1166 //==INFOS is commented
1167 // INFOS(pthread_self() << "SigIntHandler activated") ;
1169 signal( SIGINT, SigIntHandler );
1170 signal( SIGUSR1, SigIntHandler );
1176 void CallCancelThread();
1179 void SigIntHandler(int what , siginfo_t * siginfo ,
1181 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1182 // use of streams (and so on) should never be used because :
1183 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1184 // A stream operation may be interrupted by a signal and if the Handler use stream we
1185 // may have a "Dead-Lock" ===HangUp
1186 //==MESSAGE is commented
1187 // MESSAGE(pthread_self() << "SigIntHandler what " << what << endl
1188 // << " si_signo " << siginfo->si_signo << endl
1189 // << " si_code " << siginfo->si_code << endl
1190 // << " si_pid " << siginfo->si_pid) ;
1193 // MESSAGE("SigIntHandler END sleeping.") ;
1197 ActSigIntHandler() ;
1198 if ( siginfo->si_signo == SIGUSR1 ) {
1201 else if ( siginfo->si_signo == SIGUSR2 )
1203 CallCancelThread() ;
1207 // MESSAGE("SigIntHandler BEGIN sleeping.") ;
1209 while( _Sleeping ) {
1213 // MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1219 void SigIntHandler( int what ) {
1220 MESSAGE( pthread_self() << "SigIntHandler what " << what << endl );
1223 MESSAGE("SigIntHandler END sleeping.") ;
1227 ActSigIntHandler() ;
1228 if ( what == SIGUSR1 ) {
1233 MESSAGE("SigIntHandler BEGIN sleeping.") ;
1235 while( _Sleeping ) {
1239 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;