-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathServerImpl.cpp
More file actions
422 lines (369 loc) · 15 KB
/
ServerImpl.cpp
File metadata and controls
422 lines (369 loc) · 15 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/** @file
@brief Implementation
@date 2014
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/
// Copyright 2014 Sensics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Internal Includes
#include "ServerImpl.h"
#include "../Connection/VrpnConnectionKind.h" /// @todo warning - cross-library internal header!
#include <osvr/Common/AliasProcessor.h>
#include <osvr/Common/CommonComponent.h>
#include <osvr/Common/PathTreeFull.h>
#include <osvr/Common/ProcessDeviceDescriptor.h>
#include <osvr/Common/SystemComponent.h>
#include <osvr/Common/Tracing.h>
#include <osvr/Connection/Connection.h>
#include <osvr/Connection/ConnectionDevice.h>
#include <osvr/Connection/MessageType.h>
#include <osvr/PluginHost/RegistrationContext.h>
#include <osvr/Util/LogNames.h>
#include <osvr/Util/Logger.h>
#include <osvr/Util/MessageKeys.h>
#include <osvr/Util/Microsleep.h>
#include <osvr/Util/PortFlags.h>
#include <osvr/Util/StringLiteralFileToString.h>
#include <osvr/Util/Verbosity.h>
#include "osvr/Server/display_json.h" /// Fallback display descriptor.
// Library/third-party includes
#include <boost/variant.hpp>
#include <json/reader.h>
#include <vrpn_ConnectionPtr.h>
// Standard includes
#include <functional>
#include <stdexcept>
namespace osvr {
namespace server {
static vrpn_ConnectionPtr
getVRPNConnection(connection::ConnectionPtr const &conn) {
vrpn_ConnectionPtr ret;
if (std::string(conn->getConnectionKindID()) ==
osvr::connection::getVRPNConnectionKindID()) {
ret = vrpn_ConnectionPtr(
static_cast<vrpn_Connection *>(conn->getUnderlyingObject()));
}
return ret;
}
ServerImpl::ServerImpl(connection::ConnectionPtr const &conn,
boost::optional<std::string> const &host,
boost::optional<int> const &port)
: m_conn(conn), m_ctx(make_shared<pluginhost::RegistrationContext>()),
m_host(host.get_value_or("localhost")),
m_port(port.get_value_or(util::UseDefaultPort)),
m_log(util::log::make_logger(util::log::OSVR_SERVER_LOG)) {
if (!m_conn) {
throw std::logic_error(
"Can't pass a null ConnectionPtr into Server constructor!");
}
osvr::connection::Connection::storeConnection(*m_ctx, m_conn);
// Get the underlying VRPN connection, and make sure it's OK.
auto vrpnConn = getVRPNConnection(m_conn);
if (!(vrpnConn->doing_okay())) {
throw ServerCreationFailure();
}
// Set up system device/system component
m_systemDevice = common::createServerDevice(
common::SystemComponent::deviceName(), vrpnConn);
m_systemComponent =
m_systemDevice->addComponent(common::SystemComponent::create());
m_systemComponent->registerClientRouteUpdateHandler(
&ServerImpl::m_handleUpdatedRoute, this);
// Things to do when we get a new incoming connection
// No longer doing hardware detect unconditionally here - see
// triggerHardwareDetect()
m_commonComponent =
m_systemDevice->addComponent(common::CommonComponent::create());
m_commonComponent->registerPingHandler([&] { m_queueTreeSend(); });
// Set up the default display descriptor.
m_tree.getNodeByPath("/display").value() =
common::elements::StringElement(util::makeString(display_json));
// Deal with updated device descriptors.
m_conn->registerDescriptorHandler([&] { m_handleDeviceDescriptors(); });
// Set up handlers to enter/exit idle sleep mode.
// Can't do this with the nice wrappers on the CommonComponent of the
// system device, I suppose since people aren't really connecting to
// that device.
vrpnConn->register_handler(
vrpnConn->register_message_type(vrpn_got_first_connection),
&ServerImpl::m_exitIdle, this);
vrpnConn->register_handler(
vrpnConn->register_message_type(vrpn_dropped_last_connection),
&ServerImpl::m_enterIdle, this);
}
ServerImpl::~ServerImpl() {
stop();
// Not unregistering idle handlers because doing so caused crashes, and
// I think that this object should outlive the connection anyway.
}
void ServerImpl::start() {
boost::unique_lock<boost::mutex> lock(m_runControl);
if (!m_ctx || !m_conn) {
throw std::logic_error("Cannot start server - context or "
"connection destroyed (probably attempting "
"to restart a stopped server)");
}
m_everStarted = true;
m_running = true;
// Use a lambda to run the loop.
m_thread = boost::thread([&] {
bool keepRunning = true;
m_mainThreadId = m_thread.get_id();
::util::LoopGuard guard(m_run);
do {
keepRunning = this->m_loop();
} while (keepRunning);
m_orderedDestruction();
m_running = false;
});
m_run.signalAndWaitForStart();
}
void ServerImpl::startAndAwaitShutdown() {
start();
awaitShutdown();
}
void ServerImpl::awaitShutdown() { m_thread.join(); }
void ServerImpl::stop() {
boost::unique_lock<boost::mutex> lock(m_runControl);
if (m_everStarted) {
m_run.signalAndWaitForShutdown();
m_thread.join();
m_thread = boost::thread();
} else {
m_orderedDestruction();
}
}
void ServerImpl::signalStop() {
boost::unique_lock<boost::mutex> lock(m_runControl);
m_run.signalShutdown();
}
void ServerImpl::loadPlugin(std::string const &pluginName) {
m_callControlled([&, pluginName] { m_ctx->loadPlugin(pluginName); });
}
void ServerImpl::loadAutoPlugins() { m_ctx->loadPlugins(); }
void ServerImpl::setHardwareDetectOnConnection() {
m_commonComponent->registerPingHandler(
[&] { triggerHardwareDetect(); });
}
void ServerImpl::instantiateDriver(std::string const &plugin,
std::string const &driver,
std::string const ¶ms) {
BOOST_ASSERT_MSG(m_inServerThread(),
"This method is only available in the server thread!");
m_ctx->instantiateDriver(plugin, driver, params);
}
void ServerImpl::triggerHardwareDetect() {
m_callControlled([&] { m_triggeredDetect = true; });
}
void ServerImpl::registerMainloopMethod(MainloopMethod f) {
if (f) {
m_callControlled([&] { m_mainloopMethods.push_back(f); });
}
}
void ServerImpl::update() {
boost::unique_lock<boost::mutex> lock(m_runControl);
if (m_everStarted) {
throw std::logic_error("Can't call update() if you've ever started "
"the server in its own thread!");
}
m_update();
}
void ServerImpl::m_update() {
osvr::common::tracing::ServerUpdate trace;
m_conn->process();
m_systemDevice->update();
for (auto &f : m_mainloopMethods) {
f();
}
if (m_triggeredDetect) {
m_log->info() << "Performing hardware auto-detection.";
common::tracing::markHardwareDetect();
m_ctx->triggerHardwareDetect();
m_triggeredDetect = false;
}
if (m_treeDirty) {
m_log->debug() << "Path tree updated or connection detected";
m_sendTree();
m_treeDirty.reset();
}
}
bool ServerImpl::m_loop() {
bool shouldContinue;
{
/// @todo More elegant way of running queued things than grabbing a
/// mutex each time through?
boost::unique_lock<boost::mutex> lock(m_mainThreadMutex);
m_update();
shouldContinue = m_run.shouldContinue();
}
if (m_currentSleepTime > 0) {
osvr::util::time::microsleep(m_currentSleepTime);
}
return shouldContinue;
}
bool ServerImpl::addRoute(std::string const &routingDirective) {
bool wasNew;
m_callControlled([&] { wasNew = m_addRoute(routingDirective); });
return wasNew;
}
bool ServerImpl::addAlias(std::string const &path,
std::string const &source,
common::AliasPriority priority) {
bool wasChanged;
m_callControlled(
[&] { wasChanged = m_addAlias(path, source, priority); });
return wasChanged;
}
bool ServerImpl::addAliases(Json::Value const &aliases,
common::AliasPriority priority) {
bool wasChanged;
m_callControlled([&] { wasChanged = m_addAliases(aliases, priority); });
return wasChanged;
}
void ServerImpl::addExternalDevice(std::string const &path,
std::string const &deviceName,
std::string const &server,
std::string const &descriptor) {
Json::Value descriptorVal;
Json::Reader reader;
if (!reader.parse(descriptor, descriptorVal)) {
BOOST_ASSERT_MSG(0, "Should never get here - string descriptor "
"handed to us should have been generated from "
"a JSON value.");
return;
}
m_callControlled([&] {
/// Get the node
auto &node = m_tree.getNodeByPath(path);
/// Create the DeviceElement and set it as the node value - assume
/// it's a VRPN device by default.
auto elt = common::elements::DeviceElement::createVRPNDeviceElement(
deviceName, server);
elt.getDescriptor() = descriptorVal;
node.value() = elt;
m_treeDirty.set();
/// Process device descriptor
common::processDeviceDescriptorFromExistingDevice(node, elt);
});
}
bool ServerImpl::addString(std::string const &path,
std::string const &value) {
bool wasChanged = false;
auto newElement =
common::PathElement{common::elements::StringElement{value}};
m_callControlled([&] {
auto &node = m_tree.getNodeByPath(path);
if (!(newElement == node.value())) {
m_treeDirty.set();
wasChanged = true;
node.value() = newElement;
}
});
return wasChanged;
}
void ServerImpl::m_orderedDestruction() {
m_ctx.reset();
m_systemComponent = nullptr; // non-owning pointer
m_systemDevice.reset();
m_conn.reset();
}
int ServerImpl::m_handleUpdatedRoute(void *userdata, vrpn_HANDLERPARAM p) {
auto self = static_cast<ServerImpl *>(userdata);
BOOST_ASSERT_MSG(
self->m_inServerThread(),
"This callback should never happen outside the server thread!");
self->m_log->info() << "Got an updated route from a client.";
self->m_addRoute(std::string(p.buffer, p.payload_len));
return 0;
}
bool ServerImpl::m_addRoute(std::string const &routingDirective) {
bool change =
common::addAliasFromRoute(m_tree.getRoot(), routingDirective);
m_treeDirty += change;
return change;
}
bool ServerImpl::m_addAlias(std::string const &path,
std::string const &source,
common::AliasPriority priority) {
/// @todo Handle this one with AliasProcessor.
auto &node = m_tree.getNodeByPath(path);
bool change = common::addAlias(node, source, priority);
m_treeDirty += change;
return change;
}
bool ServerImpl::m_addAliases(Json::Value const &aliases,
common::AliasPriority priority) {
bool change = common::AliasProcessor()
.setDefaultPriority(priority)
.enableWildcard()
.process(m_tree.getRoot(), aliases);
m_treeDirty += change;
return change;
}
void ServerImpl::m_queueTreeSend() {
m_callControlled([&] { m_treeDirty += true; });
}
void ServerImpl::m_sendTree() {
common::tracing::markPathTreeBroadcast();
m_systemComponent->sendReplacementTree(m_tree);
m_log->info() << "Sent path tree to clients.";
}
void ServerImpl::setSleepTime(int microseconds) {
m_sleepTime = microseconds;
}
#if 0
int ServerImpl::getSleepTime() const { return m_sleepTime; }
#endif
void ServerImpl::m_handleDeviceDescriptors() {
for (auto const &dev : m_conn->getDevices()) {
auto const &descriptor = dev->getDeviceDescriptor();
if (descriptor.empty()) {
m_log->warn() << "Developer Warning: No device descriptor for "
<< dev->getName();
} else {
m_treeDirty += common::processDeviceDescriptorForPathTree(
m_tree, dev->getName(), descriptor, m_port, m_host);
}
}
}
int ServerImpl::m_exitIdle(void *userdata, vrpn_HANDLERPARAM) {
auto self = static_cast<ServerImpl *>(userdata);
/// Conditional ensures that we don't "idle" faster than we run: Make
/// sure we're sleeping longer now than we will be once we exit idle.
if (self->m_currentSleepTime > self->m_sleepTime) {
self->m_log->debug(
"Got first client connection, exiting idle mode.");
self->m_currentSleepTime = self->m_sleepTime;
}
/// Create the low-latency behavior object.
self->m_lowLatency.reset(new common::LowLatency);
return 0;
}
int ServerImpl::m_enterIdle(void *userdata, vrpn_HANDLERPARAM) {
auto self = static_cast<ServerImpl *>(userdata);
/// Conditional ensures that we don't "idle" faster than we run: Make
/// sure we're sleeping shorter now than we will be once we enter idle.
if (self->m_currentSleepTime < IDLE_SLEEP_TIME) {
self->m_log->debug(
"Dropped last client connection, entering idle mode.");
self->m_currentSleepTime = IDLE_SLEEP_TIME;
}
/// Destroy the low-latency behavior object
self->m_lowLatency.reset();
return 0;
}
} // namespace server
} // namespace osvr