Salome HOME
CCAR: import_hook.py was too strict in ensure_list (ImportError raised)
[modules/kernel.git] / src / ResourcesManager / SALOME_ResourcesManager.cxx
index 88ff54e84b5119fe1efaede0bedb4f96f9e9e582..454b9696a921855eaa197bb4c8ffb68626f4838b 100644 (file)
@@ -1,12 +1,33 @@
+//  Copyright (C) 2007-2010  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
+//  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
+//  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
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
 #include "SALOME_ResourcesManager.hxx" 
-//#include "SALOME_Container_i.hxx"
 #include "Utils_ExceptHandlers.hxx"
+#include "Utils_CorbaException.hxx"
 #include "OpUtil.hxx"
 
-#include <qdom.h>
-
 #include <stdlib.h>
-#ifndef WNT
+#include <stdio.h>
+#ifndef WIN32
 #include <unistd.h>
 #else
 #include <io.h>
@@ -25,7 +46,7 @@
 
 #define MAX_SIZE_FOR_HOSTNAME 256;
 
-using namespace std;
+const char *SALOME_ResourcesManager::_ResourcesManagerNameInNS = "/ResourcesManager";
 
 //=============================================================================
 /*!
@@ -34,44 +55,47 @@ using namespace std;
 //=============================================================================
 
 SALOME_ResourcesManager::
-SALOME_ResourcesManager(CORBA::ORB_ptr orb,
-                        const char *xmlFilePath) :
-    _path_resources(xmlFilePath)
+SALOME_ResourcesManager(CORBA::ORB_ptr orb, 
+                        PortableServer::POA_var poa, 
+                        SALOME_NamingService *ns,
+                        const char *xmlFilePath) : _rm(xmlFilePath)
 {
-  _NS = new SALOME_NamingService(orb);
+  MESSAGE("SALOME_ResourcesManager constructor");
+  _NS = ns;
+  _orb = CORBA::ORB::_duplicate(orb) ;
+  _poa = PortableServer::POA::_duplicate(poa) ;
+  PortableServer::ObjectId_var id = _poa->activate_object(this);
+  CORBA::Object_var obj = _poa->id_to_reference(id);
+  Engines::ResourcesManager_var refContMan = Engines::ResourcesManager::_narrow(obj);
+  _NS->Register(refContMan,_ResourcesManagerNameInNS);
+  MESSAGE("SALOME_ResourcesManager constructor end");
 }
 
 //=============================================================================
 /*!
  *  Standard constructor, parse resource file.
  *  - if ${APPLI} exists in environment,
- *    look for ${HOME}/*{APPLI}/CatalogResources.xml
+ *    look for ${HOME}/${APPLI}/CatalogResources.xml
  *  - else look for default:
- *    ${KERNEL_ROOT_DIR}/share/salome/resources/CatalogResources.xml
+ *    ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
  *  - parse XML resource file.
  */ 
 //=============================================================================
 
-SALOME_ResourcesManager::SALOME_ResourcesManager(CORBA::ORB_ptr orb)
+SALOME_ResourcesManager::SALOME_ResourcesManager(CORBA::ORB_ptr orb, 
+                                                 PortableServer::POA_var poa, 
+                                                 SALOME_NamingService *ns) : _rm()
 {
-  _NS = new SALOME_NamingService(orb);
-  _isAppliSalomeDefined = (getenv("APPLI") != 0);
-
-  if (_isAppliSalomeDefined)
-    {
-      _path_resources = getenv("HOME");
-      _path_resources += "/";
-      _path_resources += getenv("APPLI");
-      _path_resources += "/CatalogResources.xml";
-    }
-
-  else
-    {
-      _path_resources = getenv("KERNEL_ROOT_DIR");
-      _path_resources += "/share/salome/resources/CatalogResources.xml";
-    }
-
-  ParseXmlFile();
+  MESSAGE("SALOME_ResourcesManager constructor");
+  _NS = ns;
+  _orb = CORBA::ORB::_duplicate(orb) ;
+  _poa = PortableServer::POA::_duplicate(poa) ;
+  PortableServer::ObjectId_var id = _poa->activate_object(this);
+  CORBA::Object_var obj = _poa->id_to_reference(id);
+  Engines::ResourcesManager_var refContMan = Engines::ResourcesManager::_narrow(obj);
+  _NS->Register(refContMan,_ResourcesManagerNameInNS);
+
+  MESSAGE("SALOME_ResourcesManager constructor end");
 }
 
 //=============================================================================
