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