1 // Copyright (C) 2007-2019 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 void DataScopeServerBase::takeANap(CORBA::Double napDurationInSec)
247 if(napDurationInSec<0.)
248 throw Exception("DataScopeServerBase::takeANap : negative value !");
250 struct timespec req,rem;
251 long nbSec((long)napDurationInSec);
252 double remainTime(napDurationInSec-(double)nbSec);
254 req.tv_nsec=(long)(remainTime*1000000.);
255 int ret(nanosleep(&req,&rem));
257 throw Exception("DataScopeServerBase::takeANap : nap not finished as expected !");
259 throw Exception("DataScopeServerBase::takeANap : not implemented for Windows !");
263 void DataScopeServerBase::registerToSalomePiDict() const
265 _pyHelper->registerToSalomePiDict("SALOME_DataScopeServerBase",getpid());
268 void DataScopeServerBase::setPOA(PortableServer::POA_var poa)
273 void DataScopeServerBase::registerInNS(SALOME::DataScopeServerBase_ptr ptr)
275 std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(_name));
276 SALOME_NamingService ns(_orb);
277 ns.Register(ptr,fullScopeName.c_str());
280 std::string DataScopeServerBase::BuildTmpVarNameFrom(const std::string& varName)
282 std::ostringstream oss;
283 oss << varName << "@" << COUNTER++;
287 std::vector< std::string > DataScopeServerBase::getAllVarNames() const
289 std::size_t sz(_vars.size());
290 std::vector<std::string> ret(sz);
291 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it(_vars.begin());
292 for(std::size_t i=0;i<sz;it++,i++)
293 ret[i]=(*it).second->getVarNameCpp();
297 bool DataScopeServerBase::isExistingVar(const std::string& varName) const
299 std::vector<std::string> allNames(getAllVarNames());
300 std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
301 return it!=allNames.end();
304 void DataScopeServerBase::checkNotAlreadyExistingVar(const std::string& varName) const
306 if(isExistingVar(varName))
308 std::ostringstream oss; oss << "DataScopeServerBase::checkNotAlreadyExistingVar : name \"" << varName << "\" already exists !";
309 throw Exception(oss.str());
313 void DataScopeServerBase::checkExistingVar(const std::string& varName) const
315 std::vector<std::string> allNames(getAllVarNames());
316 std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
317 if(it==allNames.end())
319 std::ostringstream oss; oss << "DataScopeServerBase::checkExistingVar : name \"" << varName << "\" does not exist !";
320 throw Exception(oss.str());
324 PickelizedPyObjServer *DataScopeServerBase::checkVarExistingAndDict(const std::string& varName)
326 checkExistingVar(varName);
327 BasicDataServer *var(retrieveVarInternal2(varName.c_str()));
328 PickelizedPyObjServer *ret(dynamic_cast<PickelizedPyObjServer *>(var));
331 std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << varName << "\"exists but it is not serialized !";
332 throw Exception(oss.str());
336 std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << varName << "\"exists but it is not a Dict !";
337 throw Exception(oss.str());
342 void DataScopeServerBase::moveStatusOfVarFromRdWrToRdOnly(const std::string& varName)
344 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
345 std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
346 PickelizedPyObjRdWrServer *varc(dynamic_cast<PickelizedPyObjRdWrServer *>(p.second));
348 throw Exception("DataScopeServerBase::moveStatusOfVarFromRdWrToRdOnly : var is not a RdWr !");
349 PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
350 PickelizedPyObjRdOnlyServer *newVar(new PickelizedPyObjRdOnlyServer(this,varName,pyobj));
351 CORBA::Object_var obj(newVar->activate());
352 SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
353 p.first=obj2; p.second=newVar;
357 void DataScopeServerBase::moveStatusOfVarFromRdOnlyToRdWr(const std::string& varName)
359 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
360 std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
361 PickelizedPyObjRdOnlyServer *varc(dynamic_cast<PickelizedPyObjRdOnlyServer *>(p.second));
363 throw Exception("DataScopeServerBase::moveStatusOfVarFromRdOnlyToRdWr : var is not a RdWr !");
364 PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
365 PickelizedPyObjRdWrServer *newVar(new PickelizedPyObjRdWrServer(this,varName,pyobj));
366 CORBA::Object_var obj(newVar->activate());
367 SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
368 p.first=obj2; p.second=newVar;
372 void DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExtInit(const std::string& varName)
374 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
375 std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
376 PickelizedPyObjRdExtServer *varc0(dynamic_cast<PickelizedPyObjRdExtServer *>(p.second));
377 PickelizedPyObjRdExtInitServer *varc1(dynamic_cast<PickelizedPyObjRdExtInitServer *>(p.second));
379 throw Exception("DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExtInit : var is neither RdExt nor RdExtInit !");
382 PickelizedPyObjRdExtInitServer *newVar(varc0->buildInitInstanceFrom(varName));
383 newVar->incrNbClients();
384 CORBA::Object_var obj(newVar->activate());
385 SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
386 p.first=obj2; p.second=newVar;
390 varc1->incrNbClients();
393 void DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExt(const std::string& varName)
395 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
396 std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
397 PickelizedPyObjRdExtInitServer *varc0(dynamic_cast<PickelizedPyObjRdExtInitServer *>(p.second));
398 PickelizedPyObjRdExtServer *varc1(dynamic_cast<PickelizedPyObjRdExtServer *>(p.second));
400 throw Exception("DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExt : var is not a RdExtInit !");
403 if(varc0->decrNbClients())
405 PickelizedPyObjRdExtServer *newVar(varc0->buildStdInstanceFrom(varName));
406 CORBA::Object_var obj(newVar->activate());
407 SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
408 p.first=obj2; p.second=newVar;
414 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator DataScopeServerBase::retrieveVarInternal3(const std::string& varName) const
416 std::vector<std::string> allNames(getAllVarNames());
417 std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
418 if(it==allNames.end())
420 std::ostringstream oss; oss << "DataScopeServerBase::retrieveVarInternal3 : name \"" << varName << "\" does not exists ! Possibilities are :";
421 std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
422 throw Exception(oss.str());
424 std::size_t pos(std::distance(allNames.begin(),it));
425 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(_vars.begin());
426 for(std::size_t i=0;i<pos;i++,it0++);
430 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator DataScopeServerBase::retrieveVarInternal4(const std::string& varName)
432 std::vector<std::string> allNames(getAllVarNames());
433 std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
434 if(it==allNames.end())
436 std::ostringstream oss; oss << "DataScopeServerBase::retrieveVarInternal4 : name \"" << varName << "\" does not exists ! Possibilities are :";
437 std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
438 throw Exception(oss.str());
440 std::size_t pos(std::distance(allNames.begin(),it));
441 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(_vars.begin());
442 for(std::size_t i=0;i<pos;i++,it0++);
448 DataScopeServer::DataScopeServer(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):DataScopeServerBase(pyHelper,orb,killer,scopeName)
452 DataScopeServer::DataScopeServer(const DataScopeServer& other):omniServant(other),ServantBase(other),DataScopeServerBase(other)
456 SALOME::PickelizedPyObjRdOnlyServer_ptr DataScopeServer::createRdOnlyVar(const char *varName, const SALOME::ByteVec& constValue)
458 std::string varNameCpp(varName);
459 checkNotAlreadyExistingVar(varNameCpp);
460 PickelizedPyObjRdOnlyServer *tmp(new PickelizedPyObjRdOnlyServer(this,varNameCpp,constValue));
461 CORBA::Object_var ret(tmp->activate());
462 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
464 return SALOME::PickelizedPyObjRdOnlyServer::_narrow(ret);
467 SALOME::PickelizedPyObjRdExtServer_ptr DataScopeServer::createRdExtVar(const char *varName, const SALOME::ByteVec& constValue)
469 std::string varNameCpp(varName);
470 checkNotAlreadyExistingVar(varNameCpp);
471 PickelizedPyObjRdExtServer *tmp(new PickelizedPyObjRdExtServer(this,varNameCpp,constValue));
472 CORBA::Object_var ret(tmp->activate());
473 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
475 return SALOME::PickelizedPyObjRdExtServer::_narrow(ret);
478 SALOME::PickelizedPyObjRdWrServer_ptr DataScopeServer::createRdWrVar(const char *typeName, const char *varName)
480 std::string varNameCpp(varName),typeNameCpp(typeName);
481 checkNotAlreadyExistingVar(varNameCpp);
482 PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,typeNameCpp,varNameCpp));
483 CORBA::Object_var ret(tmp->activate());
484 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
486 return SALOME::PickelizedPyObjRdWrServer::_narrow(ret);
489 DataScopeServer::~DataScopeServer()
495 DataScopeServerTransaction::DataScopeServerTransaction(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):DataScopeServerBase(pyHelper,orb,killer,scopeName)
497 CORBA::Object_var obj(_orb->resolve_initial_references("RootPOA"));
498 PortableServer::POA_var poa(PortableServer::POA::_narrow(obj));
500 PortableServer::POAManager_var mgr(poa->the_POAManager());
501 CORBA::PolicyList policies;
503 PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
504 policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol);
505 PortableServer::POA_var poa2(poa->create_POA("POAForWaiters",mgr,policies));
506 threadPol->destroy();
508 _poa_for_key_waiter=poa2;
511 DataScopeServerTransaction::DataScopeServerTransaction(const DataScopeServerTransaction& other):omniServant(other),ServantBase(other),DataScopeServerBase(other),_poa_for_key_waiter(other.getPOA4KeyWaiter())
515 char *DataScopeServerTransaction::getAccessOfVar(const char *varName)
517 std::string varNameCpp(varName);
518 checkExistingVar(varNameCpp);
519 BasicDataServer *var(retrieveVarInternal2(varName));
521 throw Exception("DataScopeServerTransaction::getAccessOfVar : variable is NULL !");
522 PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
524 throw Exception("DataScopeServerTransaction::getAccessOfVar : variable is not of type PickelizedPyObjServer !");
525 std::string ret(varc->getAccessStr());
526 return CORBA::string_dup(ret.c_str());
530 * This method is here to retrieve atomically accessStr and picklization.
532 void DataScopeServerTransaction::fetchAndGetAccessOfVar(const char *varName, CORBA::String_out access, SALOME::ByteVec_out data)
534 access=getAccessOfVar(varName);
535 data=fetchSerializedContent(varName);
538 void DataScopeServerTransaction::createRdOnlyVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
540 checkNotAlreadyExistingVar(varName);
541 PickelizedPyObjRdOnlyServer *tmp(new PickelizedPyObjRdOnlyServer(this,varName,constValue));
542 CORBA::Object_var ret(tmp->activate());
543 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
547 void DataScopeServerTransaction::createRdExtVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
549 checkNotAlreadyExistingVar(varName);
550 PickelizedPyObjRdExtServer *tmp(new PickelizedPyObjRdExtServer(this,varName,constValue));
551 CORBA::Object_var ret(tmp->activate());
552 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
556 void DataScopeServerTransaction::createRdExtVarFreeStyleInternal(const std::string& varName, const SALOME::ByteVec& constValue, std::string&& compareFuncContent, SALOME::AutoPyRef&& compareFunc)
558 if(!isExistingVar(varName))
560 PickelizedPyObjRdExtFreeStyleServer *tmp(new PickelizedPyObjRdExtFreeStyleServer(this,varName,constValue,std::move(compareFuncContent),std::move(compareFunc)));
561 CORBA::Object_var ret(tmp->activate());
562 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
567 BasicDataServer *ds(retrieveVarInternal2(varName));
570 std::ostringstream oss;
571 oss << "DataScopeServerTransaction::createRdExtVarFreeStyleInternal : internal error 1 for varname \"" << varName << "\"!";
572 throw Exception(oss.str());
574 Sha1Keeper *ds2(dynamic_cast<Sha1Keeper *>(ds));
577 std::ostringstream oss;
578 oss << "DataScopeServerTransaction::createRdExtVarFreeStyleInternal : varname \"" << varName << "\" already exists with a non Sha1Keeper type !";
579 throw Exception(oss.str());
581 PickelizedPyObjServer *ds3(dynamic_cast<PickelizedPyObjServer *>(ds));
584 std::ostringstream oss;
585 oss << "DataScopeServerTransaction::createRdExtVarFreeStyleInternal : varname \"" << varName << "\" already exists with a non PickelizedPyObjServer type !";
586 throw Exception(oss.str());
588 std::vector<unsigned char> constValueAsCpp;
589 Transaction::FromByteSeqToVB(constValue,constValueAsCpp);
590 SALOME::AutoPyRef newObj(PickelizedPyObjServer::GetPyObjFromPickled(constValueAsCpp,this));
593 std::ostringstream oss;
594 oss << "DataScopeServerTransaction::createRdExtVarFreeStyleInternal : varname \"" << varName << "\" already exists but input pickelized object is not loadable !";
595 throw Exception(oss.str());
597 ds2->checkSame(varName,compareFuncContent,ds3->getPyObj(),newObj);
601 void DataScopeServerTransaction::createRdExtInitVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
603 checkNotAlreadyExistingVar(varName);
604 PickelizedPyObjRdExtInitServer *tmp(new PickelizedPyObjRdExtInitServer(this,varName,constValue));
605 CORBA::Object_var ret(tmp->activate());
606 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
610 void DataScopeServerTransaction::createRdWrVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
612 checkNotAlreadyExistingVar(varName);
613 PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,varName,constValue));
614 CORBA::Object_var ret(tmp->activate());
615 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
619 SALOME::Transaction_ptr DataScopeServerTransaction::createRdOnlyVarTransac(const char *varName, const SALOME::ByteVec& constValue)
621 checkNotAlreadyExistingVar(varName);
622 TransactionRdOnlyVarCreate *ret(new TransactionRdOnlyVarCreate(this,varName,constValue));
623 CORBA::Object_var obj(ret->activate());
624 return SALOME::Transaction::_narrow(obj);
627 SALOME::Transaction_ptr DataScopeServerTransaction::createRdExtVarTransac(const char *varName, const SALOME::ByteVec& constValue)
629 checkNotAlreadyExistingVar(varName);
630 TransactionRdExtVarCreate *ret(new TransactionRdExtVarCreate(this,varName,constValue));
631 CORBA::Object_var obj(ret->activate());
632 return SALOME::Transaction::_narrow(obj);
635 SALOME::Transaction_ptr DataScopeServerTransaction::createRdExtVarFreeStyleTransac(const char *varName, const SALOME::ByteVec& constValue, const char *compareFuncContent)
636 {// no check on varName done here. Will be done on perform
637 TransactionRdExtVarFreeStyleCreate *ret(new TransactionRdExtVarFreeStyleCreate(this,varName,constValue,compareFuncContent));
638 CORBA::Object_var obj(ret->activate());
639 return SALOME::Transaction::_narrow(obj);
642 SALOME::Transaction_ptr DataScopeServerTransaction::createRdExtInitVarTransac(const char *varName, const SALOME::ByteVec& constValue)
644 checkNotAlreadyExistingVar(varName);
645 TransactionRdExtInitVarCreate *ret(new TransactionRdExtInitVarCreate(this,varName,constValue));
646 CORBA::Object_var obj(ret->activate());
647 return SALOME::Transaction::_narrow(obj);
650 SALOME::Transaction_ptr DataScopeServerTransaction::createRdWrVarTransac(const char *varName, const SALOME::ByteVec& constValue)
652 checkNotAlreadyExistingVar(varName);
653 TransactionRdWrVarCreate *ret(new TransactionRdWrVarCreate(this,varName,constValue));
654 CORBA::Object_var obj(ret->activate());
655 return SALOME::Transaction::_narrow(obj);
658 void DataScopeServerTransaction::addWaitKey(KeyWaiter *kw)
661 throw Exception("DataScopeServerTransaction::addWaitKey : NULL input object !");
662 _waiting_keys.push_back(kw);
665 void DataScopeServerTransaction::pingKey(PyObject *keyObj)
668 // this part does nothing except to be sure that in notify key all will be OK.
669 PyObject *args(PyTuple_New(1));
670 PyTuple_SetItem(args,0,keyObj); Py_XINCREF(keyObj);
671 for(std::list< KeyWaiter *>::iterator it=_waiting_keys.begin();it!=_waiting_keys.end();it++,ii++)
673 PyObject *waitKey((*it)->getKeyPyObj());
674 PyObject *meth(PyObject_GetAttrString(waitKey,"__eq__"));
677 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " no __eq__ in pyobj !";
678 throw Exception(oss.str());
680 PyObject *res(PyObject_CallObject(meth,args));
684 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " error during cmp(k,wk[i]) !";
685 throw Exception(oss.str());
690 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " error during interpretation of cmp(k,wk[i]) !";
691 throw Exception(oss.str());
698 void DataScopeServerTransaction::notifyKey(const std::string& varName, PyObject *keyObj, PyObject *valueObj)
701 PyObject *args(PyTuple_New(1));
702 PyTuple_SetItem(args,0,keyObj); Py_XINCREF(keyObj);
703 std::list< KeyWaiter *> newList,listOfEltToWakeUp;
704 for(std::list< KeyWaiter *>::iterator it=_waiting_keys.begin();it!=_waiting_keys.end();it++,ii++)
706 if((*it)->getVarName()!=varName)
708 newList.push_back(*it);
711 PyObject *waitKey((*it)->getKeyPyObj());
712 PyObject *meth(PyObject_GetAttrString(waitKey,"__eq__"));
715 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " no __eq__ in pyobj !";
716 throw Exception(oss.str());
718 PyObject *res(PyObject_CallObject(meth,args));
720 if(!PyBool_Check(res))
722 std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " no __eq__ in pyobj !";
723 throw Exception(oss.str());
727 std::ostringstream oss; oss << "DataScopeServerTransaction::notifyKey : MAIN INTERNAL ERROR ! for object id #" << ii << " error during interpretation of cmp(k,wk[i]) !";
728 throw Exception(oss.str());
731 listOfEltToWakeUp.push_back(*it);
733 newList.push_back(*it);
737 for(std::list< KeyWaiter *>::iterator it=listOfEltToWakeUp.begin();it!=listOfEltToWakeUp.end();it++)
738 (*it)->valueJustCome(valueObj);
739 for(std::list< KeyWaiter *>::iterator it=listOfEltToWakeUp.begin();it!=listOfEltToWakeUp.end();it++)
741 _waiting_keys=newList;
744 SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarHard(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value)
746 checkVarExistingAndDict(varName);
747 TransactionAddKeyValueHard *ret(new TransactionAddKeyValueHard(this,varName,key,value));
748 CORBA::Object_var obj(ret->activate());
749 return SALOME::Transaction::_narrow(obj);
752 SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarErrorIfAlreadyExisting(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value)
754 checkVarExistingAndDict(varName);
755 TransactionAddKeyValueErrorIfAlreadyExisting *ret(new TransactionAddKeyValueErrorIfAlreadyExisting(this,varName,key,value));
756 CORBA::Object_var obj(ret->activate());
757 return SALOME::Transaction::_narrow(obj);
760 SALOME::TransactionMultiKeyAddSession_ptr DataScopeServerTransaction::addMultiKeyValueSession(const char *varName)
762 checkVarExistingAndDict(varName);
763 TransactionMultiKeyAddSession *ret(new TransactionMultiKeyAddSession(this,varName));
764 CORBA::Object_var obj(ret->activate());
765 return SALOME::TransactionMultiKeyAddSession::_narrow(obj);
768 SALOME::Transaction_ptr DataScopeServerTransaction::removeKeyInVarErrorIfNotAlreadyExisting(const char *varName, const SALOME::ByteVec& key)
770 checkVarExistingAndDict(varName);
771 TransactionRemoveKeyInVarErrorIfNotAlreadyExisting *ret(new TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(this,varName,key));
772 CORBA::Object_var obj(ret->activate());
773 return SALOME::Transaction::_narrow(obj);
776 SALOME::TransactionRdWrAccess_ptr DataScopeServerTransaction::createWorkingVarTransac(const char *varName, const SALOME::ByteVec& constValue)
778 std::string varNameCpp(varName);
779 checkNotAlreadyExistingVar(varName);
780 PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,varNameCpp,constValue));
781 CORBA::Object_var obj(tmp->activate());
782 std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(obj),tmp);
785 TransactionMorphRdWrIntoRdOnly *ret(new TransactionMorphRdWrIntoRdOnly(this,varName));
786 CORBA::Object_var obj2(ret->activate());
787 return SALOME::TransactionRdWrAccess::_narrow(obj2);
790 SALOME::Transaction_ptr DataScopeServerTransaction::killVarTransac(const char *varName)
792 std::string varNameCpp(varName);
793 checkExistingVar(varNameCpp);
795 TransactionKillVar *ret(new TransactionKillVar(this,varName));
796 CORBA::Object_var obj2(ret->activate());
797 return SALOME::Transaction::_narrow(obj2);
800 SALOME::KeyWaiter_ptr DataScopeServerTransaction::waitForKeyInVar(const char *varName, const SALOME::ByteVec& keyVal)
802 PickelizedPyObjServer *pickelObj(checkVarExistingAndDict(varName));
803 KeyWaiter *ret(new KeyWaiter(pickelObj,keyVal));
804 CORBA::Object_var obj(ret->activate());//KeyWaiter instance activated inside a multithread POA contrary to all of objects in SALOMESDS in single thread !
805 return SALOME::KeyWaiter::_narrow(obj);
808 SALOME::KeyWaiter_ptr DataScopeServerTransaction::waitForKeyInVarAndKillIt(const char *varName, const SALOME::ByteVec& keyVal, SALOME::Transaction_out transac)
810 PickelizedPyObjServer *pickelObj(checkVarExistingAndDict(varName));
811 KeyWaiter *ret0(new KeyWaiter(pickelObj,keyVal));
812 CORBA::Object_var obj(ret0->activate());//KeyWaiter instance activated inside a multithread POA contrary to all of objects in SALOMESDS in single thread !
814 TransactionRemoveKeyInVarErrorIfNotAlreadyExisting *ret1(new TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(this,varName,keyVal));
815 CORBA::Object_var obj2(ret1->activate());
816 transac=SALOME::Transaction::_narrow(obj2);
818 return SALOME::KeyWaiter::_narrow(obj);
821 SALOME::ByteVec *DataScopeServerTransaction::waitForMonoThrRev(SALOME::KeyWaiter_ptr kw)
823 PortableServer::ServantBase *ret(0);
826 ret=_poa_for_key_waiter->reference_to_servant(kw);// Warning ref cnt of ret has been incremented !
828 catch(...) { ret=0; }
829 KeyWaiter *retc(dynamic_cast<KeyWaiter *>(ret));
831 throw Exception("DataScopeServerTransaction::invokeMonoThr : internal error 1 !");
832 retc->_remove_ref();// restore the counter after _poa_for_key_waiter->reference_to_servant(kw)
833 SALOME::ByteVec *zeRet(retc->waitForMonoThr());
834 retc->enforcedRelease();
838 SALOME::ByteVec *DataScopeServerTransaction::waitForAndKill(SALOME::KeyWaiter_ptr kw)
840 PortableServer::ServantBase *ret(0);
843 ret=_poa_for_key_waiter->reference_to_servant(kw);// Warning ref cnt of ret has been incremented !
845 catch(...) { ret=0; }
846 KeyWaiter *retc(dynamic_cast<KeyWaiter *>(ret));
848 throw Exception("DataScopeServerTransaction::invokeMonoThr : internal error 1 !");
849 retc->_remove_ref();// restore the counter after _poa_for_key_waiter->reference_to_servant(kw)
850 SALOME::ByteVec *zeRet(retc->waitForAndKill());
851 retc->enforcedRelease();
855 void DataScopeServerTransaction::atomicApply(const SALOME::ListOfTransaction& transactions)
857 std::size_t sz(transactions.length());
860 std::vector< AutoServantPtr<Transaction> > transactionsCpp(sz);
861 for(std::size_t i=0;i<sz;i++)
863 PortableServer::ServantBase *eltBase(0);
867 eltBase=_poa->reference_to_servant(transactions[i]);
868 elt=dynamic_cast<Transaction *>(eltBase);
872 std::ostringstream oss; oss << "DataScopeServerTransaction::atomicApply : the elt #" << i << " is invalid !";
873 throw Exception(oss.str());
877 std::ostringstream oss; oss << "DataScopeServerTransaction::atomicApply : the elt #" << i << " is null ?";
878 throw Exception(oss.str());
881 transactionsCpp[i]=elt;
883 {// important do not merge loops !
884 std::vector<TrustTransaction> transactions2(sz);
885 bool mustRollback(true);
886 for(std::size_t i=0;i<sz;i++)
887 transactions2[i].setTransaction(transactionsCpp[i],&mustRollback);
888 for(std::size_t i=0;i<sz;i++)
889 transactions2[i].operate();
892 for(std::size_t i=0;i<sz;i++)
893 transactionsCpp[i]->notify();
896 DataScopeServerTransaction::~DataScopeServerTransaction()
900 SALOME::RequestSwitcher_ptr DataScopeServerTransaction::getRequestSwitcher()
904 _rs=new RequestSwitcher(_orb,this);
906 CORBA::Object_var obj(_rs->activate());
907 return SALOME::RequestSwitcher::_narrow(obj);