@@ -82,759 +106,420 @@ SALOME_ResourcesManager::SALOME_ResourcesManager(CORBA::ORB_ptr orb)
 
 SALOME_ResourcesManager::~SALOME_ResourcesManager()
 {
-  delete _NS;
-}
-
-//=============================================================================
-/*!
- *  get the list of name of ressources fitting for the specified module.
- *  If hostname specified, check it is local or known in resources catalog.
- *
- *  Else
- *  - select first machines with corresponding OS (all machines if
- *    parameter OS empty),
- *  - then select the sublist of machines on witch the module is known
- *    (if the result is empty, that probably means that the inventory of
- *    modules is probably not done, so give complete list from previous step)
- */ 
-//=============================================================================
-
-vector<string>
-SALOME_ResourcesManager::
-GetFittingResources(const Engines::MachineParameters& params,
-                    const char *moduleName)
-throw(SALOME_Exception)
-{
-  MESSAGE("ResourcesManager::GetFittingResources");
-  vector <std::string> ret;
-
-  // --- To be sure that we search in a correct list.
-  ParseXmlFile();
-
-  const char *hostname = (const char *)params.hostname;
-  MESSAGE("GetFittingResources " << hostname << " " << GetHostname().c_str());
-
-  if (hostname[0] != '\0')
-    {
-      MESSAGE("ResourcesManager::GetFittingResources : hostname specified" );
-
-      if ( strcmp(hostname, "localhost") == 0 ||
-           strcmp(hostname, GetHostname().c_str()) == 0 )
-        {
-          MESSAGE("ResourcesManager::GetFittingResources : localhost" );
-          ret.push_back(GetHostname().c_str());
-          MESSAGE("ResourcesManager::GetFittingResources : " << ret.size());
-        }
-
-      else if (_resourcesList.find(hostname) != _resourcesList.end())
-        {
-          // --- params.hostname is in the list of resources so return it.
-          ret.push_back(hostname);
-        }
-
-      else
-        {
-          // --- user specified an unknown hostame so notify him.
-          MESSAGE("ResourcesManager::GetFittingResources : SALOME_Exception");
-          throw SALOME_Exception("unknown host");
-        }
-    }
-
-  else
-    // --- Search for available resources sorted by priority
-    {
-      SelectOnlyResourcesWithOS(ret, params.OS);
-
-      KeepOnlyResourcesWithModule(ret, moduleName);
-
-      if (ret.size() == 0)
-        SelectOnlyResourcesWithOS(ret, params.OS);
-
-      // --- set wanted parameters
-      ResourceDataToSort::_nbOfNodesWanted = params.nb_node;
-
-      ResourceDataToSort::_nbOfProcPerNodeWanted = params.nb_proc_per_node;
-
-      ResourceDataToSort::_CPUFreqMHzWanted = params.cpu_clock;
-
-      ResourceDataToSort::_memInMBWanted = params.mem_mb;
-
-      // --- end of set
-
-      list<ResourceDataToSort> li;
-
-      for (vector<string>::iterator iter = ret.begin();
-           iter != ret.end();
-           iter++)
-        li.push_back(_resourcesList[(*iter)].DataForSort);
-
-      li.sort();
-
-      unsigned int i = 0;
-
-      for (list<ResourceDataToSort>::iterator iter2 = li.begin();
-           iter2 != li.end();
-           iter2++)
-        ret[i++] = (*iter2)._hostName;
-    }
-
-  MESSAGE("ResourcesManager::GetFittingResources : return" << ret.size());
-  return ret;
+  MESSAGE("SALOME_ResourcesManager destructor");
 }
 
-//=============================================================================
-/*!
- *  add an entry in the ressources catalog  xml file.
- *  Return 0 if OK (KERNEL found in new resources modules) else throw exception
- */ 
-//=============================================================================
-
-int
-SALOME_ResourcesManager::
-AddResourceInCatalog(const Engines::MachineParameters& paramsOfNewResources,
-                     const map<string, string>& modulesOnNewResources,
-                     const char *environPathOfPrerequired,
-                     const char *alias,
-                     const char *userName,
-                     AccessModeType mode,
-                     AccessProtocolType prot)
-throw(SALOME_Exception)
-{
-  map<string, string>::const_iterator iter =
-    modulesOnNewResources.find("KERNEL");
-
-  if (iter != modulesOnNewResources.end())
-    {
-      ParserResourcesType newElt;
-      newElt.DataForSort._hostName = paramsOfNewResources.hostname;
-      newElt.Alias = alias;
-      newElt.Protocol = prot;
-      newElt.Mode = mode;
-      newElt.UserName = userName;
-      newElt.ModulesPath = modulesOnNewResources;
-      newElt.PreReqFilePath = environPathOfPrerequired;
-      newElt.OS = paramsOfNewResources.OS;
-      newElt.DataForSort._memInMB = paramsOfNewResources.mem_mb;
-      newElt.DataForSort._CPUFreqMHz = paramsOfNewResources.cpu_clock;
-      newElt.DataForSort._nbOfNodes = paramsOfNewResources.nb_node;
-      newElt.DataForSort._nbOfProcPerNode =
-        paramsOfNewResources.nb_proc_per_node;
-      _resourcesList[newElt.DataForSort._hostName] = newElt;
-      return 0;
-    }
-
-  else
-    throw SALOME_Exception("KERNEL is not present in this resource");
-}
 
 //=============================================================================
-/*!
- *  Deletes a resource from the catalog
- */ 
+/*! CORBA method:
+ *  shutdown all the containers, then the ContainerManager servant
+ */
 //=============================================================================
 
-void SALOME_ResourcesManager::DeleteResourceInCatalog(const char *hostname)
+void SALOME_ResourcesManager::Shutdown()
 {
-  _resourcesList.erase(hostname);
+  MESSAGE("Shutdown");
+  _NS->Destroy_Name(_ResourcesManagerNameInNS);
+  PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
+  _poa->deactivate_object(oid);
 }
 
 //=============================================================================
