From 9455d9c725a87333bf50fd71d05428215a8815e7 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 19 Aug 2021 09:56:36 +0200 Subject: [PATCH] WIP --- src/Container/SALOME_CPythonHelper.cxx | 2 ++ src/Container/SALOME_CPythonHelper.hxx | 3 +++ src/KERNEL_PY/__init__.py | 5 ++++- src/Launcher/KernelLauncher.cxx | 22 +++++++++++++++++++ src/Launcher/KernelLauncher.hxx | 1 + src/Launcher/KernelLauncher.i | 7 ++++++ src/Launcher/SALOME_ExternalServerHandler.cxx | 2 +- src/Launcher/SALOME_ExternalServerHandler.hxx | 6 ++--- .../SALOME_ExternalServerLauncher.cxx | 10 ++++----- .../SALOME_ExternalServerLauncher.hxx | 12 +++++----- src/SALOMESDS/KernelSDS.cxx | 6 ++--- 11 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/Container/SALOME_CPythonHelper.cxx b/src/Container/SALOME_CPythonHelper.cxx index d6b5585e9..3aca93ee8 100644 --- a/src/Container/SALOME_CPythonHelper.cxx +++ b/src/Container/SALOME_CPythonHelper.cxx @@ -20,6 +20,8 @@ #include "SALOME_CPythonHelper.hxx" +SALOME_CPythonHelper SALOME_CPythonHelper::_CPYTHONHELPER_INSTANCE; + #if PY_VERSION_HEX < 0x03050000 static char* Py_EncodeLocale(const wchar_t *arg, size_t *size) diff --git a/src/Container/SALOME_CPythonHelper.hxx b/src/Container/SALOME_CPythonHelper.hxx index e267f64b5..c89d5ee27 100644 --- a/src/Container/SALOME_CPythonHelper.hxx +++ b/src/Container/SALOME_CPythonHelper.hxx @@ -38,6 +38,9 @@ class CONTAINER_EXPORT SALOME_CPythonHelper PyObject *getGlobals() const { return _globals; } PyObject *getLocals() const { return _locals; } PyObject *getPickler() const { return _pickler; } + static SALOME_CPythonHelper& Singleton() { return _CPYTHONHELPER_INSTANCE; } + private: + static SALOME_CPythonHelper _CPYTHONHELPER_INSTANCE; private: PyObject *_globals = nullptr; PyObject *_locals = nullptr; diff --git a/src/KERNEL_PY/__init__.py b/src/KERNEL_PY/__init__.py index 8e78458d2..02452948a 100644 --- a/src/KERNEL_PY/__init__.py +++ b/src/KERNEL_PY/__init__.py @@ -204,7 +204,7 @@ class StandAloneLifecyle: return self._rm def salome_init_without_session(): - global lcc,naming_service,myStudy,orb,modulcat,sg,cm,dsm + global lcc,naming_service,myStudy,orb,modulcat,sg,cm,dsm,esm import KernelBasis KernelBasis.setSSLMode(True) import KernelDS @@ -229,6 +229,9 @@ def salome_init_without_session(): from KernelSDS import GetDSMInstance import sys dsm = GetDSMInstance(sys.argv) + # esm inherits from SALOME_CPythonHelper singleton already initialized by GetDSMInstance + # esm inherits also from SALOME_ResourcesManager creation/initialization (concerning SingleThreadPOA POA) when KernelLauncher.GetContainerManager() has been called + esm = KernelLauncher.GetExternalServer() def salome_init_with_session(path=None, embedded=False): """ diff --git a/src/Launcher/KernelLauncher.cxx b/src/Launcher/KernelLauncher.cxx index 5eaaae9f9..4df0eecb5 100644 --- a/src/Launcher/KernelLauncher.cxx +++ b/src/Launcher/KernelLauncher.cxx @@ -24,6 +24,8 @@ #include "SALOME_Fake_NamingService.hxx" #include "SALOME_KernelServices.hxx" #include "SALOME_ResourcesManager.hxx" +#include "SALOME_ExternalServerLauncher.hxx" +#include "SALOME_CPythonHelper.hxx" #include @@ -46,3 +48,23 @@ std::string GetResourcesManagerInstance() CORBA::String_var ior = orb->object_to_string(cm); return std::string(ior.in()); } + +std::string GetExternalServerInstance() +{ + CORBA::ORB_ptr orb = KERNEL::getORB(); + CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); + PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj); + // + PortableServer::POA_var safePOA = root_poa->find_POA("SingleThreadPOA",true); + // + SALOME_CPythonHelper *cPyh(&SALOME_CPythonHelper::Singleton()); + SALOME_Fake_NamingService *ns = new SALOME_Fake_NamingService; + SALOME_ExternalServerLauncher *esm(new SALOME_ExternalServerLauncher(cPyh,orb,safePOA,ns)); + esm->_remove_ref(); + // + CORBA::Object_var esmPtr = safePOA->servant_to_reference(esm); + SALOME::ExternalServerLauncher_var esmCPtr = SALOME::ExternalServerLauncher::_narrow(esmPtr); + // + CORBA::String_var ior = orb->object_to_string(esmCPtr); + return std::string(ior.in()); +} diff --git a/src/Launcher/KernelLauncher.hxx b/src/Launcher/KernelLauncher.hxx index 567f5e581..575c8101c 100644 --- a/src/Launcher/KernelLauncher.hxx +++ b/src/Launcher/KernelLauncher.hxx @@ -23,3 +23,4 @@ std::string GetContainerManagerInstance(); std::string GetResourcesManagerInstance(); +std::string GetExternalServerInstance(); diff --git a/src/Launcher/KernelLauncher.i b/src/Launcher/KernelLauncher.i index d67ef54f5..11dc719e6 100644 --- a/src/Launcher/KernelLauncher.i +++ b/src/Launcher/KernelLauncher.i @@ -29,6 +29,7 @@ { std::string GetContainerManagerInstance(); std::string GetResourcesManagerInstance(); + std::string GetExternalServerInstance(); } %pythoncode %{ @@ -43,4 +44,10 @@ def myResourcesManager(): import CORBA orb=CORBA.ORB_init(['']) return orb.string_to_object(GetResourcesManagerInstance()) + +def GetExternalServer(): + import SALOME + import CORBA + orb=CORBA.ORB_init(['']) + return orb.string_to_object(GetExternalServerInstance()) %} diff --git a/src/Launcher/SALOME_ExternalServerHandler.cxx b/src/Launcher/SALOME_ExternalServerHandler.cxx index fb2c2106d..7f8c74665 100644 --- a/src/Launcher/SALOME_ExternalServerHandler.cxx +++ b/src/Launcher/SALOME_ExternalServerHandler.cxx @@ -36,7 +36,7 @@ unsigned SALOME_ExternalServerHandler::CNT = 0; -SALOME_ExternalServerHandler::SALOME_ExternalServerHandler(SALOME_ExternalServerLauncher *boss, const std::string& name, SALOME_NamingService *ns, long pid):_name(name),_pid(pid),_NS(ns),_boss(boss) +SALOME_ExternalServerHandler::SALOME_ExternalServerHandler(SALOME_ExternalServerLauncher *boss, const std::string& name, SALOME_NamingService_Abstract *ns, long pid):_name(name),_pid(pid),_NS(ns),_boss(boss) { } diff --git a/src/Launcher/SALOME_ExternalServerHandler.hxx b/src/Launcher/SALOME_ExternalServerHandler.hxx index 39b28df4f..542315706 100644 --- a/src/Launcher/SALOME_ExternalServerHandler.hxx +++ b/src/Launcher/SALOME_ExternalServerHandler.hxx @@ -28,14 +28,14 @@ #include -class SALOME_NamingService; +class SALOME_NamingService_Abstract; class SALOME_ExternalServerLauncher; class SALOME_CPythonHelper; class SALOMELAUNCHER_EXPORT SALOME_ExternalServerHandler : public POA_SALOME::ExternalServerHandler { public: - SALOME_ExternalServerHandler(SALOME_ExternalServerLauncher *boss, const std::string& name, SALOME_NamingService *ns, long pid); + SALOME_ExternalServerHandler(SALOME_ExternalServerLauncher *boss, const std::string& name, SALOME_NamingService_Abstract *ns, long pid); virtual ~SALOME_ExternalServerHandler(); void registerToKill(const SALOME_CPythonHelper *pyHelper) const; static void KillPID(long pid); @@ -49,7 +49,7 @@ class SALOMELAUNCHER_EXPORT SALOME_ExternalServerHandler : public POA_SALOME::Ex private: std::string _name; long _pid; - SALOME_NamingService *_NS; + SALOME_NamingService_Abstract *_NS; SALOME_ExternalServerLauncher *_boss; static unsigned CNT; }; diff --git a/src/Launcher/SALOME_ExternalServerLauncher.cxx b/src/Launcher/SALOME_ExternalServerLauncher.cxx index 2fb3ab142..47df98660 100644 --- a/src/Launcher/SALOME_ExternalServerLauncher.cxx +++ b/src/Launcher/SALOME_ExternalServerLauncher.cxx @@ -44,9 +44,9 @@ const char SALOME_ExternalServerLauncher::NAME_IN_NS[]="/ExternalServers"; unsigned SALOME_ExternalServerLauncher::CNT = 0; -SALOME_ExternalServerLauncher::SALOME_ExternalServerLauncher(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, PortableServer::POA_var poa):_pyHelper(pyHelper),_poa(poa) +SALOME_ExternalServerLauncher::SALOME_ExternalServerLauncher(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, PortableServer::POA_var poa,SALOME_NamingService_Abstract *ns):_pyHelper(pyHelper),_poa(poa) { - _NS = new SALOME_NamingService(orb); + _NS = ns == nullptr ? new SALOME_NamingService(orb) : ns; PortableServer::ObjectId_var id(_poa->activate_object(this)); CORBA::Object_var obj(_poa->id_to_reference(id)); SALOME::ExternalServerLauncher_var refPtr(SALOME::ExternalServerLauncher::_narrow(obj)); @@ -211,7 +211,7 @@ SALOME::ByteVec *SALOME_ExternalServerLauncher::fetchContentOfFileAndRm(const ch return ret.release(); } -std::vector SALOME_ExternalServerLauncher::ListOfExternalServersCpp(SALOME_NamingService *ns) +std::vector SALOME_ExternalServerLauncher::ListOfExternalServersCpp(SALOME_NamingService_Abstract *ns) { ns->Change_Directory(NAME_IN_NS); std::vector ret(ns->list_directory()); @@ -236,7 +236,7 @@ bool SALOME_ExternalServerLauncher::IsAliveAndKicking(SALOME::ExternalServerHand return ret; } -bool SALOME_ExternalServerLauncher::IsAliveAndKicking(SALOME_NamingService *ns, const std::string& serverName) +bool SALOME_ExternalServerLauncher::IsAliveAndKicking(SALOME_NamingService_Abstract *ns, const std::string& serverName) { SALOME::ExternalServerHandler_var pt(GetServerHandlerGivenName(ns, serverName)); if( CORBA::is_nil(pt) ) @@ -244,7 +244,7 @@ bool SALOME_ExternalServerLauncher::IsAliveAndKicking(SALOME_NamingService *ns, return IsAliveAndKicking(pt); } -SALOME::ExternalServerHandler_var SALOME_ExternalServerLauncher::GetServerHandlerGivenName(SALOME_NamingService *ns, const std::string& serverName) +SALOME::ExternalServerHandler_var SALOME_ExternalServerLauncher::GetServerHandlerGivenName(SALOME_NamingService_Abstract *ns, const std::string& serverName) { std::vector serverNames(ListOfExternalServersCpp(ns)); if(std::find(serverNames.begin(),serverNames.end(),serverName)==serverNames.end()) diff --git a/src/Launcher/SALOME_ExternalServerLauncher.hxx b/src/Launcher/SALOME_ExternalServerLauncher.hxx index c69664e4b..d4c617ecb 100644 --- a/src/Launcher/SALOME_ExternalServerLauncher.hxx +++ b/src/Launcher/SALOME_ExternalServerLauncher.hxx @@ -29,13 +29,13 @@ #include #include -class SALOME_NamingService; +class SALOME_NamingService_Abstract; class SALOME_CPythonHelper; class SALOMELAUNCHER_EXPORT SALOME_ExternalServerLauncher : public POA_SALOME::ExternalServerLauncher { public: - SALOME_ExternalServerLauncher(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, PortableServer::POA_var poa); + SALOME_ExternalServerLauncher(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService_Abstract *ns = nullptr); virtual ~SALOME_ExternalServerLauncher(); public: SALOME::ExternalServerHandler_ptr launchServer(const char *server_name, const char *working_dir, const SALOME::CmdList& command_list ) override; @@ -49,13 +49,13 @@ class SALOMELAUNCHER_EXPORT SALOME_ExternalServerLauncher : public POA_SALOME::E const SALOME_CPythonHelper *getPyHelper() const { return _pyHelper; } private: static std::string CreateAbsNameInNSFromServerName(const std::string& scopeName); - static std::vector ListOfExternalServersCpp(SALOME_NamingService *ns); + static std::vector ListOfExternalServersCpp(SALOME_NamingService_Abstract *ns); static bool IsAliveAndKicking(SALOME::ExternalServerHandler_ptr server); - static bool IsAliveAndKicking(SALOME_NamingService *ns, const std::string& serverName); - static SALOME::ExternalServerHandler_var GetServerHandlerGivenName(SALOME_NamingService *ns, const std::string& serverName); + static bool IsAliveAndKicking(SALOME_NamingService_Abstract *ns, const std::string& serverName); + static SALOME::ExternalServerHandler_var GetServerHandlerGivenName(SALOME_NamingService_Abstract *ns, const std::string& serverName); private: const SALOME_CPythonHelper *_pyHelper = nullptr; - SALOME_NamingService *_NS = nullptr; + SALOME_NamingService_Abstract *_NS = nullptr; PortableServer::POA_var _poa; static unsigned CNT; std::vector _list_of_pids_to_kill; diff --git a/src/SALOMESDS/KernelSDS.cxx b/src/SALOMESDS/KernelSDS.cxx index a76f99f14..d9f50fc4e 100644 --- a/src/SALOMESDS/KernelSDS.cxx +++ b/src/SALOMESDS/KernelSDS.cxx @@ -35,20 +35,20 @@ std::string GetDSMInstanceInternal(const std::vector& argv) CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); if(!CORBA::is_nil(obj)) root_poa = PortableServer::POA::_narrow(obj); - SALOME_CPythonHelper cPyh; + SALOME_CPythonHelper* cPyh(&SALOME_CPythonHelper::Singleton()); { int argcInit((int)argv.size()); char **argvInit = new char *[argcInit+1]; argvInit[argcInit] = nullptr; for(int i = 0 ; i < argcInit ; ++i) argvInit[i] = strdup(argv[i].c_str()); - cPyh.initializePython(argcInit,argvInit); + cPyh->initializePython(argcInit,argvInit); for(int i = 0 ; i < argcInit ; ++i) free(argvInit[i]); delete [] argvInit; } SALOME_Fake_NamingService *ns(new SALOME_Fake_NamingService); - SALOMESDS::DataServerManager *dsm(new SALOMESDS::DataServerManager(&cPyh,orb,root_poa,ns)); + SALOMESDS::DataServerManager *dsm(new SALOMESDS::DataServerManager(cPyh,orb,root_poa,ns)); dsm->_remove_ref(); CORBA::Object_var objRef = ns->Resolve(SALOMESDS::DataServerManager::NAME_IN_NS); _dsm_singleton = SALOME::DataServerManager::_narrow(objRef); -- 2.39.2