Salome HOME
Fix for the bug 0023165: [CEA 1590] Salome 7.6.0 on Windows does not liberate the...
[modules/yacs.git] / src / LifeCycleCORBA / SALOME_LifeCycleCORBA.cxx
index 6d138af19949ffbc07aea0262d7821255842a3bf..e6db97ac997c719f5ae49a4a61535f2fdcf92ba8 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015  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
@@ -6,7 +6,7 @@
 // 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
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 #include <ServiceUnreachable.hxx>
 
 #include "SALOME_LifeCycleCORBA.hxx"
+#include "SALOME_ResourcesManager.hxx"
+#include "SALOMESDS_DataServerManager.hxx"
+
 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
 #include CORBA_CLIENT_HEADER(SALOME_Session)
 #include CORBA_CLIENT_HEADER(DSC_Engines)
 #include CORBA_CLIENT_HEADER(SALOME_Registry)
 #include CORBA_CLIENT_HEADER(SALOMEDS)
+#include CORBA_CLIENT_HEADER(SALOME_SDS)
 #include CORBA_CLIENT_HEADER(Logger)
+#include CORBA_CLIENT_HEADER(SALOME_Launcher)
 
+#include "SALOME_ResourcesManager.hxx"
 #include "SALOME_ContainerManager.hxx"
 #include "SALOME_Component_i.hxx"
 #include "SALOME_NamingService.hxx"
@@ -60,7 +66,7 @@ IncompatibleComponent::IncompatibleComponent( void ):
 }
 
 IncompatibleComponent::IncompatibleComponent(const IncompatibleComponent &ex):
-  SALOME_Exception( ex ) 
+  SALOME_Exception( ex )
 {
 }
 
@@ -70,7 +76,7 @@ IncompatibleComponent::IncompatibleComponent(const IncompatibleComponent &ex):
 */
 
 //=============================================================================
-/*! 
+/*!
  *  Constructor
  */
 //=============================================================================
@@ -92,7 +98,7 @@ SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
     }
   else _NS = ns;
   //add try catch
-  _NS->Change_Directory("/"); // mpv 250105: current directory may be not root 
+  _NS->Change_Directory("/"); // mpv 250105: current directory may be not root
                               // (in SALOMEDS for an example)
   // not enough: set a current directory in naming service is not thread safe
   // if naming service instance is shared among several threads...
@@ -111,7 +117,7 @@ SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
 }
 
 //=============================================================================
-/*! 
+/*!
  *  Destructor
  */
 //=============================================================================
@@ -124,24 +130,24 @@ SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
 //=============================================================================
 /*! \brief Find an already existing and registered component instance.
  *
- *  \param params         machine parameters like type or name...
+ *  \param params         container parameters like type or name...
  *  \param componentName  the name of component class
  *  \param studyId        default = 0  : multistudy instance
  *  \return a CORBA reference of the component instance, or _nil if not found
  */
 //=============================================================================
 Engines::EngineComponent_ptr
