File: ConfigHandler.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (408 lines) | stat: -rw-r--r-- 11,223 bytes parent folder | download
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "ConfigHandler.h"
#include "ConfigLocater.h"
#include "ConfigSource.h"
#include "System/ContainerUtil.h"
#include "System/SafeUtil.h"
#include "System/StringUtil.h"
#include "System/Log/ILog.h"
#include "System/UnorderedMap.hpp"
#include "System/Threading/SpringThreading.h"

#include <stdio.h>
#include <string.h>

#include <stdexcept>

/******************************************************************************/

typedef std::map<std::string, std::string> StringMap;

ConfigHandler* configHandler = NULL;


/******************************************************************************/

class ConfigHandlerImpl : public ConfigHandler
{
public:
	ConfigHandlerImpl(const std::vector<std::string>& locations, const bool safemode);
	~ConfigHandlerImpl();

	void SetString(const std::string& key, const std::string& value, bool useOverlay);
	std::string GetString(const std::string& key) const;
	bool IsSet(const std::string& key) const;
	bool IsReadOnly(const std::string& key) const;
	void Delete(const std::string& key);
	std::string GetConfigFile() const;
	const StringMap GetData() const;
	StringMap GetDataWithoutDefaults() const;
	void Update();
	void EnableWriting(bool write) { writingEnabled = write; }

protected:
	struct NamedConfigNotifyCallback {
		NamedConfigNotifyCallback(ConfigNotifyCallback c, void* o): callback(c), observer(o) {}

		ConfigNotifyCallback callback;
		void* observer;
	};

protected:
	void AddObserver(ConfigNotifyCallback callback, void* observer, const std::vector<std::string>& configs);
	void RemoveObserver(void* observer);

private:
	void RemoveDefaults();

	OverlayConfigSource* overlay;
	FileConfigSource* writableSource;
	std::vector<ReadOnlyConfigSource*> sources;

	// observer related
	spring::unsynced_map<std::string, std::vector<NamedConfigNotifyCallback>> configsToCallbacks;
	spring::unsynced_map<void*, std::vector<std::string>> observersToConfigs;
	spring::mutex observerMutex;
	StringMap changedValues;
	bool writingEnabled;
};

/******************************************************************************/


/**
 * @brief Fills the list of sources based on locations.
 *
 * Sources at lower indices take precedence over sources at higher indices.
 * First there is the overlay, then one or more file sources, and the last
 * source(s) specify default values.
 */
ConfigHandlerImpl::ConfigHandlerImpl(const std::vector<std::string>& locations, const bool safemode)
{
	writingEnabled = true;
	overlay = new OverlayConfigSource();
	writableSource = new FileConfigSource(locations.front());

	size_t sources_num = 3;
	sources_num += (safemode) ? 1 : 0;
	sources_num += locations.size() - 1;
#ifdef DEDICATED
	sources_num++;
#endif
#ifdef HEADLESS
	sources_num++;
#endif
	sources.reserve(sources_num);

	sources.push_back(overlay);

	if (safemode) {
		sources.push_back(new SafemodeConfigSource());
	}

	sources.push_back(writableSource);

	// skip writableSource
	for (auto loc = ++(locations.cbegin()); loc != locations.cend(); ++loc) {
		sources.push_back(new FileConfigSource(*loc));
	}
#ifdef DEDICATED
	sources.push_back(new DedicatedConfigSource());
#endif
#ifdef HEADLESS
	sources.push_back(new HeadlessConfigSource());
#endif
	sources.push_back(new DefaultConfigSource());

	assert(sources.size() <= sources_num);

	// Perform migrations that need to happen on every load.
	RemoveDefaults();
}

ConfigHandlerImpl::~ConfigHandlerImpl()
{
	//all observers have to be deregistered by RemoveObserver()
	assert(configsToCallbacks.empty());
	assert(observersToConfigs.empty());

	for (ReadOnlyConfigSource* s: sources) {
		delete s;
	}
}

/**
 * @brief Remove config variables that are set to their default value.
 *
 * Algorithm is as follows:
 *
 * 1) Take all defaults from the DefaultConfigSource
 * 2) Go in reverse through the FileConfigSources
 * 3) For each one:
 *  3a) delete setting if equal to the one in `defaults'
 *  3b) add new value to `defaults' if different
 */
void ConfigHandlerImpl::RemoveDefaults()
{
	StringMap defaults = sources.back()->GetData();

	for (auto rsource = sources.crbegin(); rsource != sources.crend(); ++rsource) {
		FileConfigSource* source = dynamic_cast<FileConfigSource*> (*rsource);

		if (source == nullptr)
			continue;

		// Copy the map; we modify the original while iterating over the copy.
		const StringMap file = source->GetData();

		for (auto it = file.cbegin(); it != file.cend(); ++it) {
			// Does the key exist in `defaults'?
			const auto pos = defaults.find(it->first);

			if (pos != defaults.end() && pos->second == it->second) {
				// Exists and value is equal => Delete.
				source->Delete(it->first);
			} else {
				// Doesn't exist or is not equal => Store new default.
				// (It will be the default for the next FileConfigSource.)
				defaults[it->first] = it->second;
			}
		}
	}
}


StringMap ConfigHandlerImpl::GetDataWithoutDefaults() const
{
	StringMap cleanConfig;
	StringMap defaults = sources.back()->GetData();

	for (auto rsource = sources.crbegin(); rsource != sources.crend(); ++rsource) {
		const FileConfigSource* source = dynamic_cast<const FileConfigSource*> (*rsource);

		if (source == nullptr)
			continue;

		// Copy the map; we modify the original while iterating over the copy.
		const StringMap file = source->GetData();

		for (auto it = file.cbegin(); it != file.cend(); ++it) {
			const auto pos = defaults.find(it->first);
			if (pos != defaults.end() && pos->second == it->second)
				continue;

			cleanConfig[it->first] = it->second;
		}
	}

	return cleanConfig;
}


void ConfigHandlerImpl::Delete(const std::string& key)
{
	for (ReadOnlyConfigSource* s: sources) {
		// The alternative to the dynamic cast is to merge ReadWriteConfigSource
		// with ReadOnlyConfigSource, but then DefaultConfigSource would have to
		// violate Liskov Substitution Principle... (by blocking modifications)
		ReadWriteConfigSource* rwcs = dynamic_cast<ReadWriteConfigSource*>(s);

		if (rwcs == nullptr)
			continue;

		rwcs->Delete(key);
	}
}

bool ConfigHandlerImpl::IsSet(const std::string& key) const
{
	for (const ReadOnlyConfigSource* s: sources) {
		if (s->IsSet(key)) return true;
	}

	return false;
}

bool ConfigHandlerImpl::IsReadOnly(const std::string& key) const
{
	const ConfigVariableMetaData* meta = ConfigVariable::GetMetaData(key);

	if (meta == nullptr)
		return false;

	return (meta->GetReadOnly().Get());
}

std::string ConfigHandlerImpl::GetString(const std::string& key) const
{
	const ConfigVariableMetaData* meta = ConfigVariable::GetMetaData(key);

	for (const ReadOnlyConfigSource* s: sources) {
		if (s->IsSet(key)) {
			std::string value = s->GetString(key);

			if (meta != nullptr)
				value = meta->Clamp(value);

			return value;
		}
	}

	throw std::runtime_error("ConfigHandler: Error: Key does not exist: " + key +
			"\nPlease add the key to the list of allowed configuration values.");
}

/**
 * @brief Sets a config string
 *
 * This does:
 * 1) Lock file.
 * 2) Read file (in case another program modified something)
 * 3) Set data[key] = value.
 * 4) Write file (so we keep the settings in the event of a crash or error)
 * 5) Unlock file.
 *
 * We do not want conflicts when multiple instances are running
 * at the same time (which would cause data loss).
 * This would happen if e.g. unitsync and spring would access
 * the config file at the same time, if we would not lock.
 */
void ConfigHandlerImpl::SetString(const std::string& key, const std::string& value, bool useOverlay)
{
	// if we set something to be persisted,
	// we do want to override the overlay value
	if (!useOverlay)
		overlay->Delete(key);

	// Don't do anything if value didn't change.
	if (IsSet(key) && GetString(key) == value)
		return;

	if (useOverlay) {
		overlay->SetString(key, value);
	} else if (writingEnabled) {
		std::vector<ReadOnlyConfigSource*>::const_iterator it = sources.begin();

		++it; // skip overlay
		++it; // skip writableSource

		bool deleted = false;

		for (; it != sources.end(); ++it) {
			if ((*it)->IsSet(key)) {
				if ((*it)->GetString(key) == value) {
					// key is being set to the default value,
					// delete the key instead of setting it.
					writableSource->Delete(key);
					deleted = true;
				}
				break;
			}
		}

		if (!deleted) {
			// set key to the specified non-default value
			writableSource->SetString(key, value);
		}
	}

	std::lock_guard<spring::mutex> lck(observerMutex);
	changedValues[key] = value;
}

void ConfigHandlerImpl::Update()
{
	std::lock_guard<spring::mutex> lck(observerMutex);

	for (StringMap::const_iterator ut = changedValues.begin(); ut != changedValues.end(); ++ut) {
		const std::string& key = ut->first;
		const std::string& value = ut->second;

		if (configsToCallbacks.find(key) != configsToCallbacks.end()) {
			for (const NamedConfigNotifyCallback& ncnc: configsToCallbacks[key]) {
				(ncnc.callback)(key, value);
			}
		}
	}

	changedValues.clear();
}

std::string ConfigHandlerImpl::GetConfigFile() const {
	return writableSource->GetFilename();
}

const StringMap ConfigHandlerImpl::GetData() const {
	StringMap data;
	for (const ReadOnlyConfigSource* s: sources) {
		const StringMap& sourceData = s->GetData();
		// insert doesn't overwrite, so this preserves correct overrides
		data.insert(sourceData.begin(), sourceData.end());
	}
	return data;
}


void ConfigHandlerImpl::AddObserver(ConfigNotifyCallback callback, void* observer, const std::vector<std::string>& configs) {
	std::lock_guard<spring::mutex> lck(observerMutex);

	for (const std::string& config: configs) {
		configsToCallbacks[config].emplace_back(callback, observer);
		observersToConfigs[observer].push_back(config);
	}
}

void ConfigHandlerImpl::RemoveObserver(void* observer) {
	std::lock_guard<spring::mutex> lck(observerMutex);

	for (const std::string& config: observersToConfigs[observer]) {
		spring::VectorEraseIf(configsToCallbacks[config], [&](NamedConfigNotifyCallback& ncnc) {
			return (ncnc.observer == observer);
		});

		if (configsToCallbacks[config].empty())
			configsToCallbacks.erase(config);
	}

	observersToConfigs.erase(observer);
}


/******************************************************************************/

void ConfigHandler::Instantiate(const std::string configSource, const bool safemode)
{
	Deallocate();

	std::vector<std::string> locations;
	if (!configSource.empty()) {
		locations.push_back(configSource);
	} else {
		ConfigLocater::GetDefaultLocations(locations);
	}
	assert(!locations.empty());

	// log here so unitsync shows configuration source(s), too
	LOG("Using writeable configuration source: \"%s\"", locations[0].c_str());
	for (auto loc = ++(locations.cbegin()); loc != locations.cend(); ++loc) {
		LOG("Using additional read-only configuration source: \"%s\"", loc->c_str());
	}

	configHandler = new ConfigHandlerImpl(locations, safemode);

	//assert(configHandler->GetString("test") == "x y z");
}

void ConfigHandler::Deallocate()
{
	spring::SafeDelete(configHandler);
}

bool ConfigHandler::Get(const std::string& key) const
{
	return StringToBool(GetString(key));
}


/******************************************************************************/