Salome HOME
Make ContainerManager::GiveContainer threadsafe to allow // launch of containers.
[modules/yacs.git] / src / NamingService / SALOME_NamingService.cxx
index b58acc6611277bf707875c8e3c8719f769c6175c..42c754bb2c1e62452daa2c095fcdc1e96b6f5aa6 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2014  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
@@ -36,7 +36,7 @@
 #include <string>
 #include <cstdio>
 
-#ifdef WNT
+#ifdef WIN32
 #define strdup _strdup
 #endif
 
@@ -576,9 +576,9 @@ SALOME_NamingService::ResolveComponent(const char* hostname,
 
 std::string SALOME_NamingService::ContainerName(const char *containerName)
 {
-  std::string ret;
+  std::string ret,containerNameCpp(containerName);
 
-  if (strlen(containerName) == 0)
+  if (containerNameCpp.empty())
     ret = "FactoryServer";
   else
     ret = containerName;
@@ -587,50 +587,19 @@ std::string SALOME_NamingService::ContainerName(const char *containerName)
 }
 
 // ============================================================================
-/*! \brief build a container name, given a MachineParameters struct.
+/*! \brief build a container name, given a ContainerParameters struct.
  *
- *  Build a container name with a MachineParameters struct. In case of multi
- *  processor machine, container name is suffixed with _nbproc. nproc equals
- *  (number of nodes)*(number of processor per nodes).
- * \param params struct from which we get container name (may be
- *               empty),  number of nodes and number of processor
- *               per node.
+ *  Build a container name with a ContainerParameters struct. In case of multi
+ *  processor machine, container name is suffixed with number of processors.
+ * \param params struct from which we get container name (may be empty) and
+ *               number of processors.
  * \return a container name without the path.
- * \sa BuildContainerNameForNS(const Engines::MachineParameters& params,
+ * \sa BuildContainerNameForNS(const Engines::ContainerParameters& params,
  *                             const char *hostname)
  */
 // ============================================================================
 
-std::string 
-SALOME_NamingService::ContainerName(const Engines::MachineParameters& params)
-{
-  int nbproc;
-
-  if ( !params.isMPI )
-    nbproc = 0;
-  else 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::string ret = ContainerName(params.container_name);
-
-  if ( nbproc >= 1 )
-    {
-      char *suffix = new char[8];
-      sprintf(suffix, "_%d", nbproc);
-      ret += suffix;
-    }
-
-  return ret;
-}
-
-std::string 
-SALOME_NamingService::ContainerName(const Engines::ContainerParameters& params)
+std::string SALOME_NamingService::ContainerName(const Engines::ContainerParameters& params)
 {
   int nbproc;
 
@@ -641,13 +610,13 @@ SALOME_NamingService::ContainerName(const Engines::ContainerParameters& params)
   else
     nbproc = params.nb_proc;
 
-  std::string ret = ContainerName(params.container_name);
+  std::string ret(ContainerName(params.container_name));
 
   if ( nbproc >= 1 )
     {
-      char *suffix = new char[8];
-      sprintf(suffix, "_%d", nbproc);
-      ret += suffix;
+         std::ostringstream suffix;
+         suffix << "_" << nbproc;
+      ret += suffix.str();
     }
 
   return ret;
@@ -667,10 +636,9 @@ SALOME_NamingService::ContainerName(const Engines::ContainerParameters& params)
  */
 // ============================================================================
 
-std::string SALOME_NamingService::BuildContainerNameForNS(const char *containerName,
-                                                     const char *hostname)
+std::string SALOME_NamingService::BuildContainerNameForNS(const char *containerName, const char *hostname)
 {
-  std::string ret = "/Containers/";
+  std::string ret("/Containers/");
   ret += hostname;
   ret += "/";
   ret += ContainerName(containerName);
@@ -690,25 +658,9 @@ std::string SALOME_NamingService::BuildContainerNameForNS(const char *containerN
  */
 // ============================================================================
 
-std::string
-SALOME_NamingService::
-BuildContainerNameForNS(const Engines::MachineParameters& params,
-                        const char *hostname)
+std::string SALOME_NamingService::BuildContainerNameForNS(const Engines::ContainerParameters& params, const char *hostname)
 {
-  std::string ret = "/Containers/";
-  ret += hostname;
-  ret += "/";
-  ret += ContainerName(params);
-
-  return ret;
-}
-
-std::string
-SALOME_NamingService::
-BuildContainerNameForNS(const Engines::ContainerParameters& params,
-                        const char *hostname)
-{
-  std::string ret = "/Containers/";
+  std::string ret("/Containers/");
   ret += hostname;
   ret += "/";
   ret += ContainerName(params);
@@ -775,8 +727,7 @@ throw(ServiceUnreachable)
  */ 
 // ============================================================================
 
-bool SALOME_NamingService::Create_Directory(const char* Path)
-throw(ServiceUnreachable)
+bool SALOME_NamingService::Create_Directory(const char* Path) throw(ServiceUnreachable)
 {
   MESSAGE("BEGIN OF Create_Directory");
 
@@ -820,8 +771,7 @@ throw(ServiceUnreachable)
  */ 
 // ============================================================================
 
-bool SALOME_NamingService::Change_Directory(const char* Path)
-throw(ServiceUnreachable)
+bool SALOME_NamingService::Change_Directory(const char* Path) throw(ServiceUnreachable)
 {
 //   MESSAGE("BEGIN OF Change_Directory " << Path);
   Utils_Locker lock (&_myMutex);
@@ -921,8 +871,7 @@ throw(ServiceUnreachable)
  */ 
 // ============================================================================
 
-char* SALOME_NamingService::Current_Directory()
-throw(ServiceUnreachable)
+char *SALOME_NamingService::Current_Directory() throw(ServiceUnreachable)
 {
   MESSAGE("BEGIN OF Current_Directory");
 
@@ -974,8 +923,7 @@ throw(ServiceUnreachable)
  */ 
 // ============================================================================
 
-void SALOME_NamingService::list()
-throw(ServiceUnreachable)
+void SALOME_NamingService::list() throw(ServiceUnreachable)
 {
   MESSAGE("Begin of list");
 
@@ -1039,10 +987,10 @@ throw(ServiceUnreachable)
  */ 
 // ============================================================================
 
-std::vector<std::string> SALOME_NamingService::list_directory()
-throw(ServiceUnreachable)
+std::vector<std::string> SALOME_NamingService::list_directory() throw(ServiceUnreachable)
 {
 //   MESSAGE("list_directory");
+  Utils_Locker lock (&_myMutex);
   std::vector<std::string> dirList ;
   dirList.resize(0);
 
@@ -1093,10 +1041,10 @@ throw(ServiceUnreachable)
  */ 
 // ============================================================================
 
-std::vector<std::string> SALOME_NamingService::list_subdirs()
-throw(ServiceUnreachable)
+std::vector<std::string> SALOME_NamingService::list_subdirs() throw(ServiceUnreachable)
 {
   MESSAGE("list_subdirs");
+  Utils_Locker lock (&_myMutex);
   std::vector<std::string> dirList ;
   dirList.resize(0);
 
@@ -1326,8 +1274,7 @@ throw(ServiceUnreachable)
  */ 
 // ============================================================================
 
-void SALOME_NamingService::Destroy_Directory(const char* Path)
-throw(ServiceUnreachable)
+void SALOME_NamingService::Destroy_Directory(const char* Path) throw(ServiceUnreachable)
 {
   MESSAGE("BEGIN OF Destroy_Directory " << Path);
 
@@ -1501,10 +1448,10 @@ throw(ServiceUnreachable)
  */ 
 // ============================================================================
 
-void SALOME_NamingService::Destroy_FullDirectory(const char* Path)
-throw(ServiceUnreachable)
+void SALOME_NamingService::Destroy_FullDirectory(const char* Path) throw(ServiceUnreachable)
 {
   MESSAGE("begin of Destroy_FullDirectory " << Path);
+  //no need to lock here because method calls are threadsafe.
   if( Change_Directory(Path) )
     {
       std::vector<std::string> contList = list_directory();
@@ -1527,7 +1474,7 @@ throw(ServiceUnreachable)
 void SALOME_NamingService::_initialize_root_context()
 {
   //MESSAGE("Get the root context");
-
+  //no lock here because initialization is expected to be done once.
   try
     {
       CORBA::Object_var obj = _orb->resolve_initial_references("NameService");