+//! get the name of resources fitting the specified constraints (params)
 /*!
- *  write the current data in memory in file.
- */ 
-//=============================================================================
-
-void SALOME_ResourcesManager::WriteInXmlFile()
-{
-  QDomDocument doc("ResourcesCatalog");
-  SALOME_ResourcesCatalog_Handler* handler =
-    new SALOME_ResourcesCatalog_Handler(_resourcesList);
-  handler->PrepareDocToXmlFile(doc);
-  delete handler;
-
-  QFile file( _path_resources );
-
-  if ( !file.open( IO_WriteOnly ) )
-    INFOS("WRITING ERROR !");
-
-  QTextStream ts( &file );
-
-  ts << doc.toString();
-
-  file.close();
-
-  MESSAGE("WRITING DONE!");
-}
-
-//=============================================================================
-/*!
- *  parse the data type catalog
+ *  If hostname specified, check it is local or known in resources catalog.
+ *
+ *  Else
+ *  - select first machines with corresponding OS (all machines if
+ *    parameter OS empty),
+ *  - then select the sublist of machines on which the component is known
+ *    (if the result is empty, that probably means that the inventory of
+ *    components is probably not done, so give complete list from previous step)
  */ 
 //=============================================================================
 
-const MapOfParserResourcesType& SALOME_ResourcesManager::ParseXmlFile()
+Engines::ResourceList *
+SALOME_ResourcesManager::GetFittingResources(const Engines::ResourceParameters& params)
 {
-  SALOME_ResourcesCatalog_Handler* handler =
-    new SALOME_ResourcesCatalog_Handler(_resourcesList);
-  QFile xmlFile(_path_resources);
-
-  QXmlInputSource source(xmlFile);
-
-  QXmlSimpleReader reader;
-  reader.setContentHandler( handler );
-  reader.setErrorHandler( handler );
-  reader.parse( source );
-  xmlFile.close();
-  delete handler;
-  return _resourcesList;
-}
-
-//=============================================================================
-/*!
- *   consult the content of the list
- */ 
-//=============================================================================
-
-const MapOfParserResourcesType& SALOME_ResourcesManager::GetList() const
+  MESSAGE("ResourcesManager::GetFittingResources");
+  Engines::ResourceList * ret = new Engines::ResourceList;
+
+  // CORBA -> C++
+  resourceParams p;
+  p.name = params.name;
+  p.hostname = params.hostname;
+  p.OS = params.OS;
+  p.nb_proc = params.nb_proc;
+  p.nb_node = params.nb_node;
+  p.nb_proc_per_node = params.nb_proc_per_node;
+  p.cpu_clock = params.cpu_clock;
+  p.mem_mb = params.mem_mb;
+  for(unsigned int i=0; i<params.componentList.length(); i++)
+    p.componentList.push_back(std::string(params.componentList[i]));
+  for(unsigned int i=0; i<params.resList.length(); i++)
+    p.resourceList.push_back(std::string(params.resList[i]));
+  
+  try
   {
-    return _resourcesList;
-  }
-
+    // Call C++ ResourceManager
+    std::vector <std::string> vec = _rm.GetFittingResources(p);
 
-//=============================================================================
-/*!
- *  dynamically obtains the best machines
- */ 
-//=============================================================================
-
-string
-SALOME_ResourcesManager::FindBest(const Engines::MachineList& listOfMachines)
-{
-  return _dynamicResourcesSelecter.FindBest(listOfMachines);
-}
-
-
-//=============================================================================
-/*!
- *  This is no longer valid (C++ container are also python containers)
- */ 
-//=============================================================================
-
-bool isPythonContainer(const char* ContainerName)
-{
-  bool ret = false;
-  int len = strlen(ContainerName);
-
-  if (len >= 2)
-    if (strcmp(ContainerName + len - 2, "Py") == 0)
-      ret = true;
+    // C++ -> CORBA
+    ret->length(vec.size());
+    for(unsigned int i=0;i<vec.size();i++)
+      (*ret)[i] = (vec[i]).c_str();
+  }
+  catch(const ResourcesException &ex)
+  {
+    INFOS("Caught exception in GetFittingResources C++:  " << ex.msg);
+    THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
+  }  
 
   return ret;
 }
 
-
 //=============================================================================
 /*!
- *  Builds the script to be launched
- *
- *  If SALOME Application not defined ($APPLI),
- *  see BuildTempFileToLaunchRemoteContainer()
- *
- *  Else rely on distant configuration. Command is under the form (example):
- *  ssh user@machine distantPath/runRemote.sh hostNS portNS \
- *                   SALOME_Container containerName &"
-
- *  - where user is ommited if not specified in CatalogResources,
- *  - where distant path is always relative to user@machine $HOME, and
- *    equal to $APPLI if not specified in CatalogResources,
- *  - where hostNS is the hostname of CORBA naming server (set by scripts to
- *    use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
- *  - where portNS is the port used by CORBA naming server (set by scripts to
- *    use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
+ *  dynamically obtains the first machines
  */ 
 //=============================================================================
 
