Salome HOME
remover traces on POA
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_SObject_i.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SALOMEDS_SObject_i.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "utilities.h"
28 #include "SALOMEDS_SObject_i.hxx"
29 #include "SALOMEDS_SComponent_i.hxx"
30 #include "SALOMEDS_GenericAttribute_i.hxx"
31 #include "SALOMEDS_StudyManager_i.hxx"
32 #include "SALOMEDS.hxx"
33 #include "SALOMEDSImpl_GenericAttribute.hxx"
34 #include "SALOMEDSImpl_SComponent.hxx"
35 #include "SALOMEDSImpl_Study.hxx"
36 #include "SALOMEDSImpl_AttributeIOR.hxx"
37 #include "Basics_Utils.hxx"
38
39 #include <map>
40
41 #ifdef WIN32
42 #include <process.h>
43 #else
44 #include <sys/types.h>
45 #include <unistd.h>
46 #endif
47
48 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::New(const SALOMEDSImpl_SObject& theImpl, CORBA::ORB_ptr theORB)
49 {
50   SALOMEDS_SObject_i* so_servant = new SALOMEDS_SObject_i(theImpl, theORB);
51
52   return so_servant->_this();
53 }
54
55
56 //============================================================================
57 /*! Function : constructor
58  *  Purpose  :
59  */
60 //============================================================================
61 SALOMEDS_SObject_i::SALOMEDS_SObject_i(const SALOMEDSImpl_SObject& impl, CORBA::ORB_ptr orb) :
62   GenericObj_i(SALOMEDS_StudyManager_i::GetThePOA())
63 {
64   _impl = 0;
65   if(!impl.IsNull()) {
66      if(impl.IsComponent()) {
67          SALOMEDSImpl_SComponent sco = impl;
68          _impl = sco.GetPersistentCopy();       
69      }
70      else {
71          _impl = impl.GetPersistentCopy();
72      }
73   }
74   _orb = CORBA::ORB::_duplicate(orb);
75    //SALOME::GenericObj_i::myPOA = SALOMEDS_StudyManager_i::GetPOA(GetStudy());
76 }
77
78
79 //============================================================================
80 /*! Function : destructor
81  *  Purpose  :
82  */
83 //============================================================================
84 SALOMEDS_SObject_i::~SALOMEDS_SObject_i()
85 {
86    if(_impl) delete _impl;    
87 }
88
89 //============================================================================
90 /*!
91   \brief Get default POA for the servant object.
92
93   This function is implicitly called from "_this()" function.
94   Default POA can be set via the constructor.
95
96   \return reference to the default POA for the servant
97 */
98 //============================================================================
99 PortableServer::POA_ptr SALOMEDS_SObject_i::_default_POA()
100 {
101   myPOA = PortableServer::POA::_duplicate(SALOMEDS_StudyManager_i::GetThePOA());
102   //MESSAGE("SALOMEDS_SObject_i::_default_POA: " << myPOA);
103   return PortableServer::POA::_duplicate(myPOA);
104 }
105
106 //================================================================================
107 /*!
108  * \brief Returns true if the %SObject does not belong to any %Study
109  */
110 //================================================================================
111
112 CORBA::Boolean SALOMEDS_SObject_i::IsNull()
113 {
114   SALOMEDS::Locker lock;
115   return !_impl || _impl->IsNull();
116 }
117
118 //============================================================================
119 /*! Function :GetID
120  *  Purpose  :
121  */
122 //============================================================================
123 char* SALOMEDS_SObject_i::GetID()
124 {
125   SALOMEDS::Locker lock;
126   return CORBA::string_dup(_impl->GetID().c_str());
127 }
128
129 //============================================================================
130 /*! Function : GetFatherComponent
131  *  Purpose  :
132  */
133 //============================================================================
134 SALOMEDS::SComponent_ptr SALOMEDS_SObject_i::GetFatherComponent()
135 {
136   SALOMEDS::Locker lock;
137   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (_impl->GetFatherComponent(), _orb);
138   return sco._retn();
139 }
140
141 //============================================================================
142 /*! Function : GetFather
143  *  Purpose  :
144  */
145 //============================================================================
146 SALOMEDS::SObject_ptr SALOMEDS_SObject_i::GetFather()
147 {
148   SALOMEDS::Locker lock;
149   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (_impl->GetFather(), _orb);
150   return so._retn();
151 }
152
153 //============================================================================
154 /*! Function :
155  *  Purpose  :
156  */
157 //============================================================================
158 SALOMEDS::Study_ptr SALOMEDS_SObject_i::GetStudy()
159 {
160   SALOMEDS::Locker lock;
161   SALOMEDSImpl_Study* aStudy = _impl->GetStudy();
162   if(!aStudy) {
163     MESSAGE("Problem GetStudy");
164     return SALOMEDS::Study::_nil();
165   }
166
167   std::string IOR = aStudy->GetTransientReference();
168   CORBA::Object_var obj = _orb->string_to_object(IOR.c_str());
169   SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(obj) ;
170   ASSERT(!CORBA::is_nil(Study));
171   return SALOMEDS::Study::_duplicate(Study);
172 }
173
174 //============================================================================
175 /*! Function : FindAttribute
176  *  Purpose  : Find attribute of given type on this SObject
177  */
178 //============================================================================
179 CORBA::Boolean SALOMEDS_SObject_i::FindAttribute (SALOMEDS::GenericAttribute_out anAttribute,
180                                                   const char* aTypeOfAttribute)
181 {
182   SALOMEDS::Locker lock;
183   DF_Attribute* anAttr = NULL;
184   if(_impl->FindAttribute(anAttr, (char*)aTypeOfAttribute)) {
185     anAttribute = SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb);
186     return true;
187   }
188
189   return false;
190 }
191
192 //============================================================================
193 /*! Function : GetAllAttributes
194  *  Purpose  : Returns list of all attributes for this sobject
195  */
196 //============================================================================
197
198 SALOMEDS::ListOfAttributes* SALOMEDS_SObject_i::GetAllAttributes()
199 {
200   SALOMEDS::Locker lock;
201   std::vector<DF_Attribute*> aSeq = _impl->GetAllAttributes();
202   SALOMEDS::ListOfAttributes_var SeqOfAttr = new SALOMEDS::ListOfAttributes;
203   int length = aSeq.size();
204
205   SeqOfAttr->length(length);
206
207   if (length != 0) {
208     for(int i = 0; i < length; i++) {
209       SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i]);
210       SALOMEDS::GenericAttribute_var anAttribute;
211       anAttribute = SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb);
212       if (!CORBA::is_nil(anAttribute)) {
213         SeqOfAttr[i] = anAttribute;
214       }
215     }
216   }
217   return SeqOfAttr._retn();
218 }
219
220
221 //============================================================================
222 /*! Function : ReferencedObject
223  *  Purpose  :
224  */
225 //============================================================================
226 CORBA::Boolean SALOMEDS_SObject_i::ReferencedObject(SALOMEDS::SObject_out obj)
227 {
228   SALOMEDS::Locker lock;
229   SALOMEDSImpl_SObject aRefObj;
230   if(!_impl->ReferencedObject(aRefObj)) return false;
231
232   obj = SALOMEDS_SObject_i::New (aRefObj, _orb);
233   return true;
234 }
235
236 //============================================================================
237 /*! Function : FindSubObject
238  *  Purpose  :
239  */
240 //============================================================================
241 CORBA::Boolean SALOMEDS_SObject_i::FindSubObject(CORBA::Long atag, SALOMEDS::SObject_out obj)
242 {
243   SALOMEDS::Locker lock;
244   SALOMEDSImpl_SObject aSubObj;
245   if(!_impl->FindSubObject(atag, aSubObj)) return false;
246
247   obj = SALOMEDS_SObject_i::New (aSubObj, _orb);
248   return true;
249
250 }
251
252 //============================================================================
253 /*! Function : Name
254  *  Purpose  : gets a name
255  */
256 //============================================================================
257 char* SALOMEDS_SObject_i::Name()
258 {
259   SALOMEDS::Locker lock;
260   return CORBA::string_dup(_impl->Name().c_str());
261 }
262
263 //============================================================================
264 /*! Function : Name
265  *  Purpose  : sets a name
266  */
267 //============================================================================
268 void  SALOMEDS_SObject_i::Name(const char* name)
269 {
270   SALOMEDS::Locker lock;
271   std::string aName((char*)name);
272   _impl->Name(aName);
273 }
274
275 //============================================================================
276 /*! Function : Tag
277  *  Purpose  :
278  */
279 //============================================================================
280 CORBA::Short SALOMEDS_SObject_i::Tag()
281 {
282   SALOMEDS::Locker lock;
283   return _impl->Tag();
284 }
285
286 //============================================================================
287 /*! Function : GetLastChildTag
288  *  Purpose  :
289  */
290 //============================================================================
291 CORBA::Short SALOMEDS_SObject_i::GetLastChildTag()
292 {
293   SALOMEDS::Locker lock;
294   return (CORBA::Short) _impl->GetLastChildTag();
295 }
296
297 //============================================================================
298 /*! Function : Depth
299  *  Purpose  :
300  */
301 //============================================================================
302 CORBA::Short SALOMEDS_SObject_i::Depth()
303 {
304   SALOMEDS::Locker lock;
305   return _impl->Depth();
306 }
307
308 //============================================================================
309 /*! Function : GetObject
310  *  Purpose  :
311  */
312 //============================================================================
313 CORBA::Object_ptr SALOMEDS_SObject_i::GetObject()
314 {
315   SALOMEDS::Locker lock;
316   CORBA::Object_ptr obj = CORBA::Object::_nil();
317   try {
318     std::string IOR = _impl->GetIOR();
319     char* c_ior = CORBA::string_dup(IOR.c_str());
320     obj = _orb->string_to_object(c_ior);
321     CORBA::string_free(c_ior);
322   } catch(...) {}
323   return obj;
324 }
325
326 //============================================================================
327 /*! Function : GetName
328  *  Purpose  :
329  */
330 //============================================================================
331 char* SALOMEDS_SObject_i::GetName()
332 {
333   SALOMEDS::Locker lock;
334   CORBA::String_var aStr = CORBA::string_dup(_impl->GetName().c_str());
335   return aStr._retn();
336 }
337
338 //============================================================================
339 /*! Function : GetComment
340  *  Purpose  :
341  */
342 //============================================================================
343 char* SALOMEDS_SObject_i::GetComment()
344 {
345   SALOMEDS::Locker lock;
346   CORBA::String_var aStr = CORBA::string_dup(_impl->GetComment().c_str());
347   return aStr._retn();
348 }
349
350 //============================================================================
351 /*! Function : GetIOR
352  *  Purpose  :
353  */
354 //============================================================================
355 char* SALOMEDS_SObject_i::GetIOR()
356 {
357   SALOMEDS::Locker lock;
358   CORBA::String_var aStr = CORBA::string_dup(_impl->GetIOR().c_str());
359   return aStr._retn();
360 }
361
362 //============================================================================
363 /*! Function : SetAttrString
364  *  Purpose  :
365  */
366 //============================================================================
367 void SALOMEDS_SObject_i::SetAttrString(const char* name, const char* value)
368 {
369   SALOMEDS::Locker lock;
370   _impl->SetAttrString(name,value);
371 }
372
373 //===========================================================================
374 //   PRIVATE FUNCTIONS
375 //===========================================================================
376 CORBA::LongLong SALOMEDS_SObject_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
377 {
378 #ifdef WIN32
379   long pid = (long)_getpid();
380 #else
381   long pid = (long)getpid();
382 #endif
383   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
384   return reinterpret_cast<CORBA::LongLong>(_impl);
385 }