OSVR-Core
Connection.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_Connection_h_GUID_61C65986_E2C9_498F_59F0_8EFC712BA183
26 #define INCLUDED_Connection_h_GUID_61C65986_E2C9_498F_59F0_8EFC712BA183
27 
28 // Internal Includes
29 #include <osvr/Connection/Export.h>
36 #include <osvr/Util/Log.h>
37 
38 // Library/third-party includes
39 #include <boost/noncopyable.hpp>
40 #include <boost/optional.hpp>
41 #include <boost/range/iterator_range.hpp>
42 
43 // Standard includes
44 #include <string>
45 #include <vector>
46 #include <functional>
47 #include <tuple>
48 
49 namespace osvr {
52 namespace connection {
53 
56  class Connection : boost::noncopyable,
57  public enable_shared_from_this<Connection> {
58  public:
64  OSVR_CONNECTION_EXPORT static ConnectionPtr createLocalConnection();
70  OSVR_CONNECTION_EXPORT static ConnectionPtr
71  createSharedConnection(boost::optional<std::string const &> iface,
72  boost::optional<int> port);
73 
74  OSVR_CONNECTION_EXPORT static std::tuple<void *, ConnectionPtr>
75  createLoopbackConnection();
77 
81  OSVR_CONNECTION_EXPORT static ConnectionPtr
82  retrieveConnection(const pluginhost::RegistrationContext &ctx);
84  OSVR_CONNECTION_EXPORT static void
86  ConnectionPtr conn);
88 
90  OSVR_CONNECTION_EXPORT MessageTypePtr
91  registerMessageType(std::string const &messageId);
92 
100  OSVR_CONNECTION_EXPORT ConnectionDevicePtr
101  createConnectionDevice(std::string const &deviceName);
102 
104  OSVR_CONNECTION_EXPORT ConnectionDevicePtr
106 
108  OSVR_CONNECTION_EXPORT void addDevice(ConnectionDevicePtr device);
109 
113  OSVR_CONNECTION_EXPORT void process();
114 
117  OSVR_CONNECTION_EXPORT void
118  registerConnectionHandler(std::function<void()> handler);
119 
121  OSVR_CONNECTION_EXPORT void
122  registerDescriptorHandler(std::function<void()> handler);
123 
126  OSVR_CONNECTION_EXPORT void triggerDescriptorHandlers();
127 
129  OSVR_CONNECTION_EXPORT virtual ~Connection();
130 
131  typedef std::vector<ConnectionDevicePtr> DeviceList;
132 
134  boost::iterator_range<DeviceList::const_iterator> getDevices() const {
135  return boost::make_iterator_range(begin(m_devices), end(m_devices));
136  }
137 
143 
152  OSVR_CONNECTION_EXPORT ConnectionDevicePtr registerAdvancedDevice(
153  std::string const &deviceName,
154  OSVR_DeviceUpdateCallback updateFunction, void *userdata);
155 
157  typedef std::vector<std::string> NameList;
158 
169  OSVR_CONNECTION_EXPORT ConnectionDevicePtr registerAdvancedDevice(
170  NameList const &deviceNames,
171  OSVR_DeviceUpdateCallback updateFunction, void *userdata);
172 
174  OSVR_CONNECTION_EXPORT virtual void *getUnderlyingObject();
175 
178  OSVR_CONNECTION_EXPORT virtual const char *getConnectionKindID();
180 
181  protected:
184  virtual MessageTypePtr
185  m_registerMessageType(std::string const &messageId) = 0;
186 
188  virtual ConnectionDevicePtr
190 
193  virtual void
194  m_registerConnectionHandler(std::function<void()> handler) = 0;
195 
198  virtual void m_process() = 0;
199 
201  Connection();
202 
203  private:
204  DeviceList m_devices;
205  std::vector<std::function<void()> > m_descriptorHandlers;
206  util::log::LoggerPtr m_log;
207  };
208 } // namespace connection
209 } // namespace osvr
210 #endif // INCLUDED_Connection_h_GUID_61C65986_E2C9_498F_59F0_8EFC712BA183
OSVR_CONNECTION_EXPORT 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
OSVR_CONNECTION_EXPORT ConnectionDevicePtr createConnectionDevice(std::string const &deviceName)
Create a ConnectionDevice by registering a full device name.
Definition: Connection.cpp:91
OSVR_CONNECTION_EXPORT MessageTypePtr registerMessageType(std::string const &messageId)
Register (or retrieve registration) of a message type.
Definition: Connection.cpp:86
Header forward declaring MessageType and specifying a smart pointer.
OSVR_CONNECTION_EXPORT void registerDescriptorHandler(std::function< void()> handler)
Register a function to be called when a descriptor changes.
Definition: Connection.cpp:155
OSVR_CONNECTION_EXPORT void registerConnectionHandler(std::function< void()> handler)
Register a function to be called when a client connects or pings.
Definition: Connection.cpp:152
Header forward-declaring ConnectionDevice and specifying the desired pointer to hold a ConnectionDevi...
Class responsible for hosting plugins, along with their registration and destruction.
Definition: RegistrationContext.h:53
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
virtual OSVR_CONNECTION_EXPORT void * getUnderlyingObject()
Access implementation details.
Definition: Connection.cpp:170
shared_ptr< Connection > ConnectionPtr
How one must hold a Connection.
Definition: ConnectionPtr.h:40
Class wrapping a messaging transport (server or internal) connection.
Definition: Connection.h:56
virtual void m_process()=0
(Subclass implementation) Process messages.
Header for basic internal log reference.
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.
Definition: DeviceInitObject.h:59
boost::iterator_range< DeviceList::const_iterator > getDevices() const
Get the devices, as a range.
Definition: Connection.h:134
virtual void m_registerConnectionHandler(std::function< void()> handler)=0
(Subclass implementation) Register a function to handle "new connection"/ping messages.
Header declaring device callback types.
static OSVR_CONNECTION_EXPORT 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.
Definition: MessageTypePtr.h:42
static OSVR_CONNECTION_EXPORT ConnectionPtr createSharedConnection(boost::optional< std::string const &> iface, boost::optional< int > port)
Factory method to create a shared connection.
Definition: Connection.cpp:53
Connection()
brief Constructor
Definition: Connection.cpp:165
virtual OSVR_CONNECTION_EXPORT 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.
Definition: ConnectionDevicePtr.h:40
OSVR_CONNECTION_EXPORT void process()
Process messages.
Definition: Connection.cpp:143
OSVR_ReturnCode(* OSVR_DeviceUpdateCallback)(void *userData)
Function type of a Device Update callback.
Definition: DeviceCallbackTypesC.h:48
OSVR_CONNECTION_EXPORT 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
OSVR_CONNECTION_EXPORT void triggerDescriptorHandlers()
Signal a descriptor update and call any/all descriptor handlers.
Definition: Connection.cpp:159
virtual OSVR_CONNECTION_EXPORT ~Connection()
Destructor.
Definition: Connection.cpp:168
virtual ConnectionDevicePtr m_createConnectionDevice(DeviceInitObject &init)=0
(Subclass implementation) Register a full device name.
Header forward-declaring RegistrationContext.
Header forward-declaring Connection and specifying the smart pointer to hold a Connection in...