Salome HOME
updated copyright message
[modules/kernel.git] / src / NamingService / SALOME_NamingService_Abstract.cxx
1 // Copyright (C) 2021-2023  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SALOME_NamingService_Abstract.hxx"
21 #include "Utils_SALOME_Exception.hxx"
22
23 #include <sstream>
24 #include <memory>
25
26 SALOME_NamingService_Abstract *SALOME_NamingService_Abstract::cloneCoVar()
27 {
28   std::unique_ptr<SALOME_NamingService_Container_Abstract> ret(this->clone());
29   SALOME_NamingService_Abstract *ret2(dynamic_cast<SALOME_NamingService_Abstract *>(ret.get()));
30   if(!ret2)
31     throw SALOME_Exception(std::string("SALOME_NamingService_Abstract::cloneCoVar : clone is expected to return a SALOME_NamingService_Abstract type !"));
32   ret.release();
33   return ret2;
34 }
35
36 // ============================================================================
37 /*! \brief provide a default container name if empty.
38  *
39  *  the given container name is returned unchanged, unless it is empty.
40  * \param  containerName
41  * \return container name, where empty input is replaced by "FactoryServer",
42  *         without the path.
43  * \sa BuildContainerNameForNS(const char *containerName, const char *hostname)
44  */
45 // ============================================================================
46
47 std::string SALOME_NamingService_Abstract::ContainerName(const char *containerName)
48 {
49   std::string ret,containerNameCpp(containerName);
50
51   if (containerNameCpp.empty())
52     ret = "FactoryServer";
53   else
54     ret = containerName;
55
56   return ret;
57 }
58
59 // ============================================================================
60 /*! \brief build a string representing a container in Naming Service.
61  *
62  *  Build a string representing the absolute pathname of a container in
63  *  SALOME_NamingService. This form gives a suffixed containerName in case of
64  *  multi processor machine.
65  * \param containerName name of the container in which the component is
66                         instantiated.
67  * \param hostname name of the host of the container, without domain names.
68  * \return the path under the form /Containers/hostname/containerName
69  * \sa ContainerName(const Engines::MachineParameters& params)
70  */
71 // ============================================================================
72
73 std::string SALOME_NamingService_Abstract::BuildContainerNameForNS(const char *containerName, const char *hostname)
74 {
75   std::string ret("/Containers/");
76   ret += hostname;
77   ret += "/";
78   ret += ContainerName(containerName);
79
80   return ret;
81 }
82
83 // ============================================================================
84 /*! \brief build a container name, given a ContainerParameters struct.
85  *
86  *  Build a container name with a ContainerParameters struct. In case of multi
87  *  processor machine, container name is suffixed with number of processors.
88  * \param params struct from which we get container name (may be empty) and
89  *               number of processors.
90  * \return a container name without the path.
91  * \sa BuildContainerNameForNS(const Engines::ContainerParameters& params,
92  *                             const char *hostname)
93  */
94 // ============================================================================
95
96 std::string SALOME_NamingService_Abstract::ContainerName(const Engines::ContainerParameters& params)
97 {
98   int nbproc;
99
100   if ( !params.isMPI )
101     nbproc = 0;
102   else if ( params.nb_proc <= 0 )
103     nbproc = 1;
104   else
105     nbproc = params.nb_proc;
106
107   std::string ret(SALOME_NamingService_Abstract::ContainerName(params.container_name));
108
109   if ( nbproc >= 1 )
110     {
111           std::ostringstream suffix;
112           suffix << "_" << nbproc;
113       ret += suffix.str();
114     }
115
116   return ret;
117 }
118
119 // ============================================================================
120 /*! \brief build a string representing a container in Naming Service.
121  *
122  *  Build a string representing the absolute pathname of a container in
123  *  SALOME_NamingService.
124  * \param params used as it is, or replaced by FactoryServer if empty.
125  * \param hostname name of the host of the container, without domain names.
126  * \return the path under the form /Containers/hostname/containerName
127  * \sa ContainerName(const char *containerName)
128  */
129 // ============================================================================
130
131 std::string SALOME_NamingService_Abstract::BuildContainerNameForNS(const Engines::ContainerParameters& params, const char *hostname)
132 {
133   std::string ret("/Containers/");
134   ret += hostname;
135   ret += "/";
136   ret += ContainerName(params);
137
138   return ret;
139 }
140 std::string SALOME_NamingService_Abstract::BuildComponentName(const char* hostname, const char* containerName, const char* componentName, const int /*nbproc*/)
141 {
142   std::ostringstream oss;
143   oss << SEP << "Containers" << SEP << hostname << SEP << containerName << SEP << componentName << "_inst_";
144   return oss.str();
145 }
146
147 std::vector< std::string > SALOME_NamingService_Abstract_Decorator::repr()
148 {
149   return std::vector< std::string >();
150 }
151
152 void SALOME_NamingService_Abstract_Decorator::Destroy_Directory(const char* Path)
153 {
154   THROW_SALOME_EXCEPTION("SALOME_NamingService_Abstract_Decorator::Destroy_Directory");
155 }
156
157 bool SALOME_NamingService_Abstract_Decorator::Change_Directory(const char* Path)
158 {//do nothing
159   return true;
160 }
161
162 std::vector<std::string> SALOME_NamingService_Abstract_Decorator::list_subdirs()
163 {
164   THROW_SALOME_EXCEPTION("SALOME_NamingService_Abstract_Decorator::list_subdirs");
165 }
166
167 std::vector<std::string> SALOME_NamingService_Abstract_Decorator::list_directory()
168 {
169   THROW_SALOME_EXCEPTION("SALOME_NamingService_Abstract_Decorator::list_directory");
170 }
171
172 std::vector<std::string> SALOME_NamingService_Abstract_Decorator::list_directory_recurs()
173 {
174   THROW_SALOME_EXCEPTION("SALOME_NamingService_Abstract_Decorator::list_directory_recurs");
175 }
176
177 CORBA::Object_ptr SALOME_NamingService_Abstract_Decorator::ResolveComponent(const char* hostname, const char* containerName, const char* componentName, const int nbproc)
178 {
179   std::string entryToFind(BuildComponentName(hostname,containerName,componentName,nbproc));
180   return _ns_cont->Resolve(entryToFind.c_str());
181 }