Salome HOME
Fix SIGSEGV in data server, in local driver implementation, if some component does...
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager_i.cxx
index 1bcaf8caa05b8e323aad1f724a23ec6fde43d7ec..71415bbbbba02f43f3f8e1fc3f09b76b6a458b17 100644 (file)
@@ -1,26 +1,29 @@
-// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-// 
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either 
+// License as published by the Free Software Foundation; either
 // version 2.1 of the License.
-// 
-// This library is distributed in the hope that it will be useful 
-// but WITHOUT ANY WARRANTY; without even the implied warranty of 
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  File   : SALOMEDS_StudyManager_i.cxx
 //  Author : Sergey RUIN
 //  Module : SALOME
-
+//
 #include "utilities.h"
 #include "SALOME_LifeCycleCORBA.hxx"
 #include "SALOMEDS_StudyManager_i.hxx"
 #include "SALOMEDSImpl_SComponent.hxx"
 #include "SALOMEDSImpl_AttributeIOR.hxx"
 
-#include <TColStd_HArray1OfCharacter.hxx>
-#include <TCollection_ExtendedString.hxx>
-#include <TCollection_AsciiString.hxx>
-
 #include "Utils_CorbaException.hxx"
+#include "Utils_ExceptHandlers.hxx"
+#include "Basics_Utils.hxx"
+#include "SALOME_GenericObj_i.hh"
 
-#include <strstream>
+#include <sstream>
+#include <vector>
 #include <map>
-using namespace std;
 
 #ifdef WIN32
 #include <process.h>
@@ -51,16 +53,10 @@ using namespace std;
 #include <unistd.h>
 #endif
 
-#include "OpUtil.hxx"
-
-#include "SALOME_GenericObj_i.hh"
-
-#include "Utils_ExceptHandlers.hxx"
-
 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
 
-static SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb);
+static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
 
 static std::map<int, PortableServer::POA_ptr> _mapOfPOA;
 
@@ -90,7 +86,9 @@ SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
 {
   // Destroy directory to register open studies
   _name_service->Destroy_Directory("/Study");
+  delete _name_service;
   delete _factory;
+  delete _impl;
 }
 
 //============================================================================
@@ -99,7 +97,7 @@ SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
  *             context name
  */
 //============================================================================
-void SALOMEDS_StudyManager_i::register_name(char * name)
+void SALOMEDS_StudyManager_i::register_name(const char * name)
 {
   SALOMEDS::StudyManager_var aManager(_this());
   _name_service->Register(aManager.in(), name);
@@ -115,16 +113,17 @@ SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
 {
   SALOMEDS::Locker lock;
 
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->NewStudy(TCollection_AsciiString((char*)study_name));
-  if(aStudyImpl.IsNull()) {
+  SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
+  if(!aStudyImpl) {
     MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
     return SALOMEDS::Study::_nil();
   }
 
   MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
 
-  SALOMEDS_Study_i *Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
-  SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
+  SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
+  PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
+  SALOMEDS::Study_var Study = Study_servant->_this();
 
   // Register study in the naming service
   // Path to acces the study
@@ -134,13 +133,13 @@ SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
       _name_service->Register(Study, study_name);
 
   // Assign the value of the IOR in the study->root
-  const char*  IORStudy = _orb->object_to_string(Study);
+  CORBA::String_var IORStudy = _orb->object_to_string(Study);
 
-  aStudyImpl->SetTransientReference((char*)IORStudy);
+  aStudyImpl->SetTransientReference((char*)IORStudy.in());
 
   _mapOfPOA[Study->StudyId()] = _poa;
 
-  return Study;
+  return Study._retn();
 }
 
 //============================================================================
@@ -156,24 +155,30 @@ SALOMEDS::Study_ptr  SALOMEDS_StudyManager_i::Open(const char* aUrl)
   Unexpect aCatch(SalomeException);
   MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
 
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->Open(TCollection_AsciiString((char*)aUrl));
+  SALOMEDSImpl_Study* aStudyImpl = _impl->Open(std::string(aUrl));
+
+  if ( !aStudyImpl )
+    THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
 
   MESSAGE("Open : Creating the CORBA servant holding it... ");
 
   // Temporary aStudyUrl in place of study name
-  SALOMEDS_Study_i * Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
-  SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
+  SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
+  PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
+  SALOMEDS::Study_var Study = Study_servant->_this();
 
   // Assign the value of the IOR in the study->root
   CORBA::String_var IORStudy = _orb->object_to_string(Study);
-  aStudyImpl->SetTransientReference((char*)IORStudy);
+  aStudyImpl->SetTransientReference((char*)IORStudy.in());
+
+  _mapOfPOA[Study->StudyId()] = _poa;
 
   // Register study in the naming service
   // Path to acces the study
   if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
-  else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().ToCString()));
+  else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str()));
 
