Salome HOME
YACS in SSL mode
[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 }
64
65 void SALOME_Fake_NamingService::Destroy_Directory(const char* Path)
66 {
67 }
68
69 void SALOME_Fake_NamingService::Destroy_FullDirectory(const char* Path)
70 {
71 }
72
73 bool SALOME_Fake_NamingService::Change_Directory(const char* Path)
74 {
75   return true;
76 }
77
78 std::vector<std::string> SALOME_Fake_NamingService::list_subdirs()
79 {
80   return std::vector<std::string>();
81 }
82
83 std::vector<std::string> SALOME_Fake_NamingService::list_directory()
84 {
85   return std::vector<std::string>();
86 }
87
88 std::vector<std::string> SALOME_Fake_NamingService::list_directory_recurs()
89 {
90   return std::vector<std::string>();
91 }
92
93 CORBA::Object_ptr SALOME_Fake_NamingService::Resolve(const char* Path)
94 {
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();
101 }
102
103 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveFirst(const char* Path)
104 {
105   return CORBA::Object::_nil();
106 }
107
108 SALOME_NamingService_Abstract *SALOME_Fake_NamingService::clone()
109 {
110   return new SALOME_Fake_NamingService;
111 }
112
113 CORBA::Object_ptr SALOME_Fake_NamingService::ResolveComponent(const char* hostname, const char* containerName, const char* componentName, const int nbproc)
114 {
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());
119 }
120
121 std::vector< std::pair< std::string, Engines::Container_var> > SALOME_Fake_NamingService::ListOfContainersInNS_NoThreadSafe()
122 {
123   std::vector< std::pair< std::string, Engines::Container_var> > ret;
124   for(auto it : _map)
125   {
126     Engines::Container_var elt = Engines::Container::_narrow(it.second);
127     if(!CORBA::is_nil(elt))
128       ret.push_back({it.first,elt});
129   }
130   return ret;
131 }
132
133 std::string SALOME_Fake_NamingService::ReprOfContainersIORS_NoThreadSafe()
134 {
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[] = " : ";
140   for(auto it : conts)
141   {
142     CORBA::String_var ior(orb->object_to_string(it.second));
143     oss << SEP << it.first << SEP2 << ior;
144     SEP[0] = '\n';
145   }
146   return oss.str();
147 }
148
149 std::string SALOME_Fake_NamingService::ReprOfContainersIORS()
150 {
151   std::lock_guard<std::mutex> g(_mutex);
152   return ReprOfContainersIORS_NoThreadSafe();
153 }
154
155 std::string SALOME_Fake_NamingService::GetLogContainersFile()
156 {
157   return _log_container_file_name;
158 }
159
160 void SALOME_Fake_NamingService::FlushLogContainersFile()
161 {
162   std::lock_guard<std::mutex> g(_mutex);
163   FlushLogContainersFile_NoThreadSafe();
164 }
165
166 void SALOME_Fake_NamingService::FlushLogContainersFile_NoThreadSafe()
167 {
168   if(!_log_container_file_name.empty())
169   {
170     std::string content( ReprOfContainersIORS_NoThreadSafe() );
171     std::ofstream ofs(_log_container_file_name);
172     ofs.write(content.c_str(),content.length());
173   }
174 }
175
176 void SALOME_Fake_NamingService::SetLogContainersFile(const std::string& logFileName)
177 {
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 !";
181   {
182     std::ofstream ofs(logFileName);
183     if(!ofs)
184       THROW_SALOME_EXCEPTION(EXPT_CONTENT);
185   }
186   _log_container_file_name = logFileName;
187 }