-string
-SALOME_ResourcesManager::BuildCommandToLaunchRemoteContainer
-(const string& machine,
- const Engines::MachineParameters& params)
+char *
+SALOME_ResourcesManager::FindFirst(const Engines::ResourceList& listOfResources)
 {
-  string command;
-
-  if ( ! _isAppliSalomeDefined )
-    command = BuildTempFileToLaunchRemoteContainer(machine, params);
-
-  else
-    {
-      const ParserResourcesType& resInfo = _resourcesList[machine];
-
-      if (params.isMPI)
-        {
-          int nbproc;
-
-          if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
-            nbproc = 1;
-          else if ( params.nb_node == 0 )
-            nbproc = params.nb_proc_per_node;
-          else if ( params.nb_proc_per_node == 0 )
-            nbproc = params.nb_node;
-          else
-            nbproc = params.nb_node * params.nb_proc_per_node;
-        }
-
-      // "ssh user@machine distantPath/runRemote.sh hostNS portNS \
-      //  SALOME_Container containerName &"
-
-      if (resInfo.Protocol == rsh)
-        command = "rsh ";
-      else if (resInfo.Protocol == ssh)
-        command = "ssh ";
-      else
-        throw SALOME_Exception("Unknown protocol");
-
-      if (resInfo.UserName != "")
-       {
-         command += resInfo.UserName;
-         command += "@";
-       }
-
-      command += machine;
-      command += " ";
-
-      if (resInfo.AppliPath != "")
-       command += resInfo.AppliPath; // path relative to user@machine $HOME
-      else
-       {
-         ASSERT(getenv("APPLI"));
-         command += getenv("APPLI"); // path relative to user@machine $HOME
-       }
-
-      command += "/runRemote.sh ";
-
-      ASSERT(getenv("NSHOST")); 
-      command += getenv("NSHOST"); // hostname of CORBA name server
-
-      command += " ";
-      ASSERT(getenv("NSPORT"));
-      command += getenv("NSPORT"); // port of CORBA name server
-
-      command += " SALOME_Container ";
-      command += _NS->ContainerName(params);
-      command += "&";
+  // CORBA -> C++
+  std::vector<std::string> rl;
+  for(unsigned int i=0; i<listOfResources.length(); i++)
+    rl.push_back(std::string(listOfResources[i]));
 
-      MESSAGE("command =" << command);
-    }
-
-  return command;
+  return CORBA::string_dup(_rm.Find("first", rl).c_str());
 }
 
-
-//=============================================================================
-/*!
- *  builds the command to be launched.
- */ 
-//=============================================================================
-
-string
-SALOME_ResourcesManager::BuildCommandToLaunchLocalContainer
-(const Engines::MachineParameters& params)
+char *
+SALOME_ResourcesManager::Find(const char* policy, const Engines::ResourceList& listOfResources)
 {
-  _TmpFileName = "";
-  string command;
-  int nbproc = 0;
+  // CORBA -> C++
+  std::vector<std::string> rl;
+  for(unsigned int i=0; i<listOfResources.length(); i++)
+    rl.push_back(std::string(listOfResources[i]));
 
-  if (params.isMPI)
-    {
-      command = "mpirun -np ";
-
-      if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
-        nbproc = 1;
-      else if ( params.nb_node == 0 )
-        nbproc = params.nb_proc_per_node;
-      else if ( params.nb_proc_per_node == 0 )
-        nbproc = params.nb_node;
-      else
-        nbproc = params.nb_node * params.nb_proc_per_node;
-
-      std::ostringstream o;
-
-      o << nbproc << " ";
-
-      command += o.str();
-
-      if (isPythonContainer(params.container_name))
-        command += "pyMPI SALOME_ContainerPy.py ";
-      else
-        command += "SALOME_MPIContainer ";
-    }
-
-  else
-    {
-      if (isPythonContainer(params.container_name))
-        command = "SALOME_ContainerPy.py ";
-      else
-        command = "SALOME_Container ";
-    }
-
-  command += _NS->ContainerName(params);
-  command += " -";
-  AddOmninamesParams(command);
-  command += " > /tmp/";
-  command += _NS->ContainerName(params);
-  command += "_";
-  command += GetHostname();
-  command += "_";
-  command += getenv( "USER" ) ;
-  command += ".log 2>&1 &" ;
-  MESSAGE("Command is ... " << command);
-  return command;
+  return CORBA::string_dup(_rm.Find(policy, rl).c_str());
 }
 
