1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
#include "editor/config_loader.hpp"
#include "editor/editor_config.hpp"
#include "platform/platform.hpp"
#include "coding/internal/file_data.hpp"
#include "coding/reader.hpp"
#include "std/fstream.hpp"
#include "std/iterator.hpp"
#include "3party/Alohalytics/src/http_client.h"
#include "3party/pugixml/src/pugixml.hpp"
namespace
{
using alohalytics::HTTPClientPlatformWrapper;
auto const kConfigFileName = "editor.config";
auto const kHashFileName = "editor.config.hash";
auto const kSynchroTimeout = hours(4);
auto const kRemoteHashUrl = "http://osmz.ru/mwm/editor.config.date";
auto const kRemoteConfigUrl = "http://osmz.ru/mwm/editor.config";
string GetConfigFilePath() { return GetPlatform().WritablePathForFile(kConfigFileName); }
string GetHashFilePath() { return GetPlatform().WritablePathForFile(kHashFileName); }
string RunSimpleHttpRequest(string const & url)
{
HTTPClientPlatformWrapper request(url);
bool result = false;
try
{
result = request.RunHTTPRequest();
}
catch (runtime_error const & ex)
{
LOG(LWARNING, ("Exception from HTTPClientPlatformWrapper::RunHTTPRequest, message: ", ex.what()));
}
if (result && !request.was_redirected() && request.error_code() == 200) // 200 - http status OK
{
return request.server_response();
}
return {};
}
} // namespace
namespace editor
{
void Waiter::Interrupt()
{
{
lock_guard<mutex> lock(m_mutex);
m_interrupted = true;
}
m_event.notify_all();
}
ConfigLoader::ConfigLoader(EditorConfigWrapper & config) : m_config(config)
{
pugi::xml_document doc;
LoadFromLocal(doc);
ResetConfig(doc);
m_loaderThread = thread(&ConfigLoader::LoadFromServer, this);
}
ConfigLoader::~ConfigLoader()
{
m_waiter.Interrupt();
m_loaderThread.join();
}
void ConfigLoader::LoadFromServer()
{
auto const hash = LoadHash(GetHashFilePath());
try
{
do
{
auto const remoteHash = GetRemoteHash();
if (remoteHash.empty() || hash == remoteHash)
continue;
pugi::xml_document doc;
GetRemoteConfig(doc);
if (SaveAndReload(doc))
SaveHash(remoteHash, GetHashFilePath());
} while (m_waiter.Wait(kSynchroTimeout));
}
catch (RootException const & ex)
{
LOG(LERROR, (ex.Msg()));
}
}
bool ConfigLoader::SaveAndReload(pugi::xml_document const & doc)
{
if (doc.empty())
return false;
auto const filePath = GetConfigFilePath();
auto const result =
my::WriteToTempAndRenameToFile(filePath, [&doc](string const & fileName)
{
return doc.save_file(fileName.c_str(), " ");
});
if (!result)
return false;
ResetConfig(doc);
return true;
}
void ConfigLoader::ResetConfig(pugi::xml_document const & doc)
{
auto config = make_shared<EditorConfig>();
config->SetConfig(doc);
m_config.Set(config);
}
// static
void ConfigLoader::LoadFromLocal(pugi::xml_document & doc)
{
string content;
unique_ptr<ModelReader> reader;
try
{
reader = GetPlatform().GetReader(kConfigFileName);
}
catch (RootException const & ex)
{
LOG(LERROR, (ex.Msg()));
return;
}
if (reader)
reader->ReadAsString(content);
if (!doc.load_buffer(content.data(), content.size()))
{
LOG(LERROR, ("Config can not be loaded."));
doc.reset();
}
}
// static
string ConfigLoader::GetRemoteHash()
{
return RunSimpleHttpRequest(kRemoteHashUrl);
}
// static
void ConfigLoader::GetRemoteConfig(pugi::xml_document & doc)
{
auto const result = RunSimpleHttpRequest(kRemoteConfigUrl);
if (result.empty())
return;
if (!doc.load_string(result.c_str(), pugi::parse_default | pugi::parse_comments))
doc.reset();
}
// static
bool ConfigLoader::SaveHash(string const & hash, string const & filePath)
{
auto const result =
my::WriteToTempAndRenameToFile(filePath, [&hash](string const & fileName)
{
ofstream ofs(fileName, ofstream::out);
if (!ofs.is_open())
return false;
ofs.write(hash.data(), hash.size());
return true;
});
return result;
}
// static
string ConfigLoader::LoadHash(string const & filePath)
{
ifstream ifs(filePath, ifstream::in);
if (!ifs.is_open())
return {};
return {istreambuf_iterator<char>(ifs), istreambuf_iterator<char>()};
}
} // namespace editor
|