]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMESDS/SALOMESDS_DataScopeServer.cxx
Salome HOME
Merge 'master' branch into 'V9_dev' branch
[modules/kernel.git] / src / SALOMESDS / SALOMESDS_DataScopeServer.cxx
1 // Copyright (C) 2007-2016  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 #if PY_VERSION_HEX < 0x03050000
50 static char*
51 Py_EncodeLocale(const wchar_t *arg, size_t *size)
52 {
53         return _Py_wchar2char(arg, size);
54 }
55 static wchar_t*
56 Py_DecodeLocale(const char *arg, size_t *size)
57 {
58         return _Py_char2wchar(arg, size);
59 }
60 #endif
61
62 void DataScopeKiller::shutdown()
63 {
64   Py_Finalize();
65   _orb->shutdown(0);
66 }
67
68 RequestSwitcher::RequestSwitcher(CORBA::ORB_ptr orb, DataScopeServerTransaction *ds):RequestSwitcherBase(orb),_ds(ds)
69 {
70 }
71
72 SALOME::StringVec *RequestSwitcher::listVars()
73 {
74   return _ds->listVars();
75 }
76
77 SALOME::ByteVec *RequestSwitcher::fetchSerializedContent(const char *varName)
78 {
79   return _ds->fetchSerializedContent(varName);
80 }
81
82 void RequestSwitcher::fetchAndGetAccessOfVar(const char *varName, CORBA::String_out access, SALOME::ByteVec_out data)
83 {
84   return _ds->fetchAndGetAccessOfVar(varName,access,data);
85 }
86
87 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)
88 {
89 }
90
91 DataScopeServerBase::DataScopeServerBase(const DataScopeServerBase& other):omniServant(other),ServantBase(other),_globals(0),_locals(0),_pickler(0),_name(other._name),_vars(other._vars),_killer(other._killer)
92 {
93 }
94
95 DataScopeServerBase::~DataScopeServerBase()
96 {
97   // _globals is borrowed ref -> do nothing
98   Py_XDECREF(_locals);
99   Py_XDECREF(_pickler);
100   for(std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it=_vars.begin();it!=_vars.end();it++)
101     {
102        BasicDataServer *obj((*it).second);
103        if(obj)
104          {
105            obj->decrRef();
106          }
107     }
108 }
109
110 /*!
111  * Called remotely -> to protect against throw
112  */
113 void DataScopeServerBase::ping()
114 {
115 }
116
117 /*!
118  * Called remotely -> to protect against throw
119  */
120 char *DataScopeServerBase::getScopeName()
121 {
122   return CORBA::string_dup(_name.c_str());
123 }
124
125 /*!
126  * Called remotely -> to protect against throw
127  */
128 SALOME::StringVec *DataScopeServerBase::listVars()
129 {
130   SALOME::StringVec *ret(new SALOME::StringVec);
131   std::size_t sz(_vars.size());
132   ret->length(sz);
133   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(_vars.begin());
134   for(std::size_t i=0;i<sz;it++,i++)
135     {
136       BasicDataServer *obj((*it).second);
137       std::string name(obj->getVarNameCpp());
138       (*ret)[i]=CORBA::string_dup(name.c_str());
139     }
140   return ret;
141 }
142
143 CORBA::Boolean DataScopeServerBase::existVar(const char *varName)
144 {
145   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it(_vars.begin());
146   for(;it!=_vars.end();it++)
147     if((*it).second->getVarNameCpp()==varName)
148       return true;
149   return false;
150 }
151
152 SALOME::BasicDataServer_ptr DataScopeServerBase::retrieveVarInternal(const char *varName)
153 {
154   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(retrieveVarInternal3(varName));
155   return SALOME::BasicDataServer::_duplicate((*it0).first);
156 }
157
158 BasicDataServer *DataScopeServerBase::retrieveVarInternal2(const std::string& varName)
159 {
160   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(retrieveVarInternal3(varName));
161   return (*it0).second;
162 }
163
164 void DataScopeServerBase::deleteVar(const char *varName)
165 {
166   std::string varNameCpp(varName);
167   std::vector<std::string> allNames(getAllVarNames());
168   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varNameCpp));
169   if(it==allNames.end())
170     {
171       std::ostringstream oss; oss << "DataScopeServerBase::deleteVar : name \"" << varNameCpp << "\" does not exists ! Possibilities are :";
172       std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
173       throw Exception(oss.str());
174     }
175   std::size_t pos(std::distance(allNames.begin(),it));
176   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(_vars.begin());
177   for(std::size_t ii=0;ii<pos;ii++,it0++);
178   (*it0).second->decrRef();
179   _vars.erase(it0);
180 }
181
182 CORBA::Boolean DataScopeServerBase::shutdownIfNotHostedByDSM(SALOME::DataScopeKiller_out killer)
183 {
184   SALOME_NamingService ns(_orb);
185   CORBA::Object_var obj(ns.Resolve(DataServerManager::NAME_IN_NS));
186   SALOME::DataServerManager_var dsm(SALOME::DataServerManager::_narrow(obj));
187   if(CORBA::is_nil(dsm))
188     throw Exception("Unable to reach in the NS the unique DataServerManager instance of the Session !");
189   // destroy ref in the naming service
190   std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(_name));
191   ns.Destroy_Name(fullScopeName.c_str());
192   // establish if dsm and this shared the same POA. If yes dsm and this are collocated !
193   PortableServer::ServantBase *ret(0);
194   try
195     {
196       ret=_poa->reference_to_servant(dsm);
197     }
198   catch(...) { ret=0; }
199   //
200   if(!ret)
201     {
202       enforcedRelease();
203       killer=SALOME::DataScopeKiller::_duplicate(_killer);
204       return true;
205     }
206   else
207     {
208       ret->_remove_ref();
209       enforcedRelease();
210       killer=SALOME::DataScopeKiller::_duplicate(_killer);
211       return false;
212     }
213 }
214
215 SALOME::ByteVec *DataScopeServerBase::fetchSerializedContent(const char *varName)
216 {
217   BasicDataServer *var(retrieveVarInternal2(varName));
218   PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
219   if(!varc)
220     {
221       std::ostringstream oss; oss << "DataScopeServerBase::fetchSerializedContent : var \"" << varName << "\" exists but it is not serialized !";
222       throw Exception(oss.str());
223     }
224   return varc->fetchSerializedContent();
225 }
226
227 SALOME::SeqOfByteVec *DataScopeServerBase::getAllKeysOfVarWithTypeDict(const char *varName)
228 {
229   BasicDataServer *var(retrieveVarInternal2(varName));
230   PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
231   if(!varc)
232     {
233       std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" exists but it is not serialized !";
234       throw Exception(oss.str());
235     }
236   if(!varc->isDict())
237     {
238       std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" exists but it is not a PyDict !";
239       throw Exception(oss.str());
240     }
241   PyObject *keys(PyDict_Keys(varc->getPyObj()));
242   if(!PyList_Check(keys))
243     {
244       std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" has keys but not of type list !";
245       throw Exception(oss.str());
246     }
247   Py_ssize_t sz(PyList_Size(keys));
248   SALOME::SeqOfByteVec *ret(new SALOME::SeqOfByteVec);
249   ret->length(sz);
250   for(Py_ssize_t i=0;i<sz;i++)
251     {
252       PyObject *item(PyList_GetItem(keys,i));
253       Py_XINCREF(item);
254       std::string pickel(varc->pickelize(item));//item consumed
255       PickelizedPyObjServer::FromCppToByteSeq(pickel,(*ret)[i]);
256     }
257   Py_XDECREF(keys);
258   return ret;
259 }
260
261 void DataScopeServerBase::takeANap(CORBA::Double napDurationInSec)
262 {
263   if(napDurationInSec<0.)
264     throw Exception("DataScopeServerBase::takeANap : negative value !");
265 #ifndef WIN32
266   struct timespec req,rem;
267   long nbSec((long)napDurationInSec);
268   double remainTime(napDurationInSec-(double)nbSec);
269   req.tv_sec=nbSec;
270   req.tv_nsec=(long)(remainTime*1000000.);
271   int ret(nanosleep(&req,&rem));
272   if(ret!=0)
273     throw Exception("DataScopeServerBase::takeANap : nap not finished as expected !");
274 #else
275   throw Exception("DataScopeServerBase::takeANap : not implemented for Windows !");
276 #endif
277 }
278
279 void DataScopeServerBase::initializePython(int argc, char *argv[])
280 {
281   Py_Initialize();
282   PyEval_InitThreads();
283   wchar_t **changed_argv = new wchar_t*[argc]; // Setting arguments
284   for (int i = 0; i < argc; i++)
285     changed_argv[i] = Py_DecodeLocale(argv[i], NULL);
286   PySys_SetArgv(argc, changed_argv);
287   PyObject *mainmod(PyImport_AddModule("__main__"));
288   _globals=PyModule_GetDict(mainmod);
289   if(PyDict_GetItemString(_globals, "__builtins__") == NULL)
290     {
291       PyObject *bimod(PyImport_ImportModule("__builtin__"));
292       if (bimod == NULL || PyDict_SetItemString(_globals, "__builtins__", bimod) != 0)
293         Py_FatalError("can't add __builtins__ to __main__");
294       Py_XDECREF(bimod);
295     }
296   _locals=PyDict_New();
297   PyObject *tmp(PyList_New(0));
298   _pickler=PyImport_ImportModuleLevel(const_cast<char *>("pickle"),_globals,_locals,tmp,0);
299 }
300
301 void DataScopeServerBase::registerToSalomePiDict() const
302 {
303   PyObject *mod(PyImport_ImportModule("addToKillList"));//new value
304   if(!mod)
305     return;
306   PyObject *meth(PyObject_GetAttrString(mod,"addToKillList"));//new value
307   if(!meth)
308     { Py_XDECREF(mod); return ; }
309   PyObject *args(PyTuple_New(2));
310   PyTuple_SetItem(args,0,PyLong_FromLong(getpid()));
311   PyTuple_SetItem(args,1,PyUnicode_FromString("SALOME_DataScopeServerBase"));
312   PyObject *res(PyObject_CallObject(meth,args));
313   Py_XDECREF(args);
314   Py_XDECREF(res);
315   Py_XDECREF(meth);
316   Py_XDECREF(mod);
317 }
318
319 void DataScopeServerBase::setPOA(PortableServer::POA_var poa)
320 {
321   _poa=poa;
322 }
323
324 void DataScopeServerBase::registerInNS(SALOME::DataScopeServerBase_ptr ptr)
325 {
326   std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(_name));
327   SALOME_NamingService ns(_orb);
328   ns.Register(ptr,fullScopeName.c_str());
329 }
330
331 std::string DataScopeServerBase::BuildTmpVarNameFrom(const std::string& varName)
332 {
333   std::ostringstream oss;
334   oss << varName << "@" << COUNTER++;
335   return oss.str();
336 }
337
338 std::vector< std::string > DataScopeServerBase::getAllVarNames() const
339 {
340   std::size_t sz(_vars.size());
341   std::vector<std::string> ret(sz);
342   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it(_vars.begin());
343   for(std::size_t i=0;i<sz;it++,i++)
344     ret[i]=(*it).second->getVarNameCpp();
345   return ret;
346 }
347
348 void DataScopeServerBase::checkNotAlreadyExistingVar(const std::string& varName) const
349 {
350   std::vector<std::string> allNames(getAllVarNames());
351   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
352   if(it!=allNames.end())
353     {
354       std::ostringstream oss; oss << "DataScopeServerBase::checkNotAlreadyExistingVar : name \"" << varName << "\" already exists !";
355       throw Exception(oss.str());
356     }
357 }
358
359 void DataScopeServerBase::checkExistingVar(const std::string& varName) const
360 {
361   std::vector<std::string> allNames(getAllVarNames());
362   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
363   if(it==allNames.end())
364     {
365       std::ostringstream oss; oss << "DataScopeServerBase::checkExistingVar : name \"" << varName << "\" does not exist !";
366       throw Exception(oss.str());
367     }
368 }
369
370 PickelizedPyObjServer *DataScopeServerBase::checkVarExistingAndDict(const std::string& varName)
371 {
372   checkExistingVar(varName);
373   BasicDataServer *var(retrieveVarInternal2(varName.c_str()));
374   PickelizedPyObjServer *ret(dynamic_cast<PickelizedPyObjServer *>(var));
375   if(!ret)
376     {
377       std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << varName << "\"exists but it is not serialized !";
378       throw Exception(oss.str());
379     }
380   if(!ret->isDict())
381     {
382       std::ostringstream oss; oss << "TransactionAddKeyValueHard::prepareRollBackInCaseOfFailure : var \"" << varName << "\"exists but it is not a Dict !";
383       throw Exception(oss.str());
384     }
385   return ret;
386 }
387
388 void DataScopeServerBase::moveStatusOfVarFromRdWrToRdOnly(const std::string& varName)
389 {
390   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
391   std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
392   PickelizedPyObjRdWrServer *varc(dynamic_cast<PickelizedPyObjRdWrServer *>(p.second));
393   if(!varc)
394     throw Exception("DataScopeServerBase::moveStatusOfVarFromRdWrToRdOnly : var is not a RdWr !");
395   PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
396   PickelizedPyObjRdOnlyServer *newVar(new PickelizedPyObjRdOnlyServer(this,varName,pyobj));
397   CORBA::Object_var obj(newVar->activate());
398   SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
399   p.first=obj2; p.second=newVar;
400   varc->decrRef();
401 }
402
403 void DataScopeServerBase::moveStatusOfVarFromRdOnlyToRdWr(const std::string& varName)
404 {
405   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
406   std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
407   PickelizedPyObjRdOnlyServer *varc(dynamic_cast<PickelizedPyObjRdOnlyServer *>(p.second));
408   if(!varc)
409     throw Exception("DataScopeServerBase::moveStatusOfVarFromRdOnlyToRdWr : var is not a RdWr !");
410   PyObject *pyobj(varc->getPyObj()); Py_XINCREF(pyobj);
411   PickelizedPyObjRdWrServer *newVar(new PickelizedPyObjRdWrServer(this,varName,pyobj));
412   CORBA::Object_var obj(newVar->activate());
413   SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
414   p.first=obj2; p.second=newVar;
415   varc->decrRef();
416 }
417
418 void DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExtInit(const std::string& varName)
419 {
420   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
421   std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
422   PickelizedPyObjRdExtServer *varc0(dynamic_cast<PickelizedPyObjRdExtServer *>(p.second));
423   PickelizedPyObjRdExtInitServer *varc1(dynamic_cast<PickelizedPyObjRdExtInitServer *>(p.second));
424   if(!varc0 && !varc1)
425     throw Exception("DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExtInit : var is neither RdExt nor RdExtInit !");
426   if(varc0)
427     {
428       PyObject *pyobj(varc0->getPyObj()); Py_XINCREF(pyobj);
429       PickelizedPyObjRdExtInitServer *newVar(new PickelizedPyObjRdExtInitServer(this,varName,pyobj));
430       newVar->incrNbClients();
431       CORBA::Object_var obj(newVar->activate());
432       SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
433       p.first=obj2; p.second=newVar;
434       varc0->decrRef();
435     }
436   else
437     varc1->incrNbClients();
438 }
439
440 void DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExt(const std::string& varName)
441 {
442   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it(retrieveVarInternal4(varName));
443   std::pair< SALOME::BasicDataServer_var, BasicDataServer * >& p(*it);
444   PickelizedPyObjRdExtInitServer *varc0(dynamic_cast<PickelizedPyObjRdExtInitServer *>(p.second));
445   PickelizedPyObjRdExtServer *varc1(dynamic_cast<PickelizedPyObjRdExtServer *>(p.second));
446   if(!varc0 && !varc1)
447     throw Exception("DataScopeServerBase::moveStatusOfVarFromRdExtOrRdExtInitToRdExt : var is not a RdExtInit !");
448   if(varc0)
449     {
450       if(varc0->decrNbClients())
451         {
452           PyObject *pyobj(varc0->getPyObj()); Py_XINCREF(pyobj);
453           PickelizedPyObjRdExtServer *newVar(new PickelizedPyObjRdExtServer(this,varName,pyobj));
454           CORBA::Object_var obj(newVar->activate());
455           SALOME::BasicDataServer_var obj2(SALOME::BasicDataServer::_narrow(obj));
456           p.first=obj2; p.second=newVar;
457           varc0->decrRef();
458         }
459     }
460 }
461
462 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator DataScopeServerBase::retrieveVarInternal3(const std::string& varName) const
463 {
464   std::vector<std::string> allNames(getAllVarNames());
465   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
466   if(it==allNames.end())
467     {
468       std::ostringstream oss; oss << "DataScopeServerBase::retrieveVarInternal3 : name \"" << varName << "\" does not exists ! Possibilities are :";
469       std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
470       throw Exception(oss.str());
471     }
472   std::size_t pos(std::distance(allNames.begin(),it));
473   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::const_iterator it0(_vars.begin());
474   for(std::size_t i=0;i<pos;i++,it0++);
475   return it0;
476 }
477
478 std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator DataScopeServerBase::retrieveVarInternal4(const std::string& varName)
479 {
480   std::vector<std::string> allNames(getAllVarNames());
481   std::vector<std::string>::iterator it(std::find(allNames.begin(),allNames.end(),varName));
482   if(it==allNames.end())
483     {
484       std::ostringstream oss; oss << "DataScopeServerBase::retrieveVarInternal4 : name \"" << varName << "\" does not exists ! Possibilities are :";
485       std::copy(allNames.begin(),allNames.end(),std::ostream_iterator<std::string>(oss,", "));
486       throw Exception(oss.str());
487     }
488   std::size_t pos(std::distance(allNames.begin(),it));
489   std::list< std::pair< SALOME::BasicDataServer_var, BasicDataServer * > >::iterator it0(_vars.begin());
490   for(std::size_t i=0;i<pos;i++,it0++);
491   return it0;
492 }
493
494 ///////
495
496 DataScopeServer::DataScopeServer(CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):DataScopeServerBase(orb,killer,scopeName)
497 {
498 }
499
500 DataScopeServer::DataScopeServer(const DataScopeServer& other):omniServant(other),ServantBase(other),DataScopeServerBase(other)
501 {
502 }
503
504 SALOME::PickelizedPyObjRdOnlyServer_ptr DataScopeServer::createRdOnlyVar(const char *varName, const SALOME::ByteVec& constValue)
505 {
506   std::string varNameCpp(varName);
507   checkNotAlreadyExistingVar(varNameCpp);
508   PickelizedPyObjRdOnlyServer *tmp(new PickelizedPyObjRdOnlyServer(this,varNameCpp,constValue));
509   CORBA::Object_var ret(tmp->activate());
510   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
511   _vars.push_back(p);
512   return SALOME::PickelizedPyObjRdOnlyServer::_narrow(ret);
513 }
514
515 SALOME::PickelizedPyObjRdExtServer_ptr DataScopeServer::createRdExtVar(const char *varName, const SALOME::ByteVec& constValue)
516 {
517   std::string varNameCpp(varName);
518   checkNotAlreadyExistingVar(varNameCpp);
519   PickelizedPyObjRdExtServer *tmp(new PickelizedPyObjRdExtServer(this,varNameCpp,constValue));
520   CORBA::Object_var ret(tmp->activate());
521   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
522   _vars.push_back(p);
523   return SALOME::PickelizedPyObjRdExtServer::_narrow(ret);
524 }
525
526 SALOME::PickelizedPyObjRdWrServer_ptr DataScopeServer::createRdWrVar(const char *typeName, const char *varName)
527 {
528   std::string varNameCpp(varName),typeNameCpp(typeName);
529   checkNotAlreadyExistingVar(varNameCpp);
530   PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,typeNameCpp,varNameCpp));
531   CORBA::Object_var ret(tmp->activate());
532   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
533   _vars.push_back(p);
534   return SALOME::PickelizedPyObjRdWrServer::_narrow(ret);
535 }
536
537 DataScopeServer::~DataScopeServer()
538 {
539 }
540
541 ////////
542
543 DataScopeServerTransaction::DataScopeServerTransaction(CORBA::ORB_ptr orb, SALOME::DataScopeKiller_var killer, const std::string& scopeName):DataScopeServerBase(orb,killer,scopeName)
544 {
545   CORBA::Object_var obj(_orb->resolve_initial_references("RootPOA"));
546   PortableServer::POA_var poa(PortableServer::POA::_narrow(obj));
547   //
548   PortableServer::POAManager_var mgr(poa->the_POAManager());
549   CORBA::PolicyList policies;
550   policies.length(1);
551   PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
552   policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol);
553   PortableServer::POA_var poa2(poa->create_POA("POAForWaiters",mgr,policies));
554   threadPol->destroy();
555   //
556   _poa_for_key_waiter=poa2;
557 }
558
559 DataScopeServerTransaction::DataScopeServerTransaction(const DataScopeServerTransaction& other):omniServant(other),ServantBase(other),DataScopeServerBase(other),_poa_for_key_waiter(other.getPOA4KeyWaiter())
560 {
561 }
562
563 char *DataScopeServerTransaction::getAccessOfVar(const char *varName)
564 {
565   std::string varNameCpp(varName);
566   checkExistingVar(varNameCpp);
567   BasicDataServer *var(retrieveVarInternal2(varName));
568   if(!var)
569     throw Exception("DataScopeServerTransaction::getAccessOfVar : variable is NULL !");
570   PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(var));
571   if(!varc)
572     throw Exception("DataScopeServerTransaction::getAccessOfVar : variable is not of type PickelizedPyObjServer !");
573   std::string ret(varc->getAccessStr());
574   return CORBA::string_dup(ret.c_str());
575 }
576
577 /*!
578  * This method is here to retrieve atomically accessStr and picklization.
579  */
580 void DataScopeServerTransaction::fetchAndGetAccessOfVar(const char *varName, CORBA::String_out access, SALOME::ByteVec_out data)
581 {
582   access=getAccessOfVar(varName);
583   data=fetchSerializedContent(varName);
584 }
585
586 void DataScopeServerTransaction::createRdOnlyVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
587 {
588   checkNotAlreadyExistingVar(varName);
589   PickelizedPyObjRdOnlyServer *tmp(new PickelizedPyObjRdOnlyServer(this,varName,constValue));
590   CORBA::Object_var ret(tmp->activate());
591   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
592   _vars.push_back(p);
593 }
594
595 void DataScopeServerTransaction::createRdExtVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
596 {
597   checkNotAlreadyExistingVar(varName);
598   PickelizedPyObjRdExtServer *tmp(new PickelizedPyObjRdExtServer(this,varName,constValue));
599   CORBA::Object_var ret(tmp->activate());
600   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
601   _vars.push_back(p);
602 }
603
604 void DataScopeServerTransaction::createRdExtInitVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
605 {
606   checkNotAlreadyExistingVar(varName);
607   PickelizedPyObjRdExtInitServer *tmp(new PickelizedPyObjRdExtInitServer(this,varName,constValue));
608   CORBA::Object_var ret(tmp->activate());
609   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
610   _vars.push_back(p);
611 }
612
613 void DataScopeServerTransaction::createRdWrVarInternal(const std::string& varName, const SALOME::ByteVec& constValue)
614 {
615   checkNotAlreadyExistingVar(varName);
616   PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,varName,constValue));
617   CORBA::Object_var ret(tmp->activate());
618   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(ret),tmp);
619   _vars.push_back(p);
620 }
621
622 SALOME::Transaction_ptr DataScopeServerTransaction::createRdOnlyVarTransac(const char *varName, const SALOME::ByteVec& constValue)
623 {
624   checkNotAlreadyExistingVar(varName);
625   TransactionRdOnlyVarCreate *ret(new TransactionRdOnlyVarCreate(this,varName,constValue));
626   CORBA::Object_var obj(ret->activate());
627   return SALOME::Transaction::_narrow(obj);
628 }
629
630 SALOME::Transaction_ptr DataScopeServerTransaction::createRdExtVarTransac(const char *varName, const SALOME::ByteVec& constValue)
631 {
632   checkNotAlreadyExistingVar(varName);
633   TransactionRdExtVarCreate *ret(new TransactionRdExtVarCreate(this,varName,constValue));
634   CORBA::Object_var obj(ret->activate());
635   return SALOME::Transaction::_narrow(obj);
636 }
637
638 SALOME::Transaction_ptr DataScopeServerTransaction::createRdExtInitVarTransac(const char *varName, const SALOME::ByteVec& constValue)
639 {
640   checkNotAlreadyExistingVar(varName);
641   TransactionRdExtInitVarCreate *ret(new TransactionRdExtInitVarCreate(this,varName,constValue));
642   CORBA::Object_var obj(ret->activate());
643   return SALOME::Transaction::_narrow(obj);
644 }
645
646 SALOME::Transaction_ptr DataScopeServerTransaction::createRdWrVarTransac(const char *varName, const SALOME::ByteVec& constValue)
647 {
648   checkNotAlreadyExistingVar(varName);
649   TransactionRdWrVarCreate *ret(new TransactionRdWrVarCreate(this,varName,constValue));
650   CORBA::Object_var obj(ret->activate());
651   return SALOME::Transaction::_narrow(obj);
652 }
653
654 void DataScopeServerTransaction::addWaitKey(KeyWaiter *kw)
655 {
656   if(!kw)
657     throw Exception("DataScopeServerTransaction::addWaitKey : NULL input object !");
658   _waiting_keys.push_back(kw);
659 }
660
661 void DataScopeServerTransaction::pingKey(PyObject *keyObj)
662 {
663   std::size_t ii(0);
664   // this part does nothing except to be sure that in notify key all will be OK.
665   PyObject *args(PyTuple_New(1));
666   PyTuple_SetItem(args,0,keyObj); Py_XINCREF(keyObj);
667   for(std::list< KeyWaiter *>::iterator it=_waiting_keys.begin();it!=_waiting_keys.end();it++,ii++)
668     {
669       PyObject *waitKey((*it)->getKeyPyObj());
670       PyObject *meth(PyObject_GetAttrString(keyObj,"__eq__"));
671       if(!meth)
672       {
673           std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " no __eq__ in pyobj !";
674           throw Exception(oss.str());
675       }
676       PyObject *res(PyObject_CallObject(meth,args));
677       Py_XDECREF(meth);
678       if(res==NULL)
679         {
680           std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " error during cmp(k,wk[i]) !";
681           throw Exception(oss.str());
682         }
683       PyBool_Check(res);
684       if(PyErr_Occurred())
685         {
686           std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " error during interpretation of cmp(k,wk[i]) !";
687           throw Exception(oss.str());
688         }
689       Py_XDECREF(res);
690     }
691   Py_XDECREF(args);
692 }
693
694 void DataScopeServerTransaction::notifyKey(const std::string& varName, PyObject *keyObj, PyObject *valueObj)
695 {
696   std::size_t ii(0);
697   PyObject *args(PyTuple_New(1));
698   PyTuple_SetItem(args,0,keyObj); Py_XINCREF(keyObj);
699   std::list< KeyWaiter *> newList,listOfEltToWakeUp;
700   for(std::list< KeyWaiter *>::iterator it=_waiting_keys.begin();it!=_waiting_keys.end();it++,ii++)
701     {
702       if((*it)->getVarName()!=varName)
703         {
704           newList.push_back(*it);
705           continue;
706         }
707       PyObject *waitKey((*it)->getKeyPyObj());
708       PyObject *meth(PyObject_GetAttrString(keyObj,"__eq__"));
709       if(!meth)
710       {
711           std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " no __eq__ in pyobj !";
712           throw Exception(oss.str());
713       }
714       PyObject *res(PyObject_CallObject(meth,args));
715       Py_XDECREF(meth);
716       if(!PyBool_Check(res))
717       {
718           std::ostringstream oss; oss << "DataScopeServerTransaction::pingKey : for object id #" << ii << " no __eq__ in pyobj !";
719           throw Exception(oss.str());
720       }
721       if(PyErr_Occurred())
722         {
723           std::ostringstream oss; oss << "DataScopeServerTransaction::notifyKey : MAIN INTERNAL ERROR ! for object id #" << ii << " error during interpretation of cmp(k,wk[i]) !";
724           throw Exception(oss.str());
725         }
726       if(res==Py_True)
727         listOfEltToWakeUp.push_back(*it);
728       else
729         newList.push_back(*it);
730       Py_XDECREF(res);
731     }
732   Py_XDECREF(args);
733   for(std::list< KeyWaiter *>::iterator it=listOfEltToWakeUp.begin();it!=listOfEltToWakeUp.end();it++)
734     (*it)->valueJustCome(valueObj);
735   for(std::list< KeyWaiter *>::iterator it=listOfEltToWakeUp.begin();it!=listOfEltToWakeUp.end();it++)
736     (*it)->go();
737   _waiting_keys=newList;
738 }
739
740 SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarHard(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value)
741 {
742   checkVarExistingAndDict(varName);
743   TransactionAddKeyValueHard *ret(new TransactionAddKeyValueHard(this,varName,key,value));
744   CORBA::Object_var obj(ret->activate());
745   return SALOME::Transaction::_narrow(obj);
746 }
747
748 SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarErrorIfAlreadyExisting(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value)
749 {
750   checkVarExistingAndDict(varName);
751   TransactionAddKeyValueErrorIfAlreadyExisting *ret(new TransactionAddKeyValueErrorIfAlreadyExisting(this,varName,key,value));
752   CORBA::Object_var obj(ret->activate());
753   return SALOME::Transaction::_narrow(obj);
754 }
755
756 SALOME::TransactionMultiKeyAddSession_ptr DataScopeServerTransaction::addMultiKeyValueSession(const char *varName)
757 {
758   checkVarExistingAndDict(varName);
759   TransactionMultiKeyAddSession *ret(new TransactionMultiKeyAddSession(this,varName));
760   CORBA::Object_var obj(ret->activate());
761   return SALOME::TransactionMultiKeyAddSession::_narrow(obj);
762 }
763
764 SALOME::Transaction_ptr DataScopeServerTransaction::removeKeyInVarErrorIfNotAlreadyExisting(const char *varName, const SALOME::ByteVec& key)
765 {
766   checkVarExistingAndDict(varName);
767   TransactionRemoveKeyInVarErrorIfNotAlreadyExisting *ret(new TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(this,varName,key));
768   CORBA::Object_var obj(ret->activate());
769   return SALOME::Transaction::_narrow(obj);
770 }
771
772 SALOME::TransactionRdWrAccess_ptr DataScopeServerTransaction::createWorkingVarTransac(const char *varName, const SALOME::ByteVec& constValue)
773 {
774   std::string varNameCpp(varName);
775   checkNotAlreadyExistingVar(varName);
776   PickelizedPyObjRdWrServer *tmp(new PickelizedPyObjRdWrServer(this,varNameCpp,constValue));
777   CORBA::Object_var obj(tmp->activate());
778   std::pair< SALOME::BasicDataServer_var, BasicDataServer * > p(SALOME::BasicDataServer::_narrow(obj),tmp);
779   _vars.push_back(p);
780   //
781   TransactionMorphRdWrIntoRdOnly *ret(new TransactionMorphRdWrIntoRdOnly(this,varName));
782   CORBA::Object_var obj2(ret->activate());
783   return SALOME::TransactionRdWrAccess::_narrow(obj2);
784 }
785
786 SALOME::Transaction_ptr DataScopeServerTransaction::killVarTransac(const char *varName)
787 {
788   std::string varNameCpp(varName);
789   checkExistingVar(varNameCpp);
790   //
791   TransactionKillVar *ret(new TransactionKillVar(this,varName));
792   CORBA::Object_var obj2(ret->activate());
793   return SALOME::Transaction::_narrow(obj2);
794 }
795
796 SALOME::KeyWaiter_ptr DataScopeServerTransaction::waitForKeyInVar(const char *varName, const SALOME::ByteVec& keyVal)
797 {
798   PickelizedPyObjServer *pickelObj(checkVarExistingAndDict(varName));
799   KeyWaiter *ret(new KeyWaiter(pickelObj,keyVal));
800   CORBA::Object_var obj(ret->activate());//KeyWaiter instance activated inside a multithread POA contrary to all of objects in SALOMESDS in single thread !
801   return SALOME::KeyWaiter::_narrow(obj);
802 }
803
804 SALOME::KeyWaiter_ptr DataScopeServerTransaction::waitForKeyInVarAndKillIt(const char *varName, const SALOME::ByteVec& keyVal, SALOME::Transaction_out transac)
805 {
806   PickelizedPyObjServer *pickelObj(checkVarExistingAndDict(varName));
807   KeyWaiter *ret0(new KeyWaiter(pickelObj,keyVal));
808   CORBA::Object_var obj(ret0->activate());//KeyWaiter instance activated inside a multithread POA contrary to all of objects in SALOMESDS in single thread !
809   //
810   TransactionRemoveKeyInVarErrorIfNotAlreadyExisting *ret1(new TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(this,varName,keyVal));
811   CORBA::Object_var obj2(ret1->activate());
812   transac=SALOME::Transaction::_narrow(obj2);
813   //
814   return SALOME::KeyWaiter::_narrow(obj);
815 }
816
817 SALOME::ByteVec *DataScopeServerTransaction::waitForMonoThrRev(SALOME::KeyWaiter_ptr kw)
818 {
819   PortableServer::ServantBase *ret(0);
820   try
821     {
822       ret=_poa_for_key_waiter->reference_to_servant(kw);// Warning ref cnt of ret has been incremented !
823     }
824   catch(...) { ret=0; }
825   KeyWaiter *retc(dynamic_cast<KeyWaiter *>(ret));
826   if(!retc)
827     throw Exception("DataScopeServerTransaction::invokeMonoThr : internal error 1 !");
828   retc->_remove_ref();// restore the counter after _poa_for_key_waiter->reference_to_servant(kw)
829   SALOME::ByteVec *zeRet(retc->waitForMonoThr());
830   retc->enforcedRelease();
831   return zeRet;
832 }
833
834 SALOME::ByteVec *DataScopeServerTransaction::waitForAndKill(SALOME::KeyWaiter_ptr kw)
835 {
836   PortableServer::ServantBase *ret(0);
837   try
838     {
839       ret=_poa_for_key_waiter->reference_to_servant(kw);// Warning ref cnt of ret has been incremented !
840     }
841   catch(...) { ret=0; }
842   KeyWaiter *retc(dynamic_cast<KeyWaiter *>(ret));
843   if(!retc)
844     throw Exception("DataScopeServerTransaction::invokeMonoThr : internal error 1 !");
845   retc->_remove_ref();// restore the counter after _poa_for_key_waiter->reference_to_servant(kw)
846   SALOME::ByteVec *zeRet(retc->waitForAndKill());
847   retc->enforcedRelease();
848   return zeRet;
849 }
850
851 void DataScopeServerTransaction::atomicApply(const SALOME::ListOfTransaction& transactions)
852 {
853   std::size_t sz(transactions.length());
854   if(sz==0)
855     return ;
856   std::vector< AutoServantPtr<Transaction> > transactionsCpp(sz);
857   for(std::size_t i=0;i<sz;i++)
858     {
859       PortableServer::ServantBase *eltBase(0);
860       Transaction *elt(0);
861       try
862         {
863           eltBase=_poa->reference_to_servant(transactions[i]);
864           elt=dynamic_cast<Transaction *>(eltBase);
865         }
866       catch(...)
867         {
868           std::ostringstream oss; oss << "DataScopeServerTransaction::atomicApply : the elt #" << i << " is invalid !";
869           throw Exception(oss.str());
870         }
871       if(!elt)
872         {
873           std::ostringstream oss; oss << "DataScopeServerTransaction::atomicApply : the elt #" << i << " is null ?";
874           throw Exception(oss.str());
875         }
876       elt->_remove_ref();
877       transactionsCpp[i]=elt;
878     }
879   {// important do not merge loops ! 
880     std::vector<TrustTransaction> transactions2(sz);
881     bool mustRollback(true);
882     for(std::size_t i=0;i<sz;i++)
883       transactions2[i].setTransaction(transactionsCpp[i],&mustRollback);
884     for(std::size_t i=0;i<sz;i++)
885       transactions2[i].operate();
886     mustRollback=false;
887   }
888   for(std::size_t i=0;i<sz;i++)
889     transactionsCpp[i]->notify();
890 }
891
892 DataScopeServerTransaction::~DataScopeServerTransaction()
893 {
894 }
895
896 SALOME::RequestSwitcher_ptr DataScopeServerTransaction::getRequestSwitcher()
897 {
898   if(_rs.isNull())
899     {
900       _rs=new RequestSwitcher(_orb,this);
901     }
902   CORBA::Object_var obj(_rs->activate());
903   return SALOME::RequestSwitcher::_narrow(obj);
904 }