OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Server.h
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 #ifndef INCLUDED_Server_h_GUID_453158B3_45B7_478D_FB83_F2F694CA5367
26 #define INCLUDED_Server_h_GUID_453158B3_45B7_478D_FB83_F2F694CA5367
27 
28 // Internal Includes
29 #include <osvr/Server/Export.h>
30 #include <osvr/Server/ServerPtr.h>
33 #include <osvr/Util/UniquePtr.h>
34 
35 // Library/third-party includes
36 #include <boost/noncopyable.hpp>
37 #include <boost/optional.hpp>
38 
39 // Standard includes
40 #include <string>
41 #include <functional>
42 #include <stdexcept>
43 
44 namespace Json {
45 class Value;
46 }
47 
48 namespace osvr {
50 namespace server {
51  // Forward declaration for pimpl idiom.
52  class ServerImpl;
53 
56  typedef std::function<void()> MainloopMethod;
57 
58  struct ServerCreationFailure : std::runtime_error {
60  : std::runtime_error("Could not create server - there is probably "
61  "another instance of the server or a VRPN "
62  "server running already.") {}
63  };
64 
67  class Server : boost::noncopyable {
72  struct private_constructor {};
73 
74  public:
82  boost::optional<std::string> const &host,
83  boost::optional<int> const &port, private_constructor const &);
84 
86  OSVR_SERVER_EXPORT ~Server();
87 
90  OSVR_SERVER_EXPORT static ServerPtr createLocal();
91 
99  OSVR_SERVER_EXPORT static ServerPtr
100  create(connection::ConnectionPtr const &conn);
101 
106  OSVR_SERVER_EXPORT static ServerPtr
107  create(connection::ConnectionPtr const &conn,
108  boost::optional<std::string> const &host,
109  boost::optional<int> const &port);
110 
114  OSVR_SERVER_EXPORT static ServerPtr
116 
119  OSVR_SERVER_EXPORT void update();
120 
124  OSVR_SERVER_EXPORT void start();
125 
130  OSVR_SERVER_EXPORT void startAndAwaitShutdown();
131 
135  OSVR_SERVER_EXPORT void awaitShutdown();
136 
142  OSVR_SERVER_EXPORT void stop();
143 
148  OSVR_SERVER_EXPORT void signalStop();
149 
153  OSVR_SERVER_EXPORT void loadPlugin(std::string const &plugin);
154 
156  OSVR_SERVER_EXPORT void loadAutoPlugins();
157 
163  OSVR_SERVER_EXPORT void setHardwareDetectOnConnection();
164 
173  OSVR_SERVER_EXPORT void
174  instantiateDriver(std::string const &plugin, std::string const &driver,
175  std::string const &params = std::string());
176 
180  OSVR_SERVER_EXPORT void triggerHardwareDetect();
181 
186  OSVR_SERVER_EXPORT void registerMainloopMethod(MainloopMethod f);
187 
197  OSVR_SERVER_EXPORT bool addRoute(std::string const &routingDirective);
198 
205  OSVR_SERVER_EXPORT bool
206  addAlias(std::string const &path, std::string const &source,
207  common::AliasPriority priority = common::ALIASPRIORITY_MANUAL);
208 
215  OSVR_SERVER_EXPORT bool addString(std::string const &path,
216  std::string const &value);
217 
224  OSVR_SERVER_EXPORT bool addAliases(
225  Json::Value const &aliases,
226  common::AliasPriority priority = common::ALIASPRIORITY_MANUAL);
227 
234  OSVR_SERVER_EXPORT void addExternalDevice(
235  std::string const &path, std::string const &deviceName,
236  std::string const &server, std::string const &descriptor);
237 
243  OSVR_SERVER_EXPORT void setSleepTime(int microseconds);
244 
245 #if 0
246  OSVR_SERVER_EXPORT int getSleepTime() const;
251 #endif
252  private:
253  unique_ptr<ServerImpl> m_impl;
254  };
255 } // namespace server
256 } // namespace osvr
257 
258 #endif // INCLUDED_Server_h_GUID_453158B3_45B7_478D_FB83_F2F694CA5367
OSVR_SERVER_EXPORT void startAndAwaitShutdown()
Launch a thread running the server, and block until the server shuts down.
Definition: Server.cpp:79
OSVR_SERVER_EXPORT bool addAliases(Json::Value const &aliases, common::AliasPriority priority=common::ALIASPRIORITY_MANUAL)
Add alias entries to the tree from JSON.
Definition: Server.cpp:120
Header.
OSVR_SERVER_EXPORT void start()
Launch a thread running the server.
Definition: Server.cpp:75
OSVR_SERVER_EXPORT void triggerHardwareDetect()
Run all hardware detect callbacks.
Definition: Server.cpp:101
OSVR_SERVER_EXPORT bool addString(std::string const &path, std::string const &value)
Add a string entry to the tree.
Definition: Server.cpp:116
std::function< void()> MainloopMethod
A function that can be registered by the server app to run in each mainloop iteration.
Definition: Server.h:52
Class handling a run-loop with a registration context and connection.
Definition: Server.h:67
shared_ptr< Connection > ConnectionPtr
How one must hold a Connection.
Definition: ConnectionPtr.h:40
OSVR_SERVER_EXPORT void addExternalDevice(std::string const &path, std::string const &deviceName, std::string const &server, std::string const &descriptor)
Add an external device entry manually to the tree.
Definition: Server.cpp:125
OSVR_SERVER_EXPORT void loadPlugin(std::string const &plugin)
Load plugin by name.
Definition: Server.cpp:85
Header to bring unique_ptr into the osvr namespace.
OSVR_SERVER_EXPORT void registerMainloopMethod(MainloopMethod f)
Register a method to run during every time through the main loop.
Definition: Server.cpp:103
OSVR_SERVER_EXPORT void stop()
Signal the server to stop (if it is running), and block until it does so.
Definition: Server.cpp:81
OSVR_SERVER_EXPORT ~Server()
Destructor - stops the loop and blocks until it's done.
Definition: Server.cpp:145
Automatically-generated export header - do not edit!
OSVR_SERVER_EXPORT void setHardwareDetectOnConnection()
Adds the behavior that hardware detection should take place on client connection. ...
Definition: Server.cpp:91
OSVR_SERVER_EXPORT void awaitShutdown()
Block until the server shuts down.
Definition: Server.cpp:77
OSVR_SERVER_EXPORT bool addRoute(std::string const &routingDirective)
Register a JSON string as a routing directive.
Definition: Server.cpp:107
OSVR_SERVER_EXPORT bool addAlias(std::string const &path, std::string const &source, common::AliasPriority priority=common::ALIASPRIORITY_MANUAL)
Add an alias entry to the tree.
Definition: Server.cpp:111
static OSVR_SERVER_EXPORT ServerPtr create(connection::ConnectionPtr const &conn)
Create a server object with a provided connection.
Definition: Server.cpp:49
static OSVR_SERVER_EXPORT ServerPtr createNonListening(connection::ConnectionPtr const &conn)
Create a server object with a provided connection with a provided connection that has been asserted t...
Definition: Server.cpp:65
Private implementation class for Server.
Definition: ServerImpl.h:60
Definition: Server.h:44
Server(connection::ConnectionPtr const &conn, boost::optional< std::string > const &host, boost::optional< int > const &port, private_constructor const &)
Private server constructor.
Definition: Server.cpp:139
shared_ptr< Server > ServerPtr
How one should hold a Server.
Definition: ServerPtr.h:39
OSVR_SERVER_EXPORT void signalStop()
Signal the server to stop (if it is running) but return immediately.
Definition: Server.cpp:83
OSVR_SERVER_EXPORT void loadAutoPlugins()
Load all auto-loadable plugins.
Definition: Server.cpp:89
OSVR_SERVER_EXPORT void update()
If you aren't using a separate thread for the server, this method will run a single update of the ser...
Definition: Server.cpp:73
OSVR_SERVER_EXPORT void setSleepTime(int microseconds)
Sets the amount of time (in microseconds) that the server loop will sleep each loop when a client is ...
Definition: Server.cpp:132
Header forward-declaring the types in PathElementTypes.h and including the PathElement typedef...
static OSVR_SERVER_EXPORT ServerPtr createLocal()
Create a server object with a local-only (but still IP-based) connection.
Definition: Server.cpp:40
OSVR_SERVER_EXPORT void instantiateDriver(std::string const &plugin, std::string const &driver, std::string const &params=std::string())
Instantiate the named driver with parameters.
Definition: Server.cpp:95
Header forward-declaring Connection and specifying the smart pointer to hold a Connection in...