-
-//=============================================================================
-/*!
- *  removes the generated temporary file in case of a remote launch.
- */ 
-//=============================================================================
-
-void SALOME_ResourcesManager::RmTmpFile()
+Engines::ResourceDefinition* 
+SALOME_ResourcesManager::GetResourceDefinition(const char * name)
 {
-  if (_TmpFileName != "")
-    {
-      string command = "rm ";
-      command += _TmpFileName;
-      char *temp = strdup(command.c_str());
-      int lgthTemp = strlen(temp);
-      temp[lgthTemp - 3] = '*';
-      temp[lgthTemp - 2] = '\0';
-      system(temp);
-      free(temp);
-    }
+  ParserResourcesType resource = _rm.GetResourcesDescr(name);
+  Engines::ResourceDefinition *p_ptr = new Engines::ResourceDefinition;
+
+  p_ptr->name = CORBA::string_dup(resource.Name.c_str());
+  p_ptr->hostname = CORBA::string_dup(resource.HostName.c_str());
+  if( resource.Protocol == rsh )
+    p_ptr->protocol = "rsh";
+  else if( resource.Protocol == ssh )
+    p_ptr->protocol = "ssh";
+  if( resource.ClusterInternalProtocol == rsh )
+    p_ptr->iprotocol = "rsh";
+  else if( resource.ClusterInternalProtocol == ssh )
+    p_ptr->iprotocol = "ssh";
+  p_ptr->username = CORBA::string_dup(resource.UserName.c_str());
+  p_ptr->applipath = CORBA::string_dup(resource.AppliPath.c_str());
+  p_ptr->componentList.length(resource.ComponentsList.size());
+  for(unsigned int i=0;i<resource.ComponentsList.size();i++)
+    p_ptr->componentList[i] = CORBA::string_dup(resource.ComponentsList[i].c_str());
+  p_ptr->OS = CORBA::string_dup(resource.OS.c_str());
+  p_ptr->mem_mb = resource.DataForSort._memInMB;
+  p_ptr->cpu_clock = resource.DataForSort._CPUFreqMHz;
+  p_ptr->nb_proc_per_node = resource.DataForSort._nbOfProcPerNode;
+  p_ptr->nb_node = resource.DataForSort._nbOfNodes;
+
+  if( resource.mpi == lam )
+    p_ptr->mpiImpl = "lam";
+  else if( resource.mpi == mpich1 )
+    p_ptr->mpiImpl = "mpich1";
+  else if( resource.mpi == mpich2 )
+    p_ptr->mpiImpl = "mpich2";
+  else if( resource.mpi == openmpi )
+    p_ptr->mpiImpl = "openmpi";
+  else if( resource.mpi == slurm )
+    p_ptr->mpiImpl = "slurm";
+  else if( resource.mpi == prun )
+    p_ptr->mpiImpl = "prun";
+
+  if( resource.Batch == pbs )
+    p_ptr->batch = "pbs";
+  else if( resource.Batch == lsf )
+    p_ptr->batch = "lsf";
+  else if( resource.Batch == sge )
+    p_ptr->batch = "sge";
+  else if( resource.Batch == ccc )
+    p_ptr->batch = "ccc";
+  else if( resource.Batch == ssh_batch )
+    p_ptr->batch = "ssh";
+
+  return p_ptr;
 }
 
-
-//=============================================================================
-/*!
- *  builds the script to be launched
- */ 
-//=============================================================================
-
-string
-SALOME_ResourcesManager::BuildCommand
-(const string& machine,
- const char *containerName)
+void 
+SALOME_ResourcesManager::AddResource(const Engines::ResourceDefinition& new_resource,
+                                     CORBA::Boolean write,
+                                     const char * xml_file)
 {
-  // rsh -n ikkyo /export/home/rahuel/SALOME_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515 &
-  const ParserResourcesType& resInfo = _resourcesList[machine];
-  bool pyCont = isPythonContainer(containerName);
-
-  string command;
-
-  if (resInfo.Protocol == rsh)
-    command = "rsh -n " ;
-  else if (resInfo.Protocol == ssh)
-    command = "ssh -f -n ";
-  else
-    throw SALOME_Exception("Not implemented yet...");
-
-  command += machine;
-  command += " ";
-  string path = (*(resInfo.ModulesPath.find("KERNEL"))).second;
-  command += path;
-  command += "/bin/salome/";
-
-  if ( pyCont )
-    command += "SALOME_ContainerPy.py ";
-  else
-    command += "SALOME_Container ";
-
-  command += containerName;
-  command += " -";
-  AddOmninamesParams(command);
-  command += " > /tmp/";
-  command += containerName;
-  command += "_";
-  command += machine;
-  command += ".log 2>&1 &" ;
-
-  SCRUTE( command );
-  return command;
-}
-
-//=============================================================================
-/*!
- *  Gives a sublist of machines with matching OS.
- *  If parameter OS is empty, gives the complete list of machines
- */ 
-//=============================================================================
-
-// Warning need an updated parsed list : _resourcesList
-void
-SALOME_ResourcesManager::SelectOnlyResourcesWithOS
-( vector<string>& hosts,
-  const char *OS) const
-throw(SALOME_Exception)
-{
-  string base(OS);
-
-  for (map<string, ParserResourcesType>::const_iterator iter =
-         _resourcesList.begin();
-       iter != _resourcesList.end();
-       iter++)
-    {
-      if ( (*iter).second.OS == base || base.size() == 0)
-        hosts.push_back((*iter).first);
-    }
-}
-
-
-//=============================================================================
-/*!
- *  Gives a sublist of machines on which the module is known.
- */ 
-//=============================================================================
-
-//Warning need an updated parsed list : _resourcesList
-void
-SALOME_ResourcesManager::KeepOnlyResourcesWithModule
-( vector<string>& hosts,
-  const char *moduleName) const
-throw(SALOME_Exception)
-{
-  for (vector<string>::iterator iter = hosts.begin(); iter != hosts.end();)
-    {
-      MapOfParserResourcesType::const_iterator it = _resourcesList.find(*iter);
-      const map<string, string>& mapOfModulesOfCurrentHost =
-        (((*it).second).ModulesPath);
-
-      if (mapOfModulesOfCurrentHost.find(moduleName) ==
-          mapOfModulesOfCurrentHost.end())
-        hosts.erase(iter);
-      else
-        iter++;
-    }
-}
+  ParserResourcesType resource;
+  resource.Name = new_resource.name.in();
+  resource.HostName = new_resource.hostname.in();
+  resource.OS = new_resource.OS.in();
+  resource.AppliPath = new_resource.applipath.in();
+  resource.DataForSort._memInMB = new_resource.mem_mb;
+  resource.DataForSort._CPUFreqMHz = new_resource.cpu_clock;
+  resource.DataForSort._nbOfNodes = new_resource.nb_node;
+  resource.DataForSort._nbOfProcPerNode = new_resource.nb_proc_per_node;
+  resource.UserName = new_resource.username.in();
+
+  std::string aBatch = new_resource.batch.in();
+  if (aBatch == "pbs")
+    resource.Batch = pbs;
+  else if  (aBatch == "lsf")
+    resource.Batch = lsf;
+  else if  (aBatch == "sge")
+    resource.Batch = sge;
+  else if  (aBatch == "ccc")
+    resource.Batch = ccc;
+  else if  (aBatch == "ssh_batch")
+    resource.Batch = ssh_batch;
+  else if (aBatch == "")
+    resource.Batch = none;
+  else {
+    INFOS("Bad Batch definition in AddResource: " << aBatch);
+    std::string message("Bad Batch definition in AddResource: ");
+    message += aBatch;
+    THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
+  }
 
