Salome HOME
CCAR: remove some memory leaks in SALOMEDS CORBA interface
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager_i.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  File   : SALOMEDS_StudyManager_i.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include "utilities.h"
27 #include "SALOME_LifeCycleCORBA.hxx"
28 #include "SALOMEDS_StudyManager_i.hxx"
29 #include "SALOMEDS_Study_i.hxx"
30 #include "SALOMEDS_SComponent_i.hxx"
31 #include "SALOMEDS_Driver_i.hxx"
32 #include "SALOMEDS.hxx"
33
34 #include "SALOMEDSImpl_Study.hxx"
35 #include "SALOMEDSImpl_SObject.hxx"
36 #include "SALOMEDSImpl_SComponent.hxx"
37 #include "SALOMEDSImpl_AttributeIOR.hxx"
38
39 #include "Utils_CorbaException.hxx"
40 #include "Utils_ExceptHandlers.hxx"
41 #include "Basics_Utils.hxx"
42 #include "SALOME_GenericObj_i.hh"
43
44 #include <sstream>
45 #include <vector>
46 #include <map>
47
48 #ifdef WIN32
49 #include <process.h>
50 #else
51 #include <sys/types.h>
52 #include <unistd.h>
53 #endif
54
55 using namespace std;
56
57 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
58 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
59
60 static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
61
62 static std::map<int, PortableServer::POA_ptr> _mapOfPOA;
63
64 //============================================================================
65 /*! Function : SALOMEDS_StudyManager_i
66  *  Purpose  : SALOMEDS_StudyManager_i constructor
67  */
68 //============================================================================
69 SALOMEDS_StudyManager_i::SALOMEDS_StudyManager_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr thePOA)
70 {
71   _orb = CORBA::ORB::_duplicate(orb);
72   _poa = PortableServer::POA::_duplicate(thePOA);
73   _name_service = new SALOME_NamingService(_orb);
74   // Study directory creation in the naming service : to register all
75   // open studies in the session
76   _name_service->Create_Directory("/Study");
77   _impl = new SALOMEDSImpl_StudyManager;
78   _factory = new SALOMEDS_DriverFactory_i(_orb);
79 }
80
81 //============================================================================
82 /*! Function : ~SALOMEDS_StudyManager_i
83  *  Purpose  : SALOMEDS_StudyManager_i destructor
84  */
85 //============================================================================
86 SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
87 {
88   // Destroy directory to register open studies
89   _name_service->Destroy_Directory("/Study");
90   delete _name_service;
91   delete _factory;
92   delete _impl;
93 }
94
95 //============================================================================
96 /*! Function : register_name
97  *  Purpose  : Register the study Manager in the naming service under the
98  *             context name
99  */
100 //============================================================================
101 void SALOMEDS_StudyManager_i::register_name(const char * name)
102 {
103   SALOMEDS::StudyManager_var aManager(_this());
104   _name_service->Register(aManager.in(), name);
105 }
106
107
108 //============================================================================
109 /*! Function : NewStudy
110  *  Purpose  : Create a New Study of name study_name
111  */
112 //============================================================================
113 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
114 {
115   SALOMEDS::Locker lock;
116
117   SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
118   if(!aStudyImpl) {
119     MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
120     return SALOMEDS::Study::_nil();
121   }
122
123   MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
124
125   SALOMEDS_Study_i *Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
126   SALOMEDS::Study_var Study = Study_servant->_this();
127
128   // Register study in the naming service
129   // Path to acces the study
130   if(!_name_service->Change_Directory("/Study"))
131       MESSAGE( "Unable to access the study directory" )
132   else
133       _name_service->Register(Study, study_name);
134
135   // Assign the value of the IOR in the study->root
136   CORBA::String_var IORStudy = _orb->object_to_string(Study);
137
138   aStudyImpl->SetTransientReference((char*)IORStudy.in());
139
140   _mapOfPOA[Study->StudyId()] = _poa;
141
142   return Study._retn();
143 }
144
145 //============================================================================
146 /*! Function : Open
147  *  Purpose  : Open a Study from it's persistent reference
148  */
149 //============================================================================
150 SALOMEDS::Study_ptr  SALOMEDS_StudyManager_i::Open(const char* aUrl)
151      throw(SALOME::SALOME_Exception)
152 {
153   SALOMEDS::Locker lock;
154
155   Unexpect aCatch(SalomeException);
156   MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
157
158   SALOMEDSImpl_Study* aStudyImpl = _impl->Open(string(aUrl));
159
160   if ( !aStudyImpl )
161     THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
162
163   MESSAGE("Open : Creating the CORBA servant holding it... ");
164
165   // Temporary aStudyUrl in place of study name
166   SALOMEDS_Study_i * Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
167   SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
168
169   // Assign the value of the IOR in the study->root
170   CORBA::String_var IORStudy = _orb->object_to_string(Study);
171   aStudyImpl->SetTransientReference((char*)IORStudy.in());
172
173   // Register study in the naming service
174   // Path to acces the study
175   if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
176   else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str()));
177
178   return Study;
179 }
180
181
182
183 //============================================================================
184 /*! Function : Close
185  *  Purpose  : Close a study.
186  *             If the study hasn't been saved, ask the user to confirm the
187  *             close action without saving
188  */
189 //============================================================================
190 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
191 {
192   SALOMEDS::Locker lock;
193
194   if(aStudy->_is_nil()) return;
195
196   // Destroy study name in the naming service
197   if(_name_service->Change_Directory("/Study")){
198     CORBA::String_var aString(aStudy->Name());
199     _name_service->Destroy_Name(aString.in());
200   }
201
202   SALOMEDS::unlock();
203   aStudy->Close();
204   SALOMEDS::lock();
205 }
206
207 //============================================================================
208 /*! Function : Save
209  *  Purpose  : Save a Study to it's persistent reference
210  */
211 //============================================================================
212 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
213 {
214   SALOMEDS::Locker lock;
215
216   if(aStudy->_is_nil()) {
217     MESSAGE("Save error: Study is null");
218     return false;
219   }
220
221   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
222   return _impl->Save(aStudyImpl, _factory, theMultiFile);
223 }
224
225 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
226 {
227   SALOMEDS::Locker lock;
228
229   if(aStudy->_is_nil()) {
230     MESSAGE("SaveASCII error: Study is null");
231     return false;
232   }
233
234   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
235   return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
236 }
237
238 //=============================================================================
239 /*! Function : SaveAs
240  *  Purpose  : Save a study to the persistent reference aUrl
241  */
242 //============================================================================
243 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
244 {
245   SALOMEDS::Locker lock;
246
247   if(aStudy->_is_nil()) {
248     MESSAGE("SaveASCII error: Study is null");
249     return false;
250   }
251
252   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
253   return _impl->SaveAs(string(aUrl), aStudyImpl, _factory, theMultiFile);
254 }
255
256 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
257 {
258   SALOMEDS::Locker lock;
259
260   if(aStudy->_is_nil()) {
261     MESSAGE("SaveASCII error: Study is null");
262     return false;
263   }
264
265   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
266   return _impl->SaveAsASCII(string(aUrl), aStudyImpl, _factory, theMultiFile);
267 }
268
269 //============================================================================
270 /*! Function : GetOpenStudies
271  *  Purpose  : Get name list of open studies in the session
272  */
273 //============================================================================
274 SALOMEDS::ListOfOpenStudies*  SALOMEDS_StudyManager_i::GetOpenStudies()
275 {
276   SALOMEDS::Locker lock;
277
278   vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
279   int aLength = anOpened.size();
280
281   SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
282   _list_open_studies->length(aLength);
283
284   if(!aLength)
285     {
286       MESSAGE("No active study in this session");
287     }
288   else
289     {
290       for (unsigned int ind=0; ind < aLength; ind++)
291         {
292           _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
293           SCRUTE(_list_open_studies[ind]) ;
294         }
295     }
296   return _list_open_studies._retn();
297 }
298
299 //============================================================================
300 /*! Function : GetStudyByName
301  *  Purpose  : Get a study from its name
302  */
303 //============================================================================
304 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
305 {
306   SALOMEDS::Locker lock;
307
308   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(string(aStudyName));
309
310   if (!aStudyImpl)
311   {
312     MESSAGE(_impl->GetErrorCode().c_str());
313     return SALOMEDS::Study::_nil();
314   }
315
316   SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
317   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this());
318
319   return aStudy._retn();
320 }
321
322 //============================================================================
323 /*! Function : GetStudyByID
324  *  Purpose  : Get a study from its ID
325  */
326 //============================================================================
327 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
328 {
329   SALOMEDS::Locker lock;
330
331   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
332
333   if (!aStudyImpl)
334   {
335     MESSAGE(_impl->GetErrorCode().c_str());
336     return SALOMEDS::Study::_nil();
337   }
338
339   SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
340   CORBA::Object_var obj = aStudy_servant->_this();
341   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(obj);
342
343   return aStudy._retn();
344 }
345
346
347 //============================================================================
348 /*! Function : CanCopy
349  *  Purpose  :
350  */
351 //============================================================================
352 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
353 {
354   SALOMEDS::Locker lock;
355
356   SALOMEDS::Study_var aStudy = theObject->GetStudy();
357   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
358   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
359
360   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
361   bool ret = _impl->CanCopy(anObject, aDriver);
362   delete aDriver;
363   return ret;
364 }
365
366 //============================================================================
367 /*! Function : Copy
368  *  Purpose  :
369  */
370 //============================================================================
371 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
372 {
373   SALOMEDS::Locker lock;
374
375   SALOMEDS::Study_var aStudy = theObject->GetStudy();
376   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
377   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
378
379   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
380   bool ret = _impl->Copy(anObject, aDriver);
381   delete aDriver;
382   return ret;
383 }
384
385 //============================================================================
386 /*! Function : CanPaste
387  *  Purpose  :
388  */
389 //============================================================================
390 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
391 {
392   SALOMEDS::Locker lock;
393
394   SALOMEDS::Study_var aStudy = theObject->GetStudy();
395   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
396   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
397
398   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
399   bool ret = _impl->CanPaste(anObject, aDriver);
400   delete aDriver;
401   return ret;
402 }
403
404 //============================================================================
405 /*! Function : Paste
406  *  Purpose  :
407  */
408 //============================================================================
409 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
410      throw(SALOMEDS::StudyBuilder::LockProtection)
411 {
412   SALOMEDS::Locker lock;
413
414   Unexpect aCatch(LockProtection);
415   SALOMEDS::Study_var aStudy = theObject->GetStudy();
416
417   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
418   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
419   SALOMEDSImpl_SObject aNewSO;
420
421   try {
422     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
423     aNewSO =  _impl->Paste(anObject, aDriver);
424     delete aDriver;
425   }
426   catch (...) {
427     throw SALOMEDS::StudyBuilder::LockProtection();
428   }
429
430   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
431   return so._retn();
432 }
433
434
435 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
436 {
437   SALOMEDS_Driver_i* driver = NULL;
438
439   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
440   if(!aSCO.IsNull()) {
441     string IOREngine = aSCO.GetIOR();
442     if(!IOREngine.empty()) {
443       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
444       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
445       driver = new SALOMEDS_Driver_i(Engine, orb);
446     }
447   }
448
449   return driver;
450 }
451
452 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
453   if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
454   return PortableServer::POA::_nil();
455 }
456
457 CORBA::Long SALOMEDS_StudyManager_i::getPID()
458
459 #ifdef WIN32
460   return (CORBA::Long)_getpid();
461 #else
462   return (CORBA::Long)getpid();
463 #endif
464 }
465
466 void SALOMEDS_StudyManager_i::ShutdownWithExit()
467 {
468   exit( EXIT_SUCCESS );
469 }
470
471 //===========================================================================
472 //   PRIVATE FUNCTIONS
473 //===========================================================================
474 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
475 {
476 #ifdef WIN32
477   long pid = (long)_getpid();
478 #else
479   long pid = (long)getpid();
480 #endif
481   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
482   return reinterpret_cast<CORBA::LongLong>(_impl);
483 }
484
485 //===========================================================================
486 namespace SALOMEDS
487 {
488   PortableServer::ServantBase_var
489   GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
490   {
491     if(CORBA::is_nil(theObject))
492       return NULL;
493     try{
494       return thePOA->reference_to_servant(theObject);
495     }catch(...){
496       return NULL;
497     }
498   }
499
500 }
501
502 //===========================================================================