OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ConfigureServerFromFile.cpp
Go to the documentation of this file.
1 
11 // Copyright 2014 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
26 #include <osvr/Server/Server.h>
31 #include <osvr/Util/Logger.h>
32 #include <osvr/Util/LogNames.h>
33 
34 // Library/third-party includes
35 // - none
36 
37 // Standard includes
38 #include <exception>
39 #include <fstream>
40 #include <iostream>
41 #include <vector>
42 #include <sstream>
43 
44 namespace osvr {
45 namespace server {
46 
47  ServerPtr configureServerFromString(std::string const &json) {
48  auto log =
49  ::osvr::util::log::make_logger(::osvr::util::log::OSVR_SERVER_LOG);
50 
51  ServerPtr ret;
53  log->info() << "Constructing server as configured...";
54  try {
55  srvConfig.loadConfig(json);
56  ret = srvConfig.constructServer();
57  }
58  catch (std::exception &e) {
59  log->error()
60  << "Caught exception constructing server from JSON config "
61  "file: "
62  << e.what();
63  return nullptr;
64  }
65 
66  {
67  log->info() << "Loading auto-loadable plugins...";
68  srvConfig.loadAutoPlugins();
69  }
70 
71  {
72  log->info() << "Loading plugins...";
73  srvConfig.loadPlugins();
74  if (!srvConfig.getSuccessfulPlugins().empty()) {
75  log->info() << "Successfully loaded the following plugins:";
76  for (auto const &plugin : srvConfig.getSuccessfulPlugins()) {
77  log->info() << " - " << plugin;
78  }
79  }
80  if (!srvConfig.getFailedPlugins().empty()) {
81  log->warn() << "Failed to load the following plugins:";
82  for (auto const &pluginError : srvConfig.getFailedPlugins()) {
83  log->warn() << " - " << pluginError.first << "\t"
84  << pluginError.second;
85  }
86  }
87  }
88 
89  {
90  log->info() << "Instantiating configured drivers...";
91  bool success = srvConfig.instantiateDrivers();
92  if (!srvConfig.getSuccessfulInstantiations().empty()) {
93  log->info() << "Successes:";
94  for (auto const &driver :
95  srvConfig.getSuccessfulInstantiations()) {
96  log->info() << " - " << driver;
97  }
98  }
99  if (!srvConfig.getFailedInstantiations().empty()) {
100  log->error() << "Errors:";
101  for (auto const &error : srvConfig.getFailedInstantiations()) {
102  log->error() << " - " << error.first << "\t"
103  << error.second;
104  }
105  }
106  }
107 
108  if (srvConfig.processExternalDevices()) {
109  log->info()
110  << "External devices found and parsed from config file.";
111  }
112 
113  if (srvConfig.processRoutes()) {
114  log->info() << "Routes found and parsed from config file.";
115  }
116 
117  if (srvConfig.processAliases()) {
118  log->info() << "Aliases found and parsed from config file.";
119  }
120 
121  if (srvConfig.processDisplay()) {
122  log->info()
123  << "Display descriptor found and parsed from config file.";
124  } else {
125  log->info()
126  << "Using OSVR HDK for display configuration. "
127  "Did not find an alternate valid 'display' object in config "
128  "file.";
129  }
130 
131  if (srvConfig.processRenderManagerParameters()) {
132  log->info() << "RenderManager config found and parsed from the "
133  "config file.";
134  }
135 
136  log->info() << "Triggering automatic hardware detection...";
137  ret->triggerHardwareDetect();
138 
139  return ret;
140  }
141 
142  ServerPtr configureServerFromFile(std::string const &configName) {
143  auto log =
144  ::osvr::util::log::make_logger(::osvr::util::log::OSVR_SERVER_LOG);
145 
146  ServerPtr ret;
147  log->info() << "Attempting to load config file '" << configName << "'.";
148  std::ifstream config(configName);
149  if (!config.good()) {
150  log->error() << "Config file '" << configName << "' not found";
151  return nullptr;
152  }
153 
154  std::stringstream sstr;
155  sstr << config.rdbuf();
156  return configureServerFromString(sstr.str());
157  }
158 
160  std::vector<std::string> const &configNames) {
161  auto log =
162  ::osvr::util::log::make_logger(::osvr::util::log::OSVR_SERVER_LOG);
163 
164  for (const auto name : configNames) {
165  std::ifstream config(name);
166  if (config.good()) {
167  return configureServerFromFile(name);
168  }
169  }
170  log->error() << "Could not find a valid config file!";
171  return nullptr;
172  }
173 
174 } // namespace server
175 } // namespace osvr
A class used for step-by-step construction and configuration of a server.
OSVR_SERVER_EXPORT ErrorList const & getFailedInstantiations() const
Get a reference to the list of drivers instantiateDrivers() tried to instantiate but failed...
OSVR_SERVER_EXPORT void loadConfig(std::string const &json)
Loads and parses the provided json.
OSVR_SERVER_EXPORT bool processDisplay()
Process a display element in the config.
OSVR_SERVER_EXPORT bool processRenderManagerParameters()
Process a RenderManager config element in the config.
OSVR_SERVER_EXPORT ErrorList const & getFailedPlugins() const
Get a reference to the list of plugins loadPlugins() tried but failed to load, along with any excepti...
OSVR_SERVER_EXPORT ServerPtr configureServerFromFile(std::string const &configName)
This uses a file name to attempt to configure the server with that config file. Pass an empty string ...
OSVR_SERVER_EXPORT ServerPtr constructServer()
Creates a server, choosing the factory method according to the passed JSON configuration.
Header declaring osvr::server::Server.
OSVR_SERVER_EXPORT void loadAutoPlugins()
Loads all plugins not marked for manual load.
OSVR_SERVER_EXPORT bool instantiateDrivers()
Configures and instantiates the drivers as specified.
Auto-configured header.
Platform specific search paths for osvr server config files.
OSVR_SERVER_EXPORT bool processExternalDevices()
Process any external devices in the config.
OSVR_SERVER_EXPORT ServerPtr configureServerFromFirstFileInList(std::vector< std::string > const &configNames)
This iterates over a vector that contains a list of potential config files, and uses the first workin...
shared_ptr< Server > ServerPtr
How one should hold a Server.
Definition: ServerPtr.h:39
Header to include for OSVR-internal usage of the logging mechanism: provides the needed definition of...
OSVR_SERVER_EXPORT bool loadPlugins()
Loads the plugins contained in an array with key plugins in the configuration.
Header.