OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
VrpnBasedConnection.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 "VrpnBasedConnection.h"
27 #include "VrpnMessageType.h"
28 #include "VrpnConnectionDevice.h"
29 #include "VrpnConnectionKind.h"
30 #include <osvr/Util/Verbosity.h>
31 
32 // Library/third-party includes
33 // - none
34 
35 // Standard includes
36 // - none
37 
38 namespace osvr {
39 namespace connection {
40  namespace messageid {
42  const char *vrpnPing() { return "vrpn_Base ping_message"; }
43  } // namespace messageid
44 
46  switch (type) {
47  case VRPN_LOCAL_ONLY: {
48  m_initConnection("localhost");
49  break;
50  }
51  case VRPN_SHARED: {
52  m_initConnection();
53  break;
54  }
55  case VRPN_LOOPBACK: {
56  m_initConnection("loopback:");
57  break;
58  }
59  }
60  }
61 
63  boost::optional<std::string const &> iface, boost::optional<int> port) {
64  int myPort = port.get_value_or(0);
65  if (iface && !(iface->empty())) {
66  m_initConnection(iface->c_str(), myPort);
67  } else {
68  m_initConnection(nullptr, myPort);
69  }
70  }
71 
72  void VrpnBasedConnection::m_initConnection(const char iface[], int port) {
73  if (!m_network.isUp()) {
74  OSVR_DEV_VERBOSE("Network error: " << m_network.getError());
75  throw std::runtime_error(m_network.getError());
76  }
77  if (0 == port) {
78  port = vrpn_DEFAULT_LISTEN_PORT_NO;
79  }
80  m_vrpnConnection = vrpn_ConnectionPtr::create_server_connection(
81  port, nullptr, nullptr, iface);
82  }
83 
85  VrpnBasedConnection::m_registerMessageType(std::string const &messageId) {
86  MessageTypePtr ret(new VrpnMessageType(messageId, m_vrpnConnection));
87  return ret;
88  }
89 
91  VrpnBasedConnection::m_createConnectionDevice(DeviceInitObject &init) {
93  make_shared<VrpnConnectionDevice>(init, m_vrpnConnection);
94  return ret;
95  }
96 
97  void VrpnBasedConnection::m_registerConnectionHandler(
98  std::function<void()> handler) {
99  if (m_connectionHandlers.empty()) {
101  m_vrpnConnection->register_handler(
102  m_vrpnConnection->register_message_type(vrpn_got_connection),
103  &m_connectionHandler, static_cast<void *>(this),
104  vrpn_ANY_SENDER);
106 #if 0
107  m_vrpnConnection->register_handler(
108  m_vrpnConnection->register_message_type(messageid::vrpnPing()),
109  &m_connectionHandler, static_cast<void *>(this),
110  vrpn_ANY_SENDER);
111 #endif
112  }
113  m_connectionHandlers.push_back(handler);
114  }
115 
116  int VrpnBasedConnection::m_connectionHandler(void *userdata,
117  vrpn_HANDLERPARAM) {
118  VrpnBasedConnection *conn =
119  static_cast<VrpnBasedConnection *>(userdata);
120  for (auto const &f : conn->m_connectionHandlers) {
121  f();
122  }
123  return 0;
124  }
125  void VrpnBasedConnection::m_process() {
127  m_vrpnConnection->mainloop();
128  }
129 
132  }
133 
135  return static_cast<void *>(m_vrpnConnection.get());
136  }
137 
139  return getVRPNConnectionKindID();
140  }
141 
142 } // namespace connection
143 } // namespace osvr
VrpnBasedConnection(ConnectionType type)
Constructor for the VRPN connection.
std::string const & getError() const
Get error message, if any.
unique_ptr< MessageType > MessageTypePtr
a uniquely-owned handle for holding a message type registration.
virtual const char * getConnectionKindID()
Returns some implementation-defined string based on the dynamic type of the connection.
shared_ptr< ConnectionDevice > ConnectionDevicePtr
How to hold on to a ConnectionDevice.
Internal, configured header file for verbosity macros.
virtual void * getUnderlyingObject()
Returns the vrpn_Connection pointer.
bool isUp() const
Get whether the networking system is successfully "up".