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
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 "Basics_Utils.hxx"
64 #include "Container_init_python.hxx"
66 bool _Sleeping = false ;
68 // // Needed by multi-threaded Python --- Supervision
72 extern "C" {void ActSigIntHandler() ; }
74 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
76 extern "C" {void SigIntHandler( int ) ; }
81 #define ENGINESO "Engine.so"
84 #define ENGINESO "Engine.dll"
95 std::map<std::string, int> Engines_Container_i::_cntInstances_map;
96 std::map<std::string, void *> Engines_Container_i::_library_map;
97 std::map<std::string, void *> Engines_Container_i::_toRemove_map;
98 omni_mutex Engines_Container_i::_numInstanceMutex ;
100 static PyObject* _pyCont;
102 int checkifexecutable(const std::string&);
103 int findpathof(const std::string& path, std::string&, const std::string&);
105 /*! \class Engines_Container_i
106 * \brief C++ implementation of Engines::Container interface
111 //=============================================================================
113 * Default constructor, not for use
115 //=============================================================================
117 Engines_Container_i::Engines_Container_i () :
118 _numInstance(0),_id(0),_NS(0)
122 //=============================================================================
126 //=============================================================================
128 Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb,
129 PortableServer::POA_ptr poa,
130 char *containerName ,
131 int argc , char* argv[],
133 bool isServantAloneInProcess
135 _numInstance(0),_isServantAloneInProcess(isServantAloneInProcess),_id(0),_NS(0)
137 _pid = (long)getpid();
145 std::string hostname = Kernel_Utils::GetHostname();
147 MESSAGE(hostname << " " << getpid() <<
148 " Engines_Container_i starting argc " <<
149 _argc << " Thread " << pthread_self() ) ;
151 MESSAGE(hostname << " " << _getpid() <<
152 " Engines_Container_i starting argc " << _argc<< " Thread " << pthread_self().p ) ;
158 MESSAGE(" argv" << i << " " << _argv[ i ]) ;
164 INFOS("SALOME_Container usage : SALOME_Container ServerName");
168 _isSupervContainer = false;
170 _orb = CORBA::ORB::_duplicate(orb) ;
171 _poa = PortableServer::POA::_duplicate(poa) ;
173 // Pour les containers paralleles: il ne faut pas enregistrer et activer
174 // le container generique, mais le container specialise
178 _id = _poa->activate_object(this);
179 _NS = new SALOME_NamingService();
180 _NS->init_orb( _orb ) ;
181 CORBA::Object_var obj=_poa->id_to_reference(*_id);
182 Engines::Container_var pCont
183 = Engines::Container::_narrow(obj);
186 _containerName = _NS->BuildContainerNameForNS(containerName,
188 SCRUTE(_containerName);
189 _NS->Register(pCont, _containerName.c_str());
190 MESSAGE("Engines_Container_i::Engines_Container_i : Container name "
194 // import SALOME_Container
195 // pycont = SALOME_Container.SALOME_Container_i(containerIORStr)
197 CORBA::String_var sior = _orb->object_to_string(pCont);
198 std::string myCommand="pyCont = SALOME_Container.SALOME_Container_i('";
199 myCommand += _containerName + "','";
204 //[RNV]: Comment the PyEval_AcquireLock() and PyEval_ReleaseLock() because this
205 //approach leads to the deadlock of the main thread of the application on Windows platform
206 //in case if cppContainer runs in the standalone mode. The problem with the PyThreadState
207 //described by ABN seems not reproduced, to be checked carefully later...
208 PyGILState_STATE gstate = PyGILState_Ensure();
210 //// [ABN]: using the PyGILState* API here is unstable. omniORB logic is invoked
211 //// by the Python code executed below, and in some (random) cases, the Python code
212 //// execution ends with a PyThreadState which was not the one we have here.
213 //// (TODO: understand why ...)
214 //// To be on the safe side we get and load the thread state ourselves:
215 //PyEval_AcquireLock(); // get GIL
216 //PyThreadState * mainThreadState = PyThreadState_Get();
217 //PyThreadState_Swap(mainThreadState);
220 // mpv: this is temporary solution: there is a unregular crash if not
223 // first element is the path to Registry.dll, but it's wrong
224 PyRun_SimpleString("import sys\n");
225 PyRun_SimpleString("sys.path = sys.path[1:]\n");
227 PyRun_SimpleString("import SALOME_Container\n");
228 PyRun_SimpleString((char*)myCommand.c_str());
229 PyObject *mainmod = PyImport_AddModule("__main__");
230 PyObject *globals = PyModule_GetDict(mainmod);
231 _pyCont = PyDict_GetItemString(globals, "pyCont");
233 //PyThreadState_Swap(NULL);
234 //PyEval_ReleaseLock();
235 PyGILState_Release(gstate);
237 fileTransfer_i* aFileTransfer = new fileTransfer_i();
238 CORBA::Object_var obref=aFileTransfer->_this();
239 _fileTransfer = Engines::fileTransfer::_narrow(obref);
240 aFileTransfer->_remove_ref();
244 //=============================================================================
248 //=============================================================================
250 Engines_Container_i::~Engines_Container_i()
252 MESSAGE("Container_i::~Container_i()");
257 for(std::map<std::string,Engines::PyNode_var>::iterator it=_dftPyNode.begin();it!=_dftPyNode.end();it++)
259 Engines::PyNode_var tmpVar((*it).second);
260 if(!CORBA::is_nil(tmpVar))
261 tmpVar->UnRegister();
263 for(std::map<std::string,Engines::PyScriptNode_var>::iterator it=_dftPyScriptNode.begin();it!=_dftPyScriptNode.end();it++)
265 Engines::PyScriptNode_var tmpVar((*it).second);
266 if(!CORBA::is_nil(tmpVar))
267 tmpVar->UnRegister();
271 //=============================================================================
272 //! Get container name
274 * CORBA attribute: Container name (see constructor)
276 //=============================================================================
278 char* Engines_Container_i::name()
280 return CORBA::string_dup(_containerName.c_str()) ;
283 //=============================================================================
284 //! Get container working directory
286 * CORBA attribute: Container working directory
288 //=============================================================================
290 char* Engines_Container_i::workingdir()
294 return CORBA::string_dup(wd) ;
297 //=============================================================================
298 //! Get container log file name
300 * CORBA attribute: Container log file name
302 //=============================================================================
304 char* Engines_Container_i::logfilename()
306 return CORBA::string_dup(_logfilename.c_str()) ;
309 //! Set container log file name
310 void Engines_Container_i::logfilename(const char* name)
315 //=============================================================================
316 //! Get container host name
318 * CORBA method: Get the hostName of the Container (without domain extensions)
320 //=============================================================================
322 char* Engines_Container_i::getHostName()
324 std::string s = Kernel_Utils::GetHostname();
325 // MESSAGE("Engines_Container_i::getHostName " << s);
326 return CORBA::string_dup(s.c_str()) ;
329 //=============================================================================
330 //! Get container PID
332 * CORBA method: Get the PID (process identification) of the Container
334 //=============================================================================
336 CORBA::Long Engines_Container_i::getPID()
338 return (CORBA::Long)getpid();
341 //=============================================================================
342 //! Ping the servant to check it is still alive
344 * CORBA method: check if servant is still alive
346 //=============================================================================
347 void Engines_Container_i::ping()
349 MESSAGE("Engines_Container_i::ping() pid "<< getpid());
352 //=============================================================================
353 //! Shutdown the container
355 * CORBA method, oneway: Server shutdown.
356 * - Container name removed from naming service,
357 * - servant deactivation,
358 * - orb shutdown if no other servants in the process
360 //=============================================================================
361 void Engines_Container_i::Shutdown()
363 MESSAGE("Engines_Container_i::Shutdown()");
365 // Clear registered temporary files
366 clearTemporaryFiles();
368 /* For each component contained in this container
369 * tell it to self-destroy
371 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
372 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
376 itm->second->destroy();
378 catch(const CORBA::Exception& e)
380 // ignore this entry and continue
384 // ignore this entry and continue
387 _listInstances_map.clear();
389 _NS->Destroy_FullDirectory(_containerName.c_str());
390 _NS->Destroy_Name(_containerName.c_str());
391 if(_isServantAloneInProcess)
393 MESSAGE("Effective Shutdown of container Begins...");
394 if(!CORBA::is_nil(_orb))
399 //=============================================================================
400 //! load a component implementation
403 * \param componentName component name
404 * \param reason explains error when load fails
405 * \return true if dlopen successfull or already done, false otherwise
407 //=============================================================================
409 Engines_Container_i::load_component_Library(const char* componentName, CORBA::String_out reason)
412 //=================================================================
413 // --- C++ implementation section
414 //=================================================================
416 if(load_component_CppImplementation(componentName,retso))
418 reason=CORBA::string_dup("");
421 else if(retso != "ImplementationNotFound")
423 reason=CORBA::string_dup(retso.c_str());
428 retso+=componentName;
429 retso+=": Can't find C++ implementation ";
430 retso+=std::string(LIB) + componentName + ENGINESO;
432 //=================================================================
433 // --- Python implementation section
434 //=================================================================
436 if(load_component_PythonImplementation(componentName,retpy))
438 reason=CORBA::string_dup("");
441 else if(retpy != "ImplementationNotFound")
443 reason=CORBA::string_dup(retpy.c_str());
448 retpy+=componentName;
449 retpy+=": Can't find python implementation ";
450 retpy+=componentName;
453 //=================================================================
454 // -- Executable implementation section
455 //=================================================================
457 if(load_component_ExecutableImplementation(componentName,retex))
459 reason=CORBA::string_dup("");
462 else if(retex != "ImplementationNotFound")
464 reason=CORBA::string_dup(retex.c_str());
469 retex+=componentName;
470 retex+=": Can't find executable implementation ";
471 retex+=componentName;
474 std::string ret="Component implementation not found: ";
475 ret += componentName ;
481 std::cerr << ret << std::endl;
482 reason=CORBA::string_dup(ret.c_str());
487 //=============================================================================
488 //! try to load a C++ component implementation
491 * \param componentName the name of the component (COMPONENT, for example)
492 * \param reason explains error when load fails
493 * \return true if loading is successfull or already done, false otherwise
495 //=============================================================================
497 Engines_Container_i::load_component_CppImplementation(const char* componentName, std::string& reason)
499 std::string aCompName(componentName);
500 std::string impl_name = std::string(LIB) + aCompName + ENGINESO;
503 _numInstanceMutex.lock(); // lock to be alone
504 // (see decInstanceCnt, finalize_removal))
505 if (_toRemove_map.count(impl_name) != 0) _toRemove_map.erase(impl_name);
506 if (_library_map.count(impl_name) != 0)
508 MESSAGE("Library " << impl_name << " already loaded");
509 _numInstanceMutex.unlock();
513 _numInstanceMutex.unlock();
517 handle = dlopen( impl_name.c_str() , RTLD_NOW ) ;
520 //not loadable. Try to find the lib file in LD_LIBRARY_PATH
522 char* p=getenv("LD_LIBRARY_PATH");
524 path=path+SEP+"/usr/lib"+SEP+"/lib";
527 if(findpathof(path, pth, impl_name))
529 //found but not loadable
532 reason+=": C++ implementation found ";
534 reason+=" but it is not loadable. Error:\n";
536 std::cerr << reason << std::endl;
542 //continue with other implementation
543 reason="ImplementationNotFound";
549 handle = LoadLibrary( impl_name.c_str() );
552 reason="ImplementationNotFound";
558 _numInstanceMutex.lock();
559 _library_map[impl_name] = handle;
560 _numInstanceMutex.unlock();
568 //=============================================================================
569 //! try to load a Python component implementation
572 * \param componentName name of the component
573 * \param reason explains error when load fails
574 * \return true if loading is successfull or already done, false otherwise
576 //=============================================================================
578 Engines_Container_i::load_component_PythonImplementation(const char* componentName, std::string& reason)
580 std::string aCompName(componentName);
582 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
583 if (_library_map.count(aCompName) != 0)
585 _numInstanceMutex.unlock() ;
587 return true; // Python Component, already imported
589 _numInstanceMutex.unlock() ;
591 PyGILState_STATE gstate = PyGILState_Ensure();
592 PyObject *result = PyObject_CallMethod(_pyCont,
593 (char*)"import_component",
594 (char*)"s",componentName);
596 reason=PyString_AsString(result);
599 PyGILState_Release(gstate);
603 //Python component has been loaded (import componentName)
604 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
605 _library_map[aCompName] = (void *)_pyCont; // any non O value OK
606 _numInstanceMutex.unlock() ;
607 MESSAGE("import Python: "<< aCompName <<" OK");
610 else if(reason=="ImplementationNotFound")
612 //Python implementation has not been found. Continue with other implementation
613 reason="ImplementationNotFound";
617 //Python implementation has been found but loading has failed
618 std::cerr << reason << std::endl;
623 //=============================================================================
624 //! try to load a Executable component implementation
627 * \param componentName name of the component
628 * \param reason explains error when load fails
629 * \return true if loading is successfull or already done, false otherwise
631 //=============================================================================
633 Engines_Container_i::load_component_ExecutableImplementation(const char* componentName, std::string& reason)
635 std::string aCompName(componentName);
636 std::string executable=aCompName+".exe";
641 char* p=getenv("PATH");
644 if (findpathof(path, pth, executable))
646 if(checkifexecutable(pth))
648 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
649 _library_map[executable] = (void *)1; // any non O value OK
650 _numInstanceMutex.unlock() ;
651 MESSAGE("import executable: "<< pth <<" OK");
657 reason+=": implementation found ";
659 reason+=" but it is not executable";
660 std::cerr << reason << std::endl;
664 reason="ImplementationNotFound";
669 //=============================================================================
670 //! Create a new component instance
672 * CORBA method: Creates a new servant instance of a component.
673 * The servant registers itself to naming service and Registry.
674 * \param genericRegisterName Name of the component instance to register
675 * in Registry & Name Service (without _inst_n suffix)
676 * \param studyId 0 for multiStudy instance,
677 * study Id (>0) otherwise
678 * \return a loaded component
680 //=============================================================================
681 Engines::EngineComponent_ptr
682 Engines_Container_i::create_component_instance(const char*genericRegisterName,
685 Engines::FieldsDict_var env = new Engines::FieldsDict;
687 Engines::EngineComponent_ptr compo =
688 create_component_instance_env(genericRegisterName, studyId, env, reason);
689 CORBA::string_free(reason);
693 //=============================================================================
694 //! Create a new component instance with environment variables specified
696 * CORBA method: Creates a new servant instance of a component.
697 * The servant registers itself to naming service and Registry.
698 * \param genericRegisterName Name of the component instance to register
699 * in Registry & Name Service (without _inst_n suffix)
700 * \param studyId 0 for multiStudy instance,
701 * study Id (>0) otherwise
702 * \param env dict of env variables
703 * \param reason explains error when create_component_instance_env fails
704 * \return a loaded component
706 //=============================================================================
707 Engines::EngineComponent_ptr
708 Engines_Container_i::create_component_instance_env(const char*genericRegisterName,
710 const Engines::FieldsDict& env,
711 CORBA::String_out reason)
715 INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
716 reason=CORBA::string_dup("studyId must be > 0 for mono study instance, =0 for multiStudy");
717 return Engines::EngineComponent::_nil() ;
721 if (_library_map.count(genericRegisterName) != 0)
723 // It's a Python component
724 Engines::EngineComponent_ptr compo = createPythonInstance(genericRegisterName, studyId, error);
725 reason=CORBA::string_dup(error.c_str());
729 std::string impl_name = std::string(LIB) + genericRegisterName + ENGINESO;
730 if (_library_map.count(impl_name) != 0)
732 // It's a C++ component
733 void* handle = _library_map[impl_name];
734 Engines::EngineComponent_ptr compo = createInstance(genericRegisterName, handle, studyId, error);
735 reason=CORBA::string_dup(error.c_str());
739 impl_name = std::string(genericRegisterName) + ".exe";
740 if (_library_map.count(impl_name) != 0)
742 //It's an executable component
743 Engines::EngineComponent_ptr compo = createExecutableInstance(genericRegisterName, studyId, env, error);
744 reason=CORBA::string_dup(error.c_str());
748 error="load_component_Library has probably not been called for component: ";
749 error += genericRegisterName;
751 reason=CORBA::string_dup(error.c_str());
752 return Engines::EngineComponent::_nil() ;
755 //=============================================================================
756 //! Create a new component instance (Executable implementation)
758 * \param CompName Name of the component instance
759 * \param studyId 0 for multiStudy instance,
760 * study Id (>0) otherwise
761 * \param env dict of env variables
762 * \param reason explains error when creation fails
763 * \return a loaded component
765 * This component is implemented in an executable with name genericRegisterName.exe
766 * It must register itself in Naming Service. The container waits some time (10 s max)
769 //=============================================================================
770 Engines::EngineComponent_ptr
771 Engines_Container_i::createExecutableInstance(std::string CompName, int studyId,
772 const Engines::FieldsDict& env,
775 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
777 _numInstanceMutex.lock() ; // lock on the instance number
779 int numInstance = _numInstance ;
780 _numInstanceMutex.unlock() ;
783 sprintf( aNumI , "%d" , numInstance ) ;
784 std::string instanceName = CompName + "_inst_" + aNumI ;
785 std::string component_registerName = _containerName + "/" + instanceName;
787 //check if an entry exist in naming service
788 CORBA::Object_var nsobj = _NS->Resolve(component_registerName.c_str());
789 if ( !CORBA::is_nil(nsobj) )
791 // unregister the registered component
792 _NS->Destroy_Name(component_registerName.c_str());
793 //kill or shutdown it ???
796 // first arg container ior string
797 // second arg container name
798 // third arg instance name
800 Engines::Container_var pCont= _this();
801 CORBA::String_var sior = _orb->object_to_string(pCont);
805 command+=instanceName;
807 command+=instanceName;
812 command+= sior; // container ior string
814 command+=_containerName; //container name
816 command+=instanceName; //instance name
818 MESSAGE("SALOME_Container::create_component_instance command=" << command);
821 // use fork/execl instead of system to get finer control on env variables
824 if(pid == 0) // child
826 for (CORBA::ULong i=0; i < env.length(); i++)
828 if (env[i].value.type()->kind() == CORBA::tk_string)
831 env[i].value >>= value;
832 std::string s(env[i].key);
835 putenv(strdup(s.c_str()));
839 execl("/bin/sh", "sh", "-c", command.c_str() , (char *)0);
842 else if(pid < 0) // failed to fork
851 tpid = wait(&status);
852 } while (tpid != pid);
855 // launch component with a system call
856 int status=system(command.c_str());
861 reason="SALOME_Container::create_component_instance system failed (system command status -1)";
863 return Engines::EngineComponent::_nil();
866 else if (WEXITSTATUS(status) == 217)
868 reason="SALOME_Container::create_component_instance system failed (system command status 217)";
870 return Engines::EngineComponent::_nil();
876 if (getenv("TIMEOUT_TO_WAIT_EXE_COMPONENT") != 0)
878 std::string new_count_str = getenv("TIMEOUT_TO_WAIT_EXE_COMPONENT");
880 std::istringstream ss(new_count_str);
881 if (!(ss >> new_count))
883 INFOS("[Container] TIMEOUT_TO_WAIT_EXE_COMPONENT should be an int");
888 INFOS("[Container] waiting " << count << " second steps exe component ");
889 CORBA::Object_var obj = CORBA::Object::_nil() ;
890 while ( CORBA::is_nil(obj) && count )
898 MESSAGE( count << ". Waiting for component " << CompName);
899 obj = _NS->Resolve(component_registerName.c_str());
902 if(CORBA::is_nil(obj))
904 reason="SALOME_Container::create_component_instance failed";
906 return Engines::EngineComponent::_nil();
910 MESSAGE("SALOME_Container::create_component_instance successful");
911 iobject = Engines::EngineComponent::_narrow(obj);
912 _listInstances_map[instanceName] = iobject;
913 return iobject._retn();
919 //=============================================================================
920 //! Create a new component instance (Python implementation)
922 * \param CompName Name of the component instance
923 * \param studyId 0 for multiStudy instance,
924 * study Id (>0) otherwise
925 * \param reason explains error when creation fails
926 * \return a loaded component
928 //=============================================================================
929 Engines::EngineComponent_ptr
930 Engines_Container_i::createPythonInstance(std::string CompName, int studyId,
933 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
935 _numInstanceMutex.lock() ; // lock on the instance number
937 int numInstance = _numInstance ;
938 _numInstanceMutex.unlock() ;
941 sprintf( aNumI , "%d" , numInstance ) ;
942 std::string instanceName = CompName + "_inst_" + aNumI ;
943 std::string component_registerName = _containerName + "/" + instanceName;
945 PyGILState_STATE gstate = PyGILState_Ensure();
946 PyObject *result = PyObject_CallMethod(_pyCont,
947 (char*)"create_component_instance",
950 instanceName.c_str(),
954 PyArg_ParseTuple(result,"ss", &ior, &error);
955 std::string iors = ior;
958 PyGILState_Release(gstate);
962 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
963 iobject = Engines::EngineComponent::_narrow( obj ) ;
964 _listInstances_map[instanceName] = iobject;
966 return iobject._retn();
970 Engines_Container_i::create_python_service_instance(const char * CompName,
971 CORBA::String_out reason)
973 CORBA::Object_var object = CORBA::Object::_nil();
975 _numInstanceMutex.lock() ; // lock on the instance number
977 int numInstance = _numInstance ;
978 _numInstanceMutex.unlock() ;
981 sprintf( aNumI , "%d" , numInstance ) ;
982 std::string instanceName = std::string(CompName) + "_inst_" + aNumI ;
983 std::string component_registerName = _containerName + "/" + instanceName;
985 PyGILState_STATE gstate = PyGILState_Ensure();
986 PyObject *result = PyObject_CallMethod(_pyCont,
987 (char*)"create_component_instance",
990 instanceName.c_str(),
994 PyArg_ParseTuple(result,"ss", &ior, &error);
995 reason = CORBA::string_dup(error);
996 char * _ior = CORBA::string_dup(ior);
998 PyGILState_Release(gstate);
1004 //=============================================================================
1005 //! Create a new component instance (C++ implementation)
1007 * C++ method: create a servant instance of a component.
1008 * \param genericRegisterName Name of the component instance to register
1009 * in Registry & Name Service,
1010 * (without _inst_n suffix, like "COMPONENT")
1011 * \param handle loaded library handle
1012 * \param studyId 0 for multiStudy instance,
1013 * study Id (>0) otherwise
1014 * \param reason explains error when creation fails
1015 * \return a loaded component
1017 * example with names:
1018 * - aGenRegisterName = COMPONENT (= first argument)
1019 * - _containerName = /Containers/cli76ce/FactoryServer
1020 * - factoryName = COMPONENTEngine_factory
1021 * - component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
1022 * - instanceName = COMPONENT_inst_1
1023 * - component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
1025 //=============================================================================
1026 Engines::EngineComponent_ptr
1027 Engines_Container_i::createInstance(std::string genericRegisterName,
1030 std::string& reason)
1032 // --- find the factory
1034 std::string aGenRegisterName = genericRegisterName;
1035 std::string factory_name = aGenRegisterName + std::string("Engine_factory");
1036 SCRUTE(factory_name) ;
1038 typedef PortableServer::ObjectId* (*FACTORY_FUNCTION) (CORBA::ORB_ptr,
1039 PortableServer::POA_ptr,
1040 PortableServer::ObjectId *,
1045 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION)dlsym( handle, factory_name.c_str() );
1047 FACTORY_FUNCTION Component_factory = (FACTORY_FUNCTION)GetProcAddress( (HINSTANCE)handle, factory_name.c_str() );
1050 if ( !Component_factory )
1052 INFOS( "Can't resolve symbol: " + factory_name );
1057 return Engines::EngineComponent::_nil() ;
1060 // --- create instance
1062 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
1066 _numInstanceMutex.lock() ; // lock on the instance number
1068 int numInstance = _numInstance ;
1069 _numInstanceMutex.unlock() ;
1072 sprintf( aNumI , "%d" , numInstance ) ;
1073 std::string instanceName = aGenRegisterName + "_inst_" + aNumI ;
1074 std::string component_registerName =
1075 _containerName + "/" + instanceName;
1077 // --- Instanciate required CORBA object
1079 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
1080 id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(),
1081 aGenRegisterName.c_str() ) ;
1084 reason="Can't get ObjectId from factory";
1086 return iobject._retn();
1089 // --- get reference & servant from id
1091 CORBA::Object_var obj = _poa->id_to_reference(*id);
1092 iobject = Engines::EngineComponent::_narrow( obj ) ;
1094 Engines_Component_i *servant =
1095 dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
1097 //SCRUTE(servant->_refcount_value());
1098 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1099 _listInstances_map[instanceName] = iobject;
1100 _cntInstances_map[aGenRegisterName] += 1;
1101 _numInstanceMutex.unlock() ;
1102 SCRUTE(aGenRegisterName);
1103 SCRUTE(_cntInstances_map[aGenRegisterName]);
1104 servant->setStudyId(studyId);
1105 servant->_remove_ref(); // do not need servant any more (remove ref from reference_to_servant)
1106 //SCRUTE(servant->_refcount_value());
1108 // --- register the engine under the name
1109 // containerName(.dir)/instanceName(.object)
1111 _NS->Register( iobject , component_registerName.c_str() ) ;
1112 MESSAGE( component_registerName.c_str() << " bound" ) ;
1116 reason="Container_i::createInstance exception catched";
1119 return iobject._retn();
1122 //=============================================================================
1123 //! Find an existing (in the container) component instance
1125 * CORBA method: Finds a servant instance of a component
1126 * \param registeredName Name of the component in Registry or Name Service,
1127 * without instance suffix number
1128 * \param studyId 0 if instance is not associated to a study,
1129 * >0 otherwise (== study id)
1130 * \return the first instance found with same studyId
1132 //=============================================================================
1133 Engines::EngineComponent_ptr
1134 Engines_Container_i::find_component_instance( const char* registeredName,
1135 CORBA::Long studyId)
1137 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_nil();
1138 std::map<std::string,Engines::EngineComponent_var>::iterator itm =_listInstances_map.begin();
1139 while (itm != _listInstances_map.end())
1141 std::string instance = (*itm).first;
1143 if (instance.find(registeredName) == 0)
1145 anEngine = (*itm).second;
1146 if (studyId == anEngine->getStudyId())
1148 return anEngine._retn();
1153 return anEngine._retn();
1156 //=============================================================================
1157 //! Remove the component instance from container
1159 * CORBA method: Stops the component servant, and deletes all related objects
1160 * \param component_i Component to be removed
1162 //=============================================================================
1164 void Engines_Container_i::remove_impl(Engines::EngineComponent_ptr component_i)
1166 ASSERT(! CORBA::is_nil(component_i));
1167 std::string instanceName = component_i->instanceName() ;
1168 MESSAGE("unload component " << instanceName);
1169 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1170 _listInstances_map.erase(instanceName);
1171 _numInstanceMutex.unlock() ;
1172 component_i->destroy() ;
1173 _NS->Destroy_Name(instanceName.c_str());
1176 //=============================================================================
1177 //! Unload component libraries from the container
1179 * CORBA method: Discharges unused libraries from the container.
1181 //=============================================================================
1182 void Engines_Container_i::finalize_removal()
1184 MESSAGE("finalize unload : dlclose");
1185 _numInstanceMutex.lock(); // lock to be alone
1186 // (see decInstanceCnt, load_component_Library)
1187 std::map<std::string, void *>::iterator ith;
1188 for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
1190 void *handle = (*ith).second;
1191 std::string impl_name= (*ith).first;
1196 // dlclose(handle); // SALOME unstable after ...
1197 // _library_map.erase(impl_name);
1200 _toRemove_map.clear();
1201 _numInstanceMutex.unlock();
1204 //=============================================================================
1205 //! Decrement component instance reference count
1209 //=============================================================================
1210 void Engines_Container_i::decInstanceCnt(std::string genericRegisterName)
1212 if(_cntInstances_map.count(genericRegisterName)==0)
1214 std::string aGenRegisterName =genericRegisterName;
1215 MESSAGE("Engines_Container_i::decInstanceCnt " << aGenRegisterName);
1216 ASSERT(_cntInstances_map[aGenRegisterName] > 0);
1217 _numInstanceMutex.lock(); // lock to be alone
1218 // (see finalize_removal, load_component_Library)
1219 _cntInstances_map[aGenRegisterName] -= 1;
1220 SCRUTE(_cntInstances_map[aGenRegisterName]);
1221 if (_cntInstances_map[aGenRegisterName] == 0)
1223 std::string impl_name =
1224 Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str());
1226 void* handle = _library_map[impl_name];
1228 _toRemove_map[impl_name] = handle;
1230 _numInstanceMutex.unlock();
1233 //=============================================================================
1234 //! Find or create a new component instance
1236 * CORBA method: find or create an instance of the component (servant),
1237 * load a new component class (dynamic library) if required,
1239 * ---- FOR COMPATIBILITY WITH 2.2 ----
1241 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
1243 * The servant registers itself to naming service and Registry.
1244 * \param genericRegisterName Name of the component to register
1245 * in Registry & Name Service
1246 * \param componentName Name of the constructed library of the component
1247 * \return a loaded component
1249 //=============================================================================
1251 Engines::EngineComponent_ptr
1252 Engines_Container_i::load_impl( const char* genericRegisterName,
1253 const char* componentName )
1256 std::string impl_name = std::string(LIB) + genericRegisterName + ENGINESO;
1257 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
1258 if (load_component_Library(genericRegisterName,reason))
1259 iobject = find_or_create_instance(genericRegisterName, impl_name);
1260 CORBA::string_free(reason);
1261 return iobject._retn();
1264 //=============================================================================
1265 //! Finds an already existing component instance or create a new instance
1267 * C++ method: Finds an already existing servant instance of a component, or
1268 * create an instance.
1269 * ---- USE ONLY FOR MULTISTUDY INSTANCES ! --------
1270 * \param genericRegisterName Name of the component instance to register
1271 * in Registry & Name Service,
1272 * (without _inst_n suffix, like "COMPONENT")
1273 * \param componentLibraryName like "libCOMPONENTEngine.so"
1274 * \return a loaded component
1276 * example with names:
1277 * - aGenRegisterName = COMPONENT (= first argument)
1278 * - impl_name = libCOMPONENTEngine.so (= second argument)
1279 * - _containerName = /Containers/cli76ce/FactoryServer
1280 * - factoryName = COMPONENTEngine_factory
1281 * - component_registerBase = /Containers/cli76ce/FactoryServer/COMPONENT
1282 * - instanceName = COMPONENT_inst_1
1283 * - component_registerName = /Containers/cli76ce/FactoryServer/COMPONENT_inst_1
1285 //=============================================================================
1287 Engines::EngineComponent_ptr
1288 Engines_Container_i::find_or_create_instance(std::string genericRegisterName,
1289 std::string componentLibraryName)
1291 std::string aGenRegisterName = genericRegisterName;
1292 std::string impl_name = componentLibraryName;
1293 if (_library_map.count(impl_name) == 0)
1295 INFOS("shared library " << impl_name <<" must be loaded before creating instance");
1296 return Engines::EngineComponent::_nil() ;
1300 // --- find a registered instance in naming service, or create
1302 void* handle = _library_map[impl_name];
1303 std::string component_registerBase =
1304 _containerName + "/" + aGenRegisterName;
1305 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
1309 CORBA::Object_var obj =
1310 _NS->ResolveFirst( component_registerBase.c_str());
1311 if ( CORBA::is_nil( obj ) )
1313 iobject = createInstance(genericRegisterName,
1316 reason); // force multiStudy instance here !
1320 iobject = Engines::EngineComponent::_narrow( obj ) ;
1321 Engines_Component_i *servant =
1322 dynamic_cast<Engines_Component_i*>
1323 (_poa->reference_to_servant(iobject));
1325 int studyId = servant->getStudyId();
1326 ASSERT (studyId >= 0);
1327 if (studyId == 0) // multiStudy instance, OK
1330 MESSAGE(component_registerBase.c_str()<<" already bound");
1332 else // monoStudy instance: NOK
1334 iobject = Engines::EngineComponent::_nil();
1335 INFOS("load_impl & find_component_instance methods "
1336 << "NOT SUITABLE for mono study components");
1342 INFOS( "Container_i::load_impl catched" ) ;
1344 return iobject._retn();
1348 //=============================================================================
1349 //! Indicate if container is a python one
1351 * Retrieves only with container naming convention if it is a python container
1353 //=============================================================================
1354 bool Engines_Container_i::isPythonContainer(const char* ContainerName)
1357 int len=strlen(ContainerName);
1359 if(strcmp(ContainerName+len-2,"Py")==0)
1364 //=============================================================================
1365 //! Kill the container
1367 * CORBA method: Kill the container process with exit(0).
1368 * To remove : never returns !
1370 //=============================================================================
1371 bool Engines_Container_i::Kill_impl()
1373 MESSAGE("Engines_Container_i::Kill() pid "<< getpid() << " containerName "
1374 << _containerName.c_str() << " machineName "
1375 << Kernel_Utils::GetHostname().c_str());
1376 INFOS("===============================================================");
1377 INFOS("= REMOVE calls to Kill_impl in C++ container =");
1378 INFOS("===============================================================");
1384 //=============================================================================
1388 //=============================================================================
1389 void ActSigIntHandler()
1392 struct sigaction SigIntAct ;
1393 SigIntAct.sa_sigaction = &SigIntHandler ;
1394 sigemptyset(&SigIntAct.sa_mask);
1395 SigIntAct.sa_flags = SA_SIGINFO ;
1398 // DEBUG 03.02.2005 : the first parameter of sigaction is not a mask of signals
1399 // (SIGINT | SIGUSR1) :
1400 // it must be only one signal ===> one call for SIGINT
1401 // and an other one for SIGUSR1
1404 if ( sigaction( SIGINT , &SigIntAct, NULL ) )
1406 perror("SALOME_Container main ") ;
1409 if ( sigaction( SIGUSR1 , &SigIntAct, NULL ) )
1411 perror("SALOME_Container main ") ;
1414 if ( sigaction( SIGUSR2 , &SigIntAct, NULL ) )
1416 perror("SALOME_Container main ") ;
1420 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1421 // use of streams (and so on) should never be used because :
1422 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1423 // A stream operation may be interrupted by a signal and if the Handler use stream we
1424 // may have a "Dead-Lock" ===HangUp
1425 //==INFOS is commented
1426 // INFOS(pthread_self() << "SigIntHandler activated") ;
1429 signal( SIGINT, SigIntHandler );
1430 // legacy code required to supervisor. Commented in order to avoid problems on Windows
1431 // signal( SIGUSR1, SigIntHandler );
1437 void CallCancelThread() ;
1440 void SigIntHandler(int what ,
1441 siginfo_t * siginfo ,
1444 //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers)
1445 // use of streams (and so on) should never be used because :
1446 // streams of C++ are naturally thread-safe and use pthread_mutex_lock ===>
1447 // A stream operation may be interrupted by a signal and if the Handler use stream we
1448 // may have a "Dead-Lock" ===HangUp
1449 //==MESSAGE is commented
1450 // MESSAGE(pthread_self() << "SigIntHandler what " << what << std::endl
1451 // << " si_signo " << siginfo->si_signo << std::endl
1452 // << " si_code " << siginfo->si_code << std::endl
1453 // << " si_pid " << siginfo->si_pid) ;
1458 // MESSAGE("SigIntHandler END sleeping.") ;
1463 ActSigIntHandler() ;
1464 if ( siginfo->si_signo == SIGUSR1 )
1468 else if ( siginfo->si_signo == SIGUSR2 )
1470 CallCancelThread() ;
1475 // MESSAGE("SigIntHandler BEGIN sleeping.") ;
1482 // MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1488 void SigIntHandler( int what )
1491 MESSAGE( pthread_self() << "SigIntHandler what " << what << std::endl );
1493 MESSAGE( "SigIntHandler what " << what << std::endl );
1498 MESSAGE("SigIntHandler END sleeping.") ;
1503 ActSigIntHandler() ;
1504 if ( what == SIGUSR1 )
1511 MESSAGE("SigIntHandler BEGIN sleeping.") ;
1518 MESSAGE("SigIntHandler LEAVE sleeping after " << count << " s.") ;
1525 //=============================================================================
1526 //! Get or create a file reference object associated to a local file (to transfer it)
1528 * CORBA method: get or create a fileRef object associated to a local file
1529 * (a file on the computer on which runs the container server), which stores
1530 * a list of (machine, localFileName) corresponding to copies already done.
1532 * \param origFileName absolute path for a local file to copy on other
1534 * \return a fileRef object associated to the file.
1536 //=============================================================================
1537 Engines::fileRef_ptr
1538 Engines_Container_i::createFileRef(const char* origFileName)
1540 std::string origName(origFileName);
1541 Engines::fileRef_var theFileRef = Engines::fileRef::_nil();
1543 if (origName[0] != '/')
1545 INFOS("path of file to copy must be an absolute path begining with '/'");
1546 return Engines::fileRef::_nil();
1549 if (CORBA::is_nil(_fileRef_map[origName]))
1551 CORBA::Object_var obj=_poa->id_to_reference(*_id);
1552 Engines::Container_var pCont = Engines::Container::_narrow(obj);
1553 fileRef_i* aFileRef = new fileRef_i(pCont, origFileName);
1554 theFileRef = Engines::fileRef::_narrow(aFileRef->_this());
1555 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1556 _fileRef_map[origName] = theFileRef;
1557 _numInstanceMutex.unlock() ;
1560 theFileRef = Engines::fileRef::_duplicate(_fileRef_map[origName]);
1561 ASSERT(! CORBA::is_nil(theFileRef));
1562 return theFileRef._retn();
1565 //=============================================================================
1566 //! Get a fileTransfer reference
1569 * \return a reference to the fileTransfer object
1571 //=============================================================================
1572 Engines::fileTransfer_ptr
1573 Engines_Container_i::getFileTransfer()
1575 Engines::fileTransfer_var aFileTransfer
1576 = Engines::fileTransfer::_duplicate(_fileTransfer);
1577 return aFileTransfer._retn();
1580 //=============================================================================
1581 //! Create a Salome file
1582 //=============================================================================
1583 Engines::Salome_file_ptr
1584 Engines_Container_i::createSalome_file(const char* origFileName)
1586 std::string origName(origFileName);
1587 if (CORBA::is_nil(_Salome_file_map[origName]))
1589 Salome_file_i* aSalome_file = new Salome_file_i();
1590 aSalome_file->setContainer(Engines::Container::_duplicate(this->_this()));
1593 aSalome_file->setLocalFile(origFileName);
1594 aSalome_file->recvFiles();
1596 catch (const SALOME::SALOME_Exception& e)
1598 return Engines::Salome_file::_nil();
1601 Engines::Salome_file_var theSalome_file = Engines::Salome_file::_nil();
1602 theSalome_file = Engines::Salome_file::_narrow(aSalome_file->_this());
1603 _numInstanceMutex.lock() ; // lock to be alone (stl container write)
1604 _Salome_file_map[origName] = theSalome_file;
1605 _numInstanceMutex.unlock() ;
1608 Engines::Salome_file_ptr theSalome_file =
1609 Engines::Salome_file::_duplicate(_Salome_file_map[origName]);
1610 ASSERT(!CORBA::is_nil(theSalome_file));
1611 return theSalome_file;
1614 //=============================================================================
1615 /*! \brief copy a file from a remote host (container) to the local host
1616 * \param container the remote container
1617 * \param remoteFile the file to copy locally from the remote host into localFile
1618 * \param localFile the local file
1620 //=============================================================================
1621 void Engines_Container_i::copyFile(Engines::Container_ptr container, const char* remoteFile, const char* localFile)
1623 Engines::fileTransfer_var fileTransfer = container->getFileTransfer();
1626 if ((fp = fopen(localFile,"wb")) == NULL)
1628 INFOS("file " << localFile << " cannot be open for writing");
1632 CORBA::Long fileId = fileTransfer->open(remoteFile);
1635 Engines::fileBlock* aBlock;
1642 aBlock = fileTransfer->getBlock(fileId);
1643 toFollow = aBlock->length();
1645 CORBA::Octet *buf = aBlock->get_buffer();
1646 fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
1650 MESSAGE("end of transfer");
1651 fileTransfer->close(fileId);
1655 INFOS("open reference file for copy impossible");
1659 //=============================================================================
1660 /*! \brief create a PyNode object to execute remote python code
1661 * \param nodeName the name of the node
1662 * \param code the python code to load
1663 * \return the PyNode
1665 //=============================================================================
1666 Engines::PyNode_ptr Engines_Container_i::createPyNode(const char* nodeName, const char* code)
1668 Engines::PyNode_var node= Engines::PyNode::_nil();
1670 PyGILState_STATE gstate = PyGILState_Ensure();
1671 PyObject *res = PyObject_CallMethod(_pyCont,
1672 (char*)"create_pynode",
1680 PyGILState_Release(gstate);
1681 SALOME::ExceptionStruct es;
1682 es.type = SALOME::INTERNAL_ERROR;
1683 es.text = "can not create a python node";
1684 throw SALOME::SALOME_Exception(es);
1686 long ierr=PyInt_AsLong(PyTuple_GetItem(res,0));
1687 PyObject* result=PyTuple_GetItem(res,1);
1688 std::string astr=PyString_AsString(result);
1690 PyGILState_Release(gstate);
1693 Utils_Locker lck(&_mutexForDftPy);
1694 CORBA::Object_var obj=_orb->string_to_object(astr.c_str());
1695 node=Engines::PyNode::_narrow(obj);
1696 std::map<std::string,Engines::PyNode_var>::iterator it(_dftPyNode.find(nodeName));
1697 if(it==_dftPyNode.end())
1699 _dftPyNode[nodeName]=node;
1703 Engines::PyNode_var oldNode((*it).second);
1704 if(!CORBA::is_nil(oldNode))
1705 oldNode->UnRegister();
1708 if(!CORBA::is_nil(node))
1710 return node._retn();
1714 SALOME::ExceptionStruct es;
1715 es.type = SALOME::INTERNAL_ERROR;
1716 es.text = astr.c_str();
1717 throw SALOME::SALOME_Exception(es);
1721 //=============================================================================
1722 /*! \brief Retrieves the last created PyNode instance with createPyNode.
1725 //=============================================================================
1726 Engines::PyNode_ptr Engines_Container_i::getDefaultPyNode(const char *nodeName)
1728 Utils_Locker lck(&_mutexForDftPy);
1729 std::map<std::string,Engines::PyNode_var>::iterator it(_dftPyNode.find(nodeName));
1730 if(it==_dftPyNode.end())
1731 return Engines::PyNode::_nil();
1734 Engines::PyNode_var tmpVar((*it).second);
1735 if(!CORBA::is_nil(tmpVar))
1736 return Engines::PyNode::_duplicate(tmpVar);
1738 return Engines::PyNode::_nil();
1742 //=============================================================================
1743 /*! \brief create a PyScriptNode object to execute remote python code
1744 * \param nodeName the name of the node
1745 * \param code the python code to load
1746 * \return the PyScriptNode
1748 //=============================================================================
1749 Engines::PyScriptNode_ptr Engines_Container_i::createPyScriptNode(const char* nodeName, const char* code)
1751 Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil();
1753 PyGILState_STATE gstate = PyGILState_Ensure();
1754 PyObject *res = PyObject_CallMethod(_pyCont,
1755 (char*)"create_pyscriptnode",
1763 PyGILState_Release(gstate);
1764 SALOME::ExceptionStruct es;
1765 es.type = SALOME::INTERNAL_ERROR;
1766 es.text = "can not create a python node";
1767 throw SALOME::SALOME_Exception(es);
1769 long ierr=PyInt_AsLong(PyTuple_GetItem(res,0));
1770 PyObject* result=PyTuple_GetItem(res,1);
1771 std::string astr=PyString_AsString(result);
1773 PyGILState_Release(gstate);
1777 Utils_Locker lck(&_mutexForDftPy);
1778 CORBA::Object_var obj=_orb->string_to_object(astr.c_str());
1779 node=Engines::PyScriptNode::_narrow(obj);
1780 std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
1781 if(it==_dftPyScriptNode.end())
1783 _dftPyScriptNode[nodeName]=node;
1787 Engines::PyScriptNode_var oldNode((*it).second);
1788 if(!CORBA::is_nil(oldNode))
1789 oldNode->UnRegister();
1792 if(!CORBA::is_nil(node))
1794 return node._retn();
1798 SALOME::ExceptionStruct es;
1799 es.type = SALOME::INTERNAL_ERROR;
1800 es.text = astr.c_str();
1801 throw SALOME::SALOME_Exception(es);
1805 //=============================================================================
1806 /*! \brief Retrieves the last created PyScriptNode instance with createPyScriptNode.
1809 //=============================================================================
1810 Engines::PyScriptNode_ptr Engines_Container_i::getDefaultPyScriptNode(const char *nodeName)
1812 Utils_Locker lck(&_mutexForDftPy);
1813 std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
1814 if(it==_dftPyScriptNode.end())
1815 return Engines::PyScriptNode::_nil();
1818 Engines::PyScriptNode_var tmpVar((*it).second);
1819 if(!CORBA::is_nil(tmpVar))
1820 return Engines::PyScriptNode::_duplicate(tmpVar);
1822 return Engines::PyScriptNode::_nil();
1826 //=============================================================================
1827 /* int checkifexecutable(const char *filename)
1829 * Return non-zero if the name is an executable file, and
1830 * zero if it is not executable, or if it does not exist.
1832 //=============================================================================
1833 int checkifexecutable(const std::string& filename)
1836 struct stat statinfo;
1838 result = stat(filename.c_str(), &statinfo);
1839 if (result < 0) return 0;
1840 if (!S_ISREG(statinfo.st_mode)) return 0;
1845 if (statinfo.st_uid == geteuid()) return statinfo.st_mode & S_IXUSR;
1846 if (statinfo.st_gid == getegid()) return statinfo.st_mode & S_IXGRP;
1847 return statinfo.st_mode & S_IXOTH;
1852 //=============================================================================
1853 /*! \brief Find a file by searching in a path
1854 * \param filename file name to search
1855 * \param path path to search in
1856 * \param pth the complete file path if found
1857 * \return 1 if found 0 if not
1859 //=============================================================================
1860 int findpathof(const std::string& path, std::string& pth, const std::string& filename)
1862 if ( path.size() == 0 ) return 0;
1864 std::string::size_type offset = 0;
1865 std::string::size_type pos = 0;
1867 struct stat statinfo;
1871 pos = path.find( SEP, offset );
1872 pth = path.substr( offset, pos - offset );
1873 if ( pth.size() > 0 )
1875 if( pth[pth.size()-1] != SLASH ) pth += SLASH;
1877 int result=stat(pth.c_str(), &statinfo);
1878 if(result == 0) found=1;
1880 if (pos == std::string::npos) break;
1886 void Engines_Container_i::registerTemporaryFile( const std::string& fileName )
1888 _tmp_files.remove( fileName );
1889 _tmp_files.push_back( fileName );
1892 void Engines_Container_i::unregisterTemporaryFile( const std::string& fileName )
1894 _tmp_files.remove( fileName );
1897 void Engines_Container_i::clearTemporaryFiles()
1899 std::list<std::string>::const_iterator it;
1900 for ( it = _tmp_files.begin(); it != _tmp_files.end(); ++it ) {
1902 std::string command = "del /F /P";
1904 std::string command = "rm -rf ";
1907 system( command.c_str() );