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);
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);
#include "SALOME_Launcher.hxx"
#include "SALOMESDS_DataServerManager.hxx"
-#include "SALOMESDS_TransactionFactory.hxx"
#include "utilities.h"
#include <sstream>
#include <iostream>
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();
}
return false;
}
-SALOME::BasicDataServer_ptr DataScopeServerBase::retrieveVar(const char *varName)
+SALOME::BasicDataServer_ptr DataScopeServerBase::retrieveVarInternal(const char *varName)
{
- std::string varNameCpp(varName);
- std::vector<std::string> allNames(getAllVarNames());
- std::vector<std::string>::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<std::string>(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<pos;i++,it0++);
+ std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::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);
}
}
+void DataScopeServerBase::checkExistingVar(const std::string& varName)
+{
+ std::vector<std::string> allNames(getAllVarNames());
+ std::vector<std::string>::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<std::string> allNames(getAllVarNames());
+ std::vector<std::string>::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<std::string>(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<pos;i++,it0++);
+ return it0;
+}
+
///////
DataScopeServer::DataScopeServer(CORBA::ORB_ptr orb, const std::string& scopeName):DataScopeServerBase(orb,scopeName)
_vars.push_back(p);
}
+SALOME::ByteVec *DataScopeServerTransaction::fetchSerializedContent(const char *varName)
+{
+ BasicDataServer *var(retrieveVarInternal2(varName));
+ PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(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));
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:
transactions2[i].operate();
mustRollback=false;
}
+ for(std::size_t i=0;i<sz;i++)
+ transactionsCpp[i]->notify();
}
DataScopeServerTransaction::~DataScopeServerTransaction()
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();
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;
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);
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);*/
return FromCppToByteSeq(pickelize(_self));
}
+bool PickelizedPyObjServer::isDict()
+{
+ if(PyDict_Check(_self))
+ return true;
+ else
+ return false;
+}
+
+void PickelizedPyObjServer::addKeyValueHard(const std::vector<unsigned char>& key, const std::vector<unsigned char>& 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());
return ret;
}
+//! New reference returned
+PyObject *PickelizedPyObjServer::getPyObjFromPickled(const std::vector<unsigned char>& 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)
{
#include "SALOMESDS_BasicDataServer.hxx"
+#include <vector>
+
namespace SALOMESDS
{
class PickelizedPyObjServer : public BasicDataServer, public virtual POA_SALOME::PickelizedPyObjServer
~PickelizedPyObjServer();
void setSerializedContent(const SALOME::ByteVec& newValue);
SALOME::ByteVec *fetchSerializedContent();
- protected:
+ public:
+ bool isDict();
+ void addKeyValueHard(const std::vector<unsigned char>& key, const std::vector<unsigned char>& 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<unsigned char>& pickledData);
std::string pickelize(PyObject *obj);
void setNewPyObj(PyObject *obj);
void setSerializedContentInternal(const SALOME::ByteVec& newValue);
#include "SALOMESDS_Transaction.hxx"
#include "SALOMESDS_Exception.hxx"
+#include "SALOMESDS_PickelizedPyObjServer.hxx"
#include <sstream>
ret[i]=bsToBeConv[i];
}
+Transaction::~Transaction()
+{
+}
+
TransactionVarCreate::TransactionVarCreate(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& constValue):Transaction(dsct,varName)
{
FromByteSeqToVB(constValue,_data);
_dsct->deleteVar(_var_name.c_str());
}
+/*!
+ TODO : To be implemented.
+ */
+void TransactionVarCreate::notify()
+{
+}
+
void TransactionRdOnlyVarCreate::perform()
{
SALOME::ByteVec data2;
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<PickelizedPyObjServer *>(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()
+{
+}
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<unsigned char>& ret);
static void FromVBToByteSeq(const std::vector<unsigned char>& bsToBeConv, SALOME::ByteVec& ret);
{
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<unsigned char> _data;
};
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<unsigned char> _key;
+ std::vector<unsigned char> _value;
+ std::string _zeDataBefore;
+ PickelizedPyObjServer *_varc;
+ };
}
#endif