X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FContainer%2FContainer_i.cxx;h=315f7cee6c1e5f8ea7fa975b6c0d21c19892f304;hb=888f04cad3e3fa57bf92c96789959fc87bfdd555;hp=7db065eda04a31e3b978be2bfde370afa06d46e6;hpb=5fa9fa2955b0f82b2b8ad8a7cad41c26e9254b08;p=modules%2Fkernel.git diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index 7db065eda..315f7cee6 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE // // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -54,6 +54,8 @@ int SIGUSR1 = 1000; #include "SALOME_FileTransfer_i.hxx" #include "Salome_file_i.hxx" #include "SALOME_NamingService.hxx" +#include "SALOME_Fake_NamingService.hxx" +#include "SALOME_Embedded_NamingService_Client.hxx" #include "Basics_Utils.hxx" #ifdef _XOPEN_SOURCE @@ -78,7 +80,11 @@ extern "C" {void SigIntHandler( int ) ; } #ifndef WIN32 #define LIB "lib" +#ifdef __APPLE__ +#define ENGINESO "Engine.dylib" +#else #define ENGINESO "Engine.so" +#endif #else #define LIB "" #define ENGINESO "Engine.dll" @@ -115,13 +121,13 @@ int findpathof(const std::string& path, std::string&, const std::string&); //============================================================================= Engines_Container_i::Engines_Container_i () : -_numInstance(0),_id(0),_NS(0) + _NS(0),_id(0),_numInstance(0) { } //============================================================================= /*! -* Construtor to use +* Constructor to use */ //============================================================================= @@ -129,14 +135,14 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, PortableServer::POA_ptr poa, char *containerName , int argc , char* argv[], - bool activAndRegist, + SALOME_NamingService_Container_Abstract *ns, bool isServantAloneInProcess ) : - _numInstance(0),_isServantAloneInProcess(isServantAloneInProcess),_id(0),_NS(0) + _NS(nullptr),_id(0),_numInstance(0),_isServantAloneInProcess(isServantAloneInProcess) { _pid = (long)getpid(); - if(activAndRegist) + if(ns) ActSigIntHandler() ; _argc = argc ; @@ -173,22 +179,20 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, // Pour les containers paralleles: il ne faut pas enregistrer et activer // le container generique, mais le container specialise - if(activAndRegist) { _id = _poa->activate_object(this); - _NS = new SALOME_NamingService(); + // key point : if ns is nullptr : this servant is alone in its process + // if ns is not null : this servant embedded into single process. + _NS = ns==nullptr ? new SALOME_NamingService : ns->clone(); _NS->init_orb( _orb ) ; CORBA::Object_var obj=_poa->id_to_reference(*_id); - Engines::Container_var pCont - = Engines::Container::_narrow(obj); + Engines::Container_var pCont = Engines::Container::_narrow(obj); _remove_ref(); - _containerName = _NS->BuildContainerNameForNS(containerName, - hostname.c_str()); + _containerName = SALOME_NamingService_Abstract::BuildContainerNameForNS(containerName, hostname.c_str()); SCRUTE(_containerName); _NS->Register(pCont, _containerName.c_str()); - MESSAGE("Engines_Container_i::Engines_Container_i : Container name " - << _containerName); + MESSAGE("Engines_Container_i::Engines_Container_i : Container name " << _containerName); // Python: // import SALOME_Container @@ -254,18 +258,7 @@ Engines_Container_i::~Engines_Container_i() delete _id; if(_NS) delete _NS; - for(std::map::iterator it=_dftPyNode.begin();it!=_dftPyNode.end();it++) - { - Engines::PyNode_var tmpVar((*it).second); - if(!CORBA::is_nil(tmpVar)) - tmpVar->UnRegister(); - } - for(std::map::iterator it=_dftPyScriptNode.begin();it!=_dftPyScriptNode.end();it++) - { - Engines::PyScriptNode_var tmpVar((*it).second); - if(!CORBA::is_nil(tmpVar)) - tmpVar->UnRegister(); - } + cleanAllPyScripts(); } //============================================================================= @@ -370,24 +363,33 @@ void Engines_Container_i::Shutdown() */ std::map::iterator itm; for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++) + { + try { - try - { - itm->second->destroy(); - } - catch(const CORBA::Exception& e) - { - // ignore this entry and continue - } - catch(...) - { - // ignore this entry and continue - } + itm->second->destroy(); + } + catch(const CORBA::Exception&) + { + // ignore this entry and continue + } + catch(...) + { + // ignore this entry and continue } + } _listInstances_map.clear(); - _NS->Destroy_FullDirectory(_containerName.c_str()); - _NS->Destroy_Name(_containerName.c_str()); + // NS unregistering may throw in SSL mode if master process hosting SALOME_Embedded_NamingService servant has vanished + // In this case it's skip it and still continue. + try + { + _NS->Destroy_FullDirectory(_containerName.c_str()); + _NS->Destroy_Name(_containerName.c_str()); + } + catch(...) + { + } + // if(_isServantAloneInProcess) { MESSAGE("Effective Shutdown of container Begins..."); @@ -402,7 +404,7 @@ void Engines_Container_i::Shutdown() * CORBA method * \param componentName component name * \param reason explains error when load fails -* \return true if dlopen successfull or already done, false otherwise +* \return true if dlopen successful or already done, false otherwise */ //============================================================================= bool @@ -414,15 +416,15 @@ Engines_Container_i::load_component_Library(const char* componentName, CORBA::St //================================================================= std::string retso; if(load_component_CppImplementation(componentName,retso)) - { - reason=CORBA::string_dup(""); - return true; - } + { + reason=CORBA::string_dup(""); + return true; + } else if(retso != "ImplementationNotFound") - { - reason=CORBA::string_dup(retso.c_str()); - return false; - } + { + reason=CORBA::string_dup(retso.c_str()); + return false; + } retso="Component "; retso+=componentName; @@ -434,15 +436,15 @@ Engines_Container_i::load_component_Library(const char* componentName, CORBA::St //================================================================= std::string retpy; if(load_component_PythonImplementation(componentName,retpy)) - { - reason=CORBA::string_dup(""); - return true; - } + { + reason=CORBA::string_dup(""); + return true; + } else if(retpy != "ImplementationNotFound") - { - reason=CORBA::string_dup(retpy.c_str()); - return false; - } + { + reason=CORBA::string_dup(retpy.c_str()); + return false; + } retpy="Component "; retpy+=componentName; @@ -455,15 +457,15 @@ Engines_Container_i::load_component_Library(const char* componentName, CORBA::St //================================================================= std::string retex; if(load_component_ExecutableImplementation(componentName,retex)) - { - reason=CORBA::string_dup(""); - return true; - } + { + reason=CORBA::string_dup(""); + return true; + } else if(retex != "ImplementationNotFound") - { - reason=CORBA::string_dup(retex.c_str()); - return false; - } + { + reason=CORBA::string_dup(retex.c_str()); + return false; + } retex="Component "; retex+=componentName; @@ -490,7 +492,7 @@ Engines_Container_i::load_component_Library(const char* componentName, CORBA::St * C++ method: * \param componentName the name of the component (COMPONENT, for example) * \param reason explains error when load fails -* \return true if loading is successfull or already done, false otherwise +* \return true if loading is successful or already done, false otherwise */ //============================================================================= bool @@ -504,22 +506,26 @@ Engines_Container_i::load_component_CppImplementation(const char* componentName, // (see decInstanceCnt, finalize_removal)) if (_toRemove_map.count(impl_name) != 0) _toRemove_map.erase(impl_name); if (_library_map.count(impl_name) != 0) - { - MESSAGE("Library " << impl_name << " already loaded"); - _numInstanceMutex.unlock(); - reason=""; - return true; - } + { + MESSAGE("Library " << impl_name << " already loaded"); + _numInstanceMutex.unlock(); + reason=""; + return true; + } _numInstanceMutex.unlock(); #ifndef WIN32 void* handle; - handle = dlopen( impl_name.c_str() , RTLD_NOW ) ; + handle = dlopen( impl_name.c_str() , RTLD_NOW | RTLD_GLOBAL ) ; if ( !handle ) - { - //not loadable. Try to find the lib file in LD_LIBRARY_PATH - std::string path; + { + //not loadable. Try to find the lib file in LD_LIBRARY_PATH + std::string path; +#ifdef __APPLE__ + char* p=getenv("DYLD_LIBRARY_PATH"); +#else char* p=getenv("LD_LIBRARY_PATH"); +#endif if(p)path=p; path=path+SEP+"/usr/lib"+SEP+"/lib"; @@ -542,25 +548,30 @@ Engines_Container_i::load_component_CppImplementation(const char* componentName, //continue with other implementation reason="ImplementationNotFound"; return false; - } } + } #else HINSTANCE handle; - handle = LoadLibrary( impl_name.c_str() ); +#ifdef UNICODE + std::wstring libToLoad = Kernel_Utils::utf8_decode_s( impl_name ); +#else + std::string libToLoad = impl_name; +#endif + handle = LoadLibrary(libToLoad.c_str() ); if ( !handle ) - { - reason="ImplementationNotFound"; - } + { + reason="ImplementationNotFound"; + } #endif if ( handle ) - { - _numInstanceMutex.lock(); - _library_map[impl_name] = handle; - _numInstanceMutex.unlock(); - reason=""; - return true; - } + { + _numInstanceMutex.lock(); + _library_map[impl_name] = handle; + _numInstanceMutex.unlock(); + reason=""; + return true; + } return false; @@ -571,7 +582,7 @@ Engines_Container_i::load_component_CppImplementation(const char* componentName, * C++ method: * \param componentName name of the component * \param reason explains error when load fails -* \return true if loading is successfull or already done, false otherwise +* \return true if loading is successful or already done, false otherwise */ //============================================================================= bool @@ -581,11 +592,11 @@ Engines_Container_i::load_component_PythonImplementation(const char* componentNa _numInstanceMutex.lock() ; // lock to be alone (stl container write) if (_library_map.count(aCompName) != 0) - { - _numInstanceMutex.unlock() ; - reason=""; - return true; // Python Component, already imported - } + { + _numInstanceMutex.unlock() ; + reason=""; + return true; // Python Component, already imported + } _numInstanceMutex.unlock() ; PyGILState_STATE gstate = PyGILState_Ensure(); @@ -593,30 +604,30 @@ Engines_Container_i::load_component_PythonImplementation(const char* componentNa (char*)"import_component", (char*)"s",componentName); - reason=PyString_AsString(result); + reason=PyUnicode_AsUTF8(result); Py_XDECREF(result); SCRUTE(reason); PyGILState_Release(gstate); if (reason=="") - { - //Python component has been loaded (import componentName) - _numInstanceMutex.lock() ; // lock to be alone (stl container write) - _library_map[aCompName] = (void *)_pyCont; // any non O value OK - _numInstanceMutex.unlock() ; - MESSAGE("import Python: "<< aCompName <<" OK"); - return true; - } + { + //Python component has been loaded (import componentName) + _numInstanceMutex.lock() ; // lock to be alone (stl container write) + _library_map[aCompName] = (void *)_pyCont; // any non O value OK + _numInstanceMutex.unlock() ; + MESSAGE("import Python: "<< aCompName <<" OK"); + return true; + } else if(reason=="ImplementationNotFound") - { - //Python implementation has not been found. Continue with other implementation - reason="ImplementationNotFound"; - } + { + //Python implementation has not been found. Continue with other implementation + reason="ImplementationNotFound"; + } else - { - //Python implementation has been found but loading has failed - std::cerr << reason << std::endl; - } + { + //Python implementation has been found but loading has failed + std::cerr << reason << std::endl; + } return false; } @@ -626,7 +637,7 @@ Engines_Container_i::load_component_PythonImplementation(const char* componentNa * C++ method: * \param componentName name of the component * \param reason explains error when load fails -* \return true if loading is successfull or already done, false otherwise +* \return true if loading is successful or already done, false otherwise */ //============================================================================= bool @@ -642,27 +653,27 @@ Engines_Container_i::load_component_ExecutableImplementation(const char* compone if(p)path=p; if (findpathof(path, pth, executable)) + { + if(checkifexecutable(pth)) { - if(checkifexecutable(pth)) - { - _numInstanceMutex.lock() ; // lock to be alone (stl container write) - _library_map[executable] = (void *)1; // any non O value OK - _numInstanceMutex.unlock() ; - MESSAGE("import executable: "<< pth <<" OK"); - reason=""; - return true; - } - reason="Component "; - reason+=aCompName; - reason+=": implementation found "; - reason+=pth; - reason+=" but it is not executable"; - std::cerr << reason << std::endl; + _numInstanceMutex.lock() ; // lock to be alone (stl container write) + _library_map[executable] = (void *)1; // any non O value OK + _numInstanceMutex.unlock() ; + MESSAGE("import executable: "<< pth <<" OK"); + reason=""; + return true; } + reason="Component "; + reason+=aCompName; + reason+=": implementation found "; + reason+=pth; + reason+=" but it is not executable"; + std::cerr << reason << std::endl; + } else - { - reason="ImplementationNotFound"; - } + { + reason="ImplementationNotFound"; + } return false; } @@ -673,19 +684,16 @@ Engines_Container_i::load_component_ExecutableImplementation(const char* compone * The servant registers itself to naming service and Registry. * \param genericRegisterName Name of the component instance to register * in Registry & Name Service (without _inst_n suffix) -* \param studyId 0 for multiStudy instance, -* study Id (>0) otherwise * \return a loaded component */ //============================================================================= Engines::EngineComponent_ptr -Engines_Container_i::create_component_instance(const char*genericRegisterName, - CORBA::Long studyId) +Engines_Container_i::create_component_instance(const char*genericRegisterName) { Engines::FieldsDict_var env = new Engines::FieldsDict; char* reason; Engines::EngineComponent_ptr compo = - create_component_instance_env(genericRegisterName, studyId, env, reason); + create_component_instance_env(genericRegisterName, env, reason); CORBA::string_free(reason); return compo; } @@ -697,8 +705,6 @@ Engines_Container_i::create_component_instance(const char*genericRegisterName, * The servant registers itself to naming service and Registry. * \param genericRegisterName Name of the component instance to register * in Registry & Name Service (without _inst_n suffix) -* \param studyId 0 for multiStudy instance, -* study Id (>0) otherwise * \param env dict of env variables * \param reason explains error when create_component_instance_env fails * \return a loaded component @@ -706,44 +712,36 @@ Engines_Container_i::create_component_instance(const char*genericRegisterName, //============================================================================= Engines::EngineComponent_ptr Engines_Container_i::create_component_instance_env(const char*genericRegisterName, - CORBA::Long studyId, const Engines::FieldsDict& env, CORBA::String_out reason) { - if (studyId < 0) - { - INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy"); - reason=CORBA::string_dup("studyId must be > 0 for mono study instance, =0 for multiStudy"); - return Engines::EngineComponent::_nil() ; - } - std::string error; if (_library_map.count(genericRegisterName) != 0) - { - // It's a Python component - Engines::EngineComponent_ptr compo = createPythonInstance(genericRegisterName, studyId, error); - reason=CORBA::string_dup(error.c_str()); - return compo; - } + { + // It's a Python component + Engines::EngineComponent_ptr compo = createPythonInstance(genericRegisterName, error); + reason=CORBA::string_dup(error.c_str()); + return compo; + } std::string impl_name = std::string(LIB) + genericRegisterName + ENGINESO; if (_library_map.count(impl_name) != 0) - { - // It's a C++ component - void* handle = _library_map[impl_name]; - Engines::EngineComponent_ptr compo = createInstance(genericRegisterName, handle, studyId, error); - reason=CORBA::string_dup(error.c_str()); - return compo; - } + { + // It's a C++ component + void* handle = _library_map[impl_name]; + Engines::EngineComponent_ptr compo = createInstance(genericRegisterName, handle, error); + reason=CORBA::string_dup(error.c_str()); + return compo; + } impl_name = std::string(genericRegisterName) + ".exe"; if (_library_map.count(impl_name) != 0) - { - //It's an executable component - Engines::EngineComponent_ptr compo = createExecutableInstance(genericRegisterName, studyId, env, error); - reason=CORBA::string_dup(error.c_str()); - return compo; - } + { + //It's an executable component + Engines::EngineComponent_ptr compo = createExecutableInstance(genericRegisterName, env, error); + reason=CORBA::string_dup(error.c_str()); + return compo; + } error="load_component_Library has probably not been called for component: "; error += genericRegisterName; @@ -756,8 +754,6 @@ Engines_Container_i::create_component_instance_env(const char*genericRegisterNam //! Create a new component instance (Executable implementation) /*! * \param CompName Name of the component instance -* \param studyId 0 for multiStudy instance, -* study Id (>0) otherwise * \param env dict of env variables * \param reason explains error when creation fails * \return a loaded component @@ -768,7 +764,7 @@ Engines_Container_i::create_component_instance_env(const char*genericRegisterNam */ //============================================================================= Engines::EngineComponent_ptr -Engines_Container_i::createExecutableInstance(std::string CompName, int studyId, +Engines_Container_i::createExecutableInstance(std::string CompName, const Engines::FieldsDict& env, std::string& reason) { @@ -822,35 +818,35 @@ Engines_Container_i::createExecutableInstance(std::string CompName, int studyId, int status; pid_t pid = fork(); if(pid == 0) // child + { + for (CORBA::ULong i=0; i < env.length(); i++) { - for (CORBA::ULong i=0; i < env.length(); i++) - { - if (env[i].value.type()->kind() == CORBA::tk_string) - { - const char* value; - env[i].value >>= value; - std::string s(env[i].key); - s+='='; - s+=value; - putenv(strdup(s.c_str())); - } - } - - execl("/bin/sh", "sh", "-c", command.c_str() , (char *)0); - status=-1; + if (env[i].value.type()->kind() == CORBA::tk_string) + { + const char* value; + env[i].value >>= value; + std::string s(env[i].key); + s+='='; + s+=value; + putenv(strdup(s.c_str())); + } } + + execl("/bin/sh", "sh", "-c", command.c_str() , (char *)0); + status=-1; + } else if(pid < 0) // failed to fork - { - status=-1; - } + { + status=-1; + } else //parent + { + pid_t tpid; + do { - pid_t tpid; - do - { - tpid = wait(&status); - } while (tpid != pid); - } + tpid = wait(&status); + } while (tpid != pid); + } #else // launch component with a system call int status=system(command.c_str()); @@ -920,14 +916,12 @@ Engines_Container_i::createExecutableInstance(std::string CompName, int studyId, //! Create a new component instance (Python implementation) /*! * \param CompName Name of the component instance -* \param studyId 0 for multiStudy instance, -* study Id (>0) otherwise * \param reason explains error when creation fails * \return a loaded component */ //============================================================================= Engines::EngineComponent_ptr -Engines_Container_i::createPythonInstance(std::string CompName, int studyId, +Engines_Container_i::createPythonInstance(std::string CompName, std::string& reason) { Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ; @@ -945,10 +939,9 @@ Engines_Container_i::createPythonInstance(std::string CompName, int studyId, PyGILState_STATE gstate = PyGILState_Ensure(); PyObject *result = PyObject_CallMethod(_pyCont, (char*)"create_component_instance", - (char*)"ssl", + (char*)"ss", CompName.c_str(), - instanceName.c_str(), - studyId); + instanceName.c_str()); const char *ior; const char *error; PyArg_ParseTuple(result,"ss", &ior, &error); @@ -958,11 +951,11 @@ Engines_Container_i::createPythonInstance(std::string CompName, int studyId, PyGILState_Release(gstate); if( iors!="" ) - { - CORBA::Object_var obj = _orb->string_to_object(iors.c_str()); - iobject = Engines::EngineComponent::_narrow( obj ) ; - _listInstances_map[instanceName] = iobject; - } + { + CORBA::Object_var obj = _orb->string_to_object(iors.c_str()); + iobject = Engines::EngineComponent::_narrow( obj ) ; + _listInstances_map[instanceName] = iobject; + } return iobject._retn(); } @@ -985,10 +978,9 @@ Engines_Container_i::create_python_service_instance(const char * CompName, PyGILState_STATE gstate = PyGILState_Ensure(); PyObject *result = PyObject_CallMethod(_pyCont, (char*)"create_component_instance", - (char*)"ssl", + (char*)"ss", CompName, - instanceName.c_str(), - 0); + instanceName.c_str()); const char *ior; const char *error; PyArg_ParseTuple(result,"ss", &ior, &error); @@ -1009,8 +1001,6 @@ Engines_Container_i::create_python_service_instance(const char * CompName, * in Registry & Name Service, * (without _inst_n suffix, like "COMPONENT") * \param handle loaded library handle -* \param studyId 0 for multiStudy instance, -* study Id (>0) otherwise * \param reason explains error when creation fails * \return a loaded component * @@ -1026,7 +1016,6 @@ Engines_Container_i::create_python_service_instance(const char * CompName, Engines::EngineComponent_ptr Engines_Container_i::createInstance(std::string genericRegisterName, void *handle, - int studyId, std::string& reason) { // --- find the factory @@ -1049,10 +1038,10 @@ Engines_Container_i::createInstance(std::string genericRegisterName, if ( !Component_factory ) { - INFOS( "Can't resolve symbol: " + factory_name ); + MESSAGE( "Can't resolve symbol: " + factory_name ); #ifndef WIN32 reason=dlerror(); - INFOS(reason); + MESSAGE(reason); #endif return Engines::EngineComponent::_nil() ; } @@ -1074,36 +1063,29 @@ Engines_Container_i::createInstance(std::string genericRegisterName, std::string component_registerName = _containerName + "/" + instanceName; - // --- Instanciate required CORBA object + // --- Instantiate required CORBA object PortableServer::ObjectId *id ; //not owner, do not delete (nore use var) id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(), aGenRegisterName.c_str() ) ; if (id == NULL) - { - reason="Can't get ObjectId from factory"; - INFOS(reason); - return iobject._retn(); - } + { + reason="Can't get ObjectId from factory"; + INFOS(reason); + return iobject._retn(); + } - // --- get reference & servant from id + // --- get reference from id CORBA::Object_var obj = _poa->id_to_reference(*id); iobject = Engines::EngineComponent::_narrow( obj ) ; - Engines_Component_i *servant = - dynamic_cast(_poa->reference_to_servant(iobject)); - ASSERT(servant); - //SCRUTE(servant->_refcount_value()); _numInstanceMutex.lock() ; // lock to be alone (stl container write) _listInstances_map[instanceName] = iobject; _cntInstances_map[aGenRegisterName] += 1; _numInstanceMutex.unlock() ; SCRUTE(aGenRegisterName); SCRUTE(_cntInstances_map[aGenRegisterName]); - servant->setStudyId(studyId); - servant->_remove_ref(); // do not need servant any more (remove ref from reference_to_servant) - //SCRUTE(servant->_refcount_value()); // --- register the engine under the name // containerName(.dir)/instanceName(.object) @@ -1113,7 +1095,7 @@ Engines_Container_i::createInstance(std::string genericRegisterName, } catch (...) { - reason="Container_i::createInstance exception catched"; + reason="Container_i::createInstance exception caught"; INFOS(reason) ; } return iobject._retn(); @@ -1125,14 +1107,11 @@ Engines_Container_i::createInstance(std::string genericRegisterName, * CORBA method: Finds a servant instance of a component * \param registeredName Name of the component in Registry or Name Service, * without instance suffix number -* \param studyId 0 if instance is not associated to a study, -* >0 otherwise (== study id) -* \return the first instance found with same studyId +* \return the first found instance */ //============================================================================= Engines::EngineComponent_ptr -Engines_Container_i::find_component_instance( const char* registeredName, - CORBA::Long studyId) +Engines_Container_i::find_component_instance( const char* registeredName) { Engines::EngineComponent_var anEngine = Engines::EngineComponent::_nil(); std::map::iterator itm =_listInstances_map.begin(); @@ -1143,10 +1122,7 @@ Engines_Container_i::find_component_instance( const char* registeredName, if (instance.find(registeredName) == 0) { anEngine = (*itm).second; - if (studyId == anEngine->getStudyId()) - { - return anEngine._retn(); - } + return anEngine._retn(); } itm++; } @@ -1250,7 +1226,7 @@ void Engines_Container_i::decInstanceCnt(std::string genericRegisterName) Engines::EngineComponent_ptr Engines_Container_i::load_impl( const char* genericRegisterName, - const char* componentName ) + const char* /*componentName*/ ) { char* reason; std::string impl_name = std::string(LIB) + genericRegisterName + ENGINESO; @@ -1261,6 +1237,20 @@ Engines_Container_i::load_impl( const char* genericRegisterName, return iobject._retn(); } +Engines::EmbeddedNamingService_ptr Engines_Container_i::get_embedded_NS_if_ssl() +{ + SALOME_Embedded_NamingService_Client *nsc(dynamic_cast(this->_NS)); + if(nsc) + { + Engines::EmbeddedNamingService_var obj = nsc->GetObject(); + return Engines::EmbeddedNamingService::_duplicate(obj); + } + else + { + return Engines::EmbeddedNamingService::_nil(); + } +} + //============================================================================= //! Finds an already existing component instance or create a new instance /*! @@ -1312,34 +1302,16 @@ Engines_Container_i::find_or_create_instance(std::string genericRegisterName, { iobject = createInstance(genericRegisterName, handle, - 0, - reason); // force multiStudy instance here ! + reason); } else { iobject = Engines::EngineComponent::_narrow( obj ) ; - Engines_Component_i *servant = - dynamic_cast - (_poa->reference_to_servant(iobject)); - ASSERT(servant) - int studyId = servant->getStudyId(); - ASSERT (studyId >= 0); - if (studyId == 0) // multiStudy instance, OK - { - // No ReBind ! - MESSAGE(component_registerBase.c_str()<<" already bound"); - } - else // monoStudy instance: NOK - { - iobject = Engines::EngineComponent::_nil(); - INFOS("load_impl & find_component_instance methods " - << "NOT SUITABLE for mono study components"); - } } } catch (...) { - INFOS( "Container_i::load_impl catched" ) ; + INFOS( "Container_i::load_impl caught" ) ; } return iobject._retn(); } @@ -1354,7 +1326,7 @@ Engines_Container_i::find_or_create_instance(std::string genericRegisterName, bool Engines_Container_i::isPythonContainer(const char* ContainerName) { bool ret=false; - int len=strlen(ContainerName); + size_t len=strlen(ContainerName); if(len>=2) if(strcmp(ContainerName+len-2,"Py")==0) ret=true; @@ -1437,9 +1409,9 @@ void SetCpuUsed() ; void CallCancelThread() ; #ifndef WIN32 -void SigIntHandler(int what , +void SigIntHandler(int /*what*/ , siginfo_t * siginfo , - void * toto ) + void * /*toto*/ ) { //PAL9042 JR : during the execution of a Signal Handler (and of methods called through Signal Handlers) // use of streams (and so on) should never be used because : @@ -1542,7 +1514,7 @@ Engines_Container_i::createFileRef(const char* origFileName) if (origName[0] != '/') { - INFOS("path of file to copy must be an absolute path begining with '/'"); + INFOS("path of file to copy must be an absolute path beginning with '/'"); return Engines::fileRef::_nil(); } @@ -1593,7 +1565,7 @@ Engines_Container_i::createSalome_file(const char* origFileName) aSalome_file->setLocalFile(origFileName); aSalome_file->recvFiles(); } - catch (const SALOME::SALOME_Exception& e) + catch (const SALOME::SALOME_Exception& /*e*/) //!< TODO: unused variable { return Engines::Salome_file::_nil(); } @@ -1624,36 +1596,36 @@ void Engines_Container_i::copyFile(Engines::Container_ptr container, const char* FILE* fp; if ((fp = fopen(localFile,"wb")) == NULL) - { - INFOS("file " << localFile << " cannot be open for writing"); - return; - } + { + INFOS("file " << localFile << " cannot be open for writing"); + return; + } CORBA::Long fileId = fileTransfer->open(remoteFile); if (fileId > 0) + { + Engines::fileBlock* aBlock; + int toFollow = 1; + int ctr=0; + while (toFollow) { - Engines::fileBlock* aBlock; - int toFollow = 1; - int ctr=0; - while (toFollow) - { - ctr++; - SCRUTE(ctr); - aBlock = fileTransfer->getBlock(fileId); - toFollow = aBlock->length(); - SCRUTE(toFollow); - CORBA::Octet *buf = aBlock->get_buffer(); - fwrite(buf, sizeof(CORBA::Octet), toFollow, fp); - delete aBlock; - } - fclose(fp); - MESSAGE("end of transfer"); - fileTransfer->close(fileId); + ctr++; + //SCRUTE(ctr); + aBlock = fileTransfer->getBlock(fileId); + toFollow = aBlock->length(); + //SCRUTE(toFollow); + CORBA::Octet *buf = aBlock->get_buffer(); + fwrite(buf, sizeof(CORBA::Octet), toFollow, fp); + delete aBlock; } + fclose(fp); + MESSAGE("end of transfer"); + fileTransfer->close(fileId); + } else - { - INFOS("open reference file for copy impossible"); - } + { + INFOS("open reference file for copy impossible"); + } } //============================================================================= @@ -1665,57 +1637,57 @@ void Engines_Container_i::copyFile(Engines::Container_ptr container, const char* //============================================================================= Engines::PyNode_ptr Engines_Container_i::createPyNode(const char* nodeName, const char* code) { - Engines::PyNode_var node= Engines::PyNode::_nil(); + Engines::PyNode_var node= Engines::PyNode::_nil(); - PyGILState_STATE gstate = PyGILState_Ensure(); - PyObject *res = PyObject_CallMethod(_pyCont, - (char*)"create_pynode", - (char*)"ss", - nodeName, - code); - if(res==NULL) - { - //internal error - PyErr_Print(); - PyGILState_Release(gstate); - SALOME::ExceptionStruct es; - es.type = SALOME::INTERNAL_ERROR; - es.text = "can not create a python node"; - throw SALOME::SALOME_Exception(es); - } - long ierr=PyInt_AsLong(PyTuple_GetItem(res,0)); - PyObject* result=PyTuple_GetItem(res,1); - std::string astr=PyString_AsString(result); - Py_DECREF(res); + PyGILState_STATE gstate = PyGILState_Ensure(); + PyObject *res = PyObject_CallMethod(_pyCont, + (char*)"create_pynode", + (char*)"ss", + nodeName, + code); + if(res==NULL) + { + //internal error + PyErr_Print(); PyGILState_Release(gstate); - if(ierr==0) - { - Utils_Locker lck(&_mutexForDftPy); - CORBA::Object_var obj=_orb->string_to_object(astr.c_str()); - node=Engines::PyNode::_narrow(obj); - std::map::iterator it(_dftPyNode.find(nodeName)); - if(it==_dftPyNode.end()) - { - _dftPyNode[nodeName]=node; - } - else - { - Engines::PyNode_var oldNode((*it).second); - if(!CORBA::is_nil(oldNode)) - oldNode->UnRegister(); - (*it).second=node; - } - if(!CORBA::is_nil(node)) - node->Register(); - return node._retn(); - } + SALOME::ExceptionStruct es; + es.type = SALOME::INTERNAL_ERROR; + es.text = "can not create a python node"; + throw SALOME::SALOME_Exception(es); + } + long ierr=PyLong_AsLong(PyTuple_GetItem(res,0)); + PyObject* result=PyTuple_GetItem(res,1); + std::string astr=PyUnicode_AsUTF8(result); + Py_DECREF(res); + PyGILState_Release(gstate); + if(ierr==0) + { + Utils_Locker lck(&_mutexForDftPy); + CORBA::Object_var obj=_orb->string_to_object(astr.c_str()); + node=Engines::PyNode::_narrow(obj); + std::map::iterator it(_dftPyNode.find(nodeName)); + if(it==_dftPyNode.end()) + { + _dftPyNode[nodeName]=node; + } else - { - SALOME::ExceptionStruct es; - es.type = SALOME::INTERNAL_ERROR; - es.text = astr.c_str(); - throw SALOME::SALOME_Exception(es); - } + { + Engines::PyNode_var oldNode((*it).second); + if(!CORBA::is_nil(oldNode)) + oldNode->UnRegister(); + (*it).second=node; + } + if(!CORBA::is_nil(node)) + node->Register(); + return node._retn(); + } + else + { + SALOME::ExceptionStruct es; + es.type = SALOME::INTERNAL_ERROR; + es.text = astr.c_str(); + throw SALOME::SALOME_Exception(es); + } } //============================================================================= @@ -1730,13 +1702,13 @@ Engines::PyNode_ptr Engines_Container_i::getDefaultPyNode(const char *nodeName) if(it==_dftPyNode.end()) return Engines::PyNode::_nil(); else - { - Engines::PyNode_var tmpVar((*it).second); - if(!CORBA::is_nil(tmpVar)) - return Engines::PyNode::_duplicate(tmpVar); - else - return Engines::PyNode::_nil(); - } + { + Engines::PyNode_var tmpVar((*it).second); + if(!CORBA::is_nil(tmpVar)) + return Engines::PyNode::_duplicate(tmpVar); + else + return Engines::PyNode::_nil(); + } } //============================================================================= @@ -1748,58 +1720,89 @@ Engines::PyNode_ptr Engines_Container_i::getDefaultPyNode(const char *nodeName) //============================================================================= Engines::PyScriptNode_ptr Engines_Container_i::createPyScriptNode(const char* nodeName, const char* code) { - Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil(); + Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil(); - PyGILState_STATE gstate = PyGILState_Ensure(); - PyObject *res = PyObject_CallMethod(_pyCont, - (char*)"create_pyscriptnode", - (char*)"ss", - nodeName, - code); - if(res==NULL) - { - //internal error - PyErr_Print(); - PyGILState_Release(gstate); - SALOME::ExceptionStruct es; - es.type = SALOME::INTERNAL_ERROR; - es.text = "can not create a python node"; - throw SALOME::SALOME_Exception(es); - } - long ierr=PyInt_AsLong(PyTuple_GetItem(res,0)); - PyObject* result=PyTuple_GetItem(res,1); - std::string astr=PyString_AsString(result); - Py_DECREF(res); + PyGILState_STATE gstate = PyGILState_Ensure(); + PyObject *res = PyObject_CallMethod(_pyCont, + (char*)"create_pyscriptnode", + (char*)"ss", + nodeName, + code); + if(res==NULL) + { + //internal error + PyErr_Print(); PyGILState_Release(gstate); + SALOME::ExceptionStruct es; + es.type = SALOME::INTERNAL_ERROR; + es.text = "can not create a python node"; + throw SALOME::SALOME_Exception(es); + } + long ierr=PyLong_AsLong(PyTuple_GetItem(res,0)); + PyObject* result=PyTuple_GetItem(res,1); + std::string astr=PyUnicode_AsUTF8(result); + Py_DECREF(res); + PyGILState_Release(gstate); - if(ierr==0) - { - Utils_Locker lck(&_mutexForDftPy); - CORBA::Object_var obj=_orb->string_to_object(astr.c_str()); - node=Engines::PyScriptNode::_narrow(obj); - std::map::iterator it(_dftPyScriptNode.find(nodeName)); - if(it==_dftPyScriptNode.end()) - { - _dftPyScriptNode[nodeName]=node; - } - else - { - Engines::PyScriptNode_var oldNode((*it).second); - if(!CORBA::is_nil(oldNode)) - oldNode->UnRegister(); - (*it).second=node; - } - if(!CORBA::is_nil(node)) - node->Register(); - return node._retn(); - } + if(ierr==0) + { + Utils_Locker lck(&_mutexForDftPy); + CORBA::Object_var obj=_orb->string_to_object(astr.c_str()); + node=Engines::PyScriptNode::_narrow(obj); + std::map::iterator it(_dftPyScriptNode.find(nodeName)); + if(it==_dftPyScriptNode.end()) + { + _dftPyScriptNode[nodeName]=node; + } else - { - SALOME::ExceptionStruct es; - es.type = SALOME::INTERNAL_ERROR; - es.text = astr.c_str(); - throw SALOME::SALOME_Exception(es); - } + { + Engines::PyScriptNode_var oldNode((*it).second); + if(!CORBA::is_nil(oldNode)) + oldNode->UnRegister(); + (*it).second=node; + } + return node._retn(); + } + else + { + SALOME::ExceptionStruct es; + es.type = SALOME::INTERNAL_ERROR; + es.text = astr.c_str(); + throw SALOME::SALOME_Exception(es); + } +} + +void Engines_Container_i::removePyScriptNode(const char *nodeName) +{ + std::map::iterator it(_dftPyScriptNode.find(nodeName)); + if(it==_dftPyScriptNode.end()) + { + std::ostringstream oss; oss << "Engines_Container_i::removePyScriptNode : node \"" << nodeName << "\" is not map !"; + SALOME::ExceptionStruct es; + es.type = SALOME::INTERNAL_ERROR; + es.text = oss.str().c_str(); + throw SALOME::SALOME_Exception(es); + } + (*it).second->UnRegister(); + _dftPyScriptNode.erase(it); +} + +void Engines_Container_i::cleanAllPyScripts() +{ + for(std::map::iterator it=_dftPyNode.begin();it!=_dftPyNode.end();it++) + { + Engines::PyNode_var tmpVar((*it).second); + if(!CORBA::is_nil(tmpVar)) + tmpVar->UnRegister(); + } + _dftPyNode.clear(); + for(std::map::iterator it=_dftPyScriptNode.begin();it!=_dftPyScriptNode.end();it++) + { + Engines::PyScriptNode_var tmpVar((*it).second); + if(!CORBA::is_nil(tmpVar)) + tmpVar->UnRegister(); + } + _dftPyScriptNode.clear(); } //============================================================================= @@ -1814,13 +1817,13 @@ Engines::PyScriptNode_ptr Engines_Container_i::getDefaultPyScriptNode(const char if(it==_dftPyScriptNode.end()) return Engines::PyScriptNode::_nil(); else - { - Engines::PyScriptNode_var tmpVar((*it).second); - if(!CORBA::is_nil(tmpVar)) - return Engines::PyScriptNode::_duplicate(tmpVar); - else - return Engines::PyScriptNode::_nil(); - } + { + Engines::PyScriptNode_var tmpVar((*it).second); + if(!CORBA::is_nil(tmpVar)) + return Engines::PyScriptNode::_duplicate(tmpVar); + else + return Engines::PyScriptNode::_nil(); + } } //============================================================================= @@ -1898,13 +1901,57 @@ void Engines_Container_i::clearTemporaryFiles() { std::list::const_iterator it; for ( it = _tmp_files.begin(); it != _tmp_files.end(); ++it ) { -#ifdef WIN32 - std::string command = "del /F /P"; +#if defined(WIN32) && defined(UNICODE) + std::wstring aFile = Kernel_Utils::utf8_decode_s(*it); + std::wstring command = (GetFileAttributes(aFile.c_str()) == FILE_ATTRIBUTE_DIRECTORY) ? L"rd /Q \"" : L"del /F /Q \""; + command += aFile; + command += L"\" 2>NUL"; + _wsystem(command.c_str()); #else - std::string command = "rm -rf "; +#if defined(WIN32) + std::string aFile = *it; + std::string command = (GetFileAttributes(aFile.c_str()) == FILE_ATTRIBUTE_DIRECTORY) ? "rd /Q \"" : "del /F /Q \""; + command += aFile; + command += "\" 2>NUL"; +#else + std::string command = "rm -rf "; + command += *it; +#endif + system(command.c_str()); #endif - command += *it; - system( command.c_str() ); } _tmp_files.clear(); } + +static Engines_Container_i *_container_singleton_ssl = nullptr; + +static Engines::Container_var _container_ref_singleton_ssl; + +Engines_Container_i *KERNEL::getContainerSA() +{ + if(!_container_singleton_ssl) + { + int argc(0); + CORBA::ORB_var orb = CORBA::ORB_init(argc,nullptr); + CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); + PortableServer::POA_var poa = PortableServer::POA::_narrow(obj); + PortableServer::POAManager_var pman = poa->the_POAManager(); + CORBA::PolicyList policies; + policies.length(0); + // + char *argv[4] = {"Container","FactoryServer","toto",nullptr}; + SALOME_Fake_NamingService ns; + _container_singleton_ssl = new Engines_Container_i(orb,poa,"FactoryServer",2,argv,&ns,false); + PortableServer::ObjectId * cont_id = _container_singleton_ssl->getCORBAId(); + // + CORBA::Object_var zeRef = poa->id_to_reference(*cont_id); + _container_ref_singleton_ssl = Engines::Container::_narrow(zeRef); + } + return _container_singleton_ssl; +} + +Engines::Container_var KERNEL::getContainerRefSA() +{ + getContainerSA(); + return _container_ref_singleton_ssl; +}