1 // Copyright (C) 2007-2016 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, 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 + "','";
113 Py_ACQUIRE_NEW_THREAD;
114 PyRun_SimpleString("import SALOME_Container\n");
115 PyRun_SimpleString((char*)myCommand.c_str());
116 Py_RELEASE_NEW_THREAD;
118 // Init FileTransfer service
119 fileTransfer_i* aFileTransfer = new fileTransfer_i();
120 _fileTransfer = aFileTransfer->_this();
121 aFileTransfer->_remove_ref();
123 // Some signal handlers
127 //=============================================================================
131 //=============================================================================
133 Engines_Parallel_Container_i::~Engines_Parallel_Container_i()
135 MESSAGE("Container_i::~Container_i()");
142 //=============================================================================
143 //! Get container name
145 * CORBA attribute: Container name (see constructor)
147 //=============================================================================
149 char* Engines_Parallel_Container_i::name()
151 return CORBA::string_dup(_containerName.c_str()) ;
154 //=============================================================================
155 //! Get container working directory
157 * CORBA attribute: Container working directory
159 //=============================================================================
162 Engines_Parallel_Container_i::workingdir()
166 return CORBA::string_dup(wd) ;
169 //=============================================================================
170 //! Get container log file name
172 * CORBA attribute: Container log file name
174 //=============================================================================
177 Engines_Parallel_Container_i::logfilename()
179 return CORBA::string_dup(_logfilename.c_str()) ;
182 //! Set container log file name
184 Engines_Parallel_Container_i::logfilename(const char* name)
189 //=============================================================================
190 //! Get container host name
192 * CORBA method: Get the hostName of the Container (without domain extensions)
194 //=============================================================================
196 char* Engines_Parallel_Container_i::getHostName()
198 MESSAGE("Warning: getHostName of a parallel container returns the hostname of the first servant node");
199 return CORBA::string_dup(_hostname.c_str()) ;
202 //=============================================================================
203 //! Get container PID
205 * CORBA method: Get the PID (process identification) of the Container
207 //=============================================================================
209 CORBA::Long Engines_Parallel_Container_i::getPID()
211 MESSAGE("Warning: getPID of a parallel container returns the PID of the first servant node");
215 //=============================================================================
216 //! Ping the servant to check it is still alive
218 * CORBA method: check if servant is still alive
220 //=============================================================================
222 void Engines_Parallel_Container_i::ping()
224 MESSAGE("Engines_Parallel_Container_i::ping() my pid is "<< _pid);
227 //=============================================================================
228 //! Shutdown the container
230 * CORBA method, oneway: Server shutdown.
231 * - Container name removed from naming service,
232 * - servant deactivation,
233 * - orb shutdown if no other servants in the process
235 //=============================================================================
237 void Engines_Parallel_Container_i::Shutdown()
239 MESSAGE("Engines_Parallel_Container_i::Shutdown()");
241 /* For each seq component contained in this container
242 * tell it to self-destroy
244 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
245 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
249 itm->second->destroy();
251 catch(const CORBA::Exception& e)
253 // ignore this entry and continue
257 // ignore this entry and continue
261 // Destroy each parallel component node...
262 std::map<std::string, PortableServer::ObjectId *>::iterator i;
263 for (i = _par_obj_inst_map.begin(); i != _par_obj_inst_map.end(); i++)
264 _poa->deactivate_object(*(i->second));
266 _NS->Destroy_FullDirectory(_containerName.c_str());
267 _NS->Destroy_Name(_containerName.c_str());
269 if(_isServantAloneInProcess)
271 MESSAGE("Effective Shutdown of container Begins...");
272 if(!CORBA::is_nil(_orb))
278 //=============================================================================
279 //! load a new component class
281 * CORBA method: load a new component class (Python or C++ implementation)
282 * \param componentName like COMPONENT
283 * try to make a Python import of COMPONENT,
284 * then a lib open of libCOMPONENTEngine.so
285 * \return true if dlopen successfull or already done, false otherwise
287 //=============================================================================
290 Engines_Parallel_Container_i::load_component_Library(const char* componentName, CORBA::String_out reason)
292 reason=CORBA::string_dup("");
294 MESSAGE("Begin of load_component_Library : " << componentName)
296 std::string aCompName = componentName;
298 std::string impl_name = string ("lib") + aCompName + string("Engine.so");
300 std::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 * \return a loaded component
381 //=============================================================================
382 Engines::EngineComponent_ptr
383 Engines_Parallel_Container_i::create_component_instance(const char*genericRegisterName)
385 Engines::FieldsDict_var env = new Engines::FieldsDict;
387 Engines::EngineComponent_ptr compo = create_component_instance_env(genericRegisterName,env, reason);
388 CORBA::string_free(reason);
392 //=============================================================================
393 //! Create a new component instance
395 * CORBA method: Creates a new servant instance of a component.
396 * The servant registers itself to naming service and Registry.
397 * \param genericRegisterName Name of the component instance to register
398 * in Registry & Name Service (without _inst_n suffix)
399 * \param env dict of environment variables
400 * \return a loaded component
402 //=============================================================================
404 Engines::EngineComponent_ptr
405 Engines_Parallel_Container_i::create_component_instance_env(const char*genericRegisterName,
406 const Engines::FieldsDict& env,
407 CORBA::String_out reason)
409 MESSAGE("Begin of create_component_instance in node : " << getMyRank());
410 reason=CORBA::string_dup("");
412 std::string aCompName = genericRegisterName;
414 std::string impl_name = string ("lib") + aCompName +string("Engine.so");
416 std::string impl_name = aCompName +string("Engine.dll");
419 _numInstanceMutex.lock();
422 // Test if the component lib is loaded
423 std::string type_of_lib("Not Loaded");
424 void* handle = _library_map[impl_name];
427 if (_library_map.count(aCompName) != 0 and !handle)
428 type_of_lib = "python";
430 if (type_of_lib == "Not Loaded")
432 std::cerr << "Component library is not loaded or imported ! lib was : " << aCompName << std::endl;
433 _numInstanceMutex.unlock();
434 return Engines::EngineComponent::_nil();
437 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
438 if (type_of_lib == "cpp")
439 iobject = createCPPInstance(aCompName, handle);
441 iobject = createPythonInstance(aCompName);
443 _numInstanceMutex.unlock();
444 return iobject._retn();
447 //=============================================================================
448 //! Find an existing (in the container) component instance
450 * CORBA method: Finds a servant instance of a component
451 * \param registeredName Name of the component in Registry or Name Service,
452 * without instance suffix number
453 * \return the first found instance
455 //=============================================================================
457 Engines::EngineComponent_ptr Engines_Parallel_Container_i::find_component_instance(const char* registeredName)
459 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_nil();
460 std::map<std::string,Engines::EngineComponent_var>::iterator itm =_listInstances_map.begin();
461 while (itm != _listInstances_map.end())
463 std::string instance = (*itm).first;
465 if (instance.find(registeredName) == 0)
467 anEngine = (*itm).second;
468 return anEngine._retn();
472 return anEngine._retn();
475 char* Engines_Parallel_Container_i::create_python_service_instance(const char* CompName,
476 CORBA::String_out reason)
479 reason=CORBA::string_dup("");
480 return CORBA::string_dup("");
483 //=============================================================================
484 //! Find or create a new component instance
486 * CORBA method: find or create an instance of the component (servant),
487 * load a new component class (dynamic library) if required,
488 * ---- FOR COMPATIBILITY WITH 2.2 ----
489 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
490 * The servant registers itself to naming service and Registry.
491 * \param genericRegisterName Name of the component to register
492 * in Registry & Name Service
493 * \param componentName Name of the constructed library of the component
494 * \return a loaded component
496 //=============================================================================
498 Engines::EngineComponent_ptr Engines_Parallel_Container_i::load_impl( const char* genericRegisterName,
499 const char* componentName )
501 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
503 if (load_component_Library(genericRegisterName,reason))
504 iobject = find_or_create_instance(genericRegisterName);
505 CORBA::string_free(reason);
506 return iobject._retn();
510 //=============================================================================
511 //! Remove the component instance from container
513 * CORBA method: Stops the component servant, and deletes all related objects
514 * \param component_i Component to be removed
516 //=============================================================================
518 void Engines_Parallel_Container_i::remove_impl(Engines::EngineComponent_ptr component_i)
520 ASSERT(!CORBA::is_nil(component_i));
521 std::string instanceName = component_i->instanceName();
522 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
523 // Test if the component is in this container
524 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
525 itm = _listInstances_map.find(instanceName);
526 if (itm != _listInstances_map.end())
528 MESSAGE("Unloading component " << instanceName);
529 _listInstances_map.erase(instanceName);
530 component_i->destroy() ;
531 _NS->Destroy_Name(instanceName.c_str());
534 std::cerr << "WARNING !!!! component instance was not in this container !!!" << std::endl;
535 _numInstanceMutex.unlock() ;
538 //=============================================================================
539 //! Unload component libraries from the container
541 * CORBA method: Discharges unused libraries from the container.
543 //=============================================================================
545 void Engines_Parallel_Container_i::finalize_removal()
547 MESSAGE("Finalize removal : dlclose");
548 MESSAGE("WARNING FINALIZE DOES CURRENTLY NOTHING !!!");
550 // (see decInstanceCnt, load_component_Library)
551 //std::map<std::string, void *>::iterator ith;
552 //for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
554 // void *handle = (*ith).second;
555 // std::string impl_name= (*ith).first;
559 // SCRUTE(impl_name);
560 // dlclose(handle); // SALOME unstable after ...
561 // _library_map.erase(impl_name);
565 _numInstanceMutex.lock(); // lock to be alone
566 _toRemove_map.clear();
567 _numInstanceMutex.unlock();
570 //=============================================================================
571 //! Kill the container
573 * CORBA method: Kill the container process with exit(0).
574 * To remove : never returns !
576 //=============================================================================
578 bool Engines_Parallel_Container_i::Kill_impl()
580 MESSAGE("Engines_Parallel_Container_i::Kill() my pid is "<< _pid
581 << " my containerName is " << _containerName.c_str()
582 << " my machineName is " << _hostname.c_str());
583 INFOS("===============================================================");
584 INFOS("= REMOVE calls to Kill_impl in C++ container =");
585 INFOS("===============================================================");
591 //=============================================================================
592 //! Get or create a file reference object associated to a local file (to transfer it)
594 * CORBA method: get or create a fileRef object associated to a local file
595 * (a file on the computer on which runs the container server), which stores
596 * a list of (machine, localFileName) corresponding to copies already done.
598 * \param origFileName absolute path for a local file to copy on other
600 * \return a fileRef object associated to the file.
602 //=============================================================================
605 Engines_Parallel_Container_i::createFileRef(const char* origFileName)
607 std::string origName(origFileName);
608 Engines::fileRef_var theFileRef = Engines::fileRef::_nil();
610 if (origName[0] != '/')
612 INFOS("path of file to copy must be an absolute path begining with '/'");
613 return Engines::fileRef::_nil();
616 if (CORBA::is_nil(_fileRef_map[origName]))
618 CORBA::Object_var obj=_poa->id_to_reference(*_id);
619 Engines::Container_var pCont = Engines::Container::_narrow(obj);
620 fileRef_i* aFileRef = new fileRef_i(pCont, origFileName);
621 theFileRef = Engines::fileRef::_narrow(aFileRef->_this());
622 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
623 _fileRef_map[origName] = theFileRef;
624 _numInstanceMutex.unlock() ;
627 theFileRef = Engines::fileRef::_duplicate(_fileRef_map[origName]);
628 ASSERT(! CORBA::is_nil(theFileRef));
629 return theFileRef._retn();
632 //=============================================================================
635 * \return a reference to the fileTransfer object
637 //=============================================================================
639 Engines::fileTransfer_ptr
640 Engines_Parallel_Container_i::getFileTransfer()
642 Engines::fileTransfer_var aFileTransfer
643 = Engines::fileTransfer::_duplicate(_fileTransfer);
644 return aFileTransfer._retn();
648 Engines::Salome_file_ptr
649 Engines_Parallel_Container_i::createSalome_file(const char* origFileName)
651 string origName(origFileName);
652 if (CORBA::is_nil(_Salome_file_map[origName]))
654 Salome_file_i* aSalome_file = new Salome_file_i();
657 aSalome_file->setLocalFile(origFileName);
658 aSalome_file->recvFiles();
660 catch (const SALOME::SALOME_Exception& e)
662 return Engines::Salome_file::_nil();
665 Engines::Salome_file_var theSalome_file = Engines::Salome_file::_nil();
666 theSalome_file = Engines::Salome_file::_narrow(aSalome_file->_this());
667 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
668 _Salome_file_map[origName] = theSalome_file;
669 _numInstanceMutex.unlock() ;
672 Engines::Salome_file_ptr theSalome_file =
673 Engines::Salome_file::_duplicate(_Salome_file_map[origName]);
674 ASSERT(!CORBA::is_nil(theSalome_file));
675 return theSalome_file;
679 //=============================================================================
680 //! Finds an already existing component instance or create a new instance
682 * C++ method: Finds an already existing servant instance of a component, or
683 * create an instance.
684 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
685 * \param genericRegisterName Name of the component instance to register
686 * in Registry & Name Service,
687 * (without _inst_n suffix, like "COMPONENT")
688 * \return a loaded component
690 * example with names:
691 * aGenRegisterName = COMPONENT (= first argument)
692 * impl_name = libCOMPONENTEngine.so (= second argument)
693 * _containerName = /Containers/cli76ce/FactoryServer
694 * factoryName = COMPONENTEngine_factory
695 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
697 * instanceName = COMPONENT_inst_1
698 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
700 //=============================================================================
702 Engines::EngineComponent_ptr
703 Engines_Parallel_Container_i::find_or_create_instance(std::string genericRegisterName)
705 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
708 std::string aGenRegisterName = genericRegisterName;
709 // --- find a registered instance in naming service, or create
710 std::string component_registerBase = _containerName + "/" + aGenRegisterName;
711 CORBA::Object_var obj = _NS->ResolveFirst(component_registerBase.c_str());
712 if (CORBA::is_nil( obj ))
714 iobject = create_component_instance(genericRegisterName.c_str());
718 iobject = Engines::EngineComponent::_narrow(obj) ;
723 INFOS( "Container_i::load_impl catched" ) ;
725 return iobject._retn();
728 //=============================================================================
729 //! Create a new Python component instance
731 * C++ method: create a servant instance of a component.
732 * \param genericRegisterName Name of the component instance to register
733 * in Registry & Name Service,
734 * (without _inst_n suffix, like "COMPONENT")
735 * \param handle loaded library handle
736 * \return a loaded component
738 * example with names:
739 * aGenRegisterName = COMPONENT (= first argument)
740 * _containerName = /Containers/cli76ce/FactoryServer
741 * factoryName = COMPONENTEngine_factory
742 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
743 * instanceName = COMPONENT_inst_1
744 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
746 //=============================================================================
747 Engines::EngineComponent_ptr
748 Engines_Parallel_Container_i::createPythonInstance(std::string genericRegisterName)
751 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil();
753 int numInstance = _numInstance;
755 sprintf( aNumI , "%d" , numInstance ) ;
756 std::string instanceName = genericRegisterName + "_inst_" + aNumI ;
757 std::string component_registerName = _containerName + "/" + instanceName;
759 Py_ACQUIRE_NEW_THREAD;
760 PyObject *mainmod = PyImport_AddModule("__main__");
761 PyObject *globals = PyModule_GetDict(mainmod);
762 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
763 PyObject *result = PyObject_CallMethod(pyCont,
764 (char*)"create_component_instance",
766 genericRegisterName.c_str(),
767 instanceName.c_str());
770 PyArg_ParseTuple(result,"ss", &ior, &error);
773 Py_RELEASE_NEW_THREAD;
777 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
778 iobject = Engines::EngineComponent::_narrow(obj);
779 _listInstances_map[instanceName] = iobject;
782 std::cerr << "createPythonInstance ior is empty ! Error in creation" << std::endl;
784 return iobject._retn();
787 //=============================================================================
788 //! Create a new CPP component instance
790 * C++ method: create a servant instance of a component.
791 * \param genericRegisterName Name of the component instance to register
792 * in Registry & Name Service,
793 * (without _inst_n suffix, like "COMPONENT")
794 * \param handle loaded library handle
795 * \return a loaded component
797 * example with names:
798 * aGenRegisterName = COMPONENT (= first argument)
799 * _containerName = /Containers/cli76ce/FactoryServer
800 * factoryName = COMPONENTEngine_factory
801 * component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
802 * instanceName = COMPONENT_inst_1
803 * component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
805 //=============================================================================
806 Engines::EngineComponent_ptr
807 Engines_Parallel_Container_i::createCPPInstance(std::string genericRegisterName,
810 MESSAGE("Entering Engines_Parallel_Container_i::createCPPInstance");
812 // --- find the factory
814 std::string aGenRegisterName = genericRegisterName;
815 std::string factory_name = aGenRegisterName + string("Engine_factory");
817 typedef PortableServer::ObjectId * (*FACTORY_FUNCTION_2)
819 PortableServer::POA_ptr,
820 PortableServer::ObjectId *,
824 FACTORY_FUNCTION_2 Component_factory = NULL;
826 Component_factory = (FACTORY_FUNCTION_2)dlsym( handle, factory_name.c_str() );
828 Component_factory = (FACTORY_FUNCTION_2)GetProcAddress( (HINSTANCE)handle, factory_name.c_str() );
831 if (!Component_factory)
833 INFOS("Can't resolve symbol: " + factory_name);
835 INFOS("dlerror() result is : " << dlerror());
837 return Engines::EngineComponent::_nil() ;
840 // --- create instance
841 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
844 int numInstance = _numInstance;
846 sprintf( aNumI , "%d" , numInstance );
847 std::string instanceName = aGenRegisterName + "_inst_" + aNumI;
848 std::string component_registerName =
849 _containerName + "/" + instanceName;
851 // --- Instanciate required CORBA object
853 PortableServer::ObjectId *id; //not owner, do not delete (nore use var)
854 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(),
855 aGenRegisterName.c_str() );
858 INFOS("Factory function returns NULL !");
859 return iobject._retn();
862 // --- get reference from id
863 CORBA::Object_var obj = _poa->id_to_reference(*id);
864 iobject = Engines::EngineComponent::_narrow(obj);
866 _listInstances_map[instanceName] = iobject;
867 _cntInstances_map[aGenRegisterName] += 1;
869 // --- register the engine under the name
870 // containerName(.dir)/instanceName(.object)
871 _NS->Register(iobject , component_registerName.c_str());
872 MESSAGE( component_registerName.c_str() << " bound" );
876 INFOS( "Container_i::createInstance exception catched" );
878 return iobject._retn();
882 Engines_Parallel_Container_i::create_paco_component_node_instance(const char* componentName,
883 const char* proxy_containerName)
885 // Init de la méthode
887 Engines::EngineComponent_PaCO_var work_node;
888 std::string aCompName = componentName;
889 std::string _proxy_containerName = proxy_containerName;
892 string impl_name = string ("lib") + aCompName +string("Engine.so");
894 string impl_name = aCompName +string("Engine.dll");
896 void* handle = _library_map[impl_name];
897 _numInstanceMutex.lock() ; // lock on the instance number
899 int numInstance = _numInstance ;
900 _numInstanceMutex.unlock() ;
902 sprintf( aNumI , "%d" , numInstance ) ;
903 string instanceName = aCompName + "_inst_" + aNumI ;
905 // Step 1 : Get proxy !
906 string component_registerName = _proxy_containerName + "/" + instanceName;
907 CORBA::Object_var temp = _NS->Resolve(component_registerName.c_str());
908 Engines::EngineComponent_var obj_proxy = Engines::EngineComponent::_narrow(temp);
909 if (CORBA::is_nil(obj_proxy))
911 INFOS("Proxy reference from NamingService is nil !");
912 INFOS("Proxy name was : " << component_registerName);
913 SALOME::ExceptionStruct es;
914 es.type = SALOME::INTERNAL_ERROR;
915 es.text = "Proxy reference from NamingService is nil !";
916 throw SALOME::SALOME_Exception(es);
918 proxy_ior = _orb->object_to_string(obj_proxy);
921 string factory_name = aCompName + string("Engine_factory");
922 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
923 if (!Component_factory)
925 INFOS("Can't resolve symbol : " + factory_name);
927 INFOS("dlerror() result is : " << dlerror());
929 std::string ex_text = "Can't resolve symbol : " + factory_name;
930 SALOME::ExceptionStruct es;
931 es.type = SALOME::INTERNAL_ERROR;
932 es.text = CORBA::string_dup(ex_text.c_str());
933 throw SALOME::SALOME_Exception(es);
939 sprintf(aNumI2 , "%d" , getMyRank()) ;
940 std::string instanceName = aCompName + "_inst_" + aNumI + "_work_node_" + aNumI2;
941 std::string component_registerName = _containerName + "/" + instanceName;
943 // --- Instanciate work node
944 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
945 id = (Component_factory) (_orb, proxy_ior, getMyRank(), _poa, _id, instanceName.c_str(), componentName);
946 CORBA::string_free(proxy_ior);
948 // --- get reference from id
949 CORBA::Object_var obj = _poa->id_to_reference(*id);
950 work_node = Engines::EngineComponent_PaCO::_narrow(obj) ;
951 if (CORBA::is_nil(work_node))
953 INFOS("work_node reference from factory is nil !");
954 SALOME::ExceptionStruct es;
955 es.type = SALOME::INTERNAL_ERROR;
956 es.text = "work_node reference from factory is nil !";
957 throw SALOME::SALOME_Exception(es);
960 _NS->Register(work_node, component_registerName.c_str());
961 _par_obj_inst_map[instanceName] = id;
962 MESSAGE(component_registerName.c_str() << " bound" );
966 INFOS("Container_i::create_paco_component_node_instance exception catched");
967 SALOME::ExceptionStruct es;
968 es.type = SALOME::INTERNAL_ERROR;
969 es.text = "Container_i::create_paco_component_node_instance exception catched";
970 throw SALOME::SALOME_Exception(es);
974 //=============================================================================
975 //! Decrement component instance reference count
979 //=============================================================================
981 void Engines_Parallel_Container_i::decInstanceCnt(std::string genericRegisterName)
983 if(_cntInstances_map.count(genericRegisterName) !=0 )
985 std::string aGenRegisterName =genericRegisterName;
986 MESSAGE("Engines_Parallel_Container_i::decInstanceCnt " << aGenRegisterName);
987 ASSERT(_cntInstances_map[aGenRegisterName] > 0);
988 _numInstanceMutex.lock(); // lock to be alone
989 // (see finalize_removal, load_component_Library)
990 _cntInstances_map[aGenRegisterName] -= 1;
991 SCRUTE(_cntInstances_map[aGenRegisterName]);
992 if (_cntInstances_map[aGenRegisterName] == 0)
994 std::string impl_name =
995 Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str());
997 void* handle = _library_map[impl_name];
999 _toRemove_map[impl_name] = handle;
1001 _numInstanceMutex.unlock();
1005 //=============================================================================
1006 //! Indicate if container is a python one
1008 * Retrieves only with container naming convention if it is a python container
1010 //=============================================================================
1012 bool Engines_Parallel_Container_i::isPythonContainer(const char* ContainerName)
1019 // Cette méthode permet de tenir à jour le compteur des
1020 // instances pour le container parallèle.
1021 // En effet losrque l'on charge un composant séquentielle seul
1022 // le compteur du noeud 0 est augmenté, il faut donc tenir les autres
1025 Engines_Parallel_Container_i::updateInstanceNumber()
1027 if (getMyRank() != 0)
1029 _numInstanceMutex.lock();
1031 _numInstanceMutex.unlock();
1035 /*! \brief copy a file from a remote host (container) to the local host
1036 * \param container the remote container
1037 * \param remoteFile the file to copy locally from the remote host into localFile
1038 * \param localFile the local file
1041 Engines_Parallel_Container_i::copyFile(Engines::Container_ptr container, const char* remoteFile, const char* localFile)
1043 Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
1046 if ((fp = fopen(localFile,"wb")) == NULL)
1048 INFOS("file " << localFile << " cannot be open for writing");
1052 CORBA::Long fileId = fileTransfer->open(remoteFile);
1055 Engines::fileBlock* aBlock;
1062 aBlock = fileTransfer->getBlock(fileId);
1063 toFollow = aBlock->length();
1065 CORBA::Octet *buf = aBlock->get_buffer();
1066 fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
1070 MESSAGE("end of transfer");
1071 fileTransfer->close(fileId);
1075 INFOS("open reference file for copy impossible");
1079 /*! \brief create a PyNode object to execute remote python code
1080 * \param nodeName the name of the node
1081 * \param code the python code to load
1082 * \return the PyNode
1085 Engines_Parallel_Container_i::createPyNode(const char* nodeName, const char* code)
1087 INFOS("Python component not yet implemented");
1088 Engines::PyNode_var node= Engines::PyNode::_nil();
1089 return node._retn();
1092 Engines::PyNode_ptr Engines_Parallel_Container_i::getDefaultPyNode(const char *nodeName)
1094 INFOS("Python component not yet implemented");
1095 return Engines::PyNode::_nil();
1098 Engines::PyScriptNode_ptr
1099 Engines_Parallel_Container_i::createPyScriptNode(const char* nodeName, const char* cod)
1101 INFOS("Python script node not yet implemented");
1102 Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil();
1103 return node._retn();
1106 Engines::PyScriptNode_ptr Engines_Parallel_Container_i::getDefaultPyScriptNode(const char *nodeName)
1108 INFOS("Python script node not yet implemented");
1109 return Engines::PyScriptNode::_nil();
1112 //=============================================================================
1116 //=============================================================================
1118 void ActSigIntHandler()
1121 struct sigaction SigIntAct ;
1122 SigIntAct.sa_sigaction = &SigIntHandler ;
1123 SigIntAct.sa_flags = SA_SIGINFO ;
1126 // DEBUG 03.02.2005 : the first parameter of sigaction is not a mask of signals
1127 // (SIGINT | SIGUSR1) :
1128 // it must be only one signal ===> one call for SIGINT
1129 // and an other one for SIGUSR1
1132 if ( sigaction( SIGINT , &SigIntAct, NULL ) ) {
1133 perror("SALOME_Container main ") ;
1136 if ( sigaction( SIGUSR1 , &SigIntAct, NULL ) ) {
1137 perror("SALOME_Container main ") ;
1140 if ( sigaction( SIGUSR2 , &SigIntAct, NULL ) )
1142 perror("SALOME_Container main ") ;
1146 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1147 // use of streams (and so on) should never be used because :
1148 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1149 // A stream operation may be interrupted by a signal and if the Handler use stream we
1150 // may have a "Dead-Lock" ===HangUp
1151 //==INFOS is commented
1152 // INFOS(pthread_self() << "SigIntHandler activated") ;
1154 signal( SIGINT, SigIntHandler );
1155 signal( SIGUSR1, SigIntHandler );
1161 void CallCancelThread();
1164 void SigIntHandler(int what , siginfo_t * siginfo ,
1166 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1167 // use of streams (and so on) should never be used because :
1168 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1169 // A stream operation may be interrupted by a signal and if the Handler use stream we
1170 // may have a "Dead-Lock" ===HangUp
1171 //==MESSAGE is commented
1172 // MESSAGE(pthread_self() << "SigIntHandler what " << what << endl
1173 // << " si_signo " << siginfo->si_signo << endl
1174 // << " si_code " << siginfo->si_code << endl
1175 // << " si_pid " << siginfo->si_pid) ;
1178 // MESSAGE("SigIntHandler END sleeping.") ;
1182 ActSigIntHandler() ;
1183 if ( siginfo->si_signo == SIGUSR1 ) {
1186 else if ( siginfo->si_signo == SIGUSR2 )
1188 CallCancelThread() ;
1192 // MESSAGE("SigIntHandler BEGIN sleeping.") ;
1194 while( _Sleeping ) {
1198 // MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1204 void SigIntHandler( int what ) {
1205 MESSAGE( pthread_self() << "SigIntHandler what " << what << endl );
1208 MESSAGE("SigIntHandler END sleeping.") ;
1212 ActSigIntHandler() ;
1213 if ( what == SIGUSR1 ) {
1218 MESSAGE("SigIntHandler BEGIN sleeping.") ;
1220 while( _Sleeping ) {
1224 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;