OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
CommonComponent.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 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
27 
28 // Library/third-party includes
29 // - none
30 
31 // Standard includes
32 // - none
33 
34 namespace osvr {
35 namespace common {
36  namespace messages {
37 
38  // These messages all must match the ones in VRPN exactly.
39 
43  const char *VRPNPing::identifier() { return "vrpn_Base ping_message"; }
44 
48  const char *VRPNPong::identifier() { return "vrpn_Base pong_message"; }
49 
50  const char *VRPNGotFirstConnection::identifier() {
51  return "VRPN_Connection_Got_First_Connection";
52  }
53  const char *VRPNGotConnection::identifier() {
54  return "VRPN_Connection_Got_Connection";
55  }
56  const char *VRPNDroppedConnection::identifier() {
57  return "VRPN_Connection_Dropped_Connection";
58  }
59  const char *VRPNDroppedLastConnection::identifier() {
60  return "VRPN_Connection_Dropped_Last_Connection";
61  }
62 
63  } // namespace messages
64  shared_ptr<CommonComponent> CommonComponent::create() {
65  shared_ptr<CommonComponent> ret(new CommonComponent);
66  return ret;
67  }
68 
69  void CommonComponent::registerPingHandler(Handler const &handler) {
71  registerHandler(ping, handler);
72  }
73 
74  void CommonComponent::registerPongHandler(Handler const &handler) {
76  registerHandler(pong, handler);
77  }
78 
79  void CommonComponent::m_registerHandlerImpl(
80  HandlerList &handlers, osvr::common::RawMessageType rawMessageType,
81  Handler const &handler) {
82  if (handlers.empty()) {
83  m_registerHandler(&CommonComponent::m_baseHandler, &handlers,
84  rawMessageType);
85  }
86  handlers.push_back(handler);
87  }
88 
89  CommonComponent::CommonComponent() {}
90  void CommonComponent::m_parentSet() {
93  m_getParent().registerMessageType(gotFirstConnection);
94  m_getParent().registerMessageType(gotConnection);
95  m_getParent().registerMessageType(droppedConnection);
96  m_getParent().registerMessageType(droppedLastConnection);
97  }
98 
99  int CommonComponent::m_baseHandler(void *userdata, vrpn_HANDLERPARAM) {
102  auto &handlers = *static_cast<std::vector<Handler> *>(userdata);
103  for (auto const &cb : handlers) {
104  cb();
105  }
106  return 0;
107  }
108 
109 } // namespace common
110 } // namespace osvr
static shared_ptr< CommonComponent > create()
Factory method.
static const char * identifier()
static const char * identifier()
BaseDevice component, for the VRPN built-in common messages.
void registerPongHandler(Handler const &handler)
Register a pong handler: a pong replies to a ping, and is sent from a server device to the correspond...
void m_registerHandler(vrpn_MESSAGEHANDLER handler, void *userdata, RawMessageType const &msgType)
Registers a handler whose lifetime is tied to the lifetime of the component.
void registerPingHandler(Handler const &handler)
Register a ping handler: a ping is sent from a client device to the corresponding server device upon ...
Parent & m_getParent()
Gets the parent - only call if m_hasParent() is true.
Type-safe wrapper with built-in default for a VRPN "message type" integer.
void registerMessageType(MessageRegistration< T > &messageReg)
Call with a MessageRegistration object, and the message type will be registered and stored in the typ...
Definition: BaseDevice.h:72