+  std::string anMpi = new_resource.mpiImpl.in();
+  if (anMpi == "lam")
+    resource.mpi = lam;
+  else if (anMpi == "mpich1")
+    resource.mpi = mpich1;
+  else if (anMpi == "mpich2")
+    resource.mpi = mpich2;
+  else if (anMpi == "openmpi")
+    resource.mpi = openmpi;
+  else if  (anMpi == "slurm")
+    resource.mpi = slurm;
+  else if  (anMpi == "prun")
+    resource.mpi = prun;
+  else if (anMpi == "")
+    resource.mpi = nompi;
+  else {
+    INFOS("Bad MPI definition in AddResource: " << anMpi);
+    std::string message("Bad MPI definition in AddResource: ");
+    message += anMpi;
+    THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
+  }
 
-//=============================================================================
-/*!
- *   add to command all options relative to naming service.
- */ 
-//=============================================================================
+  std::string mode_str = new_resource.mode.in();
+  if (mode_str == "interactive")
+    resource.Mode = interactive;
+  else if (mode_str == "batch")
+    resource.Mode = batch;
+  else if (mode_str == "")
+    resource.Mode = interactive;
+  else {
+    INFOS("Bad mode definition in AddResource: " << mode_str);
+    std::string message("Bad mode definition in AddResource: ");
+    message += mode_str;
+    THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
+  }
+  
+  std::string protocol = new_resource.protocol.in();
+  if (protocol == "rsh")
+    resource.Protocol = rsh;
+  else if (protocol == "ssh")
+    resource.Protocol = ssh;
+  else if (protocol == "")
+    resource.Protocol = rsh;
+  else {
+    INFOS("Bad protocol definition in AddResource: " << protocol);
+    std::string message("Bad protocol definition in AddResource: ");
+    message += protocol;
+    THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
+  }
 
-void SALOME_ResourcesManager::AddOmninamesParams(string& command) const
-  {
-    // If env variable OMNIORB_CONFIG is not defined or the file is more complex than one line
-    // does not work
-    // Even if we use it we have to check if env variable exists
-    //string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
-    //ifstream omniORBfile( omniORBcfg.c_str() ) ;
-    //char ORBInitRef[11] ;
-    //char egal[3] ;
-    //char nameservice[132] ;
-    //omniORBfile >> ORBInitRef ;
-    //command += "ORBInitRef " ;
-    //omniORBfile >> egal ;
-    //omniORBfile >> nameservice ;
-    //omniORBfile.close() ;
-    //char * bsn = strchr( nameservice , '\n' ) ;
-    //if ( bsn ) {
-    //bsn[ 0 ] = '\0' ;
-    //}
-    //command += nameservice ;
-
-    char *iorstr = _NS->getIORaddr();
-    command += "ORBInitRef NameService=";
-    command += iorstr;
+  std::string iprotocol = new_resource.iprotocol.in();
+  if (iprotocol == "rsh")
+    resource.ClusterInternalProtocol = rsh;
+  else if (iprotocol == "ssh")
+    resource.ClusterInternalProtocol = ssh;
+  else if (iprotocol == "")
+    resource.ClusterInternalProtocol = rsh;
+  else {
+    INFOS("Bad iprotocol definition in AddResource: " << iprotocol);
+    std::string message("Bad iprotocol definition in AddResource: ");
+    message += iprotocol;
+    THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
   }
 
+  for (CORBA::ULong i = 0; i < new_resource.componentList.length(); i++)
+    resource.ComponentsList.push_back(new_resource.componentList[i].in());
 
-//=============================================================================
-/*!
- *  add to command all options relative to naming service.
- */ 
-//=============================================================================
+  _rm.AddResourceInCatalog(resource);
 
-void SALOME_ResourcesManager::AddOmninamesParams(ofstream& fileStream) const
+  if (write)
   {
-    string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
-    ifstream omniORBfile( omniORBcfg.c_str() ) ;
-    char ORBInitRef[11] ;
-    char egal[3] ;
-    char nameservice[132] ;
-    omniORBfile >> ORBInitRef ;
-    fileStream << "ORBInitRef ";
-    omniORBfile >> egal ;
-    omniORBfile >> nameservice ;
-    omniORBfile.close() ;
-    char * bsn = strchr( nameservice , '\n' ) ;
-
-    if ( bsn )
-      {
-        bsn[ 0 ] = '\0' ;
-      }
-
-    fileStream << nameservice;
+    _rm.WriteInXmlFile(std::string(xml_file));
+    _rm.ParseXmlFiles();
   }
+}
 
