From f65a2258839a51dde2002f375fb370980a829c83 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 30 Oct 2014 14:11:13 +0100 Subject: [PATCH] string -> sequence to support the exchance via CORBA or pickled strings + addition of some useful methods. --- idl/SALOME_SDS.idl | 12 +++- src/SALOMESDS/SALOMESDS_DataScopeServer.cxx | 7 ++ src/SALOMESDS/SALOMESDS_DataScopeServer.hxx | 1 + src/SALOMESDS/SALOMESDS_DataServerManager.cxx | 70 ++++++++++++++++++- src/SALOMESDS/SALOMESDS_DataServerManager.hxx | 4 ++ src/SALOMESDS/SALOMESDS_StringDataServer.cxx | 51 +++++++++++--- src/SALOMESDS/SALOMESDS_StringDataServer.hxx | 8 ++- 7 files changed, 136 insertions(+), 17 deletions(-) diff --git a/idl/SALOME_SDS.idl b/idl/SALOME_SDS.idl index 5c20d1bc5..88b1f5e90 100644 --- a/idl/SALOME_SDS.idl +++ b/idl/SALOME_SDS.idl @@ -21,6 +21,7 @@ module SALOME { typedef sequence StringVec; + typedef sequence 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(); }; }; diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx index 4ca656817..61ce70a58 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx @@ -46,6 +46,13 @@ DataScopeServer::~DataScopeServer() Py_XDECREF(_pickler); } +/*! + * Called remotely -> to protect against throw + */ +void DataScopeServer::ping() +{ +} + /*! * Called remotely -> to protect against throw */ diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx index ab50139d6..c74838858 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx @@ -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); diff --git a/src/SALOMESDS/SALOMESDS_DataServerManager.cxx b/src/SALOMESDS/SALOMESDS_DataServerManager.cxx index b8dac6c2a..0db72ed6d 100644 --- a/src/SALOMESDS/SALOMESDS_DataServerManager.cxx +++ b/src/SALOMESDS/SALOMESDS_DataServerManager.cxx @@ -70,11 +70,43 @@ SALOME::StringVec *DataServerManager::listScopes() return ret; } +SALOME::StringVec *DataServerManager::listAliveAndKickingScopes() +{ + std::vector scopes(listOfScopesCpp()); + std::size_t sz(scopes.size()); + std::vector retCpp; retCpp.reserve(sz); + for(std::size_t i=0;ilength(sz); + for(std::size_t i=0;iping(); + } + 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 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 scopes(listOfScopesCpp()); + for(std::vector::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 scopeNames(listOfScopesCpp()); diff --git a/src/SALOMESDS/SALOMESDS_DataServerManager.hxx b/src/SALOMESDS/SALOMESDS_DataServerManager.hxx index ec9720e0d..3badf22c2 100644 --- a/src/SALOMESDS/SALOMESDS_DataServerManager.hxx +++ b/src/SALOMESDS/SALOMESDS_DataServerManager.hxx @@ -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: diff --git a/src/SALOMESDS/SALOMESDS_StringDataServer.cxx b/src/SALOMESDS/SALOMESDS_StringDataServer.cxx index 202e6d2cc..805a2153b 100644 --- a/src/SALOMESDS/SALOMESDS_StringDataServer.cxx +++ b/src/SALOMESDS/SALOMESDS_StringDataServer.cxx @@ -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(ret.c_str())); + for(std::size_t i=0;ilength(sz); + for(std::size_t i=0;igetPickler(),"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(ret.c_str())); + for(std::size_t i=0;i