]> SALOME platform Git repositories - modules/kernel.git/blob - src/NamingService/SALOME_Fake_NamingService.cxx
Salome HOME
Merge branch 'master' into omu/launcher_ssl
[modules/kernel.git] / src / NamingService / SALOME_Fake_NamingService.cxx
1 // Copyright (C) 2021  CEA/DEN, EDF R&D, OPEN CASCADE
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   auto len = _current_dir.length();
118   for(auto it : _map)
119   {
120     std::vector<std::string> splitIt(SplitDir(it.first));
121     if(splitIt.size()<=splitCWD.size())
122       continue;
123     std::vector<std::string> partSplitIt(splitIt.cbegin(),splitIt.cbegin()+splitCWD.size());
124     if(partSplitIt!=splitCWD)
125       continue;
126     ret.push_back(splitIt.at(splitCWD.size()));
127   }
128   return ret;
129 }
130
131 std::vector<std::string> SALOME_Fake_NamingService::list_directory_recurs()
132 {
133   return std::vector<std::string>();
134 }
135
136 CORBA::Object_ptr SALOME_Fake_NamingService::Resolve(const char* Path)
137 {
138   std::lock_guard<std::mutex> g(_mutex);
139   std::string pathCpp(Path);
140   auto it = _map.find(pathCpp);
141   if( it != _map.end() )
142     return CORBA::Object::_duplicate((*it).second);
143   return CORBA::Object::_nil();
144 }
145
146 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveFirst(const char* Path)
147 {
148   return CORBA::Object::_nil();
149 }
150
151 SALOME_NamingService_Abstract *SALOME_Fake_NamingService::clone()
152 {
153   return new SALOME_Fake_NamingService;
154 }
155
156 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveComponent(const char* hostname, const char* containerName, const char* componentName, const int nbproc)
157 {
158   std::ostringstream oss;
159   oss << SEP << "Containers" << SEP << hostname << SEP << containerName << SEP << componentName;
160   std::string entryToFind(oss.str());
161   return Resolve(entryToFind.c_str());
162 }
163
164 std::vector< std::pair< std::string, Engines::Container_var> > SALOME_Fake_NamingService::ListOfContainersInNS_NoThreadSafe()
165 {
166   std::vector< std::pair< std::string, Engines::Container_var> > ret;
167   for(auto it : _map)
168   {
169     Engines::Container_var elt = Engines::Container::_narrow(it.second);
170     if(!CORBA::is_nil(elt))
171       ret.push_back({it.first,elt});
172   }
173   return ret;
174 }
175
176 std::string SALOME_Fake_NamingService::ReprOfContainersIORS_NoThreadSafe()
177 {
178   std::vector< std::pair< std::string, Engines::Container_var> > conts( ListOfContainersInNS_NoThreadSafe() );
179   std::ostringstream oss;
180   CORBA::ORB_ptr orb = KERNEL::getORB();
181   char SEP[2] = { '\0', '\0' };
182   constexpr char SEP2[] = " : ";
183   for(auto it : conts)
184   {
185     CORBA::String_var ior(orb->object_to_string(it.second));
186     oss << SEP << it.first << SEP2 << ior;
187     SEP[0] = '\n';
188   }
189   return oss.str();
190 }
191
192 std::string SALOME_Fake_NamingService::ReprOfContainersIORS()
193 {
194   std::lock_guard<std::mutex> g(_mutex);
195   return ReprOfContainersIORS_NoThreadSafe();
196 }
197
198 std::string SALOME_Fake_NamingService::GetLogContainersFile()
199 {
200   return _log_container_file_name;
201 }
202
203 void SALOME_Fake_NamingService::FlushLogContainersFile()
204 {
205   std::lock_guard<std::mutex> g(_mutex);
206   FlushLogContainersFile_NoThreadSafe();
207 }
208
209 void SALOME_Fake_NamingService::FlushLogContainersFile_NoThreadSafe()
210 {
211   if(!_log_container_file_name.empty())
212   {
213     std::string content( ReprOfContainersIORS_NoThreadSafe() );
214     std::ofstream ofs(_log_container_file_name);
215     ofs.write(content.c_str(),content.length());
216   }
217 }
218
219 void SALOME_Fake_NamingService::SetLogContainersFile(const std::string& logFileName)
220 {
221   if(logFileName.empty())
222     THROW_SALOME_EXCEPTION("SALOME_Fake_NamingService::SetLogContainersFile : empty log name !");
223   constexpr char EXPT_CONTENT[] = "SALOME_Fake_NamingService::SetLogContainersFile : input logFileName write access failed ! no log file set !";
224   {
225     std::ofstream ofs(logFileName);
226     if(!ofs)
227       THROW_SALOME_EXCEPTION(EXPT_CONTENT);
228   }
229   _log_container_file_name = logFileName;
230 }