Salome HOME
updated copyright message
[modules/kernel.git] / src / NamingService / SALOME_Fake_NamingService.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_Fake_NamingService.hxx"
21 #include "Utils_SALOME_Exception.hxx"
22 #include "SALOME_KernelORB.hxx"
23
24 #include CORBA_CLIENT_HEADER(SALOME_Component)
25
26 #include <sstream>
27 #include <fstream>
28
29 std::mutex SALOME_Fake_NamingService::_mutex;
30 std::map<std::string,CORBA::Object_var> SALOME_Fake_NamingService::_map;
31 bool SALOME_Fake_NamingService::_log_container_file_thread_launched = false;
32 std::string SALOME_Fake_NamingService::_log_container_file_name;
33
34 SALOME_Fake_NamingService::SALOME_Fake_NamingService(CORBA::ORB_ptr orb)
35 {
36 }
37
38 std::vector< std::string > SALOME_Fake_NamingService::repr()
39 {
40   std::lock_guard<std::mutex> g(_mutex);
41   std::vector< std::string > ret;
42   for(auto it : _map)
43   {
44     ret.push_back( it.first );
45   }
46   return ret;
47 }
48
49 void SALOME_Fake_NamingService::init_orb(CORBA::ORB_ptr orb)
50 {
51 }
52
53 void SALOME_Fake_NamingService::Register(CORBA::Object_ptr ObjRef, const char* Path)
54 {
55   std::lock_guard<std::mutex> g(_mutex);
56   CORBA::Object_var ObjRefAuto = CORBA::Object::_duplicate(ObjRef);
57   _map[Path] = ObjRefAuto;
58   FlushLogContainersFile_NoThreadSafe();
59 }
60
61 void SALOME_Fake_NamingService::Destroy_Name(const char* Path)
62 {
63   std::lock_guard<std::mutex> g(_mutex);
64   std::string pathCpp(Path);
65   auto it = _map.find(pathCpp);
66   if(it!=_map.end())
67     _map.erase(it);
68 }
69
70 void SALOME_Fake_NamingService::Destroy_Directory(const char* Path)
71 {
72 }
73
74 void SALOME_Fake_NamingService::Destroy_FullDirectory(const char* Path)
75 {
76 }
77
78 bool SALOME_Fake_NamingService::Change_Directory(const char* Path)
79 {
80   std::lock_guard<std::mutex> g(_mutex);
81   _current_dir = Path;
82   return true;
83 }
84
85 std::vector<std::string> SALOME_Fake_NamingService::SplitDir(const std::string& fullPath)
86 {
87   constexpr char SEP = '/';
88   std::vector<std::string> ret;
89   if(fullPath.empty())
90     THROW_SALOME_EXCEPTION("Empty input string");
91   if(fullPath.at(0) != SEP)
92     THROW_SALOME_EXCEPTION("input string is expected to start with " << SEP);
93   auto len = fullPath.length();
94   if( len == 1 )
95     THROW_SALOME_EXCEPTION("input string is expected to be different from " << SEP);
96   std::size_t pos = 1;
97   while(pos < len)
98   {
99     std::size_t endPos = fullPath.find_first_of(SEP,pos);
100     std::string elt(fullPath.substr(pos,endPos==std::string::npos?std::string::npos:endPos-pos));
101     ret.push_back(elt);
102     pos = endPos==std::string::npos?std::string::npos:endPos+1;
103   }
104   return ret;
105 }
106
107 std::vector<std::string> SALOME_Fake_NamingService::list_subdirs()
108 {
109   return std::vector<std::string>();
110 }
111
112 std::vector<std::string> SALOME_Fake_NamingService::list_directory()
113 {
114   std::lock_guard<std::mutex> g(_mutex);
115   std::vector<std::string> ret;
116   std::vector<std::string> splitCWD(SplitDir(_current_dir));
117   for(auto it : _map)
118   {
119     std::vector<std::string> splitIt(SplitDir(it.first));
120     if(splitIt.size()<=splitCWD.size())
121       continue;
122     std::vector<std::string> partSplitIt(splitIt.cbegin(),splitIt.cbegin()+splitCWD.size());
123     if(partSplitIt!=splitCWD)
124       continue;
125     ret.push_back(splitIt.at(splitCWD.size()));
126   }
127   return ret;
128 }
129
130 std::vector<std::string> SALOME_Fake_NamingService::list_directory_recurs()
131 {
132   std::vector<std::string> result;
133   for(const std::pair< std::string,CORBA::Object_var>& it : _map)
134     if( it.first.length() >= _current_dir.length() &&
135         it.first.compare(0, _current_dir.length(), _current_dir) == 0)
136       result.push_back(it.first);
137   return result;
138 }
139
140 CORBA::Object_ptr SALOME_Fake_NamingService::Resolve(const char* Path)
141 {
142   std::lock_guard<std::mutex> g(_mutex);
143   std::string pathCpp(Path);
144   auto it = _map.find(pathCpp);
145   if( it != _map.end() )
146     return CORBA::Object::_duplicate((*it).second);
147   return CORBA::Object::_nil();
148 }
149
150 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveFirst(const char* Path)
151 {
152   return CORBA::Object::_nil();
153 }
154
155 SALOME_NamingService_Abstract *SALOME_Fake_NamingService::clone()
156 {
157   return new SALOME_Fake_NamingService;
158 }
159
160 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveComponent(const char* hostname, const char* containerName, const char* componentName, const int nbproc)
161 {
162   std::string entryToFind(BuildComponentName(hostname,containerName,componentName,nbproc));
163   {
164     std::lock_guard<std::mutex> g(_mutex);
165     std::vector<std::string> candidates;
166     for(auto it : _map)
167     {
168       if(it.first.find(entryToFind) == 0)
169         candidates.push_back(it.first);
170     }
171     if(candidates.size() == 1)
172     {
173       auto it = _map.find(candidates[0]);
174       return CORBA::Object::_duplicate((*it).second);
175     }
176     else
177     {
178       return CORBA::Object::_nil();
179     }
180     
181   }
182 }
183
184 std::vector< std::pair< std::string, Engines::Container_var> > SALOME_Fake_NamingService::ListOfContainersInNS_NoThreadSafe()
185 {
186   std::vector< std::pair< std::string, Engines::Container_var> > ret;
187   for(auto it : _map)
188   {
189     Engines::Container_var elt = Engines::Container::_narrow(it.second);
190     if(!CORBA::is_nil(elt))
191       ret.push_back({it.first,elt});
192   }
193   return ret;
194 }
195
196 std::string SALOME_Fake_NamingService::ReprOfContainersIORS_NoThreadSafe()
197 {
198   std::vector< std::pair< std::string, Engines::Container_var> > conts( ListOfContainersInNS_NoThreadSafe() );
199   std::ostringstream oss;
200   CORBA::ORB_ptr orb = KERNEL::getORB();
201   char SEP[2] = { '\0', '\0' };
202   constexpr char SEP2[] = " : ";
203   for(auto it : conts)
204   {
205     CORBA::String_var ior(orb->object_to_string(it.second));
206     oss << SEP << it.first << SEP2 << ior;
207     SEP[0] = '\n';
208   }
209   return oss.str();
210 }
211
212 std::string SALOME_Fake_NamingService::ReprOfContainersIORS()
213 {
214   std::lock_guard<std::mutex> g(_mutex);
215   return ReprOfContainersIORS_NoThreadSafe();
216 }
217
218 std::string SALOME_Fake_NamingService::GetLogContainersFile()
219 {
220   return _log_container_file_name;
221 }
222
223 void SALOME_Fake_NamingService::FlushLogContainersFile()
224 {
225   std::lock_guard<std::mutex> g(_mutex);
226   FlushLogContainersFile_NoThreadSafe();
227 }
228
229 void SALOME_Fake_NamingService::FlushLogContainersFile_NoThreadSafe()
230 {
231   if(!_log_container_file_name.empty())
232   {
233     std::string content( ReprOfContainersIORS_NoThreadSafe() );
234     std::ofstream ofs(_log_container_file_name);
235     ofs.write(content.c_str(),content.length());
236   }
237 }
238
239 void SALOME_Fake_NamingService::SetLogContainersFile(const std::string& logFileName)
240 {
241   if(logFileName.empty())
242     THROW_SALOME_EXCEPTION("SALOME_Fake_NamingService::SetLogContainersFile : empty log name !");
243   constexpr char EXPT_CONTENT[] = "SALOME_Fake_NamingService::SetLogContainersFile : input logFileName write access failed ! no log file set !";
244   {
245     std::ofstream ofs(logFileName);
246     if(!ofs)
247       THROW_SALOME_EXCEPTION(EXPT_CONTENT);
248   }
249   _log_container_file_name = logFileName;
250 }