-SALOME_LifeCycleCORBA::FindComponent(const Engines::MachineParameters& params,
+SALOME_LifeCycleCORBA::FindComponent(const Engines::ContainerParameters& params,
                                      const char *componentName,
                                      int studyId)
 {
   if (! isKnownComponentClass(componentName))
     return Engines::EngineComponent::_nil();
 
-  Engines::ContainerParameters new_params;
-  convert(params, new_params);
+  Engines::ContainerParameters new_params(params);
   new_params.resource_params.componentList.length(1);
   new_params.resource_params.componentList[0] = componentName;
+  new_params.resource_params.can_run_containers = true;
   Engines::ResourceList_var listOfResources;
   try
     {
@@ -161,9 +167,9 @@ SALOME_LifeCycleCORBA::FindComponent(const Engines::MachineParameters& params,
 }
 
 //=============================================================================
-/*! \brief Load a component instance on a container defined by machine parameters
+/*! \brief Load a component instance on a container defined by its parameters
  *
- *  \param params         machine parameters like type or name...
+ *  \param params         container parameters like type or name...
  *  \param componentName  the name of component class
  *  \param studyId        default = 0  : multistudy instance
  *  \return a CORBA reference of the component instance, or _nil if problem
@@ -171,7 +177,7 @@ SALOME_LifeCycleCORBA::FindComponent(const Engines::MachineParameters& params,
 //=============================================================================
 
 Engines::EngineComponent_ptr
-SALOME_LifeCycleCORBA::LoadComponent(const Engines::MachineParameters& params,
+SALOME_LifeCycleCORBA::LoadComponent(const Engines::ContainerParameters& params,
                                      const char *componentName,
                                      int studyId)
 {
@@ -180,10 +186,10 @@ SALOME_LifeCycleCORBA::LoadComponent(const Engines::MachineParameters& params,
   if (! isKnownComponentClass(componentName))
     return Engines::EngineComponent::_nil();
 
-  Engines::ContainerParameters new_params;
-  convert(params, new_params);
+  Engines::ContainerParameters new_params(params);
   new_params.resource_params.componentList.length(1);
   new_params.resource_params.componentList[0] = componentName;
+  new_params.resource_params.can_run_containers = true;
 
   Engines::ResourceList_var listOfResources;
   try
@@ -205,64 +211,15 @@ SALOME_LifeCycleCORBA::LoadComponent(const Engines::MachineParameters& params,
 
 //=============================================================================
 /*! \brief Find an already existing and registered component instance or load a new
- *         component instance on a container defined by machine parameters.
+ *         component instance on a container defined by its parameters.
  *
- *  \param params         machine parameters like type or name...
+ *  \param params         container parameters like type or name...
  *  \param componentName  the name of component class
  *  \param studyId        default = 0  : multistudy instance
  *  \return a CORBA reference of the component instance, or _nil if problem
  */
 //=============================================================================
 
-Engines::EngineComponent_ptr
-SALOME_LifeCycleCORBA::
-FindOrLoad_Component(const Engines::MachineParameters& params,
-                     const char *componentName,
-                     int studyId)
-{
-  // --- Check if Component Name is known in ModuleCatalog
-
-  if (! isKnownComponentClass(componentName))
-    return Engines::EngineComponent::_nil();
-
-  Engines::ContainerParameters new_params;
-  convert(params, new_params);
-  new_params.resource_params.componentList.length(1);
-  new_params.resource_params.componentList[0] = componentName;
-
-  // For Compatibility -> if hostname == localhost put name == hostname
-  if (std::string(new_params.resource_params.hostname.in()) == "localhost")
-  {
-    new_params.resource_params.hostname = CORBA::string_dup(Kernel_Utils::GetHostname().c_str());
-    new_params.resource_params.name = CORBA::string_dup(Kernel_Utils::GetHostname().c_str());
-  }
-
-  Engines::ResourceList_var listOfResources;
-  try
-    {
-      listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
-    }
-  catch( const SALOME::SALOME_Exception& ex )
-    {
-      return Engines::EngineComponent::_nil();
-    }
-
-  Engines::EngineComponent_var compo = _FindComponent(new_params,
-                                                componentName,
-                                                studyId,
-                                                listOfResources);
-
-  if(CORBA::is_nil(compo))
-  {
-    new_params.resource_params.resList = listOfResources;
-    compo = _LoadComponent(new_params,
-                           componentName,
-                           studyId);
-  }
-
-  return compo._retn();
-}
-
 Engines::EngineComponent_ptr
 SALOME_LifeCycleCORBA::
 FindOrLoad_Component(const Engines::ContainerParameters& params,
@@ -277,6 +234,7 @@ FindOrLoad_Component(const Engines::ContainerParameters& params,
   Engines::ContainerParameters new_params(params);
   new_params.resource_params.componentList.length(1);
   new_params.resource_params.componentList[0] = componentName;
+  new_params.resource_params.can_run_containers = true;
 
   Engines::ResourceList_var listOfResources;
   try
@@ -331,22 +289,21 @@ SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName,
   std::string st2Container(stContainer);
   int rg=st2Container.find("/");
 
-  Engines::MachineParameters_var params=new Engines::MachineParameters;
+  Engines::ContainerParameters params;
   preSet(params);
   if (rg<0)
   {
     // containerName doesn't contain "/" => Local container
-    params->container_name=CORBA::string_dup(stContainer);
-    params->hostname="";
+    params.container_name = CORBA::string_dup(stContainer);
   }
-  else 
+  else
   {
     stContainer[rg]='\0';
-    params->container_name=CORBA::string_dup(stContainer+rg+1);
-    params->hostname=CORBA::string_dup(stContainer);
+    params.container_name = CORBA::string_dup(stContainer+rg+1);
+    params.resource_params.hostname = CORBA::string_dup(stContainer);
   }
-  params->isMPI = false;
-  SCRUTE(params->container_name);
+  params.isMPI = false;
+  SCRUTE(params.container_name);
   free(stContainer);
   return FindOrLoad_Component(params, componentName);
 }
@@ -364,12 +321,12 @@ bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
   try
   {
     CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
-    SALOME_ModuleCatalog::ModuleCatalog_var Catalog = 
+    SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
       SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
     ASSERT(! CORBA::is_nil(Catalog));
-    SALOME_ModuleCatalog::Acomponent_var compoInfo = 
+    SALOME_ModuleCatalog::Acomponent_var compoInfo =
       Catalog->GetComponent(componentName);
-    if (CORBA::is_nil (compoInfo)) 
+    if (CORBA::is_nil (compoInfo))
     {
       MESSAGE("Catalog Error: Component not found in the catalog " << componentName);
       return false;
@@ -388,55 +345,11 @@ bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
 }
 
 //=============================================================================
-/*! 
- *  Not so complex... useful ?
+/*! \brief Initialisation of a given Engines::ResourceParameters with default values.
  */
 //=============================================================================
 
-bool 
-SALOME_LifeCycleCORBA::isMpiContainer(const Engines::ContainerParameters& params)
-  throw(IncompatibleComponent)
-{
-  if( params.isMPI )
-    return true;
-  else
-    return false;
-}
-
-
-//=============================================================================
-/*! \brief Initialisation of a given Engines::MachineParameters with default values.
- *
- *  - container_name = ""  : not relevant
- *  - hostname = ""        : not relevant
- *  - OS = ""              : not relevant
- *  - nb_proc = 0          : not relevant
- *  - mem_mb = 0           : not relevant
- *  - cpu_clock = 0        : not relevant
- *  - nb_proc_per_node = 0 : not relevant
- *  - nb_node = 0          : not relevant
- *  - isMPI = false        : standard components
- */
-//=============================================================================
-
-void SALOME_LifeCycleCORBA::preSet(Engines::MachineParameters& params)
-{
-  params.container_name = "";
-  params.hostname = "";
-  params.OS = "";
-  params.mem_mb = 0;
-  params.cpu_clock = 0;
-  params.nb_proc_per_node = 0;
-  params.nb_node = 0;
-  params.isMPI = false;
-  params.workingdir = "";
-  params.mode = "";
-  params.policy = "";
-  params.parallelLib = "";
-  params.nb_component_nodes = 0;
-}
-
-void 
+void
 SALOME_LifeCycleCORBA::preSet(Engines::ResourceParameters& params)
 {
   params.name = "";
@@ -448,8 +361,15 @@ SALOME_LifeCycleCORBA::preSet(Engines::ResourceParameters& params)
   params.nb_node = 0;
   params.nb_proc_per_node = 0;
   params.policy = "";
+  params.can_launch_batch_jobs = false;
+  params.can_run_containers = false;
 }
 
+//=============================================================================
+/*! \brief Initialisation of a given Engines::ContainerParameters with default values.
+ */
+//=============================================================================
+
 void SALOME_LifeCycleCORBA::preSet( Engines::ContainerParameters& params)
 {
   params.container_name = "";
@@ -461,43 +381,15 @@ void SALOME_LifeCycleCORBA::preSet( Engines::ContainerParameters& params)
   SALOME_LifeCycleCORBA::preSet(params.resource_params);
 }
 
-void 
-SALOME_LifeCycleCORBA::convert(const Engines::MachineParameters& params_in, 
-                               Engines::ContainerParameters& params_out)
-{
-  SALOME_LifeCycleCORBA::preSet(params_out);
-
-  // Container part
-  params_out.container_name = params_in.container_name;
-  params_out.mode = params_in.mode;
-  params_out.workingdir = params_in.workingdir;
-  params_out.isMPI = params_in.isMPI;
-  params_out.parallelLib = params_in.parallelLib;
-
-  // Resource part
-  params_out.resource_params.hostname = params_in.hostname;
-  params_out.resource_params.OS = params_in.OS;
-  params_out.resource_params.mem_mb = params_in.mem_mb;
-  params_out.resource_params.cpu_clock = params_in.cpu_clock;
-  params_out.resource_params.nb_node = params_in.nb_node;
-  params_out.resource_params.nb_proc_per_node = params_in.nb_proc_per_node;
-  params_out.resource_params.policy = params_in.policy;
-  params_out.resource_params.componentList = params_in.componentList;
-
-  params_out.resource_params.resList.length(params_in.computerList.length());
-  for (CORBA::ULong i = 0; i < params_in.computerList.length(); i++)
-    params_out.resource_params.resList[i] = params_in.computerList[i];
-}
-
 //=============================================================================
-/*! 
+/*!
  *  \return a number of processors not 0, only for MPI containers
  */
 //=============================================================================
 
 int SALOME_LifeCycleCORBA::NbProc(const Engines::ContainerParameters& params)
 {
-  if( !isMpiContainer(params) )
+  if( !params.isMPI )
     return 0;
   else if( params.nb_proc <= 0 )
     return 1;
@@ -534,7 +426,7 @@ Engines::ResourcesManager_ptr SALOME_LifeCycleCORBA::getResourcesManager()
 }
 
 //=============================================================================
-/*! \brief shutdown all the SALOME servers except SALOME_Session_Server, omniNames and notifd
+/*! \brief shutdown all the SALOME servers except SALOME_Session_Server and omniNames
  */
 //=============================================================================
 
@@ -542,7 +434,7 @@ void SALOME_LifeCycleCORBA::shutdownServers()
 {
   // get each Container from NamingService => shutdown it
   // (the order is inverse to the order of servers initialization)
-  
+
   SALOME::Session_var session = SALOME::Session::_nil();
   CORBA::Long pid = 0;
   CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
@@ -557,7 +449,7 @@ void SALOME_LifeCycleCORBA::shutdownServers()
   }
 
   std::string hostname = Kernel_Utils::GetHostname();
-  
+
   // 1) ConnectionManager
   try
     {
@@ -615,8 +507,20 @@ void SALOME_LifeCycleCORBA::shutdownServers()
 #ifndef WIN32
   nanosleep(&ts_req,0);
 #endif
+  // 4 ) Remote ScopeServer (the DataServer is hosted by SalomeLauncher shutdown right after)
+  try
+    {
+      CORBA::Object_var objDSM(_NS->Resolve(SALOMESDS::DataServerManager::NAME_IN_NS));
+      SALOME::DataServerManager_var dsm(SALOME::DataServerManager::_narrow(objDSM));
+      if ( !CORBA::is_nil(dsm) )
+        dsm->shutdownScopes();
+    }
+  catch(const CORBA::Exception& e)
+    {
+       // ignore and continue
+    }
 
-  // 4) SalomeLauncher
+  // 5) SalomeLauncher
   try
     {
       CORBA::Object_var objSL = _NS->Resolve("/SalomeLauncher");
@@ -628,13 +532,13 @@ void SALOME_LifeCycleCORBA::shutdownServers()
     {
        // ignore and continue
     }
-  
+
 //Wait some time so that launcher be completely shutdown
 #ifndef WIN32
   nanosleep(&ts_req,0);
 #endif
 
-  // 5) Registry
+  // 6) Registry
   try
     {
       CORBA::Object_var objR = _NS->Resolve("/Registry");
@@ -647,20 +551,6 @@ void SALOME_LifeCycleCORBA::shutdownServers()
        // ignore and continue
     }
 
-  /*
-  // 6) Session
-  if ( !CORBA::is_nil( session ) ) {
-    try
-    {
-      session->Shutdown();
-    }
-    catch(const CORBA::Exception& e)
-    {
-      // ignore and continue
-    }
-  }
-  */
-
   // 7) Logger
   int argc = 0;
   char *xargv = (char*)"";
@@ -675,8 +565,8 @@ void SALOME_LifeCycleCORBA::shutdownServers()
   name.length(1);
   name[0].id = CORBA::string_dup(stdname.c_str());
   try
-  { 
-    if(!CORBA::is_nil(orb)) 
+  {
+    if(!CORBA::is_nil(orb))
       theObj = orb->resolve_initial_references("NameService");
     if (!CORBA::is_nil(theObj))
       inc = CosNaming::NamingContext::_narrow(theObj);
@@ -684,7 +574,7 @@ void SALOME_LifeCycleCORBA::shutdownServers()
   catch(...)
   {
   }
-  if(!CORBA::is_nil(inc)) 
+  if(!CORBA::is_nil(inc))
   {
     try
     {
@@ -700,39 +590,50 @@ void SALOME_LifeCycleCORBA::shutdownServers()
 }
 
 //=============================================================================
-/*! \brief shutdown  omniNames and notifd
+/*! \brief shutdown  omniNames
  */
 //=============================================================================
 
 void SALOME_LifeCycleCORBA::killOmniNames()
 {
   std::string portNumber (::getenv ("NSPORT") );
-  if ( !portNumber.empty() ) 
+  std::string python_exe;
+
+  python_exe = std::string("python");
+#ifdef WIN32
+  #ifdef _DEBUG_
+    python_exe += std::string("_d");
+  #endif
+#endif
+
+  if ( !portNumber.empty() )
   {
-#ifdef WNT
-#else
-    std::string cmd ;
-    cmd = std::string( "ps -eo pid,command | grep -v grep | grep -E \"omniNames.*")
-      + portNumber
-      + std::string("\" | awk '{cmd=sprintf(\"kill -9 %s\",$1); system(cmd)}'" );
+    std::string cmd = ("from salome_utils import killOmniNames; ");
+    cmd += std::string("killOmniNames(") + portNumber + "); ";
+    cmd  = python_exe + std::string(" -c \"") + cmd +"\"";
     MESSAGE(cmd);
-    try {
-      system ( cmd.c_str() );
-    }
-    catch ( ... ) {
-    }
-#endif
+    system( cmd.c_str() );
+
+    cmd = ("from killSalomeWithPort import cleanApplication; ");
+    cmd += std::string("cleanApplication(") + portNumber + "); ";
+    //cmd  = python_exe + std::string(" -c \"") + cmd +"\" > /dev/null 2> /dev/null";
+    cmd  = python_exe + std::string(" -c \"") + cmd +"\"";
+    MESSAGE(cmd);
+    system( cmd.c_str() );
   }
-  
-  // NPAL 18309  (Kill Notifd)
-  if ( !portNumber.empty() ) 
+
+#ifdef WITH_PORTMANAGER
+  // shutdown portmanager
+  if ( !portNumber.empty() )
   {
-    std::string cmd = ("from killSalomeWithPort import killNotifdAndClean; ");
-    cmd += std::string("killNotifdAndClean(") + portNumber + "); ";
-    cmd  = std::string("python -c \"") + cmd +"\" > /dev/null 2> /dev/null";
+    std::string cmd = ("from PortManager import releasePort; ");
+    cmd += std::string("releasePort(") + portNumber + "); ";
+    //cmd  = python_exe + std::string(" -c \"") + cmd +"\" > /dev/null 2> /dev/null";
+    cmd  = python_exe + std::string(" -c \"") + cmd +"\"";
     MESSAGE(cmd);
     system( cmd.c_str() );
   }
+#endif
 }
 
 //=============================================================================
@@ -768,7 +669,9 @@ _FindComponent(const Engines::ContainerParameters& params,
   for(unsigned int i=0; i < listOfResources.length(); i++)
   {
     const char * currentResource = listOfResources[i];
-    CORBA::Object_var obj = _NS->ResolveComponent(currentResource,
+    Engines::ResourceDefinition_var resource_definition =
+        _ResManager->GetResourceDefinition(currentResource);
+    CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
                                                   containerName,
                                                   componentName,
                                                   nbproc);
@@ -781,7 +684,9 @@ _FindComponent(const Engines::ContainerParameters& params,
   {
     resourcesOK->length(lghtOfresourcesOK);
     CORBA::String_var bestResource = _ResManager->FindFirst(resourcesOK);
-    CORBA::Object_var obj = _NS->ResolveComponent(bestResource,
+    Engines::ResourceDefinition_var resource_definition =
+        _ResManager->GetResourceDefinition(bestResource);
+    CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
                                                   containerName,
                                                   componentName,
                                                   nbproc);
@@ -805,9 +710,9 @@ _FindComponent(const Engines::ContainerParameters& params,
  */
 //=============================================================================
 
-Engines::EngineComponent_ptr 
+Engines::EngineComponent_ptr
 SALOME_LifeCycleCORBA::
-_LoadComponent(const Engines::ContainerParameters& params, 
+_LoadComponent(const Engines::ContainerParameters& params,
               const char *componentName,
               int studyId)
 {
@@ -821,7 +726,7 @@ _LoadComponent(const Engines::ContainerParameters& params,
 
   char* reason;
   bool isLoadable = cont->load_component_Library(componentName,reason);
-  if (!isLoadable) 
+  if (!isLoadable)
     {
       //std::cerr << reason << std::endl;
       CORBA::string_free(reason);