From 4e25b349b852f6ec582f39235719c3e1ee3bfde2 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Mon, 13 Jul 2015 18:01:54 +0200 Subject: [PATCH] Nearly ready to implement wait. --- idl/SALOME_SDS.idl | 3 +- src/Launcher/SALOME_LauncherServer.cxx | 4 -- src/SALOMESDS/SALOMESDS_DataScopeServer.cxx | 71 +++++++++++++++---- src/SALOMESDS/SALOMESDS_DataScopeServer.hxx | 10 ++- .../SALOMESDS_PickelizedPyObjServer.cxx | 37 ++++++++++ .../SALOMESDS_PickelizedPyObjServer.hxx | 8 ++- src/SALOMESDS/SALOMESDS_Transaction.cxx | 63 ++++++++++++++++ src/SALOMESDS/SALOMESDS_Transaction.hxx | 24 ++++++- 8 files changed, 198 insertions(+), 22 deletions(-) diff --git a/idl/SALOME_SDS.idl b/idl/SALOME_SDS.idl index fac3ecdfb..d5c5d4d15 100644 --- a/idl/SALOME_SDS.idl +++ b/idl/SALOME_SDS.idl @@ -63,13 +63,13 @@ module SALOME string getScopeName(); StringVec listVars(); boolean existVar(in string varName) raises (SALOME::SALOME_Exception); - BasicDataServer retrieveVar(in string varName) raises (SALOME::SALOME_Exception); void deleteVar(in string varName) raises (SALOME::SALOME_Exception); void shutdownIfNotHostedByDSM(); }; interface DataScopeServer : DataScopeServerBase { + BasicDataServer retrieveVar(in string varName) raises (SALOME::SALOME_Exception); PickelizedPyObjRdOnlyServer createRdOnlyVar(in string varName, in ByteVec constValue) raises (SALOME::SALOME_Exception); PickelizedPyObjRdExtServer createRdExtVar(in string varName, in ByteVec constValue) raises (SALOME::SALOME_Exception); //PickelizedPyObjRdWrServer createRdWrVar(in string typeName, in string varName) raises (SALOME::SALOME_Exception); @@ -83,6 +83,7 @@ module SALOME interface DataScopeServerTransaction : DataScopeServerBase { + ByteVec fetchSerializedContent(in string varName) raises (SALOME::SALOME_Exception); Transaction createRdOnlyVarTransac(in string varName, in ByteVec constValue) raises (SALOME::SALOME_Exception); Transaction createRdExtVarTransac(in string varName, in ByteVec constValue) raises (SALOME::SALOME_Exception); Transaction createRdWrVarTransac(in string varName, in ByteVec constValue) raises (SALOME::SALOME_Exception); diff --git a/src/Launcher/SALOME_LauncherServer.cxx b/src/Launcher/SALOME_LauncherServer.cxx index b5b87cdb1..d1ffcb03b 100644 --- a/src/Launcher/SALOME_LauncherServer.cxx +++ b/src/Launcher/SALOME_LauncherServer.cxx @@ -22,7 +22,6 @@ #include "SALOME_Launcher.hxx" #include "SALOMESDS_DataServerManager.hxx" -#include "SALOMESDS_TransactionFactory.hxx" #include "utilities.h" #include #include @@ -103,9 +102,6 @@ int main(int argc, char* argv[]) SALOMESDS::DataServerManager *dsm(new SALOMESDS::DataServerManager(argc,argv,orb,root_poa)); dsm->_remove_ref(); // - SALOMESDS::TransactionFactory *tf(new SALOMESDS::TransactionFactory(dsm)); - tf->_remove_ref(); - // orb->run(); orb->destroy(); } diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx index c08cfc2ee..dace3b8fa 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx @@ -100,23 +100,18 @@ CORBA::Boolean DataScopeServerBase::existVar(const char *varName) return false; } -SALOME::BasicDataServer_ptr DataScopeServerBase::retrieveVar(const char *varName) +SALOME::BasicDataServer_ptr DataScopeServerBase::retrieveVarInternal(const char *varName) { - std::string varNameCpp(varName); - std::vector allNames(getAllVarNames()); - std::vector::iterator it(std::find(allNames.begin(),allNames.end(),varNameCpp)); - if(it==allNames.end()) - { - std::ostringstream oss; oss << "DataScopeServerBase::retrieveVar : name \"" << varNameCpp << "\" does not exists ! Possibilities are :"; - std::copy(allNames.begin(),allNames.end(),std::ostream_iterator(oss,", ")); - throw Exception(oss.str()); - } - std::size_t pos(std::distance(allNames.begin(),it)); - std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(_vars.begin()); - for(std::size_t i=0;i >::iterator it0(retrieveVarInternal3(varName)); return SALOME::BasicDataServer::_duplicate((*it0).first); } +BasicDataServer *DataScopeServerBase::retrieveVarInternal2(const std::string& varName) +{ + std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(retrieveVarInternal3(varName)); + return (*it0).second; +} + void DataScopeServerBase::deleteVar(const char *varName) { std::string varNameCpp(varName); @@ -243,6 +238,33 @@ void DataScopeServerBase::checkNotAlreadyExistingVar(const std::string& varName) } } +void DataScopeServerBase::checkExistingVar(const std::string& varName) +{ + std::vector allNames(getAllVarNames()); + std::vector::iterator it(std::find(allNames.begin(),allNames.end(),varName)); + if(it==allNames.end()) + { + std::ostringstream oss; oss << "DataScopeServerBase::checkExistingVar : name \"" << varName << "\" does not exist !"; + throw Exception(oss.str()); + } +} + +std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator DataScopeServerBase::retrieveVarInternal3(const std::string& varName) +{ + std::vector allNames(getAllVarNames()); + std::vector::iterator it(std::find(allNames.begin(),allNames.end(),varName)); + if(it==allNames.end()) + { + std::ostringstream oss; oss << "DataScopeServerBase::retrieveVarInternal : name \"" << varName << "\" does not exists ! Possibilities are :"; + std::copy(allNames.begin(),allNames.end(),std::ostream_iterator(oss,", ")); + throw Exception(oss.str()); + } + std::size_t pos(std::distance(allNames.begin(),it)); + std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(_vars.begin()); + for(std::size_t i=0;i(var)); + if(!varc) + { + std::ostringstream oss; oss << "DataScopeServerTransaction::fetchSerializedContent : var \"" << varName << "\"exists but it is not serialized !"; + throw Exception(oss.str()); + } + return varc->fetchSerializedContent(); +} + SALOME::Transaction_ptr DataScopeServerTransaction::createRdOnlyVarTransac(const char *varName, const SALOME::ByteVec& constValue) { TransactionRdOnlyVarCreate *ret(new TransactionRdOnlyVarCreate(this,varName,constValue)); @@ -357,6 +391,15 @@ SALOME::Transaction_ptr DataScopeServerTransaction::createRdWrVarTransac(const c return SALOME::Transaction::_narrow(obj); } +SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarHard(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value) +{ + TransactionAddKeyValueHard *ret(new TransactionAddKeyValueHard(this,varName,key,value)); + ret->checkNotAlreadyExisting(); + PortableServer::ObjectId_var id(_poa->activate_object(ret)); + CORBA::Object_var obj(_poa->id_to_reference(id)); + return SALOME::Transaction::_narrow(obj); +} + class TrustTransaction { public: @@ -407,6 +450,8 @@ void DataScopeServerTransaction::atomicApply(const SALOME::ListOfTransaction& tr transactions2[i].operate(); mustRollback=false; } + for(std::size_t i=0;inotify(); } DataScopeServerTransaction::~DataScopeServerTransaction() diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx index 769270f43..03b5934d7 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx @@ -46,7 +46,8 @@ namespace SALOMESDS char *getScopeName(); SALOME::StringVec *listVars(); CORBA::Boolean existVar(const char *varName); - SALOME::BasicDataServer_ptr retrieveVar(const char *varName); + SALOME::BasicDataServer_ptr retrieveVarInternal(const char *varName); + BasicDataServer *retrieveVarInternal2(const std::string& varName); void deleteVar(const char *varName); void shutdownIfNotHostedByDSM(); ~DataScopeServerBase(); @@ -65,6 +66,9 @@ namespace SALOMESDS std::vector< std::string> getAllVarNames() const; CORBA::Object_var activateWithDedicatedPOA(BasicDataServer *ds); void checkNotAlreadyExistingVar(const std::string& varName); + void checkExistingVar(const std::string& varName); + protected: + std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator retrieveVarInternal3(const std::string& varName); protected: PyObject *_globals; PyObject *_locals; @@ -81,6 +85,7 @@ namespace SALOMESDS public: DataScopeServer(CORBA::ORB_ptr orb, const std::string& scopeName); DataScopeServer(const DataScopeServer& other); + SALOME::BasicDataServer_ptr retrieveVar(const char *varName) { return retrieveVarInternal(varName); } SALOME::PickelizedPyObjRdOnlyServer_ptr createRdOnlyVar(const char *varName, const SALOME::ByteVec& constValue); SALOME::PickelizedPyObjRdExtServer_ptr createRdExtVar(const char *varName, const SALOME::ByteVec& constValue); SALOME::PickelizedPyObjRdWrServer_ptr createRdWrVar(const char *typeName, const char *varName); @@ -97,15 +102,16 @@ namespace SALOMESDS void createRdExtVarInternal(const std::string& varName, const SALOME::ByteVec& constValue); void createRdWrVarInternal(const std::string& varName, const SALOME::ByteVec& constValue); public://remotely callable + SALOME::ByteVec *fetchSerializedContent(const char *varName); SALOME::Transaction_ptr createRdOnlyVarTransac(const char *varName, const SALOME::ByteVec& constValue); SALOME::Transaction_ptr createRdExtVarTransac(const char *varName, const SALOME::ByteVec& constValue); SALOME::Transaction_ptr createRdWrVarTransac(const char *varName, const SALOME::ByteVec& constValue); + SALOME::Transaction_ptr addKeyValueInVarHard(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value); void atomicApply(const SALOME::ListOfTransaction& transactions); }; /* SALOME::Transaction_ptr addKeyValueInVarErrorIfAlreadyExisting(const char *varName, const char *scopeName, const SALOME::ByteVec& key, const SALOME::ByteVec& value); - SALOME::Transaction_ptr addKeyValueInVarHard(const char *varName, const char *scopeName, const SALOME::ByteVec& key, const SALOME::ByteVec& value); SALOME::Transaction_ptr removeKeyInVarErrorIfNotAlreadyExisting(const char *varName, const char *scopeName, const SALOME::ByteVec& key); SALOME::ByteVec *waitForKeyInVar(const char *varName, const char *scopeName, const SALOME::ByteVec& constKey); SALOME::ByteVec *waitForKeyInVarAndKillIt(const char *varName, const char *scopeName, const SALOME::ByteVec& constKey, SALOME::Transaction_out transaction);*/ diff --git a/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.cxx b/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.cxx index 2bc0c120c..0dd9701e3 100644 --- a/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.cxx +++ b/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.cxx @@ -52,6 +52,27 @@ SALOME::ByteVec *PickelizedPyObjServer::fetchSerializedContent() return FromCppToByteSeq(pickelize(_self)); } +bool PickelizedPyObjServer::isDict() +{ + if(PyDict_Check(_self)) + return true; + else + return false; +} + +void PickelizedPyObjServer::addKeyValueHard(const std::vector& key, const std::vector& value) +{ + if(!isDict()) + throw Exception("PickelizedPyObjServer::addKeyValueHard : not a dict !"); + PyObject *k(getPyObjFromPickled(key)); + PyObject *v(getPyObjFromPickled(value)); + bool isOK(PyDict_SetItem(_self,k,v)==0); + Py_XDECREF(k); + Py_XDECREF(v); + if(!isOK) + throw Exception("PickelizedPyObjServer::addKeyValueHard : error when trying to add key,value to dict !"); +} + void PickelizedPyObjServer::FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv, std::string& ret) { std::size_t sz(bsToBeConv.length()); @@ -88,6 +109,22 @@ PyObject *PickelizedPyObjServer::getPyObjFromPickled(const std::string& pickledD return ret; } +//! New reference returned +PyObject *PickelizedPyObjServer::getPyObjFromPickled(const std::vector& pickledData) +{ + 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 unsigned char *inBuf(&pickledData[0]); + 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)); + Py_XDECREF(args); + Py_XDECREF(selfMeth); + return ret; +} + //! obj is consumed by this method. std::string PickelizedPyObjServer::pickelize(PyObject *obj) { diff --git a/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.hxx b/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.hxx index e216262cd..e8f3edb5f 100644 --- a/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.hxx +++ b/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.hxx @@ -28,6 +28,8 @@ #include "SALOMESDS_BasicDataServer.hxx" +#include + namespace SALOMESDS { class PickelizedPyObjServer : public BasicDataServer, public virtual POA_SALOME::PickelizedPyObjServer @@ -38,10 +40,14 @@ namespace SALOMESDS ~PickelizedPyObjServer(); void setSerializedContent(const SALOME::ByteVec& newValue); SALOME::ByteVec *fetchSerializedContent(); - protected: + public: + bool isDict(); + void addKeyValueHard(const std::vector& key, const std::vector& value); + public: static void FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv, std::string& ret); static SALOME::ByteVec *FromCppToByteSeq(const std::string& strToBeConv); PyObject *getPyObjFromPickled(const std::string& pickledData); + PyObject *getPyObjFromPickled(const std::vector& pickledData); std::string pickelize(PyObject *obj); void setNewPyObj(PyObject *obj); void setSerializedContentInternal(const SALOME::ByteVec& newValue); diff --git a/src/SALOMESDS/SALOMESDS_Transaction.cxx b/src/SALOMESDS/SALOMESDS_Transaction.cxx index 31778707c..e46a6dafb 100644 --- a/src/SALOMESDS/SALOMESDS_Transaction.cxx +++ b/src/SALOMESDS/SALOMESDS_Transaction.cxx @@ -20,6 +20,7 @@ #include "SALOMESDS_Transaction.hxx" #include "SALOMESDS_Exception.hxx" +#include "SALOMESDS_PickelizedPyObjServer.hxx" #include @@ -42,6 +43,10 @@ void Transaction::FromVBToByteSeq(const std::vector& bsToBeConv, ret[i]=bsToBeConv[i]; } +Transaction::~Transaction() +{ +} + TransactionVarCreate::TransactionVarCreate(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& constValue):Transaction(dsct,varName) { FromByteSeqToVB(constValue,_data); @@ -58,6 +63,13 @@ void TransactionVarCreate::rollBack() _dsct->deleteVar(_var_name.c_str()); } +/*! + TODO : To be implemented. + */ +void TransactionVarCreate::notify() +{ +} + void TransactionRdOnlyVarCreate::perform() { SALOME::ByteVec data2; @@ -78,3 +90,54 @@ void TransactionRdWrVarCreate::perform() FromVBToByteSeq(_data,data2); _dsct->createRdWrVarInternal(_var_name,data2); } + +TransactionAddKeyValueHard::TransactionAddKeyValueHard(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value):Transaction(dsct,varName),_varc(0) +{ + FromByteSeqToVB(key,_key); + FromByteSeqToVB(value,_value); +} + +void TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure() +{ + checkNotAlreadyExisting(); + // + BasicDataServer *var(_dsct->retrieveVarInternal2(_var_name.c_str())); + _varc=dynamic_cast(var); + if(!_varc) + { + std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << _var_name << "\"exists but it is not serialized !"; + throw Exception(oss.str()); + } + if(!_varc->isDict()) + { + std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << _var_name << "\"exists but it is not a Dict !"; + throw Exception(oss.str()); + } + // + _zeDataBefore.clear(); + SALOME::ByteVec *zeDataBefore(_varc->fetchSerializedContent()); + PickelizedPyObjServer::FromByteSeqToCpp(*zeDataBefore,_zeDataBefore); +} + +void TransactionAddKeyValueHard::perform() +{ + _varc->addKeyValueHard(_key,_value); +} + +void TransactionAddKeyValueHard::rollBack() +{ + PyObject *obj(_varc->getPyObjFromPickled(_zeDataBefore)); + _varc->setNewPyObj(obj); + _zeDataBefore.clear(); +} + +/*! + TODO : To be implemented. + */ +void TransactionAddKeyValueHard::notify() +{ +} + +TransactionAddKeyValueHard::~TransactionAddKeyValueHard() +{ +} diff --git a/src/SALOMESDS/SALOMESDS_Transaction.hxx b/src/SALOMESDS/SALOMESDS_Transaction.hxx index 805b8d647..f934cdf5c 100644 --- a/src/SALOMESDS/SALOMESDS_Transaction.hxx +++ b/src/SALOMESDS/SALOMESDS_Transaction.hxx @@ -38,9 +38,13 @@ namespace SALOMESDS public: Transaction(DataScopeServerTransaction *dsct, const std::string& varName):_dsct(dsct),_var_name(varName) { if(!_dsct) throw Exception("Transaction constructor error !"); } std::string getVarName() const { return _var_name; } + void checkNotAlreadyExisting() { _dsct->checkNotAlreadyExistingVar(_var_name); } + void checkVarExisting() { _dsct->checkExistingVar(_var_name); } virtual void prepareRollBackInCaseOfFailure() = 0; virtual void perform() = 0; virtual void rollBack() = 0; + virtual void notify() = 0; + virtual ~Transaction(); public: static void FromByteSeqToVB(const SALOME::ByteVec& bsToBeConv, std::vector& ret); static void FromVBToByteSeq(const std::vector& bsToBeConv, SALOME::ByteVec& ret); @@ -53,9 +57,9 @@ namespace SALOMESDS { public: TransactionVarCreate(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& constValue); - void checkNotAlreadyExisting() { _dsct->checkNotAlreadyExistingVar(_var_name); } void prepareRollBackInCaseOfFailure(); void rollBack(); + void notify(); protected: std::vector _data; }; @@ -80,6 +84,24 @@ namespace SALOMESDS TransactionRdWrVarCreate(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& constValue):TransactionVarCreate(dsct,varName,constValue) { } void perform(); }; + + class PickelizedPyObjServer; + + class TransactionAddKeyValueHard : public Transaction + { + public: + TransactionAddKeyValueHard(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value); + void prepareRollBackInCaseOfFailure(); + void perform(); + void rollBack(); + void notify(); + ~TransactionAddKeyValueHard(); + private: + std::vector _key; + std::vector _value; + std::string _zeDataBefore; + PickelizedPyObjServer *_varc; + }; } #endif -- 2.39.2