-  return Study;
+  return Study._retn();
 }
 
 
@@ -200,6 +205,14 @@ void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
   SALOMEDS::unlock();
   aStudy->Close();
   SALOMEDS::lock();
+
+  //remove study servant
+  PortableServer::POA_ptr poa=GetPOA(aStudy);
+  PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
+  PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
+  poa->deactivate_object(anObjectId.in());
+  aservant->_remove_ref(); // decrement for the call to reference_to_servant
+  aservant->_remove_ref(); // to delete the object
 }
 
 //============================================================================
@@ -216,7 +229,7 @@ CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::
     return false;
   }
 
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
   return _impl->Save(aStudyImpl, _factory, theMultiFile);
 }
 
@@ -229,7 +242,7 @@ CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CO
     return false;
   }
 
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
   return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
 }
 
@@ -247,8 +260,8 @@ CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study
     return false;
   }
 
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
-  return _impl->SaveAs(TCollection_AsciiString((char*)aUrl), aStudyImpl, _factory, theMultiFile);
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
+  return _impl->SaveAs(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
 }
 
 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
@@ -260,8 +273,8 @@ CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::
     return false;
   }
 
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
-  return _impl->SaveAsASCII(TCollection_AsciiString((char*)aUrl), aStudyImpl, _factory, theMultiFile);
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
+  return _impl->SaveAsASCII(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
 }
 
 //============================================================================
@@ -273,8 +286,8 @@ SALOMEDS::ListOfOpenStudies*  SALOMEDS_StudyManager_i::GetOpenStudies()
 {
   SALOMEDS::Locker lock;
 
-  Handle(TColStd_HSequenceOfTransient) anOpened = _impl->GetOpenStudies();
-  int aLength = anOpened->Length();
+  std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
+  int aLength = anOpened.size();
 
   SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
   _list_open_studies->length(aLength);
@@ -285,12 +298,11 @@ SALOMEDS::ListOfOpenStudies*  SALOMEDS_StudyManager_i::GetOpenStudies()
     }
   else
     {
-      for (unsigned int ind=1; ind <= aLength; ind++)
-       {
-         Handle(SALOMEDSImpl_Study) aStudy = Handle(SALOMEDSImpl_Study)::DownCast(anOpened->Value(ind));
-         _list_open_studies[ind-1] = CORBA::string_dup(aStudy->Name().ToCString());
-         SCRUTE(_list_open_studies[ind-1]) ;
-       }
+      for (unsigned int ind=0; ind < aLength; ind++)
+        {
+          _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
+          SCRUTE(_list_open_studies[ind]) ;
+        }
     }
   return _list_open_studies._retn();
 }
@@ -304,19 +316,16 @@ SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyNa
 {
   SALOMEDS::Locker lock;
 
-  Handle(SALOMEDSImpl_Study) aStudyImpl =
-    _impl->GetStudyByName(TCollection_AsciiString((char*)aStudyName));
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(std::string(aStudyName));
 
-  if (aStudyImpl.IsNull())
+  if (!aStudyImpl)
   {
-    MESSAGE(_impl->GetErrorCode().ToCString());
+    MESSAGE(_impl->GetErrorCode().c_str());
     return SALOMEDS::Study::_nil();
   }
 
-  SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
-  SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this());
-
-  return aStudy._retn();
+  SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
+  return aStudy_servant->_this();
 }
 
 //============================================================================
@@ -328,18 +337,16 @@ SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
 {
   SALOMEDS::Locker lock;
 
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudyID);
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
 
