-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.cc
359 lines (322 loc) · 12.3 KB
/
reader.cc
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2018 [email protected]
// College of Information and Computer Sciences,
// University of Massachusetts Amherst
//
// This software is free: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License Version 3,
// as published by the Free Software Foundation.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// Version 3 in the file COPYING that came with this distribution.
// If not, see <http://www.gnu.org/licenses/>.
// ========================================================================
#include "reader.h"
namespace configuration_reader {
class MapSingleton {
public:
static std::unordered_map<std::string,
std::unique_ptr<config_types::ConfigInterface>>&
singleton() {
// Needs large number of buckets because references are not stable across
// map growths.
static std::unordered_map<std::string,
std::unique_ptr<config_types::ConfigInterface>>
config(1000000);
return config;
}
};
// Define constants
#define EVENT_SIZE (sizeof(struct inotify_event))
#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16))
static constexpr int kInotifySleep = 50;
// Atomic bool for the thread
std::atomic_bool is_running_;
std::thread daemon_;
// namespace {
// std::unordered_map<std::string,
// std::unique_ptr<config_types::ConfigInterface>>
// config;
// } // namespace
/*
The LuaRead() function takes in a filename as a parameter
and updates all the config values from the unordered map
*/
// void LuaRead(std::string filename) {
void LuaRead(std::vector<std::string> files) {
// Create the LuaScript object
LuaScript script(files);
// Loop through the unordered map
for (const auto& pair : MapSingleton::singleton()) {
// Create a temporary pointer because you can't static_cast a unique_ptr
config_types::ConfigInterface* t = pair.second.get();
// Switch statement that serves as a runtime typecheck
// See the ConfigInterface.h file for documentation on the ConfigType enum
// and the GetType() function
switch (pair.second->GetType()) {
case (config_types::cint): {
config_types::ConfigInt* temp =
static_cast<config_types::ConfigInt*>(t);
temp->SetVal(&script);
std::cout << temp->GetKey() << " (int) was set to " << temp->GetVal()
<< std::endl;
break;
}
case (config_types::cuint): {
config_types::ConfigUint* temp =
static_cast<config_types::ConfigUint*>(t);
temp->SetVal(&script);
std::cout << temp->GetKey() << " (uint) was set to " << temp->GetVal()
<< std::endl;
break;
}
case (config_types::cdouble): {
config_types::ConfigDouble* temp =
static_cast<config_types::ConfigDouble*>(t);
temp->SetVal(&script);
std::cout << temp->GetKey() << " (double) was set to " << temp->GetVal()
<< std::endl;
break;
}
case (config_types::cfloat): {
config_types::ConfigFloat* temp =
static_cast<config_types::ConfigFloat*>(t);
temp->SetVal(&script);
std::cout << temp->GetKey() << " (float) was set to " << temp->GetVal()
<< std::endl;
break;
}
case (config_types::cstring): {
config_types::ConfigString* temp =
static_cast<config_types::ConfigString*>(t);
temp->SetVal(&script);
std::cout << temp->GetKey() << " (string) was set to " << temp->GetVal()
<< std::endl;
break;
}
case (config_types::cvector2f): {
config_types::ConfigVector2f* temp =
static_cast<config_types::ConfigVector2f*>(t);
temp->SetVal(&script);
std::cout << temp->GetKey() << " (Vector2f) was set to "
<< temp->GetVal() << std::endl;
break;
}
case (config_types::cbool): {
config_types::ConfigBool* temp =
static_cast<config_types::ConfigBool*>(t);
temp->SetVal(&script);
std::cout << temp->GetKey() << " (boolean) was set to "
<< temp->GetVal() << std::endl;
break;
}
case (config_types::cvector2d): {
config_types::ConfigVector2d* temp =
static_cast<config_types::ConfigVector2d*>(t);
temp->SetVal(&script);
std::cout << temp->GetKey() << " (Vector2d) was set to "
<< temp->GetVal() << std::endl;
break;
}
case (config_types::cvector3d): {
config_types::ConfigVector3d* temp =
static_cast<config_types::ConfigVector3d*>(t);
temp->SetVal(&script);
std::cout << temp->GetKey() << " (Vector3d) was set to "
<< temp->GetVal() << std::endl;
break;
}
default: // null type: the type value used when a ConfigInterface is
// constructed -> should never actually be used
std::cerr << "ERROR: ConfigType enum has a value of null, this should "
"never happen!"
<< std::endl;
break;
}
}
}
const int& InitInt(const std::string& key) {
MapSingleton::singleton()[key] =
std::unique_ptr<config_types::ConfigInterface>(
new config_types::ConfigInt(key));
config_types::ConfigInterface* t =
MapSingleton::singleton().find(key)->second.get();
config_types::ConfigInt* temp = static_cast<config_types::ConfigInt*>(t);
return temp->GetVal();
}
const unsigned int& InitUnsignedInt(const std::string& key) {
MapSingleton::singleton()[key] =
std::unique_ptr<config_types::ConfigInterface>(
new config_types::ConfigUint(key));
config_types::ConfigInterface* t =
MapSingleton::singleton().find(key)->second.get();
config_types::ConfigUint* temp = static_cast<config_types::ConfigUint*>(t);
return temp->GetVal();
}
const double& InitDouble(const std::string& key) {
MapSingleton::singleton()[key] =
std::unique_ptr<config_types::ConfigInterface>(
new config_types::ConfigDouble(key));
config_types::ConfigInterface* t =
MapSingleton::singleton().find(key)->second.get();
config_types::ConfigDouble* temp =
static_cast<config_types::ConfigDouble*>(t);
return temp->GetVal();
}
const float& InitFloat(const std::string& key) {
MapSingleton::singleton()[key] =
std::unique_ptr<config_types::ConfigInterface>(
new config_types::ConfigFloat(key));
config_types::ConfigInterface* t =
MapSingleton::singleton().find(key)->second.get();
config_types::ConfigFloat* temp = static_cast<config_types::ConfigFloat*>(t);
return temp->GetVal();
}
const std::string& InitString(const std::string& key) {
MapSingleton::singleton()[key] =
std::unique_ptr<config_types::ConfigInterface>(
new config_types::ConfigString(key));
config_types::ConfigInterface* t =
MapSingleton::singleton().find(key)->second.get();
config_types::ConfigString* temp =
static_cast<config_types::ConfigString*>(t);
return temp->GetVal();
}
const Eigen::Vector2f& InitVector2f(const std::string& key) {
MapSingleton::singleton()[key] =
std::unique_ptr<config_types::ConfigInterface>(
new config_types::ConfigVector2f(key));
config_types::ConfigInterface* t =
MapSingleton::singleton().find(key)->second.get();
config_types::ConfigVector2f* temp =
static_cast<config_types::ConfigVector2f*>(t);
return temp->GetVal();
}
const bool& InitBool(const std::string& key) {
MapSingleton::singleton()[key] =
std::unique_ptr<config_types::ConfigInterface>(
new config_types::ConfigBool(key));
config_types::ConfigInterface* t =
MapSingleton::singleton().find(key)->second.get();
config_types::ConfigBool* temp = static_cast<config_types::ConfigBool*>(t);
return temp->GetVal();
}
const Eigen::Vector2d& InitVector2d(const std::string& key) {
MapSingleton::singleton()[key] =
std::unique_ptr<config_types::ConfigInterface>(
new config_types::ConfigVector2d(key));
config_types::ConfigInterface* t =
MapSingleton::singleton().find(key)->second.get();
config_types::ConfigVector2d* temp =
static_cast<config_types::ConfigVector2d*>(t);
return temp->GetVal();
}
const Eigen::Vector3d& InitVector3d(const std::string& key) {
MapSingleton::singleton()[key] =
std::unique_ptr<config_types::ConfigInterface>(
new config_types::ConfigVector3d(key));
config_types::ConfigInterface* t =
MapSingleton::singleton().find(key)->second.get();
config_types::ConfigVector3d* temp =
static_cast<config_types::ConfigVector3d*>(t);
return temp->GetVal();
}
void HelpText() {
std::cout << "Please pass in zero or more lua files as an "
"argument to the program."
<< std::endl;
std::cout << "If you do not pass in a file, the program will use the "
"default filename which is currently set to: "
<< kDefaultFileName << std::endl;
std::cout << "Usage: ./reader filename.lua" << std::endl;
}
void InitDaemon(const std::vector<std::string>& files) {
int length = 0;
char buffer[EVENT_BUF_LEN];
// Initialize inotify
int fd = inotify_init();
if (fd < 0) {
std::cerr << "ERROR: Couldn't initialize inotify" << std::endl;
exit(-1);
}
// Each watch descriptor is associated with a directory that contains a set
// of watched files
std::unordered_map<int, std::set<std::string>> wd_to_files;
// Add a listener on each parent directory
for (const std::string& filepath : files) {
std::string filename = basename(strdup(filepath.c_str()));
std::string directory = dirname(strdup(filepath.c_str()));
// (Will be duplicate wd if we've already add_watched the directory)
int wd = inotify_add_watch(fd, directory.c_str(), IN_MODIFY);
if (wd == -1) {
std::cerr << "ERROR: Couldn't add watch to the file: "
<< filepath << std::endl;
exit(-1);
}
// Add to list of watched files
wd_to_files[wd].insert(filename);
}
int epfd = epoll_create(1);
if (epfd < 0) {
std::cerr << "ERROR: Call to epoll_create failed." << std::endl;
}
epoll_event ready_to_read = {0};
ready_to_read.data.fd = fd;
ready_to_read.events = EPOLLIN;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ready_to_read)) {
std::cerr << "ERROR: Call to epoll_ctl failed." << std::endl;
}
epoll_event epoll_events;
auto last_notify = std::chrono::system_clock::now();
bool needs_update = false;
// Loop forever, checking for changes to the files above
while (is_running_) {
// Wait for 50 ms for there to be an available inotify event
int nr_events = epoll_wait(epfd, &epoll_events, 1, kInotifySleep);
if (nr_events < 0) {
// If the call to epoll_wait failed
std::cerr << "ERROR: Call to epoll_wait failed." << std::endl;
}
if (nr_events > 0) {
// Else if the inotify fd has recieved something that can be read
length = read(fd, buffer, EVENT_BUF_LEN);
if (length < 0) std::cerr << "ERROR: Inotify read failed" << std::endl;
for (int i = 0; i < length;) {
inotify_event* event = reinterpret_cast<inotify_event*>(&buffer[i]);
i += EVENT_SIZE + event->len;
// Length of file name must be positive
if (event->len <= 0) {
continue;
}
// If file is in our list of files to track for this wd, then update
if (wd_to_files[event->wd].count(event->name) != 0) {
last_notify = std::chrono::system_clock::now();
needs_update = true;
}
}
}
if (needs_update && std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - last_notify)
.count() > 2 * kInotifySleep) {
LuaRead(files);
needs_update = false;
}
}
// Clean up
close(epfd);
close(fd);
}
void CreateDaemon(const std::vector<std::string>& files) {
LuaRead(files);
is_running_ = true;
daemon_ = std::thread(InitDaemon, files);
}
void Stop() {
is_running_ = false;
if (daemon_.joinable()) daemon_.join();
}
} // namespace configuration_reader