Salome HOME
add method NameChanged to update title name
[modules/kernel.git] / src / ResourcesManager / ResourcesManager.cxx
index 23d8e781e1221018ac15a5a9ccebf766964bdde4..03a0bf6847d35356505e60685af12c5eafc149d3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  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
@@ -21,6 +21,7 @@
 //
 
 #include "ResourcesManager.hxx" 
+#include "SALOME_ResourcesCatalog_Handler.hxx"
 #include <Basics_Utils.hxx>
 #include <fstream>
 #include <iostream>
@@ -30,7 +31,7 @@
 #include <list>
 #include <sys/types.h>
 #include <sys/stat.h>
-#ifdef WNT
+#ifdef WIN32
 #else
 #include <unistd.h>
 #endif
 
 #define MAX_SIZE_FOR_HOSTNAME 256;
 
+using namespace std;
+
+const string ResourcesManager_cpp::DEFAULT_RESOURCE_NAME = "localhost";
+
 static LoadRateManagerFirst first;
 static LoadRateManagerCycl cycl;
 static LoadRateManagerAltCycl altcycl;
+
+resourceParams::resourceParams()
+: can_launch_batch_jobs(false),
+  can_run_containers(false),
+  nb_proc(-1),
+  nb_node(-1),
+  nb_proc_per_node(-1),
+  cpu_clock(-1),
+  mem_mb(-1)
+{
+}
+
 //=============================================================================
 /*!
  * just for test
@@ -61,6 +78,8 @@ ResourcesManager_cpp(const char *xmlFilePath)
   _resourceManagerMap["altcycl"]=&altcycl;
   _resourceManagerMap["best"]=&altcycl;
   _resourceManagerMap[""]=&altcycl;
+
+  AddDefaultResourceInCatalog();
 }
 
 //=============================================================================
@@ -84,6 +103,8 @@ ResourcesManager_cpp::ResourcesManager_cpp() throw(ResourcesException)
   _resourceManagerMap["best"]=&altcycl;
   _resourceManagerMap[""]=&altcycl;
 
+  AddDefaultResourceInCatalog();
+
   bool default_catalog_resource = true;
   if (getenv("USER_CATALOG_RESOURCES_FILE") != 0)
   {
@@ -266,13 +287,25 @@ ResourcesManager_cpp::GetFittingResources(const resourceParams& params) throw(Re
 
   // Step 5
   SelectOnlyResourcesWithOS(vec, params.OS.c_str());
-    
+
   // Step 6
   std::vector<std::string> vec_save(vec);
   KeepOnlyResourcesWithComponent(vec, params.componentList);
   if (vec.size() == 0)
     vec = vec_save;
 
+  // Step 7 : Filter on possible usage
+  vector<string> prev_list(vec);
+  vec.clear();
+  for (vector<string>::iterator iter = prev_list.begin() ; iter != prev_list.end() ; iter++)
+  {
+    MapOfParserResourcesType::const_iterator it = _resourcesList.find(*iter);
+    if (it != _resourcesList.end() &&
+        (!params.can_launch_batch_jobs || it->second.can_launch_batch_jobs) &&
+        (!params.can_run_containers || it->second.can_run_containers))
+      vec.push_back(*iter);
+  }
+
   // End
   // Send an exception if return list is empty...
   if (vec.size() == 0)
@@ -291,8 +324,12 @@ ResourcesManager_cpp::GetFittingResources(const resourceParams& params) throw(Re
 //=============================================================================
 
 void
-ResourcesManager_cpp::AddResourceInCatalog(const ParserResourcesType & new_resource) throw(ResourcesException)
+ResourcesManager_cpp::AddResourceInCatalog(const ParserResourcesType & new_resource)
 {
+  if (new_resource.Name == DEFAULT_RESOURCE_NAME){
+    std::string error("Cannot modify default local resource \"" + DEFAULT_RESOURCE_NAME + "\"");
+    throw ResourcesException(error);
+  }
   // TODO - Add minimal check
   _resourcesList[new_resource.Name] = new_resource;
 }
@@ -305,6 +342,10 @@ ResourcesManager_cpp::AddResourceInCatalog(const ParserResourcesType & new_resou
 
 void ResourcesManager_cpp::DeleteResourceInCatalog(const char * name)
 {
+  if (DEFAULT_RESOURCE_NAME == name){
+    std::string error("Cannot delete default local resource \"" + DEFAULT_RESOURCE_NAME + "\"");
+    throw ResourcesException(error);
+  }
   MapOfParserResourcesType_it it = _resourcesList.find(name);
   if (it != _resourcesList.end())
     _resourcesList.erase(name);
@@ -322,6 +363,16 @@ void ResourcesManager_cpp::WriteInXmlFile(std::string xml_file)
 {
   RES_MESSAGE("WriteInXmlFile : start");
 
+  MapOfParserResourcesType resourceListToSave(_resourcesList);
+  // We do not save default local resource because it is automatically created at startup
+  resourceListToSave.erase(DEFAULT_RESOURCE_NAME);
+  if (resourceListToSave.empty())
+  {
+    RES_MESSAGE("WriteInXmlFile: nothing to do, no resource except default \"" <<
+                DEFAULT_RESOURCE_NAME << "\"");
+    return;
+  }
+
   if (xml_file == "")
   {
     _path_resources_it = _path_resources.begin();
@@ -341,7 +392,7 @@ void ResourcesManager_cpp::WriteInXmlFile(std::string xml_file)
   xmlNewDocComment(aDoc, BAD_CAST "ResourcesCatalog");
 
   SALOME_ResourcesCatalog_Handler* handler =
-    new SALOME_ResourcesCatalog_Handler(_resourcesList);
+    new SALOME_ResourcesCatalog_Handler(resourceListToSave);
   handler->PrepareDocToXmlFile(aDoc);
   delete handler;
 
@@ -371,7 +422,7 @@ const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFiles()
     int result = stat((*_path_resources_it).c_str(), &statinfo);
     if (result < 0)
     {
-      std::cerr << "Error in method stat for file : " << (*_path_resources_it).c_str() << " no new xml file is parsed" << std::endl;
+      RES_MESSAGE("Resource file " << *_path_resources_it << " does not exist");
       return _resourcesList;
     }
 
@@ -385,14 +436,14 @@ const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFiles()
   if (to_parse)
   {
     _resourcesList.clear();
+    AddDefaultResourceInCatalog();
     // On parse tous les fichiers
     for(_path_resources_it = _path_resources.begin(); _path_resources_it != _path_resources.end(); ++_path_resources_it)
     {
       MapOfParserResourcesType _resourcesList_tmp;
       MapOfParserResourcesType _resourcesBatchList_tmp;
-      SALOME_ResourcesCatalog_Handler* handler =
-        new SALOME_ResourcesCatalog_Handler(_resourcesList_tmp);
-      const char* aFilePath = (*_path_resources_it).c_str();
+      SALOME_ResourcesCatalog_Handler *handler( new SALOME_ResourcesCatalog_Handler(_resourcesList_tmp) );
+      const char *aFilePath( (*_path_resources_it).c_str() );
       FILE* aFile = fopen(aFilePath, "r");
 
       if (aFile != NULL)
@@ -406,13 +457,28 @@ const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFiles()
           for (MapOfParserResourcesType_it i = _resourcesList_tmp.begin(); i != _resourcesList_tmp.end(); ++i)
           {
             MapOfParserResourcesType_it j = _resourcesList.find(i->first);
-            if (j == _resourcesList.end())
+            if (i->second.HostName == DEFAULT_RESOURCE_NAME || i->second.HostName == Kernel_Utils::GetHostname())
             {
-              _resourcesList[i->first] = i->second;
+              MapOfParserResourcesType_it it0(_resourcesList.find(DEFAULT_RESOURCE_NAME));
+              if(it0!=_resourcesList.end())
+                {
+                  ParserResourcesType& localhostElt((*it0).second);
+                  localhostElt.DataForSort._nbOfNodes=(*i).second.DataForSort._nbOfNodes;
+                  localhostElt.DataForSort._nbOfProcPerNode=(*i).second.DataForSort._nbOfProcPerNode;
+                  localhostElt.DataForSort._CPUFreqMHz=(*i).second.DataForSort._CPUFreqMHz;
+                  localhostElt.DataForSort._memInMB=(*i).second.DataForSort._memInMB;
+                }
+              RES_MESSAGE("Resource " << i->first << " is not added because it is the same "
+                          "machine as default local resource \"" << DEFAULT_RESOURCE_NAME << "\"");
+            }
+            else if (j != _resourcesList.end())
+            {
+              cerr << "ParseXmlFiles Warning, two resources with the same name were found, "
+                      "taking the first declaration : " << i->first << endl;
             }
             else
             {
-              std::cerr << "ParseXmlFiles Warning, two resources with the same name were found, taking the first declaration : " << i->first << std::endl;
+              _resourcesList[i->first] = i->second;
             }
           }
         }
@@ -442,11 +508,16 @@ const MapOfParserResourcesType& ResourcesManager_cpp::GetList() const
   return _resourcesList;
 }
 
-std::string ResourcesManager_cpp::Find(const std::string& policy, const std::vector<std::string>& listOfResources)
+//! threadsafe
+std::string ResourcesManager_cpp::Find(const std::string& policy, const std::vector<std::string>& listOfResources) const
 {
-  if(_resourceManagerMap.count(policy)==0)
-    return _resourceManagerMap[""]->Find(listOfResources, _resourcesList);
-  return _resourceManagerMap[policy]->Find(listOfResources, _resourcesList);
+  std::map<std::string , LoadRateManager*>::const_iterator it(_resourceManagerMap.find(policy));
+  if(it==_resourceManagerMap.end())
+       {
+         it=_resourceManagerMap.find("");
+         return ((*it).second)->Find(listOfResources, _resourcesList);
+       }
+  return ((*it).second)->Find(listOfResources, _resourcesList);
 }
 
 //=============================================================================
@@ -512,12 +583,12 @@ ResourcesManager_cpp::KeepOnlyResourcesWithComponent(std::vector<std::string>& r
   resources=kept_resources;
 }
 
-
-ParserResourcesType 
-ResourcesManager_cpp::GetResourcesDescr(const std::string & name)
+//! thread safe
+ParserResourcesType ResourcesManager_cpp::GetResourcesDescr(const std::string & name) const
 {
-  if (_resourcesList.find(name) != _resourcesList.end())
-    return _resourcesList[name];
+  MapOfParserResourcesType::const_iterator it(_resourcesList.find(name));
+  if (it != _resourcesList.end())
+    return (*it).second;
   else
   {
     std::string error("[GetResourcesDescr] Resource does not exist: ");
@@ -525,3 +596,28 @@ ResourcesManager_cpp::GetResourcesDescr(const std::string & name)
     throw ResourcesException(error);
   }
 }
+
+void ResourcesManager_cpp::AddDefaultResourceInCatalog()
+{
+  ParserResourcesType resource;
+  resource.Name = DEFAULT_RESOURCE_NAME;
+  // We can't use "localhost" for parameter hostname because the containers are registered in the
+  // naming service with the real hostname, not "localhost"
+  resource.HostName = Kernel_Utils::GetHostname();
+  resource.DataForSort._Name = DEFAULT_RESOURCE_NAME;
+  resource.Protocol = sh;
+  resource.Batch = none;
+  if (getenv("HOME") != NULL && getenv("APPLI") != NULL)
+  {
+    resource.AppliPath = string(getenv("HOME")) + "/" + getenv("APPLI");
+  }
+  string tmpdir = "/tmp";
+  if (getenv("TMPDIR") != NULL)
+    tmpdir = getenv("TMPDIR");
+  resource.working_directory = tmpdir + "/salome_localres_workdir";
+  if (getenv("USER") != NULL)
+    resource.working_directory += string("_") + getenv("USER");
+  resource.can_launch_batch_jobs = true;
+  resource.can_run_containers = true;
+  _resourcesList[resource.Name] = resource;
+}