1 // Copyright (C) 2021-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SALOME_Fake_NamingService.hxx"
21 #include "Utils_SALOME_Exception.hxx"
22 #include "SALOME_KernelORB.hxx"
24 #include CORBA_CLIENT_HEADER(SALOME_Component)
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;
34 SALOME_Fake_NamingService::SALOME_Fake_NamingService(CORBA::ORB_ptr orb)
38 std::vector< std::string > SALOME_Fake_NamingService::repr()
40 std::lock_guard<std::mutex> g(_mutex);
41 std::vector< std::string > ret;
44 ret.push_back( it.first );
49 void SALOME_Fake_NamingService::init_orb(CORBA::ORB_ptr orb)
53 void SALOME_Fake_NamingService::Register(CORBA::Object_ptr ObjRef, const char* Path)
55 std::lock_guard<std::mutex> g(_mutex);
56 CORBA::Object_var ObjRefAuto = CORBA::Object::_duplicate(ObjRef);
57 _map[Path] = ObjRefAuto;
58 FlushLogContainersFile_NoThreadSafe();
61 void SALOME_Fake_NamingService::Destroy_Name(const char* Path)
63 std::lock_guard<std::mutex> g(_mutex);
64 std::string pathCpp(Path);
65 auto it = _map.find(pathCpp);
70 void SALOME_Fake_NamingService::Destroy_Directory(const char* Path)
74 void SALOME_Fake_NamingService::Destroy_FullDirectory(const char* Path)
78 bool SALOME_Fake_NamingService::Change_Directory(const char* Path)
80 std::lock_guard<std::mutex> g(_mutex);
85 std::vector<std::string> SALOME_Fake_NamingService::SplitDir(const std::string& fullPath)
87 constexpr char SEP = '/';
88 std::vector<std::string> ret;
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();
95 THROW_SALOME_EXCEPTION("input string is expected to be different from " << SEP);
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));
102 pos = endPos==std::string::npos?std::string::npos:endPos+1;
107 std::vector<std::string> SALOME_Fake_NamingService::list_subdirs()
109 return std::vector<std::string>();
112 std::vector<std::string> SALOME_Fake_NamingService::list_directory()
114 std::lock_guard<std::mutex> g(_mutex);
115 std::vector<std::string> ret;
116 std::vector<std::string> splitCWD(SplitDir(_current_dir));
119 std::vector<std::string> splitIt(SplitDir(it.first));
120 if(splitIt.size()<=splitCWD.size())
122 std::vector<std::string> partSplitIt(splitIt.cbegin(),splitIt.cbegin()+splitCWD.size());
123 if(partSplitIt!=splitCWD)
125 ret.push_back(splitIt.at(splitCWD.size()));
130 std::vector<std::string> SALOME_Fake_NamingService::list_directory_recurs()
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);
140 CORBA::Object_ptr SALOME_Fake_NamingService::Resolve(const char* Path)
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();
150 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveFirst(const char* Path)
152 return CORBA::Object::_nil();
155 SALOME_NamingService_Abstract *SALOME_Fake_NamingService::clone()
157 return new SALOME_Fake_NamingService;
160 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveComponent(const char* hostname, const char* containerName, const char* componentName, const int nbproc)
162 std::string entryToFind(BuildComponentName(hostname,containerName,componentName,nbproc));
164 std::lock_guard<std::mutex> g(_mutex);
165 std::vector<std::string> candidates;
168 if(it.first.find(entryToFind) == 0)
169 candidates.push_back(it.first);
171 if(candidates.size() == 1)
173 auto it = _map.find(candidates[0]);
174 return CORBA::Object::_duplicate((*it).second);
178 return CORBA::Object::_nil();
184 std::vector< std::pair< std::string, Engines::Container_var> > SALOME_Fake_NamingService::ListOfContainersInNS_NoThreadSafe()
186 std::vector< std::pair< std::string, Engines::Container_var> > ret;
189 Engines::Container_var elt = Engines::Container::_narrow(it.second);
190 if(!CORBA::is_nil(elt))
191 ret.push_back({it.first,elt});
196 std::string SALOME_Fake_NamingService::ReprOfContainersIORS_NoThreadSafe()
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[] = " : ";
205 CORBA::String_var ior(orb->object_to_string(it.second));
206 oss << SEP << it.first << SEP2 << ior;
212 std::string SALOME_Fake_NamingService::ReprOfContainersIORS()
214 std::lock_guard<std::mutex> g(_mutex);
215 return ReprOfContainersIORS_NoThreadSafe();
218 std::string SALOME_Fake_NamingService::GetLogContainersFile()
220 return _log_container_file_name;
223 void SALOME_Fake_NamingService::FlushLogContainersFile()
225 std::lock_guard<std::mutex> g(_mutex);
226 FlushLogContainersFile_NoThreadSafe();
229 void SALOME_Fake_NamingService::FlushLogContainersFile_NoThreadSafe()
231 if(!_log_container_file_name.empty())
233 std::string content( ReprOfContainersIORS_NoThreadSafe() );
234 std::ofstream ofs(_log_container_file_name);
235 ofs.write(content.c_str(),content.length());
239 void SALOME_Fake_NamingService::SetLogContainersFile(const std::string& logFileName)
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 !";
245 std::ofstream ofs(logFileName);
247 THROW_SALOME_EXCEPTION(EXPT_CONTENT);
249 _log_container_file_name = logFileName;