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