-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathRegistrationContext.cpp
More file actions
232 lines (200 loc) · 8.29 KB
/
RegistrationContext.cpp
File metadata and controls
232 lines (200 loc) · 8.29 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
/** @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.
#ifndef __ANDROID__
#define OSVR_DEV_VERBOSE_DISABLE
#endif
// Internal Includes
#include <osvr/PluginHost/RegistrationContext.h>
#include "PluginSpecificRegistrationContextImpl.h"
#include <osvr/PluginHost/PathConfig.h>
#include <osvr/PluginHost/SearchPath.h>
#include <osvr/Util/Log.h>
#include <osvr/Util/Verbosity.h>
// Library/third-party includes
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <boost/noncopyable.hpp>
#include <boost/range/adaptor/map.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <libfunctionality/LoadPlugin.h>
// Standard includes
#include <algorithm>
#include <iterator>
namespace osvr {
namespace pluginhost {
static const auto PLUGIN_HOST_LOGGER_NAME = "PluginHost";
namespace fs = boost::filesystem;
struct RegistrationContext::Impl : private boost::noncopyable {
/// constructor - creates and caches the plugin search path
Impl() : pluginPaths(pluginhost::getPluginSearchPath()) {}
const std::vector<std::string> pluginPaths;
};
RegistrationContext::RegistrationContext()
: m_impl(new Impl),
m_logger(util::log::make_logger(PLUGIN_HOST_LOGGER_NAME)) {}
RegistrationContext::~RegistrationContext() {
// Reset the plugins in reverse order.
for (auto &ptr : m_regMap | boost::adaptors::map_values |
boost::adaptors::reversed) {
ptr.reset();
}
}
template <typename MapType>
static inline bool isPluginLoaded(MapType const ®Map,
std::string const &pluginName) {
return (regMap.find(pluginName) != end(regMap));
}
/// Only called by manually-named plugins to load: the plugins that are
/// loaded automatically don't go through this function.
static inline bool tryLoadingPlugin(util::log::Logger &log,
libfunc::PluginHandle &plugin,
std::string const &name,
OSVR_PluginRegContext ctx,
bool shouldRethrow = false) {
log.debug() << "Trying to load a plugin with the name "
<< name;
try {
plugin = libfunc::loadPluginByName(name, ctx);
return true;
} catch (std::runtime_error const &e) {
log.debug() << "Failed: " << e.what();
if (shouldRethrow) {
throw;
}
return false;
} catch (...) {
throw;
}
}
void RegistrationContext::loadPlugin(std::string const &pluginName) {
if (isPluginLoaded(m_regMap, pluginName)) {
throw std::runtime_error("Already loaded a plugin named " +
pluginName);
}
PluginRegPtr pluginReg(
PluginSpecificRegistrationContext::create(pluginName));
pluginReg->setParent(*this);
bool success = false;
libfunc::PluginHandle plugin;
auto ctx = pluginReg->extractOpaquePointer();
const std::string pluginPathName =
pluginhost::findPlugin(m_impl->pluginPaths, pluginName);
if (pluginPathName.empty()) {
// was the plugin pre-loaded or statically linked? Try loading
// it by name.
success = tryLoadingPlugin(*m_logger, plugin, pluginName, ctx);
if (!success) {
throw std::runtime_error("Could not find plugin named " +
pluginName);
}
}
if (!success) {
const auto pluginPathNameNoExt =
(fs::path(pluginPathName).parent_path() /
fs::path(pluginPathName).stem())
.generic_string();
success =
tryLoadingPlugin(*m_logger, plugin, pluginPathName, ctx) ||
tryLoadingPlugin(*m_logger, plugin, pluginPathNameNoExt, ctx,
true);
if (!success) {
throw std::runtime_error(
"Unusual error occurred trying to load plugin named " +
pluginName);
}
}
pluginReg->takePluginHandle(plugin);
adoptPluginRegistrationContext(pluginReg);
}
void RegistrationContext::loadPlugins() {
// Build a list of all the plugins we can find
auto pluginPathNames = pluginhost::getAllFilesWithExt(
m_impl->pluginPaths, OSVR_PLUGIN_EXTENSION);
// Load all of the non-.manualload plugins
for (const auto &plugin : pluginPathNames) {
m_logger->debug() << "Examining plugin '" << plugin << "'...";
const auto pluginBaseName =
fs::path(plugin).filename().stem().generic_string();
if (boost::iends_with(pluginBaseName, OSVR_PLUGIN_IGNORE_SUFFIX)) {
m_logger->debug() << "Ignoring manual-load plugin: "
<< pluginBaseName;
continue;
}
#if defined(_MSC_VER)
// Visual C++ debug runtime: we append to the plugin name. Must only
// load debug plugins iff we're a debug server
const auto isDebugRuntimePlugin =
boost::iends_with(pluginBaseName, OSVR_PLUGIN_DEBUG_SUFFIX);
#if defined(NDEBUG)
/// This is a non-debug build.
if (isDebugRuntimePlugin) {
m_logger->debug() << "Ignoring debug-runtime plugin: "
<< pluginBaseName;
continue;
}
#else
/// This is a debug build
if (!isDebugRuntimePlugin) {
m_logger->debug() << "Ignoring non-debug-runtime plugin: "
<< pluginBaseName;
continue;
}
#endif // NDEBUG
#endif // _MSC_VER
try {
loadPlugin(pluginBaseName);
m_logger->debug() << "Successfully loaded plugin: "
<< pluginBaseName;
} catch (const std::exception &e) {
m_logger->warn() << "Failed to load plugin " << pluginBaseName
<< ": " << e.what();
} catch (...) {
m_logger->warn() << "Failed to load plugin " << pluginBaseName
<< ": Unknown error.";
}
}
}
void RegistrationContext::adoptPluginRegistrationContext(PluginRegPtr ctx) {
/// This set parent might be a duplicate, but won't be if the plugin reg
/// ctx is not created by loadPlugin above.
ctx->setParent(*this);
m_regMap.insert(std::make_pair(ctx->getName(), ctx));
}
void RegistrationContext::triggerHardwareDetect() {
for (auto &pluginPtr : m_regMap | boost::adaptors::map_values) {
pluginPtr->triggerHardwareDetectCallbacks();
}
}
void
RegistrationContext::instantiateDriver(const std::string &pluginName,
const std::string &driverName,
const std::string ¶ms) const {
auto pluginIt = m_regMap.find(pluginName);
if (pluginIt == end(m_regMap)) {
throw std::runtime_error("Could not find plugin named " +
pluginName);
}
pluginIt->second->instantiateDriver(driverName, params);
}
util::AnyMap &RegistrationContext::data() { return m_data; }
util::AnyMap const &RegistrationContext::data() const { return m_data; }
} // namespace pluginhost
} // namespace osvr