-
-//=============================================================================
-/*!
- *  generate a file name in /tmp directory
- */ 
-//=============================================================================
-
-string SALOME_ResourcesManager::BuildTemporaryFileName() const
+void 
+SALOME_ResourcesManager::RemoveResource(const char * resource_name,
+                                       CORBA::Boolean write,
+                                       const char * xml_file)
+{
+  _rm.DeleteResourceInCatalog(resource_name);
+  if (write)
   {
-    //build more complex file name to support multiple salome session
-    char *temp = new char[19];
-    strcpy(temp, "/tmp/command");
-    strcat(temp, "XXXXXX");
-#ifndef WNT
-
-    mkstemp(temp);
-#else
-
-    char aPID[80];
-    itoa(getpid(), aPID, 10);
-    strcat(temp, aPID);
-#endif
-
-    string command(temp);
-    delete [] temp;
-    command += ".sh";
-    return command;
+    _rm.WriteInXmlFile(std::string(xml_file));
+    _rm.ParseXmlFiles();
   }
+}
 
-
-//=============================================================================
-/*!
- *  Builds in a temporary file the script to be launched.
- *  
- *  Used if SALOME Application ($APPLI) is not defined.
- *  The command is build with data from CatalogResources, in which every path
- *  used on remote computer must be defined.
- */ 
-//=============================================================================
-
-string
-SALOME_ResourcesManager::BuildTempFileToLaunchRemoteContainer
-(const string& machine,
- const Engines::MachineParameters& params)
+std::string 
+SALOME_ResourcesManager::getMachineFile(std::string resource_name, 
+                                        CORBA::Long nb_procs, 
+                                        std::string parallelLib)
 {
-  _TmpFileName = BuildTemporaryFileName();
-  ofstream tempOutputFile;
-  tempOutputFile.open(_TmpFileName.c_str(), ofstream::out );
-  const ParserResourcesType& resInfo = _resourcesList[machine];
-  tempOutputFile << "#! /bin/sh" << endl;
-
-  // --- set env vars
+  std::string machine_file_name("");
 
-  for (map<string, string>::const_iterator iter = resInfo.ModulesPath.begin();
-       iter != resInfo.ModulesPath.end();
-       iter++)
+  if (parallelLib == "Dummy")
+  {
+    MESSAGE("[getMachineFile] parallelLib is Dummy");
+    MapOfParserResourcesType resourcesList = _rm.GetList();
+    if (resourcesList.find(resource_name) != resourcesList.end())
     {
-      string curModulePath((*iter).second);
-      tempOutputFile << (*iter).first << "_ROOT_DIR=" << curModulePath << endl;
-      tempOutputFile << "export " << (*iter).first << "_ROOT_DIR" << endl;
-      tempOutputFile << "LD_LIBRARY_PATH=" << curModulePath
-                    << "/lib/salome" << ":${LD_LIBRARY_PATH}" << endl;
-      tempOutputFile << "PYTHONPATH=" << curModulePath << "/bin/salome:"
-                    << curModulePath << "/lib/salome:" << curModulePath
-                    << "/lib/python2.2/site-packages/salome:";
-      tempOutputFile << curModulePath
-      << "/lib/python2.2/site-packages/salome/shared_modules:${PYTHONPATH}"
-      << endl;
-    }
+      ParserResourcesType resource = resourcesList[resource_name];
 
-  tempOutputFile << "export LD_LIBRARY_PATH" << endl;
-  tempOutputFile << "export PYTHONPATH" << endl;
-  tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
-
-  // ! env vars
+      // Check if resource is cluster or not
+      if (resource.ClusterMembersList.empty())
+      {
+        //It is not a cluster so we create a cluster with one machine
+        ParserResourcesClusterMembersType fake_node;
+        fake_node.HostName = resource.HostName;
+        fake_node.Protocol = resource.Protocol;
+        fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
+        fake_node.UserName = resource.UserName;
+        fake_node.AppliPath = resource.AppliPath;
+        fake_node.DataForSort = resource.DataForSort;
+
+        resource.ClusterMembersList.push_front(fake_node);
+      }
 
-  if (params.isMPI)
-    {
-      tempOutputFile << "mpirun -np ";
-      int nbproc;
-
-      if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
-        nbproc = 1;
-      else if ( params.nb_node == 0 )
-        nbproc = params.nb_proc_per_node;
-      else if ( params.nb_proc_per_node == 0 )
-        nbproc = params.nb_node;
-      else
-        nbproc = params.nb_node * params.nb_proc_per_node;
+      // Creating list of machines for creating the machine file
+      std::list<std::string> list_of_machines;
+      std::list<ParserResourcesClusterMembersType>::iterator cluster_it = 
+        resource.ClusterMembersList.begin();
+      while (cluster_it != resource.ClusterMembersList.end())
+      {
+        // For each member of the cluster we add a nbOfNodes * nbOfProcPerNode in the list
+        unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes * 
+                                      (*cluster_it).DataForSort._nbOfProcPerNode;
+        for (unsigned int i = 0; i < number_of_proc; i++)
+          list_of_machines.push_back((*cluster_it).HostName);
+        cluster_it++;
+      }
 
-      std::ostringstream o;
+      // Creating machine file
+      machine_file_name = tmpnam(NULL);
+         std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
 
-      tempOutputFile << nbproc << " ";
+      CORBA::Long machine_number = 0;
+      std::list<std::string>::iterator it = list_of_machines.begin();
+      while (machine_number != nb_procs)
+      {
+        // Adding a new node to the machine file
+        machine_file << *it << std::endl;
+
+        // counting...
+        it++;
+        if (it == list_of_machines.end())
+          it = list_of_machines.begin();
+        machine_number++;
+      }
     }
+    else
+      INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
+  }
+  else if (parallelLib == "Mpi")
+  {
+    MESSAGE("[getMachineFile] parallelLib is Mpi");
 
-  tempOutputFile << (*(resInfo.ModulesPath.find("KERNEL"))).second
-                << "/bin/salome/";
-
-  if (params.isMPI)
+    MapOfParserResourcesType resourcesList = _rm.GetList();
+    if (resourcesList.find(resource_name) != resourcesList.end())
     {
-      if (isPythonContainer(params.container_name))
-        tempOutputFile << "pyMPI SALOME_ContainerPy.py ";
-      else
-        tempOutputFile << "SALOME_MPIContainer ";
-    }
+      ParserResourcesType resource = resourcesList[resource_name];
+      // Check if resource is cluster or not
+      if (resource.ClusterMembersList.empty())
+      {
+        //It is not a cluster so we create a cluster with one machine
+        ParserResourcesClusterMembersType fake_node;
+        fake_node.HostName = resource.HostName;
+        fake_node.Protocol = resource.Protocol;
+        fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
+        fake_node.UserName = resource.UserName;
+        fake_node.AppliPath = resource.AppliPath;
+        fake_node.DataForSort = resource.DataForSort;
+
+        resource.ClusterMembersList.push_front(fake_node);
+      }
 
-  else
-    {
-      if (isPythonContainer(params.container_name))
-        tempOutputFile << "SALOME_ContainerPy.py ";
+      // Choose mpi implementation -> each MPI implementation has is own machinefile...
+      if (resource.mpi == lam)
+      {
+        // Creating machine file
+        machine_file_name = tmpnam(NULL);
+        std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
+
+        // We add all cluster machines to the file
+        std::list<ParserResourcesClusterMembersType>::iterator cluster_it = 
+          resource.ClusterMembersList.begin();
+        while (cluster_it != resource.ClusterMembersList.end())
+        {
+          unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes * 
+            (*cluster_it).DataForSort._nbOfProcPerNode;
+          machine_file << (*cluster_it).HostName << " cpu=" << number_of_proc << std::endl;
+          cluster_it++;
+        }
+      }
+      else if (resource.mpi == openmpi)
+      {
+       // Creating machine file
+       machine_file_name = tmpnam(NULL);
+       std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
+
+       // We add all cluster machines to the file
+       std::list<ParserResourcesClusterMembersType>::iterator cluster_it = 
+         resource.ClusterMembersList.begin();
+       while (cluster_it != resource.ClusterMembersList.end())
+       {
+         unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes * 
+           (*cluster_it).DataForSort._nbOfProcPerNode;
+         machine_file << (*cluster_it).HostName << " slots=" << number_of_proc << std::endl;
+         cluster_it++;
+       }
+      }
+      else if (resource.mpi == nompi)
+      {
+        INFOS("[getMachineFile] Error resource_name MPI implementation was defined for " << resource_name);
+      }
       else
-        tempOutputFile << "SALOME_Container ";
-    }
-
-  tempOutputFile << _NS->ContainerName(params) << " -";
-  AddOmninamesParams(tempOutputFile);
-  tempOutputFile << " &" << endl;
-  tempOutputFile.flush();
-  tempOutputFile.close();
-  chmod(_TmpFileName.c_str(), 0x1ED);
-
-  // --- Build command
-
-  string command;
-
-  if (resInfo.Protocol == rsh)
-    {
-      command = "rsh ";
-      string commandRcp = "rcp ";
-      commandRcp += _TmpFileName;
-      commandRcp += " ";
-      commandRcp += machine;
-      commandRcp += ":";
-      commandRcp += _TmpFileName;
-      system(commandRcp.c_str());
+        INFOS("[getMachineFile] Error resource_name MPI implementation not currenly handled for " << resource_name);
     }
-
-  else if (resInfo.Protocol == ssh)
-    command = "ssh ";
+    else
+      INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
+  }
   else
-    throw SALOME_Exception("Unknown protocol");
-
-  command += machine;
-  _CommandForRemAccess = command;
-  command += " ";
-  command += _TmpFileName;
-  command += " > ";
-  command += "/tmp/";
-  command += _NS->ContainerName(params);
-  command += "_";
-  command += machine;
-  command += ".log 2>&1 &";
-  SCRUTE(command);
-
-  return command;
+    INFOS("[getMachineFile] Error parallelLib is not handled -> " << parallelLib);
 
+  return machine_file_name;
 }
-
-
-
-