]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMESDS/SALOMESDS_DataScopeServer.cxx
Salome HOME
528e2566400f9b1a5898f800af7f5950e892f17c
[modules/kernel.git] / src / SALOMESDS / SALOMESDS_DataScopeServer.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony GEAY (EDF R&D)
20
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"
32
33 #include <sstream>
34 #include <iterator>
35 #include <algorithm>
36
37 // agy : awful, to be factorized with ContainerManager.
38 #ifndef WIN32
39 #include <unistd.h>
40 #else
41 #include <process.h>
42 #define getpid _getpid
43 #endif
44
45 using namespace SALOMESDS;
46
47 std::size_t DataScopeServerBase::COUNTER=0;
48
49 void DataScopeKiller::shutdown()
50 {
51   Py_Finalize();
52   _orb->shutdown(0);
53 }
54
55 DataScopeServerBase::DataScopeServerBase(CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):_globals(0),_locals(0),_pickler(0),_orb(CORBA::ORB::_duplicate(orb)),_name(scopeName),_killer(killer)
56 {
57 }
58
59 DataScopeServerBase::DataScopeServerBase(const DataScopeServerBase& other):_globals(0),_locals(0),_pickler(0),_name(other._name),_vars(other._vars),_killer(other._killer)
60 {
61 }
62
63 DataScopeServerBase::~DataScopeServerBase()
64 {
65   // _globals is borrowed ref -> do nothing
66   Py_XDECREF(_locals);
67   Py_XDECREF(_pickler);
68   for(std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it=_vars.begin();it!=_vars.end();it++)
69     {
70        BasicDataServer *obj((*it).second);
71        if(obj)
72          {
73            obj->decrRef();
74          }
75     }
76 }
77
78 /*!
79  * Called remotely -> to protect against throw
80  */
81 void DataScopeServerBase::ping()
82 {
83 }
84
85 /*!
86  * Called remotely -> to protect against throw
87  */
88 char *DataScopeServerBase::getScopeName()
89 {
90   return CORBA::string_dup(_name.c_str());
91 }
92
93 /*!
94  * Called remotely -> to protect against throw
95  */
96 SALOME::StringVec *DataScopeServerBase::listVars()
97 {
98   SALOME::StringVec *ret(new SALOME::StringVec);
99   std::size_t sz(_vars.size());
100   ret->length(sz);
101   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(_vars.begin());
102   for(std::size_t i=0;i<sz;it++,i++)
103     {
104       BasicDataServer *obj((*it).second);
105       std::string name(obj->getVarNameCpp());
106       (*ret)[i]=CORBA::string_dup(name.c_str());
107     }
108   return ret;
109 }
110
111 CORBA::Boolean DataScopeServerBase::existVar(const char *varName)
112 {
113   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it(_vars.begin());
114   for(;it!=_vars.end();it++)
115     if((*it).second->getVarNameCpp()==varName)
116       return true;
117   return false;
118 }
119
120 SALOME::BasicDataServer_ptr DataScopeServerBase::retrieveVarInternal(const char *varName)
121 {
122   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(retrieveVarInternal3(varName));
123   return SALOME::BasicDataServer::_duplicate((*it0).first);
124 }
125
126 BasicDataServer *DataScopeServerBase::retrieveVarInternal2(const std::string& varName)
127 {
128   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(retrieveVarInternal3(varName));
129   return (*it0).second;
130 }
131
132 void DataScopeServerBase::deleteVar(const char *varName)
133 {
134   std::string varNameCpp(varName);
135   std::vector<std::string> allNames(getAllVarNames());
136   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varNameCpp));
137   if(it==allNames.end())
138     {
139       std::ostringstream oss; oss << "DataScopeServerBase::deleteVar : name \"" << varNameCpp << "\" does not exists ! Possibilities are :";
140       std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
141       throw Exception(oss.str());
142     }
143   std::size_t pos(std::distance(allNames.begin(),it));
144   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(_vars.begin());
145   for(std::size_t ii=0;ii<pos;ii++,it0++);
146   (*it0).second->decrRef();
147   _vars.erase(it0);
148 }
149
150 CORBA::Boolean DataScopeServerBase::shutdownIfNotHostedByDSM(SALOME::DataScopeKiller_out killer)
151 {
152   SALOME_NamingService ns(_orb);
153   CORBA::Object_var obj(ns.Resolve(DataServerManager::NAME_IN_NS));
154   SALOME::DataServerManager_var dsm(SALOME::DataServerManager::_narrow(obj));
155   if(CORBA::is_nil(dsm))
156     throw Exception("Unable to reach in the NS the unique DataServerManager instance of the Session !");
157   // destroy ref in the naming service
158   std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(_name));
159   ns.Destroy_Name(fullScopeName.c_str());
160   // establish if dsm and this shared the same POA. If yes dsm and this are collocated !
161   PortableServer::ServantBase *ret(0);
162   try
163     {
164       ret=_poa->reference_to_servant(dsm);
165     }
166   catch(...) { ret=0; }
167   //
168   if(!ret)
169     {
170       enforcedRelease();
171       killer=SALOME::DataScopeKiller::_duplicate(_killer);
172       return true;
173     }
174   else
175     {
176       ret->_remove_ref();
177       enforcedRelease();
178       killer=SALOME::DataScopeKiller::_duplicate(_killer);
179       return false;
180     }
181 }
182
183 SALOME::ByteVec *DataScopeServerBase::fetchSerializedContent(const char *varName)
184 {
185   BasicDataServer *var(retrieveVarInternal2(varName));
186   PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
187   if(!varc)
188     {
189       std::ostringstream oss; oss << "DataScopeServerBase::fetchSerializedContent : var \"" << varName << "\" exists but it is not serialized !";
190       throw Exception(oss.str());
191     }
192   return varc->fetchSerializedContent();
193 }
194
195 SALOME::SeqOfByteVec *DataScopeServerBase::getAllKeysOfVarWithTypeDict(const char *varName)
196 {
197   BasicDataServer *var(retrieveVarInternal2(varName));
198   PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
199   if(!varc)
200     {
201       std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" exists but it is not serialized !";
202       throw Exception(oss.str());
203     }
204   if(!varc->isDict())
205     {
206       std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" exists but it is not a PyDict !";
207       throw Exception(oss.str());
208     }
209   PyObject *keys(PyDict_Keys(varc->getPyObj()));
210   if(!PyList_Check(keys))
211     {
212       std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" has keys but not of type list !";
213       throw Exception(oss.str());
214     }
215   Py_ssize_t sz(PyList_Size(keys));
216   SALOME::SeqOfByteVec *ret(new SALOME::SeqOfByteVec);
217   ret->length(sz);
218   for(Py_ssize_t i=0;i<sz;i++)
219     {
220       PyObject *item(PyList_GetItem(keys,i));
221       Py_XINCREF(item);
222       std::string pickel(varc->pickelize(item));//item consumed
223       PickelizedPyObjServer::FromCppToByteSeq(pickel,(*ret)[i]);
224     }
225   Py_XDECREF(keys);
226   return ret;
227 }
228
229 void DataScopeServerBase::initializePython(int argc, char *argv[])
230 {
231   Py_Initialize();
232   PyEval_InitThreads();
233   PySys_SetArgv(argc,argv);
234   PyObject *mainmod(PyImport_AddModule("__main__"));
235   _globals=PyModule_GetDict(mainmod);
236   if(PyDict_GetItemString(_globals, "__builtins__") == NULL)
237     {
238       PyObject *bimod(PyImport_ImportModule("__builtin__"));
239       if (bimod == NULL || PyDict_SetItemString(_globals, "__builtins__", bimod) != 0)
240         Py_FatalError("can't add __builtins__ to __main__");
241       Py_XDECREF(bimod);
242     }
243   _locals=PyDict_New();
244   PyObject *tmp(PyList_New(0));
245   _pickler=PyImport_ImportModuleLevel(const_cast<char *>("cPickle"),_globals,_locals,tmp,-1);
246 }
247
248 void DataScopeServerBase::registerToSalomePiDict() const
249 {
250   PyObject *mod(PyImport_ImportModule("addToKillList"));//new value
251   if(!mod)
252     return;
253   PyObject *meth(PyObject_GetAttrString(mod,"addToKillList"));//new value
254   if(!meth)
255     { Py_XDECREF(mod); return ; }
256   PyObject *args(PyTuple_New(2));
257   PyTuple_SetItem(args,0,PyInt_FromLong(getpid()));
258   PyTuple_SetItem(args,1,PyString_FromString("SALOME_DataScopeServerBase"));
259   PyObject *res(PyObject_CallObject(meth,args));
260   Py_XDECREF(args);
261   Py_XDECREF(res);
262   Py_XDECREF(meth);
263   Py_XDECREF(mod);
264 }
265
266 void DataScopeServerBase::setPOA(PortableServer::POA_var poa)
267 {
268   _poa=poa;
269 }
270
271 void DataScopeServerBase::registerInNS(SALOME::DataScopeServerBase_ptr ptr)
272 {
273   std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(_name));
274   SALOME_NamingService ns(_orb);
275   ns.Register(ptr,fullScopeName.c_str());
276 }
277
278 std::string DataScopeServerBase::BuildTmpVarNameFrom(const std::string& varName)
279 {
280   std::ostringstream oss;
281   oss << varName << "@" << COUNTER++;
282   return oss.str();
283 }
284
285 std::vector< std::string > DataScopeServerBase::getAllVarNames() const
286 {
287   std::size_t sz(_vars.size());
288   std::vector<std::string> ret(sz);
289   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it(_vars.begin());
290   for(std::size_t i=0;i<sz;it++,i++)
291     ret[i]=(*it).second->getVarNameCpp();
292   return ret;
293 }
294
295 void DataScopeServerBase::checkNotAlreadyExistingVar(const std::string& varName) const
296 {
297   std::vector<std::string> allNames(getAllVarNames());
298   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
299   if(it!=allNames.end())
300     {
301       std::ostringstream oss; oss << "DataScopeServerBase::checkNotAlreadyExistingVar : name \"" << varName << "\" already exists !";
302       throw Exception(oss.str());
303     }
304 }
305
306 void DataScopeServerBase::checkExistingVar(const std::string& varName) const
307 {
308   std::vector<std::string> allNames(getAllVarNames());
309   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
310   if(it==allNames.end())
311     {
312       std::ostringstream oss; oss << "DataScopeServerBase::checkExistingVar : name \"" << varName << "\" does not exist !";
313       throw Exception(oss.str());
314     }
315 }
316
317 PickelizedPyObjServer *DataScopeServerBase::checkVarExistingAndDict(const std::string& varName)
318 {
319   checkExistingVar(varName);
320   BasicDataServer *var(retrieveVarInternal2(varName.c_str()));
321   PickelizedPyObjServer *ret(dynamic_cast<PickelizedPyObjServer *>(var));
322   if(!ret)
323     {
324       std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << varName << "\"exists but it is not serialized !";
325       throw Exception(oss.str());
326     }
327   if(!ret->isDict())
328     {
329       std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << varName << "\"exists but it is not a Dict !";
330       throw Exception(oss.str());
331     }
332   return ret;
333 }
334
335 void DataScopeServerBase::moveStatusOfVarFromRdWrToRdOnly(const std::string& varName)
336 {
337   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
338   std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
339   PickelizedPyObjRdWrServer *varc(dynamic_cast<PickelizedPyObjRdWrServer *>(p.second));
340   if(!varc)
341     throw Exception("DataScopeServerBase::moveStatusOfVarFromRdWrToRdOnly : var is not a RdWr !");
342   PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
343   PickelizedPyObjRdOnlyServer *newVar(new PickelizedPyObjRdOnlyServer(this,varName,pyobj));
344   CORBA::Object_var obj(newVar->activate());
345   SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
346   p.first=obj2; p.second=newVar;
347   varc->decrRef();
348 }
349
350 void DataScopeServerBase::moveStatusOfVarFromRdOnlyToRdWr(const std::string& varName)
351 {
352   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
353   std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
354   PickelizedPyObjRdOnlyServer *varc(dynamic_cast<PickelizedPyObjRdOnlyServer *>(p.second));
355   if(!varc)
356     throw Exception("DataScopeServerBase::moveStatusOfVarFromRdOnlyToRdWr : var is not a RdWr !");
357   PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
358   PickelizedPyObjRdWrServer *newVar(new PickelizedPyObjRdWrServer(this,varName,pyobj));
359   CORBA::Object_var obj(newVar->activate());
360   SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
361   p.first=obj2; p.second=newVar;
362   varc->decrRef();
363 }
364
365 void DataScopeServerBase::moveStatusOfVarFromRdExtToRdExtInit(const std::string& varName)
366 {
367   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
368   std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
369   PickelizedPyObjRdExtServer *varc(dynamic_cast<PickelizedPyObjRdExtServer *>(p.second));
370   if(!varc)
371     throw Exception("DataScopeServerBase::moveStatusOfVarFromRdExtToRdExtInit : var is not a RdExt !");
372   PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
373   PickelizedPyObjRdExtInitServer *newVar(new PickelizedPyObjRdExtInitServer(this,varName,pyobj));
374   CORBA::Object_var obj(newVar->activate());
375   SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
376   p.first=obj2; p.second=newVar;
377   varc->decrRef();
378 }
379
380 void DataScopeServerBase::moveStatusOfVarFromRdExtInitToRdExt(const std::string& varName)
381 {
382   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
383   std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
384   PickelizedPyObjRdExtInitServer *varc(dynamic_cast<PickelizedPyObjRdExtInitServer *>(p.second));
385   if(!varc)
386     throw Exception("DataScopeServerBase::moveStatusOfVarFromRdExtInitToRdExt : var is not a RdExtInit !");
387   PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
388   PickelizedPyObjRdExtServer *newVar(new PickelizedPyObjRdExtServer(this,varName,pyobj));
389   CORBA::Object_var obj(newVar->activate());
390   SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
391   p.first=obj2; p.second=newVar;
392   varc->decrRef();
393 }
394
395 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator DataScopeServerBase::retrieveVarInternal3(const std::string& varName) const
396 {
397   std::vector<std::string> allNames(getAllVarNames());
398   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
399   if(it==allNames.end())
400     {
401       std::ostringstream oss; oss << "DataScopeServerBase::retrieveVarInternal3 : name \"" << varName << "\" does not exists ! Possibilities are :";
402       std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
403       throw Exception(oss.str());
404     }
405   std::size_t pos(std::distance(allNames.begin(),it));
406   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(_vars.begin());
407   for(std::size_t i=0;i<pos;i++,it0++);
408   return it0;
409 }
410
411 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator DataScopeServerBase::retrieveVarInternal4(const std::string& varName)
412 {
413   std::vector<std::string> allNames(getAllVarNames());
414   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
415   if(it==allNames.end())
416     {
417       std::ostringstream oss; oss << "DataScopeServerBase::retrieveVarInternal4 : name \"" << varName << "\" does not exists ! Possibilities are :";
418       std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
419       throw Exception(oss.str());
420     }
421   std::size_t pos(std::distance(allNames.begin(),it));
422   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(_vars.begin());
423   for(std::size_t i=0;i<pos;i++,it0++);
424   return it0;
425 }
426
427 ///////
428
429 DataScopeServer::DataScopeServer(CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):DataScopeServerBase(orb,killer,scopeName)
430 {
431 }
432
433 DataScopeServer::DataScopeServer(const DataScopeServer& other):DataScopeServerBase(other)
434 {
435 }
436
437 SALOME::PickelizedPyObjRdOnlyServer_ptr DataScopeServer::createRdOnlyVar(const char *varName, const SALOME::ByteVec& constValue)
438 {
439   std::string varNameCpp(varName);
440   checkNotAlreadyExistingVar(varNameCpp);
441   PickelizedPyObjRdOnlyServer *tmp(new PickelizedPyObjRdOnlyServer(this,varNameCpp,constValue));
442   CORBA::Object_var ret(tmp->activate());
443   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
444   _vars.push_back(p);
445   return SALOME::PickelizedPyObjRdOnlyServer::_narrow(ret);
446 }
447
448 SALOME::PickelizedPyObjRdExtServer_ptr DataScopeServer::createRdExtVar(const char *varName, const SALOME::ByteVec& constValue)
449 {
450   std::string varNameCpp(varName);
451   checkNotAlreadyExistingVar(varNameCpp);
452   PickelizedPyObjRdExtServer *tmp(new PickelizedPyObjRdExtServer(this,varNameCpp,constValue));
453   CORBA::Object_var ret(tmp->activate());
454   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
455   _vars.push_back(p);
456   return SALOME::PickelizedPyObjRdExtServer::_narrow(ret);
457 }
458
459 SALOME::PickelizedPyObjRdWrServer_ptr DataScopeServer::createRdWrVar(const char *typeName, const char *varName)
460 {
461   std::string varNameCpp(varName),typeNameCpp(typeName);
462   checkNotAlreadyExistingVar(varNameCpp);
463   PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,typeNameCpp,varNameCpp));
464   CORBA::Object_var ret(tmp->activate());
465   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
466   _vars.push_back(p);
467   return SALOME::PickelizedPyObjRdWrServer::_narrow(ret);
468 }
469
470 DataScopeServer::~DataScopeServer()
471 {
472 }
473
474 ////////
475
476 DataScopeServerTransaction::DataScopeServerTransaction(CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):DataScopeServerBase(orb,killer,scopeName)
477 {
478   CORBA::Object_var obj(_orb->resolve_initial_references("RootPOA"));
479   PortableServer::POA_var poa(PortableServer::POA::_narrow(obj));
480   //
481   PortableServer::POAManager_var mgr(poa->the_POAManager());
482   CORBA::PolicyList policies;
483   policies.length(1);
484   PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
485   policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol);
486   PortableServer::POA_var poa2(poa->create_POA("POAForWaiters",mgr,policies));
487   threadPol->destroy();
488   //
489   _poa_for_key_waiter=poa2;
490 }
491
492 DataScopeServerTransaction::DataScopeServerTransaction(const DataScopeServerTransaction& other):DataScopeServerBase(other),_poa_for_key_waiter(other.getPOA4KeyWaiter())
493 {
494 }
495
496 char *DataScopeServerTransaction::getAccessOfVar(const char *varName)
497 {
498   std::string varNameCpp(varName);
499   checkExistingVar(varNameCpp);
500   BasicDataServer *var(retrieveVarInternal2(varName));
501   if(!var)
502     throw Exception("DataScopeServerTransaction::getAccessOfVar : variable is NULL !");
503   PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
504   if(!varc)
505     throw Exception("DataScopeServerTransaction::getAccessOfVar : variable is not of type PickelizedPyObjServer !");
506   std::string ret(varc->getAccessStr());
507   return CORBA::string_dup(ret.c_str());
508 }
509
510 void DataScopeServerTransaction::createRdOnlyVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
511 {
512   checkNotAlreadyExistingVar(varName);
513   PickelizedPyObjRdOnlyServer *tmp(new PickelizedPyObjRdOnlyServer(this,varName,constValue));
514   CORBA::Object_var ret(tmp->activate());
515   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
516   _vars.push_back(p);
517 }
518
519 void DataScopeServerTransaction::createRdExtVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
520 {
521   checkNotAlreadyExistingVar(varName);
522   PickelizedPyObjRdExtServer *tmp(new PickelizedPyObjRdExtServer(this,varName,constValue));
523   CORBA::Object_var ret(tmp->activate());
524   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
525   _vars.push_back(p);
526 }
527
528 void DataScopeServerTransaction::createRdWrVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
529 {
530   checkNotAlreadyExistingVar(varName);
531   PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,varName,constValue));
532   CORBA::Object_var ret(tmp->activate());
533   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
534   _vars.push_back(p);
535 }
536
537 SALOME::Transaction_ptr DataScopeServerTransaction::createRdOnlyVarTransac(const char *varName, const SALOME::ByteVec& constValue)
538 {
539   checkNotAlreadyExistingVar(varName);
540   TransactionRdOnlyVarCreate *ret(new TransactionRdOnlyVarCreate(this,varName,constValue));
541   CORBA::Object_var obj(ret->activate());
542   return SALOME::Transaction::_narrow(obj);
543 }
544
545 SALOME::Transaction_ptr DataScopeServerTransaction::createRdExtVarTransac(const char *varName, const SALOME::ByteVec& constValue)
546 {
547   checkNotAlreadyExistingVar(varName);
548   TransactionRdExtVarCreate *ret(new TransactionRdExtVarCreate(this,varName,constValue));
549   CORBA::Object_var obj(ret->activate());
550   return SALOME::Transaction::_narrow(obj);
551 }
552
553 SALOME::Transaction_ptr DataScopeServerTransaction::createRdWrVarTransac(const char *varName, const SALOME::ByteVec& constValue)
554 {
555   checkNotAlreadyExistingVar(varName);
556   TransactionRdWrVarCreate *ret(new TransactionRdWrVarCreate(this,varName,constValue));
557   CORBA::Object_var obj(ret->activate());
558   return SALOME::Transaction::_narrow(obj);
559 }
560
561 void DataScopeServerTransaction::addWaitKey(KeyWaiter *kw)
562 {
563   if(!kw)
564     throw Exception("DataScopeServerTransaction::addWaitKey : NULL input object !");
565   _waiting_keys.push_back(kw);
566 }
567
568 void DataScopeServerTransaction::pingKey(PyObject *keyObj)
569 {
570   PyObject *cmpObj(getPyCmpFunc());
571   if(!keyObj)
572     throw Exception("ataScopeServerTransaction::pingKey : Key Object is NULL !");
573   PyObject *args(PyTuple_New(2));
574   PyTuple_SetItem(args,0,keyObj); Py_XINCREF(keyObj);
575   std::size_t ii(0);
576   // this part does nothing except to be sure that in notify key all will be OK.
577   for(std::list< KeyWaiter *>::iterator it=_waiting_keys.begin();it!=_waiting_keys.end();it++,ii++)
578     {
579       PyObject *waitKey((*it)->getKeyPyObj());
580       PyTuple_SetItem(args,1,waitKey); Py_XINCREF(waitKey);
581       PyObject *res(PyObject_CallObject(cmpObj,args));
582       if(res==NULL)
583         {
584           std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " error during cmp(k,wk[i]) !";
585           throw Exception(oss.str());
586         }
587       PyInt_AsLong(res);
588       if(PyErr_Occurred())
589         {
590           std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " error during interpretation of cmp(k,wk[i]) !";
591           throw Exception(oss.str());
592         }
593       Py_XDECREF(res);
594     }
595 }
596
597 void DataScopeServerTransaction::notifyKey(const std::string& varName, PyObject *keyObj, PyObject *valueObj)
598 {
599   PyObject *cmpObj(getPyCmpFunc());
600   if(!keyObj)
601     throw Exception("DataScopeServerTransaction::notifyKey : MAIN INTERNAL ERROR ! Key Object is NULL !");
602   PyObject *args(PyTuple_New(2));
603   PyTuple_SetItem(args,0,keyObj); Py_XINCREF(keyObj);
604   std::size_t ii(0);
605   std::list< KeyWaiter *> newList,listOfEltToWakeUp;
606   for(std::list< KeyWaiter *>::iterator it=_waiting_keys.begin();it!=_waiting_keys.end();it++,ii++)
607     {
608       if((*it)->getVarName()!=varName)
609         {
610           newList.push_back(*it);
611           continue;
612         }
613       PyObject *waitKey((*it)->getKeyPyObj());
614       PyTuple_SetItem(args,1,waitKey); Py_XINCREF(waitKey);
615       PyObject *res(PyObject_CallObject(cmpObj,args));
616       if(res==NULL)
617         {
618           std::ostringstream oss; oss << "DataScopeServerTransaction::notifyKey : MAIN INTERNAL ERROR ! for object id #" << ii << " error during cmp(k,wk[i]) !";
619           throw Exception(oss.str());
620         }
621       long resCpp(PyInt_AsLong(res));
622       if(PyErr_Occurred())
623         {
624           std::ostringstream oss; oss << "DataScopeServerTransaction::notifyKey : MAIN INTERNAL ERROR ! for object id #" << ii << " error during interpretation of cmp(k,wk[i]) !";
625           throw Exception(oss.str());
626         }
627       Py_XDECREF(res);
628       if(resCpp==0)
629         listOfEltToWakeUp.push_back(*it);
630       else
631         newList.push_back(*it);
632     }
633   for(std::list< KeyWaiter *>::iterator it=listOfEltToWakeUp.begin();it!=listOfEltToWakeUp.end();it++)
634     (*it)->valueJustCome(valueObj);
635   for(std::list< KeyWaiter *>::iterator it=listOfEltToWakeUp.begin();it!=listOfEltToWakeUp.end();it++)
636     (*it)->go();
637   _waiting_keys=newList;
638 }
639
640 SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarHard(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value)
641 {
642   checkVarExistingAndDict(varName);
643   TransactionAddKeyValueHard *ret(new TransactionAddKeyValueHard(this,varName,key,value));
644   CORBA::Object_var obj(ret->activate());
645   return SALOME::Transaction::_narrow(obj);
646 }
647
648 SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarErrorIfAlreadyExisting(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value)
649 {
650   checkVarExistingAndDict(varName);
651   TransactionAddKeyValueErrorIfAlreadyExisting *ret(new TransactionAddKeyValueErrorIfAlreadyExisting(this,varName,key,value));
652   CORBA::Object_var obj(ret->activate());
653   return SALOME::Transaction::_narrow(obj);
654 };
655
656 SALOME::TransactionMultiKeyAddSession_ptr DataScopeServerTransaction::addMultiKeyValueSession(const char *varName)
657 {
658   checkVarExistingAndDict(varName);
659   TransactionMultiKeyAddSession *ret(new TransactionMultiKeyAddSession(this,varName));
660   CORBA::Object_var obj(ret->activate());
661   return SALOME::TransactionMultiKeyAddSession::_narrow(obj);
662 }
663
664 SALOME::Transaction_ptr DataScopeServerTransaction::removeKeyInVarErrorIfNotAlreadyExisting(const char *varName, const SALOME::ByteVec& key)
665 {
666   checkVarExistingAndDict(varName);
667   TransactionRemoveKeyInVarErrorIfNotAlreadyExisting *ret(new TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(this,varName,key));
668   CORBA::Object_var obj(ret->activate());
669   return SALOME::Transaction::_narrow(obj);
670 }
671
672 SALOME::TransactionRdWrAccess_ptr DataScopeServerTransaction::createWorkingVarTransac(const char *varName, const SALOME::ByteVec& constValue)
673 {
674   std::string varNameCpp(varName);
675   checkNotAlreadyExistingVar(varName);
676   PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,varNameCpp,constValue));
677   CORBA::Object_var obj(tmp->activate());
678   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(obj),tmp);
679   _vars.push_back(p);
680   //
681   TransactionMorphRdWrIntoRdOnly *ret(new TransactionMorphRdWrIntoRdOnly(this,varName));
682   CORBA::Object_var obj2(ret->activate());
683   return SALOME::TransactionRdWrAccess::_narrow(obj2);
684 }
685
686 SALOME::KeyWaiter_ptr DataScopeServerTransaction::waitForKeyInVar(const char *varName, const SALOME::ByteVec& keyVal)
687 {
688   PickelizedPyObjServer *pickelObj(checkVarExistingAndDict(varName));
689   KeyWaiter *ret(new KeyWaiter(pickelObj,keyVal));
690   CORBA::Object_var obj(ret->activate());//KeyWaiter instance activated inside a multithread POA contrary to all of objects in SALOMESDS in single thread !
691   return SALOME::KeyWaiter::_narrow(obj);
692 }
693
694 SALOME::KeyWaiter_ptr DataScopeServerTransaction::waitForKeyInVarAndKillIt(const char *varName, const SALOME::ByteVec& keyVal, SALOME::Transaction_out transac)
695 {
696   PickelizedPyObjServer *pickelObj(checkVarExistingAndDict(varName));
697   KeyWaiter *ret0(new KeyWaiter(pickelObj,keyVal));
698   CORBA::Object_var obj(ret0->activate());//KeyWaiter instance activated inside a multithread POA contrary to all of objects in SALOMESDS in single thread !
699   //
700   TransactionRemoveKeyInVarErrorIfNotAlreadyExisting *ret1(new TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(this,varName,keyVal));
701   CORBA::Object_var obj2(ret1->activate());
702   transac=SALOME::Transaction::_narrow(obj2);
703   //
704   return SALOME::KeyWaiter::_narrow(obj);
705 }
706
707 SALOME::ByteVec *DataScopeServerTransaction::waitForMonoThrRev(SALOME::KeyWaiter_ptr kw)
708 {
709   PortableServer::ServantBase *ret(0);
710   try
711     {
712       ret=_poa_for_key_waiter->reference_to_servant(kw);
713     }
714   catch(...) { ret=0; }
715   KeyWaiter *retc(dynamic_cast<KeyWaiter *>(ret));
716   if(!retc)
717     throw Exception("DataScopeServerTransaction::invokeMonoThr : internal error 1 !");
718   retc->_remove_ref();
719   retc->waitForMonoThr();
720 }
721
722 void DataScopeServerTransaction::atomicApply(const SALOME::ListOfTransaction& transactions)
723 {
724   std::size_t sz(transactions.length());
725   if(sz==0)
726     return ;
727   std::vector< AutoServantPtr<Transaction> > transactionsCpp(sz);
728   for(std::size_t i=0;i<sz;i++)
729     {
730       PortableServer::ServantBase *eltBase(0);
731       Transaction *elt(0);
732       try
733         {
734           eltBase=_poa->reference_to_servant(transactions[i]);
735           elt=dynamic_cast<Transaction *>(eltBase);
736         }
737       catch(...)
738         {
739           std::ostringstream oss; oss << "DataScopeServerTransaction::atomicApply : the elt #" << i << " is invalid !";
740           throw Exception(oss.str());
741         }
742       if(!elt)
743         {
744           std::ostringstream oss; oss << "DataScopeServerTransaction::atomicApply : the elt #" << i << " is null ?";
745           throw Exception(oss.str());
746         }
747       elt->_remove_ref();
748       transactionsCpp[i]=elt;
749     }
750   {// important do not merge loops ! 
751     std::vector<TrustTransaction> transactions2(sz);
752     bool mustRollback(true);
753     for(std::size_t i=0;i<sz;i++)
754       transactions2[i].setTransaction(transactionsCpp[i],&mustRollback);
755     for(std::size_t i=0;i<sz;i++)
756       transactions2[i].operate();
757     mustRollback=false;
758   }
759   for(std::size_t i=0;i<sz;i++)
760     transactionsCpp[i]->notify();
761 }
762
763 /*!
764  * Returns borrowed reference.
765  */
766 PyObject *DataScopeServerTransaction::getPyCmpFunc()
767 {
768   PyObject *builtins(PyDict_GetItemString(_globals,"__builtins__"));//borrowed
769   if(builtins==NULL)
770     throw Exception("Fail to find reference to builtins !");
771   PyObject *builtins2(PyModule_GetDict(builtins));//borrowed
772   if(builtins2==NULL)
773     throw Exception("Fail to invoke __dict__ on builtins !");
774   PyObject *cmpObj(PyDict_GetItemString(builtins2,"cmp"));
775   if(cmpObj==NULL)
776     throw Exception("Fail to find cmp in __builtins__ !");
777   return cmpObj;
778 }
779
780 DataScopeServerTransaction::~DataScopeServerTransaction()
781 {
782 }
783