]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
ExternalServerLauncher is available in SSL mode.
authorAnthony Geay <anthony.geay@edf.fr>
Thu, 19 Aug 2021 07:56:36 +0000 (09:56 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Thu, 19 Aug 2021 12:22:34 +0000 (14:22 +0200)
14 files changed:
src/Container/SALOME_CPythonHelper.cxx
src/Container/SALOME_CPythonHelper.hxx
src/KERNEL_PY/__init__.py
src/Launcher/KernelLauncher.cxx
src/Launcher/KernelLauncher.hxx
src/Launcher/KernelLauncher.i
src/Launcher/SALOME_ExternalServerHandler.cxx
src/Launcher/SALOME_ExternalServerHandler.hxx
src/Launcher/SALOME_ExternalServerLauncher.cxx
src/Launcher/SALOME_ExternalServerLauncher.hxx
src/SALOMESDS/KernelSDS.cxx
src/SALOMESDS/KernelSDS.i
src/SALOMESDS/TestSalomeSDS.py
src/SALOMESDS/TestSalomeSDSHelper0.py

index d6b5585e9ae31ef3f9adf36f9f0a225ac059c5a7..e536fe20525599641ec92bf5d6933164d5670502 100644 (file)
@@ -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);
 }
index e267f64b5d2dd55acd347b26abf113eb835ff435..b14d6d1f291e4c3f32e14799cc0ac254333efdc6 100644 (file)
@@ -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;
index 8e78458d28ada9842164057fcbcdebccfb5947be..0c06b1cfdd35132d617846b9eecfa89062a89567 100644 (file)
@@ -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()
index 5eaaae9f9942cf932a17c641ff4792372296a373..37ed632a328850093eb90a705a2be464871459c6 100644 (file)
@@ -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 <cstring>
 
@@ -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());
+}
index 567f5e581758f9f0310999e23783189d75042640..575c8101c4d9679b6431fcd52b36a305f0db0af4 100644 (file)
@@ -23,3 +23,4 @@
 
 std::string GetContainerManagerInstance();
 std::string GetResourcesManagerInstance();
+std::string GetExternalServerInstance();
index d67ef54f542e51713d54b32eb8e4ebf09aff1d89..11dc719e6ab5e46278c7b823d9efccde1fd9d8d5 100644 (file)
@@ -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())
 %}
index fb2c2106d83fc198b3d2958abb17f65d3f093905..7f8c7466580c7ce6cdafe22edc70c8aaebf57d59 100644 (file)
@@ -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)
 {
 }
 
index 39b28df4fcdd14a6d73c87c50f4024f1cd33a768..542315706c904426032401db74837fe41db833e6 100644 (file)
 
 #include <string>
 
-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;
 };
index 2fb3ab142a9dda213d8e84bf97ed13dd4869a01f..47df986605b0c142688a0827712105c40f3cf630 100644 (file)
@@ -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<std::string> SALOME_ExternalServerLauncher::ListOfExternalServersCpp(SALOME_NamingService *ns)
+std::vector<std::string> SALOME_ExternalServerLauncher::ListOfExternalServersCpp(SALOME_NamingService_Abstract *ns)
 {
   ns->Change_Directory(NAME_IN_NS);
   std::vector<std::string> 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<std::string> serverNames(ListOfExternalServersCpp(ns));
   if(std::find(serverNames.begin(),serverNames.end(),serverName)==serverNames.end())
index c69664e4b664fd059f553fd54c5bdc3f9bc56f96..d4c617ecb50d0de4ba67c53cb567b354b54ea670 100644 (file)
 #include <vector>
 #include <string>
 
-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<std::string> ListOfExternalServersCpp(SALOME_NamingService *ns);
+  static std::vector<std::string> 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<long> _list_of_pids_to_kill;
index a76f99f14db60a7a8f72f3d4f9b1dcee10fce796..19d99a499f3ca1c6bfcc0885208e2060ab56b1a1 100644 (file)
@@ -35,20 +35,20 @@ std::string GetDSMInstanceInternal(const std::vector<std::string>& 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);
index 74779d8b0fd6c87205f3f9d61aa0f5d85a984e81..b8716a8715b41d093fa75f1c156a2526d395c19a 100644 (file)
@@ -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))
index 54d3c1947be98c6a7bdf268dc8f76557cb453063..2c4c27dc32ed905d1adc75c3803650c2886da490 100644 (file)
@@ -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()
+
 
index 7f44e3cd97a09298d4feb1a1adf5212335484370..8eef5121c3c7a38c268fa36f3a643ee324906e91 100644 (file)
@@ -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")