MCPcopy Create free account
hub / github.com/OSVR/OSVR-Core / constructServer

Method constructServer

src/osvr/Server/ConfigureServer.cpp:103–159  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

101 static const char SLEEP_KEY[] = "sleep";
102
103 ServerPtr ConfigureServer::constructServer() {
104 Json::Value const &root(m_data->root);
105 bool local = true;
106 std::string iface;
107 boost::optional<int> port;
108 int sleepTime = 1000; // microseconds
109
110 /// Extract data from the JSON structure.
111 if (root.isMember(SERVER_KEY)) {
112 /// @todo Detect/report invalid or contradictory options here.
113 Json::Value jsonServer = root[SERVER_KEY];
114 Json::Value jsonInterface = jsonServer[INTERFACE_KEY];
115 Json::Value jsonLocal = jsonServer[LOCAL_KEY];
116 if (jsonInterface.isString()) {
117 local = false;
118 iface = jsonInterface.asString();
119 } else if (jsonLocal.isBool()) {
120 local = jsonLocal.asBool();
121 }
122
123 Json::Value jsonPort = jsonServer[PORT_KEY];
124 if (jsonPort.isInt()) {
125 int myPort = jsonPort.asInt();
126 if (myPort < 1) {
127 throw std::out_of_range("Invalid port value: must be >= 1 "
128 "and for a non-admin execution, "
129 ">1024");
130 }
131 port = myPort;
132 }
133
134 Json::Value jsonSleepTime = jsonServer[SLEEP_KEY];
135 if (jsonSleepTime.isDouble()) {
136 // Sleep time is in milliseconds in the config file.
137 // Convert to microseconds for internal use.
138 sleepTime = static_cast<int>(jsonSleepTime.asDouble() * 1000.0);
139 }
140 }
141
142 /// Construct a server, or a connection then a server, based on the
143 /// configuration we've extracted.
144 if (local && !port) {
145 m_server = Server::createLocal();
146 } else {
147 connection::ConnectionPtr connPtr(
148 connection::Connection::createSharedConnection(iface, port));
149 m_server = Server::create(connPtr);
150 }
151
152 if (sleepTime > 0.0) {
153 m_server->setSleepTime(sleepTime);
154 }
155
156 m_server->setHardwareDetectOnConnection();
157
158 return m_server;
159 }
160

Callers 1

configureServerFromFileFunction · 0.80

Calls 2

setSleepTimeMethod · 0.45

Tested by

no test coverage detected