1 // Copyright (C) 2007-2021 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
23 // SALOME Container : implementation of container and engine for Kernel
24 // File : Container_i.cxx
25 // Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
28 //#define private public
33 #include <sys/types.h>
46 #include "utilities.h"
47 #include <SALOMEconfig.h>
48 #include CORBA_SERVER_HEADER(SALOME_Component)
49 #include CORBA_SERVER_HEADER(SALOME_Exception)
50 #include <pthread.h> // must be before Python.h !
51 #include "SALOME_Container_i.hxx"
52 #include "SALOME_Component_i.hxx"
53 #include "SALOME_FileRef_i.hxx"
54 #include "SALOME_FileTransfer_i.hxx"
55 #include "Salome_file_i.hxx"
56 #include "SALOME_NamingService.hxx"
57 #include "SALOME_Fake_NamingService.hxx"
58 #include "SALOME_Embedded_NamingService_Client.hxx"
59 #include "Basics_Utils.hxx"
66 #include "Container_init_python.hxx"
68 bool _Sleeping = false ;
70 // // Needed by multi-threaded Python --- Supervision
74 extern "C" {void ActSigIntHandler() ; }
76 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
78 extern "C" {void SigIntHandler( int ) ; }
84 #define ENGINESO "Engine.dylib"
86 #define ENGINESO "Engine.so"
90 #define ENGINESO "Engine.dll"
101 std::map<std::string, int> Engines_Container_i::_cntInstances_map;
102 std::map<std::string, void *> Engines_Container_i::_library_map;
103 std::map<std::string, void *> Engines_Container_i::_toRemove_map;
104 omni_mutex Engines_Container_i::_numInstanceMutex ;
106 static PyObject* _pyCont;
108 int checkifexecutable(const std::string&);
109 int findpathof(const std::string& path, std::string&, const std::string&);
111 /*! \class Engines_Container_i
112 * \brief C++ implementation of Engines::Container interface
117 //=============================================================================
119 * Default constructor, not for use
121 //=============================================================================
123 Engines_Container_i::Engines_Container_i () :
124 _NS(0),_id(0),_numInstance(0)
128 //=============================================================================
132 //=============================================================================
134 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb,
135 PortableServer::POA_ptr poa,
136 char *containerName ,
137 int argc , char* argv[],
138 SALOME_NamingService_Container_Abstract *ns,
139 bool isServantAloneInProcess
141 _NS(nullptr),_id(0),_numInstance(0),_isServantAloneInProcess(isServantAloneInProcess)
143 _pid = (long)getpid();
151 std::string hostname = Kernel_Utils::GetHostname();
153 MESSAGE(hostname << " " << getpid() <<
154 " Engines_Container_i starting argc " <<
155 _argc << " Thread " << pthread_self() ) ;
157 MESSAGE(hostname << " " << _getpid() <<
158 " Engines_Container_i starting argc " << _argc<< " Thread " << pthread_self().p ) ;
164 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
170 INFOS("SALOME_Container usage : SALOME_Container ServerName");
174 _isSupervContainer = false;
176 _orb = CORBA::ORB::_duplicate(orb) ;
177 _poa = PortableServer::POA::_duplicate(poa) ;
179 // Pour les containers paralleles: il ne faut pas enregistrer et activer
180 // le container generique, mais le container specialise
183 _id = _poa->activate_object(this);
184 // key point : if ns is nullptr : this servant is alone in its process
185 // if ns is not null : this servant embedded into single process.
186 _NS = ns==nullptr ? new SALOME_NamingService : ns->clone();
187 _NS->init_orb( _orb ) ;
188 CORBA::Object_var obj=_poa->id_to_reference(*_id);
189 Engines::Container_var pCont = Engines::Container::_narrow(obj);
192 _containerName = SALOME_NamingService_Abstract::BuildContainerNameForNS(containerName, hostname.c_str());
193 SCRUTE(_containerName);
194 _NS->Register(pCont, _containerName.c_str());
195 MESSAGE("Engines_Container_i::Engines_Container_i : Container name " << _containerName);
198 // import SALOME_Container
199 // pycont = SALOME_Container.SALOME_Container_i(containerIORStr)
201 CORBA::String_var sior = _orb->object_to_string(pCont);
202 std::string myCommand="pyCont = SALOME_Container.SALOME_Container_i('";
203 myCommand += _containerName + "','";
208 //[RNV]: Comment the PyEval_AcquireLock() and PyEval_ReleaseLock() because this
209 //approach leads to the deadlock of the main thread of the application on Windows platform
210 //in case if cppContainer runs in the standalone mode. The problem with the PyThreadState
211 //described by ABN seems not reproduced, to be checked carefully later...
212 PyGILState_STATE gstate = PyGILState_Ensure();
214 //// [ABN]: using the PyGILState* API here is unstable. omniORB logic is invoked
215 //// by the Python code executed below, and in some (random) cases, the Python code
216 //// execution ends with a PyThreadState which was not the one we have here.
217 //// (TODO: understand why ...)
218 //// To be on the safe side we get and load the thread state ourselves:
219 //PyEval_AcquireLock(); // get GIL
220 //PyThreadState * mainThreadState = PyThreadState_Get();
221 //PyThreadState_Swap(mainThreadState);
224 // mpv: this is temporary solution: there is a unregular crash if not
227 // first element is the path to Registry.dll, but it's wrong
228 PyRun_SimpleString("import sys\n");
229 PyRun_SimpleString("sys.path = sys.path[1:]\n");
231 PyRun_SimpleString("import SALOME_Container\n");
232 PyRun_SimpleString((char*)myCommand.c_str());
233 PyObject *mainmod = PyImport_AddModule("__main__");
234 PyObject *globals = PyModule_GetDict(mainmod);
235 _pyCont = PyDict_GetItemString(globals, "pyCont");
237 //PyThreadState_Swap(NULL);
238 //PyEval_ReleaseLock();
239 PyGILState_Release(gstate);
241 fileTransfer_i* aFileTransfer = new fileTransfer_i();
242 CORBA::Object_var obref=aFileTransfer->_this();
243 _fileTransfer = Engines::fileTransfer::_narrow(obref);
244 aFileTransfer->_remove_ref();
248 //=============================================================================
252 //=============================================================================
254 Engines_Container_i::~Engines_Container_i()
256 MESSAGE("Container_i::~Container_i()");
264 //=============================================================================
265 //! Get container name
267 * CORBA attribute: Container name (see constructor)
269 //=============================================================================
271 char* Engines_Container_i::name()
273 return CORBA::string_dup(_containerName.c_str()) ;
276 //=============================================================================
277 //! Get container working directory
279 * CORBA attribute: Container working directory
281 //=============================================================================
283 char* Engines_Container_i::workingdir()
287 return CORBA::string_dup(wd) ;
290 //=============================================================================
291 //! Get container log file name
293 * CORBA attribute: Container log file name
295 //=============================================================================
297 char* Engines_Container_i::logfilename()
299 return CORBA::string_dup(_logfilename.c_str()) ;
302 //! Set container log file name
303 void Engines_Container_i::logfilename(const char* name)
308 //=============================================================================
309 //! Get container host name
311 * CORBA method: Get the hostName of the Container (without domain extensions)
313 //=============================================================================
315 char* Engines_Container_i::getHostName()
317 std::string s = Kernel_Utils::GetHostname();
318 // MESSAGE("Engines_Container_i::getHostName " << s);
319 return CORBA::string_dup(s.c_str()) ;
322 //=============================================================================
323 //! Get container PID
325 * CORBA method: Get the PID (process identification) of the Container
327 //=============================================================================
329 CORBA::Long Engines_Container_i::getPID()
331 return (CORBA::Long)getpid();
334 //=============================================================================
335 //! Ping the servant to check it is still alive
337 * CORBA method: check if servant is still alive
339 //=============================================================================
340 void Engines_Container_i::ping()
342 MESSAGE("Engines_Container_i::ping() pid "<< getpid());
345 //=============================================================================
346 //! Shutdown the container
348 * CORBA method, oneway: Server shutdown.
349 * - Container name removed from naming service,
350 * - servant deactivation,
351 * - orb shutdown if no other servants in the process
353 //=============================================================================
354 void Engines_Container_i::Shutdown()
356 MESSAGE("Engines_Container_i::Shutdown()");
358 // Clear registered temporary files
359 clearTemporaryFiles();
361 /* For each component contained in this container
362 * tell it to self-destroy
364 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
365 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
369 itm->second->destroy();
371 catch(const CORBA::Exception&)
373 // ignore this entry and continue
377 // ignore this entry and continue
380 _listInstances_map.clear();
382 // NS unregistering may throw in SSL mode if master process hosting SALOME_Embedded_NamingService servant has vanished
383 // In this case it's skip it and still continue.
386 _NS->Destroy_FullDirectory(_containerName.c_str());
387 _NS->Destroy_Name(_containerName.c_str());
393 if(_isServantAloneInProcess)
395 MESSAGE("Effective Shutdown of container Begins...");
396 if(!CORBA::is_nil(_orb))
401 //=============================================================================
402 //! load a component implementation
405 * \param componentName component name
406 * \param reason explains error when load fails
407 * \return true if dlopen successful or already done, false otherwise
409 //=============================================================================
411 Engines_Container_i::load_component_Library(const char* componentName, CORBA::String_out reason)
414 //=================================================================
415 // --- C++ implementation section
416 //=================================================================
418 if(load_component_CppImplementation(componentName,retso))
420 reason=CORBA::string_dup("");
423 else if(retso != "ImplementationNotFound")
425 reason=CORBA::string_dup(retso.c_str());
430 retso+=componentName;
431 retso+=": Can't find C++ implementation ";
432 retso+=std::string(LIB) + componentName + ENGINESO;
434 //=================================================================
435 // --- Python implementation section
436 //=================================================================
438 if(load_component_PythonImplementation(componentName,retpy))
440 reason=CORBA::string_dup("");
443 else if(retpy != "ImplementationNotFound")
445 reason=CORBA::string_dup(retpy.c_str());
450 retpy+=componentName;
451 retpy+=": Can't find python implementation ";
452 retpy+=componentName;
455 //=================================================================
456 // -- Executable implementation section
457 //=================================================================
459 if(load_component_ExecutableImplementation(componentName,retex))
461 reason=CORBA::string_dup("");
464 else if(retex != "ImplementationNotFound")
466 reason=CORBA::string_dup(retex.c_str());
471 retex+=componentName;
472 retex+=": Can't find executable implementation ";
473 retex+=componentName;
476 std::string ret="Component implementation not found: ";
477 ret += componentName ;
483 std::cerr << ret << std::endl;
484 reason=CORBA::string_dup(ret.c_str());
489 //=============================================================================
490 //! try to load a C++ component implementation
493 * \param componentName the name of the component (COMPONENT, for example)
494 * \param reason explains error when load fails
495 * \return true if loading is successful or already done, false otherwise
497 //=============================================================================
499 Engines_Container_i::load_component_CppImplementation(const char* componentName, std::string& reason)
501 std::string aCompName(componentName);
502 std::string impl_name = std::string(LIB) + aCompName + ENGINESO;
505 _numInstanceMutex.lock(); // lock to be alone
506 // (see decInstanceCnt, finalize_removal))
507 if (_toRemove_map.count(impl_name) != 0) _toRemove_map.erase(impl_name);
508 if (_library_map.count(impl_name) != 0)
510 MESSAGE("Library " << impl_name << " already loaded");
511 _numInstanceMutex.unlock();
515 _numInstanceMutex.unlock();
519 handle = dlopen( impl_name.c_str() , RTLD_NOW | RTLD_GLOBAL ) ;
522 //not loadable. Try to find the lib file in LD_LIBRARY_PATH
525 char* p=getenv("DYLD_LIBRARY_PATH");
527 char* p=getenv("LD_LIBRARY_PATH");
530 path=path+SEP+"/usr/lib"+SEP+"/lib";
533 if(findpathof(path, pth, impl_name))
535 //found but not loadable
538 reason+=": C++ implementation found ";
540 reason+=" but it is not loadable. Error:\n";
542 std::cerr << reason << std::endl;
548 //continue with other implementation
549 reason="ImplementationNotFound";
556 std::wstring libToLoad = Kernel_Utils::utf8_decode_s( impl_name );
558 std::string libToLoad = impl_name;
560 handle = LoadLibrary(libToLoad.c_str() );
563 reason="ImplementationNotFound";
569 _numInstanceMutex.lock();
570 _library_map[impl_name] = handle;
571 _numInstanceMutex.unlock();
579 //=============================================================================
580 //! try to load a Python component implementation
583 * \param componentName name of the component
584 * \param reason explains error when load fails
585 * \return true if loading is successful or already done, false otherwise
587 //=============================================================================
589 Engines_Container_i::load_component_PythonImplementation(const char* componentName, std::string& reason)
591 std::string aCompName(componentName);
593 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
594 if (_library_map.count(aCompName) != 0)
596 _numInstanceMutex.unlock() ;
598 return true; // Python Component, already imported
600 _numInstanceMutex.unlock() ;
602 PyGILState_STATE gstate = PyGILState_Ensure();
603 PyObject *result = PyObject_CallMethod(_pyCont,
604 (char*)"import_component",
605 (char*)"s",componentName);
607 reason=PyUnicode_AsUTF8(result);
610 PyGILState_Release(gstate);
614 //Python component has been loaded (import componentName)
615 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
616 _library_map[aCompName] = (void *)_pyCont; // any non O value OK
617 _numInstanceMutex.unlock() ;
618 MESSAGE("import Python: "<< aCompName <<" OK");
621 else if(reason=="ImplementationNotFound")
623 //Python implementation has not been found. Continue with other implementation
624 reason="ImplementationNotFound";
628 //Python implementation has been found but loading has failed
629 std::cerr << reason << std::endl;
634 //=============================================================================
635 //! try to load a Executable component implementation
638 * \param componentName name of the component
639 * \param reason explains error when load fails
640 * \return true if loading is successful or already done, false otherwise
642 //=============================================================================
644 Engines_Container_i::load_component_ExecutableImplementation(const char* componentName, std::string& reason)
646 std::string aCompName(componentName);
647 std::string executable=aCompName+".exe";
652 char* p=getenv("PATH");
655 if (findpathof(path, pth, executable))
657 if(checkifexecutable(pth))
659 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
660 _library_map[executable] = (void *)1; // any non O value OK
661 _numInstanceMutex.unlock() ;
662 MESSAGE("import executable: "<< pth <<" OK");
668 reason+=": implementation found ";
670 reason+=" but it is not executable";
671 std::cerr << reason << std::endl;
675 reason="ImplementationNotFound";
680 //=============================================================================
681 //! Create a new component instance
683 * CORBA method: Creates a new servant instance of a component.
684 * The servant registers itself to naming service and Registry.
685 * \param genericRegisterName Name of the component instance to register
686 * in Registry & Name Service (without _inst_n suffix)
687 * \return a loaded component
689 //=============================================================================
690 Engines::EngineComponent_ptr
691 Engines_Container_i::create_component_instance(const char*genericRegisterName)
693 Engines::FieldsDict_var env = new Engines::FieldsDict;
695 Engines::EngineComponent_ptr compo =
696 create_component_instance_env(genericRegisterName, env, reason);
697 CORBA::string_free(reason);
701 //=============================================================================
702 //! Create a new component instance with environment variables specified
704 * CORBA method: Creates a new servant instance of a component.
705 * The servant registers itself to naming service and Registry.
706 * \param genericRegisterName Name of the component instance to register
707 * in Registry & Name Service (without _inst_n suffix)
708 * \param env dict of env variables
709 * \param reason explains error when create_component_instance_env fails
710 * \return a loaded component
712 //=============================================================================
713 Engines::EngineComponent_ptr
714 Engines_Container_i::create_component_instance_env(const char*genericRegisterName,
715 const Engines::FieldsDict& env,
716 CORBA::String_out reason)
719 if (_library_map.count(genericRegisterName) != 0)
721 // It's a Python component
722 Engines::EngineComponent_ptr compo = createPythonInstance(genericRegisterName, error);
723 reason=CORBA::string_dup(error.c_str());
727 std::string impl_name = std::string(LIB) + genericRegisterName + ENGINESO;
728 if (_library_map.count(impl_name) != 0)
730 // It's a C++ component
731 void* handle = _library_map[impl_name];
732 Engines::EngineComponent_ptr compo = createInstance(genericRegisterName, handle, error);
733 reason=CORBA::string_dup(error.c_str());
737 impl_name = std::string(genericRegisterName) + ".exe";
738 if (_library_map.count(impl_name) != 0)
740 //It's an executable component
741 Engines::EngineComponent_ptr compo = createExecutableInstance(genericRegisterName, env, error);
742 reason=CORBA::string_dup(error.c_str());
746 error="load_component_Library has probably not been called for component: ";
747 error += genericRegisterName;
749 reason=CORBA::string_dup(error.c_str());
750 return Engines::EngineComponent::_nil() ;
753 //=============================================================================
754 //! Create a new component instance (Executable implementation)
756 * \param CompName Name of the component instance
757 * \param env dict of env variables
758 * \param reason explains error when creation fails
759 * \return a loaded component
761 * This component is implemented in an executable with name genericRegisterName.exe
762 * It must register itself in Naming Service. The container waits some time (10 s max)
765 //=============================================================================
766 Engines::EngineComponent_ptr
767 Engines_Container_i::createExecutableInstance(std::string CompName,
768 const Engines::FieldsDict& env,
771 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
773 _numInstanceMutex.lock() ; // lock on the instance number
775 int numInstance = _numInstance ;
776 _numInstanceMutex.unlock() ;
779 sprintf( aNumI , "%d" , numInstance ) ;
780 std::string instanceName = CompName + "_inst_" + aNumI ;
781 std::string component_registerName = _containerName + "/" + instanceName;
783 //check if an entry exist in naming service
784 CORBA::Object_var nsobj = _NS->Resolve(component_registerName.c_str());
785 if ( !CORBA::is_nil(nsobj) )
787 // unregister the registered component
788 _NS->Destroy_Name(component_registerName.c_str());
789 //kill or shutdown it ???
792 // first arg container ior string
793 // second arg container name
794 // third arg instance name
796 Engines::Container_var pCont= _this();
797 CORBA::String_var sior = _orb->object_to_string(pCont);
801 command+=instanceName;
803 command+=instanceName;
808 command+= sior; // container ior string
810 command+=_containerName; //container name
812 command+=instanceName; //instance name
814 MESSAGE("SALOME_Container::create_component_instance command=" << command);
817 // use fork/execl instead of system to get finer control on env variables
820 if(pid == 0) // child
822 for (CORBA::ULong i=0; i < env.length(); i++)
824 if (env[i].value.type()->kind() == CORBA::tk_string)
827 env[i].value >>= value;
828 std::string s(env[i].key);
831 putenv(strdup(s.c_str()));
835 execl("/bin/sh", "sh", "-c", command.c_str() , (char *)0);
838 else if(pid < 0) // failed to fork
847 tpid = wait(&status);
848 } while (tpid != pid);
851 // launch component with a system call
852 int status=system(command.c_str());
857 reason="SALOME_Container::create_component_instance system failed (system command status -1)";
859 return Engines::EngineComponent::_nil();
862 else if (WEXITSTATUS(status) == 217)
864 reason="SALOME_Container::create_component_instance system failed (system command status 217)";
866 return Engines::EngineComponent::_nil();
872 if (getenv("TIMEOUT_TO_WAIT_EXE_COMPONENT") != 0)
874 std::string new_count_str = getenv("TIMEOUT_TO_WAIT_EXE_COMPONENT");
876 std::istringstream ss(new_count_str);
877 if (!(ss >> new_count))
879 INFOS("[Container] TIMEOUT_TO_WAIT_EXE_COMPONENT should be an int");
884 INFOS("[Container] waiting " << count << " second steps exe component ");
885 CORBA::Object_var obj = CORBA::Object::_nil() ;
886 while ( CORBA::is_nil(obj) && count )
894 MESSAGE( count << ". Waiting for component " << CompName);
895 obj = _NS->Resolve(component_registerName.c_str());
898 if(CORBA::is_nil(obj))
900 reason="SALOME_Container::create_component_instance failed";
902 return Engines::EngineComponent::_nil();
906 MESSAGE("SALOME_Container::create_component_instance successful");
907 iobject = Engines::EngineComponent::_narrow(obj);
908 _listInstances_map[instanceName] = iobject;
909 return iobject._retn();
915 //=============================================================================
916 //! Create a new component instance (Python implementation)
918 * \param CompName Name of the component instance
919 * \param reason explains error when creation fails
920 * \return a loaded component
922 //=============================================================================
923 Engines::EngineComponent_ptr
924 Engines_Container_i::createPythonInstance(std::string CompName,
927 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
929 _numInstanceMutex.lock() ; // lock on the instance number
931 int numInstance = _numInstance ;
932 _numInstanceMutex.unlock() ;
935 sprintf( aNumI , "%d" , numInstance ) ;
936 std::string instanceName = CompName + "_inst_" + aNumI ;
937 std::string component_registerName = _containerName + "/" + instanceName;
939 PyGILState_STATE gstate = PyGILState_Ensure();
940 PyObject *result = PyObject_CallMethod(_pyCont,
941 (char*)"create_component_instance",
944 instanceName.c_str());
947 PyArg_ParseTuple(result,"ss", &ior, &error);
948 std::string iors = ior;
951 PyGILState_Release(gstate);
955 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
956 iobject = Engines::EngineComponent::_narrow( obj ) ;
957 _listInstances_map[instanceName] = iobject;
959 return iobject._retn();
963 Engines_Container_i::create_python_service_instance(const char * CompName,
964 CORBA::String_out reason)
966 CORBA::Object_var object = CORBA::Object::_nil();
968 _numInstanceMutex.lock() ; // lock on the instance number
970 int numInstance = _numInstance ;
971 _numInstanceMutex.unlock() ;
974 sprintf( aNumI , "%d" , numInstance ) ;
975 std::string instanceName = std::string(CompName) + "_inst_" + aNumI ;
976 std::string component_registerName = _containerName + "/" + instanceName;
978 PyGILState_STATE gstate = PyGILState_Ensure();
979 PyObject *result = PyObject_CallMethod(_pyCont,
980 (char*)"create_component_instance",
983 instanceName.c_str());
986 PyArg_ParseTuple(result,"ss", &ior, &error);
987 reason = CORBA::string_dup(error);
988 char * _ior = CORBA::string_dup(ior);
990 PyGILState_Release(gstate);
996 //=============================================================================
997 //! Create a new component instance (C++ implementation)
999 * C++ method: create a servant instance of a component.
1000 * \param genericRegisterName Name of the component instance to register
1001 * in Registry & Name Service,
1002 * (without _inst_n suffix, like "COMPONENT")
1003 * \param handle loaded library handle
1004 * \param reason explains error when creation fails
1005 * \return a loaded component
1007 * example with names:
1008 * - aGenRegisterName = COMPONENT (= first argument)
1009 * - _containerName = /Containers/cli76ce/FactoryServer
1010 * - factoryName = COMPONENTEngine_factory
1011 * - component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
1012 * - instanceName = COMPONENT_inst_1
1013 * - component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
1015 //=============================================================================
1016 Engines::EngineComponent_ptr
1017 Engines_Container_i::createInstance(std::string genericRegisterName,
1019 std::string& reason)
1021 // --- find the factory
1023 std::string aGenRegisterName = genericRegisterName;
1024 std::string factory_name = aGenRegisterName + std::string("Engine_factory");
1025 SCRUTE(factory_name) ;
1027 typedef PortableServer::ObjectId* (*FACTORY_FUNCTION) (CORBA::ORB_ptr,
1028 PortableServer::POA_ptr,
1029 PortableServer::ObjectId *,
1034 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION)dlsym( handle, factory_name.c_str() );
1036 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION)GetProcAddress( (HINSTANCE)handle, factory_name.c_str() );
1039 if ( !Component_factory )
1041 MESSAGE( "Can't resolve symbol: " + factory_name );
1046 return Engines::EngineComponent::_nil() ;
1049 // --- create instance
1051 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
1055 _numInstanceMutex.lock() ; // lock on the instance number
1057 int numInstance = _numInstance ;
1058 _numInstanceMutex.unlock() ;
1061 sprintf( aNumI , "%d" , numInstance ) ;
1062 std::string instanceName = aGenRegisterName + "_inst_" + aNumI ;
1063 std::string component_registerName =
1064 _containerName + "/" + instanceName;
1066 // --- Instantiate required CORBA object
1068 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
1069 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(),
1070 aGenRegisterName.c_str() ) ;
1073 reason="Can't get ObjectId from factory";
1075 return iobject._retn();
1078 // --- get reference from id
1080 CORBA::Object_var obj = _poa->id_to_reference(*id);
1081 iobject = Engines::EngineComponent::_narrow( obj ) ;
1083 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1084 _listInstances_map[instanceName] = iobject;
1085 _cntInstances_map[aGenRegisterName] += 1;
1086 _numInstanceMutex.unlock() ;
1087 SCRUTE(aGenRegisterName);
1088 SCRUTE(_cntInstances_map[aGenRegisterName]);
1090 // --- register the engine under the name
1091 // containerName(.dir)/instanceName(.object)
1093 _NS->Register( iobject , component_registerName.c_str() ) ;
1094 MESSAGE( component_registerName.c_str() << " bound" ) ;
1098 reason="Container_i::createInstance exception caught";
1101 return iobject._retn();
1104 //=============================================================================
1105 //! Find an existing (in the container) component instance
1107 * CORBA method: Finds a servant instance of a component
1108 * \param registeredName Name of the component in Registry or Name Service,
1109 * without instance suffix number
1110 * \return the first found instance
1112 //=============================================================================
1113 Engines::EngineComponent_ptr
1114 Engines_Container_i::find_component_instance( const char* registeredName)
1116 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_nil();
1117 std::map<std::string,Engines::EngineComponent_var>::iterator itm =_listInstances_map.begin();
1118 while (itm != _listInstances_map.end())
1120 std::string instance = (*itm).first;
1122 if (instance.find(registeredName) == 0)
1124 anEngine = (*itm).second;
1125 return anEngine._retn();
1129 return anEngine._retn();
1132 //=============================================================================
1133 //! Remove the component instance from container
1135 * CORBA method: Stops the component servant, and deletes all related objects
1136 * \param component_i Component to be removed
1138 //=============================================================================
1140 void Engines_Container_i::remove_impl(Engines::EngineComponent_ptr component_i)
1142 ASSERT(! CORBA::is_nil(component_i));
1143 std::string instanceName = component_i->instanceName() ;
1144 MESSAGE("unload component " << instanceName);
1145 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1146 _listInstances_map.erase(instanceName);
1147 _numInstanceMutex.unlock() ;
1148 component_i->destroy() ;
1149 _NS->Destroy_Name(instanceName.c_str());
1152 //=============================================================================
1153 //! Unload component libraries from the container
1155 * CORBA method: Discharges unused libraries from the container.
1157 //=============================================================================
1158 void Engines_Container_i::finalize_removal()
1160 MESSAGE("finalize unload : dlclose");
1161 _numInstanceMutex.lock(); // lock to be alone
1162 // (see decInstanceCnt, load_component_Library)
1163 std::map<std::string, void *>::iterator ith;
1164 for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
1166 void *handle = (*ith).second;
1167 std::string impl_name= (*ith).first;
1172 // dlclose(handle); // SALOME unstable after ...
1173 // _library_map.erase(impl_name);
1176 _toRemove_map.clear();
1177 _numInstanceMutex.unlock();
1180 //=============================================================================
1181 //! Decrement component instance reference count
1185 //=============================================================================
1186 void Engines_Container_i::decInstanceCnt(std::string genericRegisterName)
1188 if(_cntInstances_map.count(genericRegisterName)==0)
1190 std::string aGenRegisterName =genericRegisterName;
1191 MESSAGE("Engines_Container_i::decInstanceCnt " << aGenRegisterName);
1192 ASSERT(_cntInstances_map[aGenRegisterName] > 0);
1193 _numInstanceMutex.lock(); // lock to be alone
1194 // (see finalize_removal, load_component_Library)
1195 _cntInstances_map[aGenRegisterName] -= 1;
1196 SCRUTE(_cntInstances_map[aGenRegisterName]);
1197 if (_cntInstances_map[aGenRegisterName] == 0)
1199 std::string impl_name =
1200 Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str());
1202 void* handle = _library_map[impl_name];
1204 _toRemove_map[impl_name] = handle;
1206 _numInstanceMutex.unlock();
1209 //=============================================================================
1210 //! Find or create a new component instance
1212 * CORBA method: find or create an instance of the component (servant),
1213 * load a new component class (dynamic library) if required,
1215 * ---- FOR COMPATIBILITY WITH 2.2 ----
1217 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
1219 * The servant registers itself to naming service and Registry.
1220 * \param genericRegisterName Name of the component to register
1221 * in Registry & Name Service
1222 * \param componentName Name of the constructed library of the component
1223 * \return a loaded component
1225 //=============================================================================
1227 Engines::EngineComponent_ptr
1228 Engines_Container_i::load_impl( const char* genericRegisterName,
1229 const char* /*componentName*/ )
1232 std::string impl_name = std::string(LIB) + genericRegisterName + ENGINESO;
1233 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
1234 if (load_component_Library(genericRegisterName,reason))
1235 iobject = find_or_create_instance(genericRegisterName, impl_name);
1236 CORBA::string_free(reason);
1237 return iobject._retn();
1240 Engines::EmbeddedNamingService_ptr Engines_Container_i::get_embedded_NS_if_ssl()
1242 SALOME_Embedded_NamingService_Client *nsc(dynamic_cast<SALOME_Embedded_NamingService_Client *>(this->_NS));
1245 Engines::EmbeddedNamingService_var obj = nsc->GetObject();
1246 return Engines::EmbeddedNamingService::_duplicate(obj);
1250 return Engines::EmbeddedNamingService::_nil();
1254 //=============================================================================
1255 //! Finds an already existing component instance or create a new instance
1257 * C++ method: Finds an already existing servant instance of a component, or
1258 * create an instance.
1259 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
1260 * \param genericRegisterName Name of the component instance to register
1261 * in Registry & Name Service,
1262 * (without _inst_n suffix, like "COMPONENT")
1263 * \param componentLibraryName like "libCOMPONENTEngine.so"
1264 * \return a loaded component
1266 * example with names:
1267 * - aGenRegisterName = COMPONENT (= first argument)
1268 * - impl_name = libCOMPONENTEngine.so (= second argument)
1269 * - _containerName = /Containers/cli76ce/FactoryServer
1270 * - factoryName = COMPONENTEngine_factory
1271 * - component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
1272 * - instanceName = COMPONENT_inst_1
1273 * - component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
1275 //=============================================================================
1277 Engines::EngineComponent_ptr
1278 Engines_Container_i::find_or_create_instance(std::string genericRegisterName,
1279 std::string componentLibraryName)
1281 std::string aGenRegisterName = genericRegisterName;
1282 std::string impl_name = componentLibraryName;
1283 if (_library_map.count(impl_name) == 0)
1285 INFOS("shared library " << impl_name <<" must be loaded before creating instance");
1286 return Engines::EngineComponent::_nil() ;
1290 // --- find a registered instance in naming service, or create
1292 void* handle = _library_map[impl_name];
1293 std::string component_registerBase =
1294 _containerName + "/" + aGenRegisterName;
1295 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
1299 CORBA::Object_var obj =
1300 _NS->ResolveFirst( component_registerBase.c_str());
1301 if ( CORBA::is_nil( obj ) )
1303 iobject = createInstance(genericRegisterName,
1309 iobject = Engines::EngineComponent::_narrow( obj ) ;
1314 INFOS( "Container_i::load_impl caught" ) ;
1316 return iobject._retn();
1320 //=============================================================================
1321 //! Indicate if container is a python one
1323 * Retrieves only with container naming convention if it is a python container
1325 //=============================================================================
1326 bool Engines_Container_i::isPythonContainer(const char* ContainerName)
1329 size_t len=strlen(ContainerName);
1331 if(strcmp(ContainerName+len-2,"Py")==0)
1336 //=============================================================================
1337 //! Kill the container
1339 * CORBA method: Kill the container process with exit(0).
1340 * To remove : never returns !
1342 //=============================================================================
1343 bool Engines_Container_i::Kill_impl()
1345 MESSAGE("Engines_Container_i::Kill() pid "<< getpid() << " containerName "
1346 << _containerName.c_str() << " machineName "
1347 << Kernel_Utils::GetHostname().c_str());
1348 INFOS("===============================================================");
1349 INFOS("= REMOVE calls to Kill_impl in C++ container =");
1350 INFOS("===============================================================");
1356 //=============================================================================
1360 //=============================================================================
1361 void ActSigIntHandler()
1364 struct sigaction SigIntAct ;
1365 SigIntAct.sa_sigaction = &SigIntHandler ;
1366 sigemptyset(&SigIntAct.sa_mask);
1367 SigIntAct.sa_flags = SA_SIGINFO ;
1370 // DEBUG 03.02.2005 : the first parameter of sigaction is not a mask of signals
1371 // (SIGINT | SIGUSR1) :
1372 // it must be only one signal ===> one call for SIGINT
1373 // and an other one for SIGUSR1
1376 if ( sigaction( SIGINT , &SigIntAct, NULL ) )
1378 perror("SALOME_Container main ") ;
1381 if ( sigaction( SIGUSR1 , &SigIntAct, NULL ) )
1383 perror("SALOME_Container main ") ;
1386 if ( sigaction( SIGUSR2 , &SigIntAct, NULL ) )
1388 perror("SALOME_Container main ") ;
1392 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1393 // use of streams (and so on) should never be used because :
1394 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1395 // A stream operation may be interrupted by a signal and if the Handler use stream we
1396 // may have a "Dead-Lock" ===HangUp
1397 //==INFOS is commented
1398 // INFOS(pthread_self() << "SigIntHandler activated") ;
1401 signal( SIGINT, SigIntHandler );
1402 // legacy code required to supervisor. Commented in order to avoid problems on Windows
1403 // signal( SIGUSR1, SigIntHandler );
1409 void CallCancelThread() ;
1412 void SigIntHandler(int /*what*/ ,
1413 siginfo_t * siginfo ,
1416 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1417 // use of streams (and so on) should never be used because :
1418 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1419 // A stream operation may be interrupted by a signal and if the Handler use stream we
1420 // may have a "Dead-Lock" ===HangUp
1421 //==MESSAGE is commented
1422 // MESSAGE(pthread_self() << "SigIntHandler what " << what << std::endl
1423 // << " si_signo " << siginfo->si_signo << std::endl
1424 // << " si_code " << siginfo->si_code << std::endl
1425 // << " si_pid " << siginfo->si_pid) ;
1430 // MESSAGE("SigIntHandler END sleeping.") ;
1435 ActSigIntHandler() ;
1436 if ( siginfo->si_signo == SIGUSR1 )
1440 else if ( siginfo->si_signo == SIGUSR2 )
1442 CallCancelThread() ;
1447 // MESSAGE("SigIntHandler BEGIN sleeping.") ;
1454 // MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1460 void SigIntHandler( int what )
1463 MESSAGE( pthread_self() << "SigIntHandler what " << what << std::endl );
1465 MESSAGE( "SigIntHandler what " << what << std::endl );
1470 MESSAGE("SigIntHandler END sleeping.") ;
1475 ActSigIntHandler() ;
1476 if ( what == SIGUSR1 )
1483 MESSAGE("SigIntHandler BEGIN sleeping.") ;
1490 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1497 //=============================================================================
1498 //! Get or create a file reference object associated to a local file (to transfer it)
1500 * CORBA method: get or create a fileRef object associated to a local file
1501 * (a file on the computer on which runs the container server), which stores
1502 * a list of (machine, localFileName) corresponding to copies already done.
1504 * \param origFileName absolute path for a local file to copy on other
1506 * \return a fileRef object associated to the file.
1508 //=============================================================================
1509 Engines::fileRef_ptr
1510 Engines_Container_i::createFileRef(const char* origFileName)
1512 std::string origName(origFileName);
1513 Engines::fileRef_var theFileRef = Engines::fileRef::_nil();
1515 if (origName[0] != '/')
1517 INFOS("path of file to copy must be an absolute path beginning with '/'");
1518 return Engines::fileRef::_nil();
1521 if (CORBA::is_nil(_fileRef_map[origName]))
1523 CORBA::Object_var obj=_poa->id_to_reference(*_id);
1524 Engines::Container_var pCont = Engines::Container::_narrow(obj);
1525 fileRef_i* aFileRef = new fileRef_i(pCont, origFileName);
1526 theFileRef = Engines::fileRef::_narrow(aFileRef->_this());
1527 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1528 _fileRef_map[origName] = theFileRef;
1529 _numInstanceMutex.unlock() ;
1532 theFileRef = Engines::fileRef::_duplicate(_fileRef_map[origName]);
1533 ASSERT(! CORBA::is_nil(theFileRef));
1534 return theFileRef._retn();
1537 //=============================================================================
1538 //! Get a fileTransfer reference
1541 * \return a reference to the fileTransfer object
1543 //=============================================================================
1544 Engines::fileTransfer_ptr
1545 Engines_Container_i::getFileTransfer()
1547 Engines::fileTransfer_var aFileTransfer
1548 = Engines::fileTransfer::_duplicate(_fileTransfer);
1549 return aFileTransfer._retn();
1552 //=============================================================================
1553 //! Create a Salome file
1554 //=============================================================================
1555 Engines::Salome_file_ptr
1556 Engines_Container_i::createSalome_file(const char* origFileName)
1558 std::string origName(origFileName);
1559 if (CORBA::is_nil(_Salome_file_map[origName]))
1561 Salome_file_i* aSalome_file = new Salome_file_i();
1562 aSalome_file->setContainer(Engines::Container::_duplicate(this->_this()));
1565 aSalome_file->setLocalFile(origFileName);
1566 aSalome_file->recvFiles();
1568 catch (const SALOME::SALOME_Exception& /*e*/) //!< TODO: unused variable
1570 return Engines::Salome_file::_nil();
1573 Engines::Salome_file_var theSalome_file = Engines::Salome_file::_nil();
1574 theSalome_file = Engines::Salome_file::_narrow(aSalome_file->_this());
1575 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1576 _Salome_file_map[origName] = theSalome_file;
1577 _numInstanceMutex.unlock() ;
1580 Engines::Salome_file_ptr theSalome_file =
1581 Engines::Salome_file::_duplicate(_Salome_file_map[origName]);
1582 ASSERT(!CORBA::is_nil(theSalome_file));
1583 return theSalome_file;
1586 //=============================================================================
1587 /*! \brief copy a file from a remote host (container) to the local host
1588 * \param container the remote container
1589 * \param remoteFile the file to copy locally from the remote host into localFile
1590 * \param localFile the local file
1592 //=============================================================================
1593 void Engines_Container_i::copyFile(Engines::Container_ptr container, const char* remoteFile, const char* localFile)
1595 Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
1598 if ((fp = fopen(localFile,"wb")) == NULL)
1600 INFOS("file " << localFile << " cannot be open for writing");
1604 CORBA::Long fileId = fileTransfer->open(remoteFile);
1607 Engines::fileBlock* aBlock;
1614 aBlock = fileTransfer->getBlock(fileId);
1615 toFollow = aBlock->length();
1617 CORBA::Octet *buf = aBlock->get_buffer();
1618 fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
1622 MESSAGE("end of transfer");
1623 fileTransfer->close(fileId);
1627 INFOS("open reference file for copy impossible");
1631 //=============================================================================
1632 /*! \brief create a PyNode object to execute remote python code
1633 * \param nodeName the name of the node
1634 * \param code the python code to load
1635 * \return the PyNode
1637 //=============================================================================
1638 Engines::PyNode_ptr Engines_Container_i::createPyNode(const char* nodeName, const char* code)
1640 Engines::PyNode_var node= Engines::PyNode::_nil();
1642 PyGILState_STATE gstate = PyGILState_Ensure();
1643 PyObject *res = PyObject_CallMethod(_pyCont,
1644 (char*)"create_pynode",
1652 PyGILState_Release(gstate);
1653 SALOME::ExceptionStruct es;
1654 es.type = SALOME::INTERNAL_ERROR;
1655 es.text = "can not create a python node";
1656 throw SALOME::SALOME_Exception(es);
1658 long ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
1659 PyObject* result=PyTuple_GetItem(res,1);
1660 std::string astr=PyUnicode_AsUTF8(result);
1662 PyGILState_Release(gstate);
1665 Utils_Locker lck(&_mutexForDftPy);
1666 CORBA::Object_var obj=_orb->string_to_object(astr.c_str());
1667 node=Engines::PyNode::_narrow(obj);
1668 std::map<std::string,Engines::PyNode_var>::iterator it(_dftPyNode.find(nodeName));
1669 if(it==_dftPyNode.end())
1671 _dftPyNode[nodeName]=node;
1675 Engines::PyNode_var oldNode((*it).second);
1676 if(!CORBA::is_nil(oldNode))
1677 oldNode->UnRegister();
1680 if(!CORBA::is_nil(node))
1682 return node._retn();
1686 SALOME::ExceptionStruct es;
1687 es.type = SALOME::INTERNAL_ERROR;
1688 es.text = astr.c_str();
1689 throw SALOME::SALOME_Exception(es);
1693 //=============================================================================
1694 /*! \brief Retrieves the last created PyNode instance with createPyNode.
1697 //=============================================================================
1698 Engines::PyNode_ptr Engines_Container_i::getDefaultPyNode(const char *nodeName)
1700 Utils_Locker lck(&_mutexForDftPy);
1701 std::map<std::string,Engines::PyNode_var>::iterator it(_dftPyNode.find(nodeName));
1702 if(it==_dftPyNode.end())
1703 return Engines::PyNode::_nil();
1706 Engines::PyNode_var tmpVar((*it).second);
1707 if(!CORBA::is_nil(tmpVar))
1708 return Engines::PyNode::_duplicate(tmpVar);
1710 return Engines::PyNode::_nil();
1714 //=============================================================================
1715 /*! \brief create a PyScriptNode object to execute remote python code
1716 * \param nodeName the name of the node
1717 * \param code the python code to load
1718 * \return the PyScriptNode
1720 //=============================================================================
1721 Engines::PyScriptNode_ptr Engines_Container_i::createPyScriptNode(const char* nodeName, const char* code)
1723 Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil();
1725 PyGILState_STATE gstate = PyGILState_Ensure();
1726 PyObject *res = PyObject_CallMethod(_pyCont,
1727 (char*)"create_pyscriptnode",
1735 PyGILState_Release(gstate);
1736 SALOME::ExceptionStruct es;
1737 es.type = SALOME::INTERNAL_ERROR;
1738 es.text = "can not create a python node";
1739 throw SALOME::SALOME_Exception(es);
1741 long ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
1742 PyObject* result=PyTuple_GetItem(res,1);
1743 std::string astr=PyUnicode_AsUTF8(result);
1745 PyGILState_Release(gstate);
1749 Utils_Locker lck(&_mutexForDftPy);
1750 CORBA::Object_var obj=_orb->string_to_object(astr.c_str());
1751 node=Engines::PyScriptNode::_narrow(obj);
1752 std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
1753 if(it==_dftPyScriptNode.end())
1755 _dftPyScriptNode[nodeName]=node;
1759 Engines::PyScriptNode_var oldNode((*it).second);
1760 if(!CORBA::is_nil(oldNode))
1761 oldNode->UnRegister();
1764 return node._retn();
1768 SALOME::ExceptionStruct es;
1769 es.type = SALOME::INTERNAL_ERROR;
1770 es.text = astr.c_str();
1771 throw SALOME::SALOME_Exception(es);
1775 void Engines_Container_i::removePyScriptNode(const char *nodeName)
1777 std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
1778 if(it==_dftPyScriptNode.end())
1780 std::ostringstream oss; oss << "Engines_Container_i::removePyScriptNode : node \"" << nodeName << "\" is not map !";
1781 SALOME::ExceptionStruct es;
1782 es.type = SALOME::INTERNAL_ERROR;
1783 es.text = oss.str().c_str();
1784 throw SALOME::SALOME_Exception(es);
1786 (*it).second->UnRegister();
1787 _dftPyScriptNode.erase(it);
1790 void Engines_Container_i::cleanAllPyScripts()
1792 for(std::map<std::string,Engines::PyNode_var>::iterator it=_dftPyNode.begin();it!=_dftPyNode.end();it++)
1794 Engines::PyNode_var tmpVar((*it).second);
1795 if(!CORBA::is_nil(tmpVar))
1796 tmpVar->UnRegister();
1799 for(std::map<std::string,Engines::PyScriptNode_var>::iterator it=_dftPyScriptNode.begin();it!=_dftPyScriptNode.end();it++)
1801 Engines::PyScriptNode_var tmpVar((*it).second);
1802 if(!CORBA::is_nil(tmpVar))
1803 tmpVar->UnRegister();
1805 _dftPyScriptNode.clear();
1808 //=============================================================================
1809 /*! \brief Retrieves the last created PyScriptNode instance with createPyScriptNode.
1812 //=============================================================================
1813 Engines::PyScriptNode_ptr Engines_Container_i::getDefaultPyScriptNode(const char *nodeName)
1815 Utils_Locker lck(&_mutexForDftPy);
1816 std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
1817 if(it==_dftPyScriptNode.end())
1818 return Engines::PyScriptNode::_nil();
1821 Engines::PyScriptNode_var tmpVar((*it).second);
1822 if(!CORBA::is_nil(tmpVar))
1823 return Engines::PyScriptNode::_duplicate(tmpVar);
1825 return Engines::PyScriptNode::_nil();
1829 //=============================================================================
1830 /* int checkifexecutable(const char *filename)
1832 * Return non-zero if the name is an executable file, and
1833 * zero if it is not executable, or if it does not exist.
1835 //=============================================================================
1836 int checkifexecutable(const std::string& filename)
1839 struct stat statinfo;
1841 result = stat(filename.c_str(), &statinfo);
1842 if (result < 0) return 0;
1843 if (!S_ISREG(statinfo.st_mode)) return 0;
1848 if (statinfo.st_uid == geteuid()) return statinfo.st_mode & S_IXUSR;
1849 if (statinfo.st_gid == getegid()) return statinfo.st_mode & S_IXGRP;
1850 return statinfo.st_mode & S_IXOTH;
1855 //=============================================================================
1856 /*! \brief Find a file by searching in a path
1857 * \param filename file name to search
1858 * \param path path to search in
1859 * \param pth the complete file path if found
1860 * \return 1 if found 0 if not
1862 //=============================================================================
1863 int findpathof(const std::string& path, std::string& pth, const std::string& filename)
1865 if ( path.size() == 0 ) return 0;
1867 std::string::size_type offset = 0;
1868 std::string::size_type pos = 0;
1870 struct stat statinfo;
1874 pos = path.find( SEP, offset );
1875 pth = path.substr( offset, pos - offset );
1876 if ( pth.size() > 0 )
1878 if( pth[pth.size()-1] != SLASH ) pth += SLASH;
1880 int result=stat(pth.c_str(), &statinfo);
1881 if(result == 0) found=1;
1883 if (pos == std::string::npos) break;
1889 void Engines_Container_i::registerTemporaryFile( const std::string& fileName )
1891 _tmp_files.remove( fileName );
1892 _tmp_files.push_back( fileName );
1895 void Engines_Container_i::unregisterTemporaryFile( const std::string& fileName )
1897 _tmp_files.remove( fileName );
1900 void Engines_Container_i::clearTemporaryFiles()
1902 std::list<std::string>::const_iterator it;
1903 for ( it = _tmp_files.begin(); it != _tmp_files.end(); ++it ) {
1904 #if defined(WIN32) && defined(UNICODE)
1905 std::wstring aFile = Kernel_Utils::utf8_decode_s(*it);
1906 std::wstring command = (GetFileAttributes(aFile.c_str()) == FILE_ATTRIBUTE_DIRECTORY) ? L"rd /Q \"" : L"del /F /Q \"";
1908 command += L"\" 2>NUL";
1909 _wsystem(command.c_str());
1912 std::string aFile = *it;
1913 std::string command = (GetFileAttributes(aFile.c_str()) == FILE_ATTRIBUTE_DIRECTORY) ? "rd /Q \"" : "del /F /Q \"";
1915 command += "\" 2>NUL";
1917 std::string command = "rm -rf ";
1920 system(command.c_str());
1926 static Engines_Container_i *_container_singleton_ssl = nullptr;
1928 static Engines::Container_var _container_ref_singleton_ssl;
1930 Engines_Container_i *KERNEL::getContainerSA()
1932 if(!_container_singleton_ssl)
1935 CORBA::ORB_var orb = CORBA::ORB_init(argc,nullptr);
1936 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
1937 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
1938 PortableServer::POAManager_var pman = poa->the_POAManager();
1939 CORBA::PolicyList policies;
1942 char *argv[4] = {"Container","FactoryServer","toto",nullptr};
1943 SALOME_Fake_NamingService ns;
1944 _container_singleton_ssl = new Engines_Container_i(orb,poa,"FactoryServer",2,argv,&ns,false);
1945 PortableServer::ObjectId * cont_id = _container_singleton_ssl->getCORBAId();
1947 CORBA::Object_var zeRef = poa->id_to_reference(*cont_id);
1948 _container_ref_singleton_ssl = Engines::Container::_narrow(zeRef);
1950 return _container_singleton_ssl;
1953 Engines::Container_var KERNEL::getContainerRefSA()
1956 return _container_ref_singleton_ssl;