From 5e09acc3e3cba3ad6fbe7c39970f62b81651357e Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 19 Aug 2021 09:56:36 +0200 Subject: [PATCH] ExternalServerLauncher is available in SSL mode. --- src/Container/SALOME_CPythonHelper.cxx | 19 ++++++++++++++++ src/Container/SALOME_CPythonHelper.hxx | 4 ++++ src/KERNEL_PY/__init__.py | 19 +++++++++++++--- 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 ++--- src/SALOMESDS/KernelSDS.i | 5 +++++ src/SALOMESDS/TestSalomeSDS.py | 8 +++---- src/SALOMESDS/TestSalomeSDSHelper0.py | 6 ++--- 14 files changed, 98 insertions(+), 29 deletions(-) diff --git a/src/Container/SALOME_CPythonHelper.cxx b/src/Container/SALOME_CPythonHelper.cxx index d6b5585e9..e536fe205 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 = nullptr; + #if PY_VERSION_HEX < 0x03050000 static char* Py_EncodeLocale(const wchar_t *arg, size_t *size) @@ -33,6 +35,19 @@ Py_DecodeLocale(const char *arg, size_t *size) } #endif +SALOME_CPythonHelper *SALOME_CPythonHelper::Singleton() +{ + if(!_CPYTHONHELPER_INSTANCE) + _CPYTHONHELPER_INSTANCE = new SALOME_CPythonHelper; + return _CPYTHONHELPER_INSTANCE; +} + +void SALOME_CPythonHelper::KillSingleton() +{ + delete _CPYTHONHELPER_INSTANCE; + _CPYTHONHELPER_INSTANCE = nullptr; +} + void SALOME_CPythonHelper::initializePython(int argc, char *argv[]) { Py_Initialize(); @@ -105,9 +120,13 @@ std::string SALOME_CPythonHelper::evalS(const std::string& pyCode) const return ret; } + SALOME_CPythonHelper::~SALOME_CPythonHelper() { // _globals is borrowed ref -> do nothing + + /*if(_locals){ auto refcount_locals = Py_REFCNT(_locals); }*/ + Py_XDECREF(_locals); Py_XDECREF(_pickler); } diff --git a/src/Container/SALOME_CPythonHelper.hxx b/src/Container/SALOME_CPythonHelper.hxx index e267f64b5..b14d6d1f2 100644 --- a/src/Container/SALOME_CPythonHelper.hxx +++ b/src/Container/SALOME_CPythonHelper.hxx @@ -38,6 +38,10 @@ class CONTAINER_EXPORT SALOME_CPythonHelper PyObject *getGlobals() const { return _globals; } PyObject *getLocals() const { return _locals; } PyObject *getPickler() const { return _pickler; } + static SALOME_CPythonHelper *Singleton(); + static void KillSingleton(); + 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..0c06b1cfd 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): """ @@ -285,8 +288,12 @@ def salome_close(): salome_iapp_close() salome_study_close() myStudy, myStudyName = None, None - import KernelDS - KernelDS.KillGlobalSessionInstance() + import KernelBasis + if KernelBasis.getSSLMode(): + import KernelDS + KernelDS.KillGlobalSessionInstance() + import KernelSDS + KernelSDS.KillCPythonHelper() pass def salome_NS(): @@ -332,6 +339,12 @@ def salome_shutdown_containers(): naming_service.Destroy_Name(ref_in_ns) print("Number of containers in NS after clean : {}".format( len( list(salome_walk_on_containers(ns,[""])) ))) +class SessionContextManager: + def __enter__(self): + standalone() + salome_init() + def __exit__(self, type, value, traceback): + salome_close() #to expose all objects to pydoc __all__=dir() diff --git a/src/Launcher/KernelLauncher.cxx b/src/Launcher/KernelLauncher.cxx index 5eaaae9f9..37ed632a3 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..19d99a499 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); diff --git a/src/SALOMESDS/KernelSDS.i b/src/SALOMESDS/KernelSDS.i index 74779d8b0..b8716a871 100644 --- a/src/SALOMESDS/KernelSDS.i +++ b/src/SALOMESDS/KernelSDS.i @@ -24,6 +24,7 @@ %{ #include "KernelSDS.hxx" #include "Utils_SALOME_Exception.hxx" +#include "SALOME_CPythonHelper.hxx" %} @@ -53,6 +54,10 @@ class SALOME_Exception %inline { + void KillCPythonHelper() + { + SALOME_CPythonHelper::KillSingleton(); + } std::string GetDSMInstanceInternal(PyObject *argv) { if(!PyList_Check(argv)) diff --git a/src/SALOMESDS/TestSalomeSDS.py b/src/SALOMESDS/TestSalomeSDS.py index 54d3c1947..2c4c27dc3 100644 --- a/src/SALOMESDS/TestSalomeSDS.py +++ b/src/SALOMESDS/TestSalomeSDS.py @@ -493,13 +493,11 @@ class SalomeSDSTest(unittest.TestCase): p.join() dsm.removeDataScope(scopeName) pass - - def setUp(self): - salome.salome_init_without_session() - pass pass if __name__=="__main__": - unittest.main() + with salome.SessionContextManager(): + unittest.main() + diff --git a/src/SALOMESDS/TestSalomeSDSHelper0.py b/src/SALOMESDS/TestSalomeSDSHelper0.py index 7f44e3cd9..8eef5121c 100644 --- a/src/SALOMESDS/TestSalomeSDSHelper0.py +++ b/src/SALOMESDS/TestSalomeSDSHelper0.py @@ -21,8 +21,6 @@ import pickle import salome import sys -salome.salome_init_without_session() - scopeName="Scope1" varName="a" @@ -47,4 +45,6 @@ def waitKey(IORNS): if __name__=="__main__": IORNS = sys.argv[-1] - sys.exit(not waitKey(IORNS)) + with salome.SessionContextManager(): + if waitKey(IORNS) is not True: + raise AssertionError("Not the expected result") -- 2.39.2