Salome HOME
YACS in SSL mode
[modules/kernel.git] / src / Container / Container_i.cxx
index d34b36ba839674dd460eded40ab72b38c99b31ff..315f7cee6c1e5f8ea7fa975b6c0d21c19892f304 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  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
@@ -119,7 +121,7 @@ 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)
 {
 }
 
@@ -133,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 ;
@@ -177,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
@@ -258,18 +258,7 @@ Engines_Container_i::~Engines_Container_i()
     delete _id;
   if(_NS)
     delete _NS;
-  for(std::map<std::string,Engines::PyNode_var>::iterator it=_dftPyNode.begin();it!=_dftPyNode.end();it++)
-  {
-    Engines::PyNode_var tmpVar((*it).second);
-    if(!CORBA::is_nil(tmpVar))
-      tmpVar->UnRegister();
-  }
-  for(std::map<std::string,Engines::PyScriptNode_var>::iterator it=_dftPyScriptNode.begin();it!=_dftPyScriptNode.end();it++)
-  {
-    Engines::PyScriptNode_var tmpVar((*it).second);
-    if(!CORBA::is_nil(tmpVar))
-      tmpVar->UnRegister();
-  }
+  cleanAllPyScripts();
 }
 
 //=============================================================================
@@ -379,7 +368,7 @@ void Engines_Container_i::Shutdown()
     {
       itm->second->destroy();
     }
-    catch(const CORBA::Exception& e)
+    catch(const CORBA::Exception&)
     {
       // ignore this entry and continue
     }
@@ -390,8 +379,17 @@ void Engines_Container_i::Shutdown()
   }
   _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...");
@@ -554,7 +552,12 @@ Engines_Container_i::load_component_CppImplementation(const char* componentName,
   }
 #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";
@@ -1223,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;
@@ -1234,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<SALOME_Embedded_NamingService_Client *>(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
 /*!
@@ -1309,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;
@@ -1392,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 :
@@ -1548,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();
     }
@@ -1744,8 +1761,6 @@ Engines::PyScriptNode_ptr Engines_Container_i::createPyScriptNode(const char* no
         oldNode->UnRegister();
       (*it).second=node;
     }
-    if(!CORBA::is_nil(node))
-      node->Register();
     return node._retn();
   }
   else
@@ -1757,6 +1772,39 @@ Engines::PyScriptNode_ptr Engines_Container_i::createPyScriptNode(const char* no
   }
 }
 
+void Engines_Container_i::removePyScriptNode(const char *nodeName)
+{
+  std::map<std::string,Engines::PyScriptNode_var>::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<std::string,Engines::PyNode_var>::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<std::string,Engines::PyScriptNode_var>::iterator it=_dftPyScriptNode.begin();it!=_dftPyScriptNode.end();it++)
+    {
+      Engines::PyScriptNode_var tmpVar((*it).second);
+      if(!CORBA::is_nil(tmpVar))
+        tmpVar->UnRegister();
+    }
+  _dftPyScriptNode.clear();
+}
+
 //=============================================================================
 /*! \brief Retrieves the last created PyScriptNode instance with createPyScriptNode.
  *
@@ -1853,53 +1901,57 @@ void Engines_Container_i::clearTemporaryFiles()
 {
   std::list<std::string>::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
+#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 ";
+       std::string command = "rm -rf ";
+       command += *it;
+#endif
+       system(command.c_str());
 #endif
-    command += *it;
-    system( command.c_str() );
   }
   _tmp_files.clear();
 }
 
-/*
-std::string Engines_Container_i::AnotherMethodeToReplace_PyString_AsString(PyObject * result)
+static Engines_Container_i *_container_singleton_ssl = nullptr;
+
+static Engines::Container_var _container_ref_singleton_ssl;
+
+Engines_Container_i *KERNEL::getContainerSA()
 {
-    std::string my_result = "";
-    if (PyUnicode_Check(result)) {
-        // Convert string to bytes.
-        // strdup() bytes into my_result.
-        PyObject * temp_bytes = PyUnicode_AsEncodedString(result, "ASCII", "strict"); // Owned reference
-        if (temp_bytes != NULL) {
-            my_result = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer
-            my_result = strdup(my_result);
-            Py_DECREF(temp_bytes);
-        } else {
-            // TODO PY3: Handle encoding error.
-            Py_DECREF(temp_bytes);
-        }
+  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;
+}
 
-    } else if (PyBytes_Check(result)) {
-        // strdup() bytes into my_result.
-        my_result = PyBytes_AS_STRING(result); // Borrowed pointer
-        my_result = strdup(my_result);
-    } else {
-        // Convert into your favorite string representation.
-        // Convert string to bytes if it is not already.
-        // strdup() bytes into my_result.
-        // TODO PY3: Check if only bytes is ok. 
-        PyObject * temp_bytes = PyObject_Bytes(result); // Owned reference
-        if (temp_bytes != NULL) {
-            my_result = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer
-            my_result = strdup(my_result);
-            Py_DECREF(temp_bytes);
-        } else {
-            // TODO PY3: Handle error.
-            Py_DECREF(temp_bytes);
-        }
-    }
-    return my_result;
+Engines::Container_var KERNEL::getContainerRefSA()
+{
+  getContainerSA();
+  return _container_ref_singleton_ssl;
 }
-*/