]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
string -> sequence<octet> to support the exchance via CORBA or pickled strings +...
authorAnthony Geay <anthony.geay@edf.fr>
Thu, 30 Oct 2014 13:11:13 +0000 (14:11 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Thu, 30 Oct 2014 13:11:13 +0000 (14:11 +0100)
idl/SALOME_SDS.idl
src/SALOMESDS/SALOMESDS_DataScopeServer.cxx
src/SALOMESDS/SALOMESDS_DataScopeServer.hxx
src/SALOMESDS/SALOMESDS_DataServerManager.cxx
src/SALOMESDS/SALOMESDS_DataServerManager.hxx
src/SALOMESDS/SALOMESDS_StringDataServer.cxx
src/SALOMESDS/SALOMESDS_StringDataServer.hxx

index 5c20d1bc50add4923a3a11581d0ad4c2b1bd8537..88b1f5e90f7365af46b0b73bc712a1e72a205cf6 100644 (file)
@@ -21,6 +21,7 @@
 module SALOME
 {
   typedef sequence<string> StringVec;
+  typedef sequence<octet> ByteVec;
   
   interface BasicDataServer
   {
@@ -31,9 +32,9 @@ module SALOME
 
   interface StringDataServer : BasicDataServer
   {
-    void setValueOf(in string newValue);
-    string getValueOf();
-    string invokePythonMethodOn(in string method, in string args);
+    void setValueOf(in ByteVec newValue);
+    ByteVec getValueOf();
+    ByteVec invokePythonMethodOn(in string method, in ByteVec args);
   };
 
   interface AnyDataServer : BasicDataServer
@@ -46,6 +47,7 @@ module SALOME
 
   interface DataScopeServer
   {
+    void ping();
     string getScopeName();
     StringVec listVars();
     BasicDataServer retrieveVar(in string varName);
@@ -57,10 +59,14 @@ module SALOME
   interface DataServerManager
   {
     StringVec listScopes();
+    StringVec listAliveAndKickingScopes();
     DataScopeServer getDefaultScope();
+    boolean isAliveAndKicking(in string scopeName);
     DataScopeServer createDataScope(in string scopeName);
     DataScopeServer retriveDataScope(in string scopeName);
+    DataScopeServer giveADataScopeCalled(in string scopeName);
     void removeDataScope(in string scopeName);
+    void cleanScopesInNS();
     void shutdownScopes();
   };
 };
index 4ca656817d3d6a668e33c1c01ae340b427645a2e..61ce70a588420313ab3940d31af61c59c20cb771 100644 (file)
@@ -46,6 +46,13 @@ DataScopeServer::~DataScopeServer()
   Py_XDECREF(_pickler);
 }
 
+/*!
+ * Called remotely -> to protect against throw
+ */
+void DataScopeServer::ping()
+{
+}
+
 /*!
  * Called remotely -> to protect against throw
  */
index ab50139d6f395f22055efa51347d46194db6e388..c748388581f8219de9ec41121ef36421648cbb3a 100644 (file)
@@ -41,6 +41,7 @@ namespace SALOMESDS
   public:
     DataScopeServer(CORBA::ORB_ptr orb, const std::string& scopeName);
     DataScopeServer(const DataScopeServer& other);
+    void ping();
     char *getScopeName();
     SALOME::StringVec *listVars();
     SALOME::BasicDataServer_ptr retrieveVar(const char *varName);
index b8dac6c2ac8f0cc47b009d476df8326e1864b51f..0db72ed6d86a2e958f84ae4c6248ef827bb19814 100644 (file)
@@ -70,11 +70,43 @@ SALOME::StringVec *DataServerManager::listScopes()
   return ret;
 }
 
+SALOME::StringVec *DataServerManager::listAliveAndKickingScopes()
+{
+  std::vector<std::string> scopes(listOfScopesCpp());
+  std::size_t sz(scopes.size());
+  std::vector<std::string> retCpp; retCpp.reserve(sz);
+  for(std::size_t i=0;i<sz;i++)
+    {
+      if(isAliveAndKicking(scopes[i].c_str()))
+        retCpp.push_back(scopes[i]);
+    }
+  //
+  SALOME::StringVec *ret(new SALOME::StringVec);
+  sz=retCpp.size();
+  ret->length(sz);
+  for(std::size_t i=0;i<sz;i++)
+    (*ret)[i]=CORBA::string_dup(retCpp[i].c_str());
+  return ret;
+}
+
 SALOME::DataScopeServer_ptr DataServerManager::getDefaultScope()
 {
   return retriveDataScope(DFT_SCOPE_NAME_IN_NS);
 }
 
+CORBA::Boolean DataServerManager::isAliveAndKicking(const char *scopeName)
+{
+  SALOME::DataScopeServer_var scopePtr(getScopePtrGivenName(scopeName));
+  CORBA::Boolean ret(true);
+  try
+    {
+      scopePtr->ping();
+    }
+  catch(...)
+    { ret=false; }
+  return ret;
+}
+
 SALOME::DataScopeServer_ptr DataServerManager::createDataScope(const char *scopeName)
 {
   std::string scopeNameCpp(scopeName);
@@ -87,7 +119,7 @@ SALOME::DataScopeServer_ptr DataServerManager::createDataScope(const char *scope
   //
   SALOME_NamingService ns(_orb);
   std::string fullScopeName(CreateAbsNameInNSFromScopeName(scopeName));
-  std::ostringstream oss; oss << "valgrind SALOME_DataScopeServer" << " " << scopeName << " ";
+  std::ostringstream oss; oss << "SALOME_DataScopeServer" << " " << scopeName << " ";
   SALOME_ContainerManager::AddOmninamesParams(oss,&ns);
   std::string command(oss.str());
   SALOME_ContainerManager::MakeTheCommandToBeLaunchedASync(command);
@@ -104,6 +136,28 @@ SALOME::DataScopeServer_ptr DataServerManager::createDataScope(const char *scope
   return SALOME::DataScopeServer::_duplicate(ret);
 }
 
+SALOME::DataScopeServer_ptr DataServerManager::giveADataScopeCalled(const char *scopeName)
+{
+  std::string scopeNameCpp(scopeName);
+  std::vector<std::string> scopes(listOfScopesCpp());
+  if(std::find(scopes.begin(),scopes.end(),scopeNameCpp)==scopes.end())
+    {
+      return createDataScope(scopeName);
+    }
+  else
+    {
+      if(isAliveAndKicking(scopeName))
+        return retriveDataScope(scopeName);
+      else
+        {
+          SALOME_NamingService ns(_orb);
+          std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(scopeNameCpp));
+          ns.Destroy_Name(fullScopeName.c_str());
+          return createDataScope(scopeName);
+        }
+    }
+}
+
 SALOME::DataScopeServer_ptr DataServerManager::retriveDataScope(const char *scopeName)
 {
   SALOME::DataScopeServer_var ret(getScopePtrGivenName(scopeName));
@@ -116,6 +170,20 @@ void DataServerManager::removeDataScope(const char *scopeName)
   scs->shutdownIfNotHostedByDSM();
 }
 
+void DataServerManager::cleanScopesInNS()
+{
+  SALOME_NamingService ns(_orb);
+  std::vector<std::string> scopes(listOfScopesCpp());
+  for(std::vector<std::string>::const_iterator it=scopes.begin();it!=scopes.end();it++)
+    {
+      if(!isAliveAndKicking((*it).c_str()))
+        {
+          std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(*it));
+          ns.Destroy_Name(fullScopeName.c_str());
+        }
+    }
+}
+
 void DataServerManager::shutdownScopes()
 {
   std::vector<std::string> scopeNames(listOfScopesCpp());
index ec9720e0d2ad4d8e3a9d645de5ebc03de362bb9a..3badf22c23a8eb92558ab7651ed5230fa0707929 100644 (file)
@@ -38,10 +38,14 @@ namespace SALOMESDS
   public:
     DataServerManager(int argc, char *argv[], CORBA::ORB_ptr orb, PortableServer::POA_ptr poa);
     SALOME::StringVec *listScopes();
+    SALOME::StringVec *listAliveAndKickingScopes();
     SALOME::DataScopeServer_ptr getDefaultScope();
+    CORBA::Boolean isAliveAndKicking(const char *scopeName);
     SALOME::DataScopeServer_ptr createDataScope(const char *scopeName);
     SALOME::DataScopeServer_ptr retriveDataScope(const char *scopeName);
+    SALOME::DataScopeServer_ptr giveADataScopeCalled(const char *scopeName);
     void removeDataScope(const char *scopeName);
+    void cleanScopesInNS();
     void shutdownScopes();
     static std::string CreateAbsNameInNSFromScopeName(const std::string& scopeName);
   public:
index 202e6d2cc70470e9b6d8130cf5caed7dceb34e94..805a2153b3bde8ca789d802dfe3f340971706d1f 100644 (file)
@@ -37,27 +37,29 @@ StringDataServer::~StringDataServer()
 /*!
  * Called remotely -> to protect against throw
  */
-void StringDataServer::setValueOf(const char *newValue)
+void StringDataServer::setValueOf(const SALOME::ByteVec& newValue)
 {
   checkReadOnlyStatusRegardingConstness("StringDataServer::setValueOf : read only var !");
-  _data=newValue;
+  FromByteSeqToCpp(newValue,_data);
 }
 
 /*!
  * Called remotely -> to protect against throw
  */
-char *StringDataServer::getValueOf()
+SALOME::ByteVec *StringDataServer::getValueOf()
 {
-  return CORBA::string_dup(_data.c_str());
+  return FromCppToByteSeq(_data);
 }
 
 /*!
  * Called remotely -> to protect against throw
  */
-char *StringDataServer::invokePythonMethodOn(const char *method, const char *args)
+SALOME::ByteVec *StringDataServer::invokePythonMethodOn(const char *method, const SALOME::ByteVec& args)
 {
   PyObject *self(getPyObjFromPickled(_data));
-  PyObject *argsPy(getPyObjFromPickled(args));
+  std::string argsCpp;
+  FromByteSeqToCpp(args,argsCpp);
+  PyObject *argsPy(getPyObjFromPickled(argsCpp));
   //
   PyObject *selfMeth(PyObject_GetAttrString(self,method));
   if(!selfMeth)
@@ -67,18 +69,42 @@ char *StringDataServer::invokePythonMethodOn(const char *method, const char *arg
     }
   PyObject *res(PyObject_CallObject(selfMeth,argsPy));
   _data=pickelize(self);// if it is a non const method !
-  std::string ret(pickelize(res));
+  std::string retCpp(pickelize(res));
   // to test : res and self
   Py_XDECREF(selfMeth);
   //
   Py_XDECREF(argsPy);
-  return CORBA::string_dup(ret.c_str());
+  return FromCppToByteSeq(retCpp);
+}
+
+void StringDataServer::FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv, std::string& ret)
+{
+  std::size_t sz(bsToBeConv.length());
+  ret.resize(sz,' ');
+  char *buf(const_cast<char *>(ret.c_str()));
+  for(std::size_t i=0;i<sz;i++)
+    buf[i]=bsToBeConv[i];
+}
+
+SALOME::ByteVec *StringDataServer::FromCppToByteSeq(const std::string& strToBeConv)
+{
+  SALOME::ByteVec *ret(new SALOME::ByteVec);
+  const char *buf(strToBeConv.c_str());
+  std::size_t sz(strToBeConv.size());
+  ret->length(sz);
+  for(std::size_t i=0;i<sz;i++)
+    (*ret)[i]=buf[i];
+  return ret;
 }
 
 //! New reference returned
 PyObject *StringDataServer::getPyObjFromPickled(const std::string& pickledData)
 {
-  PyObject *pickledDataPy(PyString_FromString(pickledData.c_str()));
+  std::size_t sz(pickledData.size());
+  PyObject *pickledDataPy(PyString_FromStringAndSize(NULL,sz));// agy : do not use PyString_FromString because std::string hides a vector of byte.
+  char *buf(PyString_AsString(pickledDataPy));// this buf can be used thanks to python documentation.
+  const char *inBuf(pickledData.c_str());
+  std::copy(inBuf,inBuf+sz,buf);
   PyObject *selfMeth(PyObject_GetAttrString(_father->getPickler(),"loads"));
   PyObject *args(PyTuple_New(1)); PyTuple_SetItem(args,0,pickledDataPy);
   PyObject *ret(PyObject_CallObject(selfMeth,args));
@@ -97,7 +123,12 @@ std::string StringDataServer::pickelize(PyObject *obj)
   PyObject *retPy(PyObject_CallObject(selfMeth,args));
   Py_XDECREF(selfMeth);
   Py_XDECREF(args);
-  std::string ret(PyString_AsString(retPy));
+  std::size_t sz(PyString_Size(retPy));
+  std::string ret(sz,'\0');
+  const char *buf(PyString_AsString(retPy));
+  char *inBuf(const_cast<char *>(ret.c_str()));
+  for(std::size_t i=0;i<sz;i++)
+    inBuf[i]=buf[i];
   Py_XDECREF(retPy);
   return ret;
 }
index eb37142ae60ecdabf6821dc29ed7da8795d7436b..f6a1352f0ff517c8df48785fd6cde8ec91ba8043 100644 (file)
@@ -35,10 +35,12 @@ namespace SALOMESDS
   public:
     StringDataServer(DataScopeServer *father, const std::string& varName);
     ~StringDataServer();
-    void setValueOf(const char *newValue);
-    char *getValueOf();
-    char *invokePythonMethodOn(const char *method, const char *args);
+    void setValueOf(const SALOME::ByteVec& newValue);
+    SALOME::ByteVec *getValueOf();
+    SALOME::ByteVec *invokePythonMethodOn(const char *method, const SALOME::ByteVec& args);
   private:
+    static void FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv, std::string& ret);
+    static SALOME::ByteVec *FromCppToByteSeq(const std::string& strToBeConv);
     PyObject *getPyObjFromPickled(const std::string& pickledData);
     std::string pickelize(PyObject *obj);
   private: