Skip to content

Commit cea5f20

Browse files
committed
Split up config reading and loading. Server now uses blank config in absense of config file
1 parent d2bbb3b commit cea5f20

File tree

2 files changed

+50
-39
lines changed

2 files changed

+50
-39
lines changed

apps/osvr_server.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <boost/program_options.hpp>
3232
#include <boost/filesystem.hpp>
3333
#include <boost/filesystem/fstream.hpp>
34+
#include <boost/optional.hpp>
3435

3536
// Standard includes
3637
#include <iostream>
@@ -91,37 +92,36 @@ int main(int argc, char *argv[]) {
9192
return 0;
9293
}
9394

94-
if (!values.count(configOpt)) {
95-
out << "Using default config file - pass a filename on the command "
96-
"line to use a different one."
97-
<< endl;
98-
} else {
99-
configName = values[configOpt].as<std::string>();
100-
}
95+
configName = values[configOpt].as<std::string>();
10196

102-
fs::path configPath(configName);
103-
if (!fs::exists(configPath)) {
104-
try {
105-
out << "Creating blank config at \"" << configName << "\"" << endl;
106-
fs::ofstream configOut{configPath};
107-
configOut << "{ }\n";
108-
configOut.close();
109-
} catch (fs::filesystem_error &e) {
110-
err << "Could not create config file at \"" << configName << "\""
111-
<< endl;
112-
err << "Reason " << e.what() << endl;
97+
boost::optional<fs::path> configPath(configName);
98+
try {
99+
if (!fs::exists(*configPath)) {
100+
out << "File '" << configName
101+
<< "' not found. Using blank config" << endl;
102+
configPath = boost::none;
103+
} else {
104+
if (fs::is_directory(*configPath)) {
105+
err << "'" << configName << "' is a directory" << endl;
106+
return -1;
107+
} else if (!fs::is_regular_file(*configPath)) {
108+
err << "'" << configName << "' is special file" << endl;
109+
return -1;
110+
}
113111
}
112+
} catch (fs::filesystem_error &e) {
113+
err << "Could not open config file at '" << configName << "'"
114+
<< endl;
115+
err << "Reason " << e.what() << endl;
116+
configPath = boost::none;
117+
}
118+
119+
if (configPath) {
120+
server = osvr::server::configureServerFromFile(configName);
114121
} else {
115-
if (fs::is_directory(configPath)) {
116-
err << "\"" << configName << "\" is a directory" << endl;
117-
return -1;
118-
} else if (!fs::is_regular_file(configPath)) {
119-
err << "\"" << configName << "\" is special file" << endl;
120-
return -1;
121-
}
122+
server = osvr::server::configureServerFromString("{ }");
122123
}
123-
124-
server = osvr::server::configureServerFromFile(configName);
124+
125125
if (!server) {
126126
return -1;
127127
}

inc/osvr/Server/ConfigureServerFromFile.h

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,16 @@ namespace server {
6464
/// @brief This is the basic common code of a server app's setup, ripped out
6565
/// of the main server app to make alternate server-acting apps simpler to
6666
/// develop.
67-
inline ServerPtr configureServerFromFile(std::string const &configName) {
67+
inline ServerPtr configureServerFromString(std::string const &json) {
6868
using detail::out;
6969
using detail::err;
7070
using std::endl;
71+
7172
ServerPtr ret;
72-
out << "Using config file '" << configName << "'" << endl;
73-
std::ifstream config(configName);
74-
if (!config.good()) {
75-
err << "\n"
76-
<< "Could not open config file!" << endl;
77-
err << "Searched in the current directory; file may be "
78-
"misspelled, missing, or in a different directory." << endl;
79-
return nullptr;
80-
}
81-
8273
osvr::server::ConfigureServer srvConfig;
8374
out << "Constructing server as configured..." << endl;
8475
try {
85-
srvConfig.loadConfig(config);
76+
srvConfig.loadConfig(json);
8677
ret = srvConfig.constructServer();
8778
} catch (std::exception &e) {
8879
err << "Caught exception constructing server from JSON config "
@@ -169,6 +160,26 @@ namespace server {
169160

170161
return ret;
171162
}
163+
164+
/// @Brief Convenience wrapper for configureServerFromString
165+
inline ServerPtr configureServerFromFile(std::string const &configName) {
166+
using detail::out;
167+
using detail::err;
168+
using std::endl;
169+
170+
out << "Using config file '" << configName << "'" << endl;
171+
std::ifstream config(configName);
172+
if (!config.good()) {
173+
err << "\n"
174+
<< "Could not open config file!" << endl;
175+
err << "Searched in the current directory; file may be "
176+
"misspelled, missing, or in a different directory." << endl;
177+
return nullptr;
178+
}
179+
180+
std::string json(std::istreambuf_iterator<char>(config), {});
181+
return configureServerFromString(json);
182+
}
172183
} // namespace server
173184
} // namespace osvr
174185

0 commit comments

Comments
 (0)