1 // Copyright (C) 2021 CEA/DEN, EDF R&D, OPEN CASCADE
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)
65 void SALOME_Fake_NamingService::Destroy_Directory(const char* Path)
69 void SALOME_Fake_NamingService::Destroy_FullDirectory(const char* Path)
73 bool SALOME_Fake_NamingService::Change_Directory(const char* Path)
78 std::vector<std::string> SALOME_Fake_NamingService::list_subdirs()
80 return std::vector<std::string>();
83 std::vector<std::string> SALOME_Fake_NamingService::list_directory()
85 return std::vector<std::string>();
88 std::vector<std::string> SALOME_Fake_NamingService::list_directory_recurs()
90 return std::vector<std::string>();
93 CORBA::Object_ptr SALOME_Fake_NamingService::Resolve(const char* Path)
95 std::lock_guard<std::mutex> g(_mutex);
96 std::string pathCpp(Path);
97 auto it = _map.find(pathCpp);
98 if( it != _map.end() )
99 return CORBA::Object::_duplicate((*it).second);
100 return CORBA::Object::_nil();
103 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveFirst(const char* Path)
105 return CORBA::Object::_nil();
108 SALOME_NamingService_Abstract *SALOME_Fake_NamingService::clone()
110 return new SALOME_Fake_NamingService;
113 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveComponent(const char* hostname, const char* containerName, const char* componentName, const int nbproc)
115 std::ostringstream oss;
116 oss << SEP << "Containers" << SEP << hostname << SEP << containerName << SEP << componentName;
117 std::string entryToFind(oss.str());
118 return Resolve(entryToFind.c_str());
121 std::vector< std::pair< std::string, Engines::Container_var> > SALOME_Fake_NamingService::ListOfContainersInNS_NoThreadSafe()
123 std::vector< std::pair< std::string, Engines::Container_var> > ret;
126 Engines::Container_var elt = Engines::Container::_narrow(it.second);
127 if(!CORBA::is_nil(elt))
128 ret.push_back({it.first,elt});
133 std::string SALOME_Fake_NamingService::ReprOfContainersIORS_NoThreadSafe()
135 std::vector< std::pair< std::string, Engines::Container_var> > conts( ListOfContainersInNS_NoThreadSafe() );
136 std::ostringstream oss;
137 CORBA::ORB_ptr orb = KERNEL::getORB();
138 char SEP[2] = { '\0', '\0' };
139 constexpr char SEP2[] = " : ";
142 CORBA::String_var ior(orb->object_to_string(it.second));
143 oss << SEP << it.first << SEP2 << ior;
149 std::string SALOME_Fake_NamingService::ReprOfContainersIORS()
151 std::lock_guard<std::mutex> g(_mutex);
152 return ReprOfContainersIORS_NoThreadSafe();
155 std::string SALOME_Fake_NamingService::GetLogContainersFile()
157 return _log_container_file_name;
160 void SALOME_Fake_NamingService::FlushLogContainersFile()
162 std::lock_guard<std::mutex> g(_mutex);
163 FlushLogContainersFile_NoThreadSafe();
166 void SALOME_Fake_NamingService::FlushLogContainersFile_NoThreadSafe()
168 if(!_log_container_file_name.empty())
170 std::string content( ReprOfContainersIORS_NoThreadSafe() );
171 std::ofstream ofs(_log_container_file_name);
172 ofs.write(content.c_str(),content.length());
176 void SALOME_Fake_NamingService::SetLogContainersFile(const std::string& logFileName)
178 if(logFileName.empty())
179 THROW_SALOME_EXCEPTION("SALOME_Fake_NamingService::SetLogContainersFile : empty log name !");
180 constexpr char EXPT_CONTENT[] = "SALOME_Fake_NamingService::SetLogContainersFile : input logFileName write access failed ! no log file set !";
182 std::ofstream ofs(logFileName);
184 THROW_SALOME_EXCEPTION(EXPT_CONTENT);
186 _log_container_file_name = logFileName;