1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony GEAY (EDF R&D)
21 #include "SALOMESDS_DataScopeServer.hxx"
22 #include "SALOMESDS_DataServerManager.hxx"
23 #include "SALOMESDS_PickelizedPyObjRdOnlyServer.hxx"
24 #include "SALOMESDS_PickelizedPyObjRdExtServer.hxx"
25 #include "SALOMESDS_PickelizedPyObjRdWrServer.hxx"
26 #include "SALOMESDS_PickelizedPyObjRdExtInitServer.hxx"
27 #include "SALOMESDS_TrustTransaction.hxx"
28 #include "SALOMESDS_KeyWaiter.hxx"
29 #include "SALOMESDS_Transaction.hxx"
30 #include "SALOME_NamingService.hxx"
31 #include "SALOMESDS_Exception.hxx"
37 // agy : awful, to be factorized with ContainerManager.
42 #define getpid _getpid
45 using namespace SALOMESDS;
47 std::size_t DataScopeServerBase::COUNTER=0;
49 void DataScopeKiller::shutdown()
55 RequestSwitcher::RequestSwitcher(CORBA::ORB_ptr orb, DataScopeServerTransaction *ds):RequestSwitcherBase(orb),_ds(ds)
59 SALOME::StringVec *RequestSwitcher::listVars()
61 return _ds->listVars();
64 SALOME::ByteVec *RequestSwitcher::fetchSerializedContent(const char *varName)
66 return _ds->fetchSerializedContent(varName);
69 void RequestSwitcher::fetchAndGetAccessOfVar(const char *varName, CORBA::String_out access, SALOME::ByteVec_out data)
71 return _ds->fetchAndGetAccessOfVar(varName,access,data);
74 DataScopeServerBase::DataScopeServerBase(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):_pyHelper(pyHelper),_orb(CORBA::ORB::_duplicate(orb)),_name(scopeName),_killer(killer)
78 DataScopeServerBase::DataScopeServerBase(const DataScopeServerBase& other):omniServant(other),ServantBase(other),_pyHelper(other._pyHelper),_name(other._name),_vars(other._vars),_killer(other._killer)
82 DataScopeServerBase::~DataScopeServerBase()
84 for(std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it=_vars.begin();it!=_vars.end();it++)
86 BasicDataServer *obj((*it).second);
95 * Called remotely -> to protect against throw
97 void DataScopeServerBase::ping()
102 * Called remotely -> to protect against throw
104 char *DataScopeServerBase::getScopeName()
106 return CORBA::string_dup(_name.c_str());
110 * Called remotely -> to protect against throw
112 SALOME::StringVec *DataScopeServerBase::listVars()
114 SALOME::StringVec *ret(new SALOME::StringVec);
115 std::size_t sz(_vars.size());
117 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(_vars.begin());
118 for(std::size_t i=0;i<sz;it++,i++)
120 BasicDataServer *obj((*it).second);
121 std::string name(obj->getVarNameCpp());
122 (*ret)[i]=CORBA::string_dup(name.c_str());
127 CORBA::Boolean DataScopeServerBase::existVar(const char *varName)
129 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it(_vars.begin());
130 for(;it!=_vars.end();it++)
131 if((*it).second->getVarNameCpp()==varName)
136 SALOME::BasicDataServer_ptr DataScopeServerBase::retrieveVarInternal(const char *varName)
138 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(retrieveVarInternal3(varName));
139 return SALOME::BasicDataServer::_duplicate((*it0).first);
142 BasicDataServer *DataScopeServerBase::retrieveVarInternal2(const std::string& varName)
144 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(retrieveVarInternal3(varName));
145 return (*it0).second;
148 void DataScopeServerBase::deleteVar(const char *varName)
150 std::string varNameCpp(varName);
151 std::vector<std::string> allNames(getAllVarNames());
152 std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varNameCpp));
153 if(it==allNames.end())
155 std::ostringstream oss; oss << "DataScopeServerBase::deleteVar : name \"" << varNameCpp << "\" does not exists ! Possibilities are :";
156 std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
157 throw Exception(oss.str());
159 std::size_t pos(std::distance(allNames.begin(),it));
160 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(_vars.begin());
161 for(std::size_t ii=0;ii<pos;ii++,it0++);
162 (*it0).second->decrRef();
166 CORBA::Boolean DataScopeServerBase::shutdownIfNotHostedByDSM(SALOME::DataScopeKiller_out killer)
168 SALOME_NamingService ns(_orb);
169 CORBA::Object_var obj(ns.Resolve(DataServerManager::NAME_IN_NS));
170 SALOME::DataServerManager_var dsm(SALOME::DataServerManager::_narrow(obj));
171 if(CORBA::is_nil(dsm))
172 throw Exception("Unable to reach in the NS the unique DataServerManager instance of the Session !");
173 // destroy ref in the naming service
174 std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(_name));
175 ns.Destroy_Name(fullScopeName.c_str());
176 // establish if dsm and this shared the same POA. If yes dsm and this are collocated !
177 PortableServer::ServantBase *ret(0);
180 ret=_poa->reference_to_servant(dsm);
182 catch(...) { ret=0; }
187 killer=SALOME::DataScopeKiller::_duplicate(_killer);
194 killer=SALOME::DataScopeKiller::_duplicate(_killer);
199 SALOME::ByteVec *DataScopeServerBase::fetchSerializedContent(const char *varName)
201 BasicDataServer *var(retrieveVarInternal2(varName));
202 PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
205 std::ostringstream oss; oss << "DataScopeServerBase::fetchSerializedContent : var \"" << varName << "\" exists but it is not serialized !";
206 throw Exception(oss.str());
208 return varc->fetchSerializedContent();
211 SALOME::SeqOfByteVec *DataScopeServerBase::getAllKeysOfVarWithTypeDict(const char *varName)
213 BasicDataServer *var(retrieveVarInternal2(varName));
214 PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
217 std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" exists but it is not serialized !";
218 throw Exception(oss.str());
222 std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" exists but it is not a PyDict !";
223 throw Exception(oss.str());
225 PyObject *keys(PyDict_Keys(varc->getPyObj()));
226 if(!PyList_Check(keys))
228 std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" has keys but not of type list !";
229 throw Exception(oss.str());
231 Py_ssize_t sz(PyList_Size(keys));
232 SALOME::SeqOfByteVec *ret(new SALOME::SeqOfByteVec);
234 for(Py_ssize_t i=0;i<sz;i++)
236 PyObject *item(PyList_GetItem(keys,i));
238 std::string pickel(varc->pickelize(item));//item consumed
239 PickelizedPyObjServer::FromCppToByteSeq(pickel,(*ret)[i]);
245 SALOME::ByteVec *DataScopeServerBase::getValueOfVarWithTypeDict(const char *varName, const SALOME::ByteVec& constKey)
247 BasicDataServer *var(retrieveVarInternal2(varName));
248 PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
251 std::ostringstream oss; oss << "DataScopeServerBase::getValueOfVarWithTypeDict : var \"" << varName << "\" exists but it is not serialized !";
252 throw Exception(oss.str());
256 std::ostringstream oss; oss << "DataScopeServerBase::getValueOfVarWithTypeDict : var \"" << varName << "\" exists but it is not a PyDict !";
257 throw Exception(oss.str());
261 PickelizedPyObjServer::FromByteSeqToCpp(constKey,keyCpp);
262 SALOME::AutoPyRef key(PickelizedPyObjServer::GetPyObjFromPickled(keyCpp,this));
263 PyObject *value(PyDict_GetItem(varc->getPyObj(),key.get()));//borrowed
266 std::ostringstream oss; oss << "DataScopeServerBase::getValueOfVarWithTypeDict : var \"" << varName << "\" seems to not have key specified !";
267 throw Exception(oss.str());
270 std::string ret(PickelizedPyObjServer::Pickelize(value,this));//value is consumed
271 return PickelizedPyObjServer::FromCppToByteSeq(ret);
274 void DataScopeServerBase::takeANap(CORBA::Double napDurationInSec)
276 if(napDurationInSec<0.)
277 throw Exception("DataScopeServerBase::takeANap : negative value !");
279 struct timespec req,rem;
280 long nbSec((long)napDurationInSec);
281 double remainTime(napDurationInSec-(double)nbSec);
283 req.tv_nsec=(long)(remainTime*1000000.);
284 int ret(nanosleep(&req,&rem));
286 throw Exception("DataScopeServerBase::takeANap : nap not finished as expected !");
288 throw Exception("DataScopeServerBase::takeANap : not implemented for Windows !");
292 void DataScopeServerBase::registerToSalomePiDict() const
294 _pyHelper->registerToSalomePiDict("SALOME_DataScopeServerBase",getpid());
297 void DataScopeServerBase::setPOA(PortableServer::POA_var poa)
302 void DataScopeServerBase::registerInNS(SALOME::DataScopeServerBase_ptr ptr)
304 std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(_name));
305 SALOME_NamingService ns(_orb);
306 ns.Register(ptr,fullScopeName.c_str());
309 std::string DataScopeServerBase::BuildTmpVarNameFrom(const std::string& varName)
311 std::ostringstream oss;
312 oss << varName << "@" << COUNTER++;
316 std::vector< std::string > DataScopeServerBase::getAllVarNames() const
318 std::size_t sz(_vars.size());
319 std::vector<std::string> ret(sz);
320 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it(_vars.begin());
321 for(std::size_t i=0;i<sz;it++,i++)
322 ret[i]=(*it).second->getVarNameCpp();
326 bool DataScopeServerBase::isExistingVar(const std::string& varName) const
328 std::vector<std::string> allNames(getAllVarNames());
329 std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
330 return it!=allNames.end();
333 void DataScopeServerBase::checkNotAlreadyExistingVar(const std::string& varName) const
335 if(isExistingVar(varName))
337 std::ostringstream oss; oss << "DataScopeServerBase::checkNotAlreadyExistingVar : name \"" << varName << "\" already exists !";
338 throw Exception(oss.str());
342 void DataScopeServerBase::checkExistingVar(const std::string& varName) const
344 std::vector<std::string> allNames(getAllVarNames());
345 std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
346 if(it==allNames.end())
348 std::ostringstream oss; oss << "DataScopeServerBase::checkExistingVar : name \"" << varName << "\" does not exist !";
349 throw Exception(oss.str());
353 PickelizedPyObjServer *DataScopeServerBase::checkVarExistingAndDict(const std::string& varName)
355 checkExistingVar(varName);
356 BasicDataServer *var(retrieveVarInternal2(varName.c_str()));
357 PickelizedPyObjServer *ret(dynamic_cast<PickelizedPyObjServer *>(var));
360 std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << varName << "\"exists but it is not serialized !";
361 throw Exception(oss.str());
365 std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << varName << "\"exists but it is not a Dict !";
366 throw Exception(oss.str());
371 void DataScopeServerBase::moveStatusOfVarFromRdWrToRdOnly(const std::string& varName)
373 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
374 std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
375 PickelizedPyObjRdWrServer *varc(dynamic_cast<PickelizedPyObjRdWrServer *>(p.second));
377 throw Exception("DataScopeServerBase::moveStatusOfVarFromRdWrToRdOnly : var is not a RdWr !");
378 PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
379 PickelizedPyObjRdOnlyServer *newVar(new PickelizedPyObjRdOnlyServer(this,varName,pyobj));
380 CORBA::Object_var obj(newVar->activate());
381 SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
382 p.first=obj2; p.second=newVar;
386 void DataScopeServerBase::moveStatusOfVarFromRdOnlyToRdWr(const std::string& varName)
388 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
389 std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
390 PickelizedPyObjRdOnlyServer *varc(dynamic_cast<PickelizedPyObjRdOnlyServer *>(p.second));
392 throw Exception("DataScopeServerBase::moveStatusOfVarFromRdOnlyToRdWr : var is not a RdWr !");
393 PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
394 PickelizedPyObjRdWrServer *newVar(new PickelizedPyObjRdWrServer(this,varName,pyobj));
395 CORBA::Object_var obj(newVar->activate());
396 SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
397 p.first=obj2; p.second=newVar;
401 void DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExtInit(const std::string& varName)
403 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
404 std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
405 PickelizedPyObjRdExtServer *varc0(dynamic_cast<PickelizedPyObjRdExtServer *>(p.second));
406 PickelizedPyObjRdExtInitServer *varc1(dynamic_cast<PickelizedPyObjRdExtInitServer *>(p.second));
408 throw Exception("DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExtInit : var is neither RdExt nor RdExtInit !");
411 PickelizedPyObjRdExtInitServer *newVar(varc0->buildInitInstanceFrom(varName));
412 newVar->incrNbClients();
413 CORBA::Object_var obj(newVar->activate());
414 SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
415 p.first=obj2; p.second=newVar;
419 varc1->incrNbClients();
422 void DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExt(const std::string& varName)
424 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
425 std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
426 PickelizedPyObjRdExtInitServer *varc0(dynamic_cast<PickelizedPyObjRdExtInitServer *>(p.second));
427 PickelizedPyObjRdExtServer *varc1(dynamic_cast<PickelizedPyObjRdExtServer *>(p.second));
429 throw Exception("DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExt : var is not a RdExtInit !");
432 if(varc0->decrNbClients())
434 PickelizedPyObjRdExtServer *newVar(varc0->buildStdInstanceFrom(varName));
435 CORBA::Object_var obj(newVar->activate());
436 SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
437 p.first=obj2; p.second=newVar;
443 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator DataScopeServerBase::retrieveVarInternal3(const std::string& varName) const
445 std::vector<std::string> allNames(getAllVarNames());
446 std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
447 if(it==allNames.end())
449 std::ostringstream oss; oss << "DataScopeServerBase::retrieveVarInternal3 : name \"" << varName << "\" does not exists ! Possibilities are :";
450 std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
451 throw Exception(oss.str());
453 std::size_t pos(std::distance(allNames.begin(),it));
454 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(_vars.begin());
455 for(std::size_t i=0;i<pos;i++,it0++);
459 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator DataScopeServerBase::retrieveVarInternal4(const std::string& varName)
461 std::vector<std::string> allNames(getAllVarNames());
462 std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
463 if(it==allNames.end())
465 std::ostringstream oss; oss << "DataScopeServerBase::retrieveVarInternal4 : name \"" << varName << "\" does not exists ! Possibilities are :";
466 std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
467 throw Exception(oss.str());
469 std::size_t pos(std::distance(allNames.begin(),it));
470 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(_vars.begin());
471 for(std::size_t i=0;i<pos;i++,it0++);
477 DataScopeServer::DataScopeServer(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):DataScopeServerBase(pyHelper,orb,killer,scopeName)
481 DataScopeServer::DataScopeServer(const DataScopeServer& other):omniServant(other),ServantBase(other),DataScopeServerBase(other)
485 SALOME::PickelizedPyObjRdOnlyServer_ptr DataScopeServer::createRdOnlyVar(const char *varName, const SALOME::ByteVec& constValue)
487 std::string varNameCpp(varName);
488 checkNotAlreadyExistingVar(varNameCpp);
489 PickelizedPyObjRdOnlyServer *tmp(new PickelizedPyObjRdOnlyServer(this,varNameCpp,constValue));
490 CORBA::Object_var ret(tmp->activate());
491 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
493 return SALOME::PickelizedPyObjRdOnlyServer::_narrow(ret);
496 SALOME::PickelizedPyObjRdExtServer_ptr DataScopeServer::createRdExtVar(const char *varName, const SALOME::ByteVec& constValue)
498 std::string varNameCpp(varName);
499 checkNotAlreadyExistingVar(varNameCpp);
500 PickelizedPyObjRdExtServer *tmp(new PickelizedPyObjRdExtServer(this,varNameCpp,constValue));
501 CORBA::Object_var ret(tmp->activate());
502 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
504 return SALOME::PickelizedPyObjRdExtServer::_narrow(ret);
507 SALOME::PickelizedPyObjRdWrServer_ptr DataScopeServer::createRdWrVar(const char *typeName, const char *varName)
509 std::string varNameCpp(varName),typeNameCpp(typeName);
510 checkNotAlreadyExistingVar(varNameCpp);
511 PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,typeNameCpp,varNameCpp));
512 CORBA::Object_var ret(tmp->activate());
513 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
515 return SALOME::PickelizedPyObjRdWrServer::_narrow(ret);
518 DataScopeServer::~DataScopeServer()
524 DataScopeServerTransaction::DataScopeServerTransaction(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):DataScopeServerBase(pyHelper,orb,killer,scopeName)
526 CORBA::Object_var obj(_orb->resolve_initial_references("RootPOA"));
527 PortableServer::POA_var poa(PortableServer::POA::_narrow(obj));
529 PortableServer::POAManager_var mgr(poa->the_POAManager());
530 CORBA::PolicyList policies;
532 PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
533 policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol);
534 PortableServer::POA_var poa2(poa->create_POA("POAForWaiters",mgr,policies));
535 threadPol->destroy();
537 _poa_for_key_waiter=poa2;
540 DataScopeServerTransaction::DataScopeServerTransaction(const DataScopeServerTransaction& other):omniServant(other),ServantBase(other),DataScopeServerBase(other),_poa_for_key_waiter(other.getPOA4KeyWaiter())
544 char *DataScopeServerTransaction::getAccessOfVar(const char *varName)
546 std::string varNameCpp(varName);
547 checkExistingVar(varNameCpp);
548 BasicDataServer *var(retrieveVarInternal2(varName));
550 throw Exception("DataScopeServerTransaction::getAccessOfVar : variable is NULL !");
551 PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
553 throw Exception("DataScopeServerTransaction::getAccessOfVar : variable is not of type PickelizedPyObjServer !");
554 std::string ret(varc->getAccessStr());
555 return CORBA::string_dup(ret.c_str());
559 * This method is here to retrieve atomically accessStr and picklization.
561 void DataScopeServerTransaction::fetchAndGetAccessOfVar(const char *varName, CORBA::String_out access, SALOME::ByteVec_out data)
563 access=getAccessOfVar(varName);
564 data=fetchSerializedContent(varName);
567 void DataScopeServerTransaction::createRdOnlyVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
569 checkNotAlreadyExistingVar(varName);
570 PickelizedPyObjRdOnlyServer *tmp(new PickelizedPyObjRdOnlyServer(this,varName,constValue));
571 CORBA::Object_var ret(tmp->activate());
572 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
576 void DataScopeServerTransaction::createRdExtVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
578 checkNotAlreadyExistingVar(varName);
579 PickelizedPyObjRdExtServer *tmp(new PickelizedPyObjRdExtServer(this,varName,constValue));
580 CORBA::Object_var ret(tmp->activate());
581 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
585 void DataScopeServerTransaction::createRdExtVarFreeStyleInternal(const std::string& varName, const SALOME::ByteVec& constValue, std::string&& compareFuncContent, SALOME::AutoPyRef&& compareFunc)
587 if(!isExistingVar(varName))
589 PickelizedPyObjRdExtFreeStyleServer *tmp(new PickelizedPyObjRdExtFreeStyleServer(this,varName,constValue,std::move(compareFuncContent),std::move(compareFunc)));
590 CORBA::Object_var ret(tmp->activate());
591 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
596 BasicDataServer *ds(retrieveVarInternal2(varName));
599 std::ostringstream oss;
600 oss << "DataScopeServerTransaction::createRdExtVarFreeStyleInternal : internal error 1 for varname \"" << varName << "\"!";
601 throw Exception(oss.str());
603 Sha1Keeper *ds2(dynamic_cast<Sha1Keeper *>(ds));
606 std::ostringstream oss;
607 oss << "DataScopeServerTransaction::createRdExtVarFreeStyleInternal : varname \"" << varName << "\" already exists with a non Sha1Keeper type !";
608 throw Exception(oss.str());
610 PickelizedPyObjServer *ds3(dynamic_cast<PickelizedPyObjServer *>(ds));
613 std::ostringstream oss;
614 oss << "DataScopeServerTransaction::createRdExtVarFreeStyleInternal : varname \"" << varName << "\" already exists with a non PickelizedPyObjServer type !";
615 throw Exception(oss.str());
617 std::vector<unsigned char> constValueAsCpp;
618 Transaction::FromByteSeqToVB(constValue,constValueAsCpp);
619 SALOME::AutoPyRef newObj(PickelizedPyObjServer::GetPyObjFromPickled(constValueAsCpp,this));
622 std::ostringstream oss;
623 oss << "DataScopeServerTransaction::createRdExtVarFreeStyleInternal : varname \"" << varName << "\" already exists but input pickelized object is not loadable !";
624 throw Exception(oss.str());
626 ds2->checkSame(varName,compareFuncContent,ds3->getPyObj(),newObj);
630 void DataScopeServerTransaction::createRdExtInitVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
632 checkNotAlreadyExistingVar(varName);
633 PickelizedPyObjRdExtInitServer *tmp(new PickelizedPyObjRdExtInitServer(this,varName,constValue));
634 CORBA::Object_var ret(tmp->activate());
635 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
639 void DataScopeServerTransaction::createRdWrVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
641 checkNotAlreadyExistingVar(varName);
642 PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,varName,constValue));
643 CORBA::Object_var ret(tmp->activate());
644 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
648 SALOME::Transaction_ptr DataScopeServerTransaction::createRdOnlyVarTransac(const char *varName, const SALOME::ByteVec& constValue)
650 checkNotAlreadyExistingVar(varName);
651 TransactionRdOnlyVarCreate *ret(new TransactionRdOnlyVarCreate(this,varName,constValue));
652 CORBA::Object_var obj(ret->activate());
653 return SALOME::Transaction::_narrow(obj);
656 SALOME::Transaction_ptr DataScopeServerTransaction::createRdExtVarTransac(const char *varName, const SALOME::ByteVec& constValue)
658 checkNotAlreadyExistingVar(varName);
659 TransactionRdExtVarCreate *ret(new TransactionRdExtVarCreate(this,varName,constValue));
660 CORBA::Object_var obj(ret->activate());
661 return SALOME::Transaction::_narrow(obj);
664 SALOME::Transaction_ptr DataScopeServerTransaction::createRdExtVarFreeStyleTransac(const char *varName, const SALOME::ByteVec& constValue, const char *compareFuncContent)
665 {// no check on varName done here. Will be done on perform
666 TransactionRdExtVarFreeStyleCreate *ret(new TransactionRdExtVarFreeStyleCreate(this,varName,constValue,compareFuncContent));
667 CORBA::Object_var obj(ret->activate());
668 return SALOME::Transaction::_narrow(obj);
671 SALOME::Transaction_ptr DataScopeServerTransaction::createRdExtInitVarTransac(const char *varName, const SALOME::ByteVec& constValue)
673 checkNotAlreadyExistingVar(varName);
674 TransactionRdExtInitVarCreate *ret(new TransactionRdExtInitVarCreate(this,varName,constValue));
675 CORBA::Object_var obj(ret->activate());
676 return SALOME::Transaction::_narrow(obj);
679 SALOME::Transaction_ptr DataScopeServerTransaction::createRdWrVarTransac(const char *varName, const SALOME::ByteVec& constValue)
681 checkNotAlreadyExistingVar(varName);
682 TransactionRdWrVarCreate *ret(new TransactionRdWrVarCreate(this,varName,constValue));
683 CORBA::Object_var obj(ret->activate());
684 return SALOME::Transaction::_narrow(obj);
687 void DataScopeServerTransaction::addWaitKey(KeyWaiter *kw)
690 throw Exception("DataScopeServerTransaction::addWaitKey : NULL input object !");
691 _waiting_keys.push_back(kw);
694 void DataScopeServerTransaction::pingKey(PyObject *keyObj)
697 // this part does nothing except to be sure that in notify key all will be OK.
698 PyObject *args(PyTuple_New(1));
699 PyTuple_SetItem(args,0,keyObj); Py_XINCREF(keyObj);
700 for(std::list< KeyWaiter *>::iterator it=_waiting_keys.begin();it!=_waiting_keys.end();it++,ii++)
702 PyObject *waitKey((*it)->getKeyPyObj());
703 PyObject *meth(PyObject_GetAttrString(waitKey,"__eq__"));
706 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " no __eq__ in pyobj !";
707 throw Exception(oss.str());
709 PyObject *res(PyObject_CallObject(meth,args));
713 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " error during cmp(k,wk[i]) !";
714 throw Exception(oss.str());
719 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " error during interpretation of cmp(k,wk[i]) !";
720 throw Exception(oss.str());
727 void DataScopeServerTransaction::notifyKey(const std::string& varName, PyObject *keyObj, PyObject *valueObj)
730 PyObject *args(PyTuple_New(1));
731 PyTuple_SetItem(args,0,keyObj); Py_XINCREF(keyObj);
732 std::list< KeyWaiter *> newList,listOfEltToWakeUp;
733 for(std::list< KeyWaiter *>::iterator it=_waiting_keys.begin();it!=_waiting_keys.end();it++,ii++)
735 if((*it)->getVarName()!=varName)
737 newList.push_back(*it);
740 PyObject *waitKey((*it)->getKeyPyObj());
741 PyObject *meth(PyObject_GetAttrString(waitKey,"__eq__"));
744 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " no __eq__ in pyobj !";
745 throw Exception(oss.str());
747 PyObject *res(PyObject_CallObject(meth,args));
749 if(!PyBool_Check(res))
751 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " no __eq__ in pyobj !";
752 throw Exception(oss.str());
756 std::ostringstream oss; oss << "DataScopeServerTransaction::notifyKey : MAIN INTERNAL ERROR ! for object id #" << ii << " error during interpretation of cmp(k,wk[i]) !";
757 throw Exception(oss.str());
760 listOfEltToWakeUp.push_back(*it);
762 newList.push_back(*it);
766 for(std::list< KeyWaiter *>::iterator it=listOfEltToWakeUp.begin();it!=listOfEltToWakeUp.end();it++)
767 (*it)->valueJustCome(valueObj);
768 for(std::list< KeyWaiter *>::iterator it=listOfEltToWakeUp.begin();it!=listOfEltToWakeUp.end();it++)
770 _waiting_keys=newList;
773 SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarHard(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value)
775 checkVarExistingAndDict(varName);
776 TransactionAddKeyValueHard *ret(new TransactionAddKeyValueHard(this,varName,key,value));
777 CORBA::Object_var obj(ret->activate());
778 return SALOME::Transaction::_narrow(obj);
781 SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarErrorIfAlreadyExisting(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value)
783 checkVarExistingAndDict(varName);
784 TransactionAddKeyValueErrorIfAlreadyExisting *ret(new TransactionAddKeyValueErrorIfAlreadyExisting(this,varName,key,value));
785 CORBA::Object_var obj(ret->activate());
786 return SALOME::Transaction::_narrow(obj);
789 SALOME::TransactionMultiKeyAddSession_ptr DataScopeServerTransaction::addMultiKeyValueSession(const char *varName)
791 checkVarExistingAndDict(varName);
792 TransactionMultiKeyAddSession *ret(new TransactionMultiKeyAddSession(this,varName));
793 CORBA::Object_var obj(ret->activate());
794 return SALOME::TransactionMultiKeyAddSession::_narrow(obj);
797 SALOME::Transaction_ptr DataScopeServerTransaction::removeKeyInVarErrorIfNotAlreadyExisting(const char *varName, const SALOME::ByteVec& key)
799 checkVarExistingAndDict(varName);
800 TransactionRemoveKeyInVarErrorIfNotAlreadyExisting *ret(new TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(this,varName,key));
801 CORBA::Object_var obj(ret->activate());
802 return SALOME::Transaction::_narrow(obj);
805 SALOME::TransactionRdWrAccess_ptr DataScopeServerTransaction::createWorkingVarTransac(const char *varName, const SALOME::ByteVec& constValue)
807 std::string varNameCpp(varName);
808 checkNotAlreadyExistingVar(varName);
809 PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,varNameCpp,constValue));
810 CORBA::Object_var obj(tmp->activate());
811 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(obj),tmp);
814 TransactionMorphRdWrIntoRdOnly *ret(new TransactionMorphRdWrIntoRdOnly(this,varName));
815 CORBA::Object_var obj2(ret->activate());
816 return SALOME::TransactionRdWrAccess::_narrow(obj2);
819 SALOME::Transaction_ptr DataScopeServerTransaction::killVarTransac(const char *varName)
821 std::string varNameCpp(varName);
822 checkExistingVar(varNameCpp);
824 TransactionKillVar *ret(new TransactionKillVar(this,varName));
825 CORBA::Object_var obj2(ret->activate());
826 return SALOME::Transaction::_narrow(obj2);
829 SALOME::KeyWaiter_ptr DataScopeServerTransaction::waitForKeyInVar(const char *varName, const SALOME::ByteVec& keyVal)
831 PickelizedPyObjServer *pickelObj(checkVarExistingAndDict(varName));
832 KeyWaiter *ret(new KeyWaiter(pickelObj,keyVal));
833 CORBA::Object_var obj(ret->activate());//KeyWaiter instance activated inside a multithread POA contrary to all of objects in SALOMESDS in single thread !
834 return SALOME::KeyWaiter::_narrow(obj);
837 SALOME::KeyWaiter_ptr DataScopeServerTransaction::waitForKeyInVarAndKillIt(const char *varName, const SALOME::ByteVec& keyVal, SALOME::Transaction_out transac)
839 PickelizedPyObjServer *pickelObj(checkVarExistingAndDict(varName));
840 KeyWaiter *ret0(new KeyWaiter(pickelObj,keyVal));
841 CORBA::Object_var obj(ret0->activate());//KeyWaiter instance activated inside a multithread POA contrary to all of objects in SALOMESDS in single thread !
843 TransactionRemoveKeyInVarErrorIfNotAlreadyExisting *ret1(new TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(this,varName,keyVal));
844 CORBA::Object_var obj2(ret1->activate());
845 transac=SALOME::Transaction::_narrow(obj2);
847 return SALOME::KeyWaiter::_narrow(obj);
850 SALOME::ByteVec *DataScopeServerTransaction::waitForMonoThrRev(SALOME::KeyWaiter_ptr kw)
852 PortableServer::ServantBase *ret(0);
855 ret=_poa_for_key_waiter->reference_to_servant(kw);// Warning ref cnt of ret has been incremented !
857 catch(...) { ret=0; }
858 KeyWaiter *retc(dynamic_cast<KeyWaiter *>(ret));
860 throw Exception("DataScopeServerTransaction::invokeMonoThr : internal error 1 !");
861 retc->_remove_ref();// restore the counter after _poa_for_key_waiter->reference_to_servant(kw)
862 SALOME::ByteVec *zeRet(retc->waitForMonoThr());
863 retc->enforcedRelease();
867 SALOME::ByteVec *DataScopeServerTransaction::waitForAndKill(SALOME::KeyWaiter_ptr kw)
869 PortableServer::ServantBase *ret(0);
872 ret=_poa_for_key_waiter->reference_to_servant(kw);// Warning ref cnt of ret has been incremented !
874 catch(...) { ret=0; }
875 KeyWaiter *retc(dynamic_cast<KeyWaiter *>(ret));
877 throw Exception("DataScopeServerTransaction::invokeMonoThr : internal error 1 !");
878 retc->_remove_ref();// restore the counter after _poa_for_key_waiter->reference_to_servant(kw)
879 SALOME::ByteVec *zeRet(retc->waitForAndKill());
880 retc->enforcedRelease();
884 void DataScopeServerTransaction::atomicApply(const SALOME::ListOfTransaction& transactions)
886 std::size_t sz(transactions.length());
889 std::vector< AutoServantPtr<Transaction> > transactionsCpp(sz);
890 for(std::size_t i=0;i<sz;i++)
892 PortableServer::ServantBase *eltBase(0);
896 eltBase=_poa->reference_to_servant(transactions[i]);
897 elt=dynamic_cast<Transaction *>(eltBase);
901 std::ostringstream oss; oss << "DataScopeServerTransaction::atomicApply : the elt #" << i << " is invalid !";
902 throw Exception(oss.str());
906 std::ostringstream oss; oss << "DataScopeServerTransaction::atomicApply : the elt #" << i << " is null ?";
907 throw Exception(oss.str());
910 transactionsCpp[i]=elt;
912 {// important do not merge loops !
913 std::vector<TrustTransaction> transactions2(sz);
914 bool mustRollback(true);
915 for(std::size_t i=0;i<sz;i++)
916 transactions2[i].setTransaction(transactionsCpp[i],&mustRollback);
917 for(std::size_t i=0;i<sz;i++)
918 transactions2[i].operate();
921 for(std::size_t i=0;i<sz;i++)
922 transactionsCpp[i]->notify();
925 DataScopeServerTransaction::~DataScopeServerTransaction()
929 SALOME::RequestSwitcher_ptr DataScopeServerTransaction::getRequestSwitcher()
933 _rs=new RequestSwitcher(_orb,this);
935 CORBA::Object_var obj(_rs->activate());
936 return SALOME::RequestSwitcher::_narrow(obj);