OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Connection.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
28 #include <osvr/Util/SharedPtr.h>
31 #include "VrpnBasedConnection.h"
33 #include <osvr/Util/LogNames.h>
34 #include <osvr/Util/Verbosity.h>
35 
36 // Library/third-party includes
37 #include <boost/range/algorithm.hpp>
38 #include <boost/assert.hpp>
39 
40 // Standard includes
41 // - none
42 
43 namespace osvr {
44 namespace connection {
46  static const char CONNECTION_KEY[] = "com.osvr.ConnectionPtr";
47 
48  ConnectionPtr Connection::createLocalConnection() {
49  ConnectionPtr conn(make_shared<VrpnBasedConnection>(
50  VrpnBasedConnection::VRPN_LOCAL_ONLY));
51  return conn;
52  }
54  boost::optional<std::string const &> iface, boost::optional<int> port) {
55  ConnectionPtr conn(make_shared<VrpnBasedConnection>(iface, port));
56  return conn;
57  }
58  std::tuple<void *, ConnectionPtr> Connection::createLoopbackConnection() {
59  auto conn = make_shared<VrpnBasedConnection>(
60  VrpnBasedConnection::VRPN_LOOPBACK);
61  return std::make_tuple(conn->getUnderlyingObject(),
62  ConnectionPtr{conn});
63  }
64 
66  Connection::retrieveConnection(const pluginhost::RegistrationContext &ctx) {
67  ConnectionPtr ret;
68  boost::any anyConn = ctx.data().get(CONNECTION_KEY);
69  if (anyConn.empty()) {
70  return ret;
71  }
72  ConnectionPtr *retPtr = boost::any_cast<ConnectionPtr>(&anyConn);
73  if (retPtr) {
74  ret = *retPtr;
75  }
76  return ret;
77  }
78 
80  ConnectionPtr conn) {
81  ctx.data().set(CONNECTION_KEY, conn);
82  }
83 
86  Connection::registerMessageType(std::string const &messageId) {
87  return m_registerMessageType(messageId);
88  }
89 
91  Connection::createConnectionDevice(std::string const &deviceName) {
92  DeviceInitObject init(shared_from_this());
93  init.setName(deviceName);
94  return createConnectionDevice(init);
95  }
96 
100  if (dev) {
101  addDevice(dev);
102  }
103  return dev;
104  }
105 
107  Connection::registerAdvancedDevice(std::string const &deviceName,
108  OSVR_DeviceUpdateCallback updateFunction,
109  void *userdata) {
111  new GenericConnectionDevice(deviceName, [updateFunction, userdata] {
112  return updateFunction(userdata);
113  }));
114  addDevice(dev);
115  return dev;
116  }
117 
120  OSVR_DeviceUpdateCallback updateFunction,
121  void *userdata) {
123  deviceNames,
124  [updateFunction, userdata] { return updateFunction(userdata); }));
125  addDevice(dev);
126  return dev;
127  }
128 
130  BOOST_ASSERT_MSG(device, "Device must be non-null!");
131  auto const &names = device->getNames();
132  if (names.size() == 1) {
133  m_log->notice() << "Added device: " << names.front();
134  } else {
135  m_log->notice() << "Added device with names:";
136  for (auto const &name : names) {
137  m_log->notice() << " - " << name;
138  }
139  }
140  m_devices.push_back(device);
141  }
142 
144  // Process the connection first.
145  m_process();
146  // Process all devices.
147  for (auto &dev : m_devices) {
148  dev->process();
149  }
150  }
151 
152  void Connection::registerConnectionHandler(std::function<void()> handler) {
154  }
155  void Connection::registerDescriptorHandler(std::function<void()> handler) {
156  m_descriptorHandlers.push_back(handler);
157  }
158 
160  for (auto &handler : m_descriptorHandlers) {
161  handler();
162  }
163  }
164 
166  : m_log(util::log::make_logger(util::log::OSVR_SERVER_LOG)) {}
167 
169 
170  void *Connection::getUnderlyingObject() { return nullptr; }
171 
172  const char *Connection::getConnectionKindID() { return nullptr; }
173 
174 } // namespace connection
175 } // namespace osvr
ConnectionDevicePtr registerAdvancedDevice(std::string const &deviceName, OSVR_DeviceUpdateCallback updateFunction, void *userdata)
Record a full device name (namespaced with the plugin name) associated with a given callback...
Definition: Connection.cpp:107
ConnectionDevicePtr createConnectionDevice(std::string const &deviceName)
Create a ConnectionDevice by registering a full device name. This should be namespaced with the plugi...
Definition: Connection.cpp:91
MessageTypePtr registerMessageType(std::string const &messageId)
Register (or retrieve registration) of a message type.
Definition: Connection.cpp:86
Header.
void registerDescriptorHandler(std::function< void()> handler)
Register a function to be called when a descriptor changes.
Definition: Connection.cpp:155
void registerConnectionHandler(std::function< void()> handler)
Register a function to be called when a client connects or pings.
Definition: Connection.cpp:152
Class responsible for hosting plugins, along with their registration and destruction.
static ConnectionPtr createSharedConnection(boost::optional< std::string const & > iface, boost::optional< int > port)
Factory method to create a shared connection.
Definition: Connection.cpp:53
virtual void * getUnderlyingObject()
Access implementation details.
Definition: Connection.cpp:170
shared_ptr< Connection > ConnectionPtr
How one must hold a Connection.
Definition: ConnectionPtr.h:40
virtual void m_process()=0
(Subclass implementation) Process messages. This shouldn't block.
void setName(std::string const &n)
Set the (unqualified) name of the device to create.
Header to bring shared_ptr into the osvr namespace.
virtual MessageTypePtr m_registerMessageType(std::string const &messageId)=0
(Subclass implementation) Register (or retrieve registration) of a message type.
Structure used internally to construct the desired type of device.
void set(std::string const &key, boost::any const &value)
Set data for the given key.
Definition: AnyMap.cpp:46
util::AnyMap & data()
Access the data storage map.
virtual void m_registerConnectionHandler(std::function< void()> handler)=0
(Subclass implementation) Register a function to handle "new connection"/ping messages.
static void storeConnection(pluginhost::RegistrationContext &ctx, ConnectionPtr conn)
Store a connection pointer in a RegistrationContext.
Definition: Connection.cpp:79
unique_ptr< MessageType > MessageTypePtr
a uniquely-owned handle for holding a message type registration.
Connection()
brief Constructor
Definition: Connection.cpp:165
virtual const char * getConnectionKindID()
Returns some implementation-defined string based on the dynamic type of the connection.
Definition: Connection.cpp:172
shared_ptr< ConnectionDevice > ConnectionDevicePtr
How to hold on to a ConnectionDevice.
void process()
Process messages. This shouldn't block.
Definition: Connection.cpp:143
Internal, configured header file for verbosity macros.
void addDevice(ConnectionDevicePtr device)
Add an externally-constructed device to the device list.
Definition: Connection.cpp:129
std::vector< std::string > NameList
Type of list of device names.
Definition: Connection.h:157
void triggerDescriptorHandlers()
Signal a descriptor update and call any/all descriptor handlers.
Definition: Connection.cpp:159
virtual ~Connection()
Destructor.
Definition: Connection.cpp:168
OSVR_ReturnCode(* OSVR_DeviceUpdateCallback)(void *userData)
Function type of a Device Update callback.
virtual ConnectionDevicePtr m_createConnectionDevice(DeviceInitObject &init)=0
(Subclass implementation) Register a full device name.
ConnectionDevice implementation for advanced devices.
Header.