-  if (aStudyImpl.IsNull())
+  if (!aStudyImpl)
   {
-    MESSAGE(_impl->GetErrorCode().ToCString());
+    MESSAGE(_impl->GetErrorCode().c_str());
     return SALOMEDS::Study::_nil();
   }
 
-  SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
-  SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this());
-
-  return aStudy._retn();
+  SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
+  return aStudy_servant->_this();
 }
 
 
@@ -353,8 +360,9 @@ CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
   SALOMEDS::Locker lock;
 
   SALOMEDS::Study_var aStudy = theObject->GetStudy();
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
-  Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
+  CORBA::String_var anID = theObject->GetID();
+  SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
 
   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
   bool ret = _impl->CanCopy(anObject, aDriver);
@@ -372,8 +380,9 @@ CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
   SALOMEDS::Locker lock;
 
   SALOMEDS::Study_var aStudy = theObject->GetStudy();
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
-  Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
+  CORBA::String_var anID = theObject->GetID();
+  SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
 
   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
   bool ret = _impl->Copy(anObject, aDriver);
@@ -391,8 +400,9 @@ CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject
   SALOMEDS::Locker lock;
 
   SALOMEDS::Study_var aStudy = theObject->GetStudy();
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
-  Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
+  CORBA::String_var anID = theObject->GetID();
+  SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
 
   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
   bool ret = _impl->CanPaste(anObject, aDriver);
@@ -413,9 +423,10 @@ SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theOb
   Unexpect aCatch(LockProtection);
   SALOMEDS::Study_var aStudy = theObject->GetStudy();
 
-  Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
-  Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
-  Handle(SALOMEDSImpl_SObject) aNewSO;
+  SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
+  CORBA::String_var anID = theObject->GetID();
+  SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
+  SALOMEDSImpl_SObject aNewSO;
 
   try {
     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
@@ -431,15 +442,15 @@ SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theOb
 }
 
 
-SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
+SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
 {
   SALOMEDS_Driver_i* driver = NULL;
 
-  Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
+  SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
   if(!aSCO.IsNull()) {
-    TCollection_AsciiString IOREngine = aSCO->GetIOR();
-    if(!IOREngine.IsEmpty()) {
-      CORBA::Object_var obj = orb->string_to_object(IOREngine.ToCString());
+    std::string IOREngine = aSCO.GetIOR();
+    if(!IOREngine.empty()) {
+      CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
       driver = new SALOMEDS_Driver_i(Engine, orb);
     }
@@ -453,38 +464,37 @@ PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_pt
   return PortableServer::POA::_nil();
 }
 
+CORBA::Long SALOMEDS_StudyManager_i::getPID()
+{ 
+#ifdef WIN32
+  return (CORBA::Long)_getpid();
+#else
+  return (CORBA::Long)getpid();
+#endif
+}
+
+void SALOMEDS_StudyManager_i::ShutdownWithExit()
+{
+  exit( EXIT_SUCCESS );
+}
+
 //===========================================================================
 //   PRIVATE FUNCTIONS
 //===========================================================================
-long SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
+CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
 {
 #ifdef WIN32
   long pid = (long)_getpid();
 #else
   long pid = (long)getpid();
 #endif
-  isLocal = (strcmp(theHostname, GetHostname().c_str()) == 0 && pid == thePID)?1:0;
-  SALOMEDSImpl_StudyManager* aManager = _impl.operator->();
-  return ((long)aManager);
+  isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
+  return reinterpret_cast<CORBA::LongLong>(_impl);
 }
 
 //===========================================================================
 namespace SALOMEDS
 {
-/*
-  CORBA::Object_var
-  GetObject(const TDF_Label& theLabel, CORBA::ORB_ptr theORB)
-  {
-    try {
-      Handle(SALOMEDS_IORAttribute) anAttr;
-      if(theLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(),anAttr))
-        return theORB->string_to_object(TCollection_AsciiString(anAttr->Get()).ToCString());
-    }catch(...){
-    }
-    return CORBA::Object::_nil();
-  }
-*/
-
   PortableServer::ServantBase_var
   GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
   {