OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
SystemComponent.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 #include <osvr/Common/BaseDevice.h>
28 #include <osvr/Util/MessageKeys.h>
31 #include <osvr/Common/Buffer.h>
33 
34 // Library/third-party includes
35 #include <json/value.h>
36 
37 // Standard includes
38 // - none
39 
40 namespace osvr {
41 namespace common {
42  namespace messages {
44  public:
45  MessageSerialization(std::string const &str = std::string())
46  : m_str(str) {}
47 
48  template <typename T> void processMessage(T &p) {
50  }
51 
52  private:
53  std::string m_str;
54  };
55  const char *RoutesFromServer::identifier() {
57  }
58 
59  const char *AppStartupToServer::identifier() {
60  return "com.osvr.system.appstartup";
61  }
62 
64  public:
65  MessageSerialization(std::string const &str = std::string())
66  : m_str(str) {}
67 
68  template <typename T> void processMessage(T &p) {
70  }
71 
72  private:
73  std::string m_str;
74  };
75  const char *ClientRouteToServer::identifier() {
76  return "com.osvr.system.updateroutetoserver";
77  }
78 
80  public:
81  MessageSerialization(Json::Value const &msg = Json::arrayValue)
82  : m_msg(msg) {}
83 
84  template <typename T> void processMessage(T &p) {
86  }
87 
88  Json::Value const &getValue() const { return m_msg; }
89 
90  private:
91  Json::Value m_msg;
92  };
93  const char *ReplacementTreeFromServer::identifier() {
94  return "com.osvr.system.ReplacementTreeFromServer";
95  }
96  } // namespace messages
97 
100  }
101 
102  shared_ptr<SystemComponent> SystemComponent::create() {
103  shared_ptr<SystemComponent> ret(new SystemComponent);
104  return ret;
105  }
106 
107  SystemComponent::SystemComponent() {}
108 
109  void SystemComponent::sendRoutes(std::string const &routes) {
110  Buffer<> buf;
111  messages::RoutesFromServer::MessageSerialization msg(routes);
112  serialize(buf, msg);
113  m_getParent().packMessage(buf, routesOut.getMessageType());
114  }
115 
116  void SystemComponent::registerRoutesHandler(vrpn_MESSAGEHANDLER handler,
117  void *userdata) {
118  m_registerHandler(handler, userdata, routesOut.getMessageType());
119  }
120 
121  void SystemComponent::sendClientRouteUpdate(std::string const &route) {
122  Buffer<> buf;
123  messages::ClientRouteToServer::MessageSerialization msg(route);
124  serialize(buf, msg);
125  m_getParent().packMessage(buf, routeIn.getMessageType());
126  }
127 
128  void SystemComponent::registerClientRouteUpdateHandler(
129  vrpn_MESSAGEHANDLER handler, void *userdata) {
130  m_registerHandler(handler, userdata, routeIn.getMessageType());
131  }
132 
133  void SystemComponent::sendReplacementTree(PathTree &tree) {
134  auto config = pathTreeToJson(tree);
135  Buffer<> buf;
136  messages::ReplacementTreeFromServer::MessageSerialization msg(config);
137  serialize(buf, msg);
138  m_getParent().packMessage(buf, treeOut.getMessageType());
139 
140  m_getParent().sendPending(); // forcing this since it will cause
141  // shuffling of remotes on the client.
142  }
143  void SystemComponent::registerReplaceTreeHandler(JsonHandler cb) {
144  if (m_replaceTreeHandlers.empty()) {
145  m_registerHandler(&SystemComponent::m_handleReplaceTree, this,
146  treeOut.getMessageType());
147  }
148  m_replaceTreeHandlers.push_back(cb);
149  }
150 
151  void SystemComponent::m_parentSet() {
156  }
157 
158  int SystemComponent::m_handleReplaceTree(void *userdata,
159  vrpn_HANDLERPARAM p) {
160  auto self = static_cast<SystemComponent *>(userdata);
161  auto bufReader = readExternalBuffer(p.buffer, p.payload_len);
162  messages::ReplacementTreeFromServer::MessageSerialization msg;
163  deserialize(bufReader, msg);
164  auto timestamp = util::time::fromStructTimeval(p.msg_time);
165  BOOST_ASSERT_MSG(msg.getValue().isArray(),
166  "replace tree message must be an array of nodes!");
167  for (auto const &cb : self->m_replaceTreeHandlers) {
168  cb(msg.getValue(), timestamp);
169  }
170  return 0;
171  }
172 } // namespace common
173 } // namespace osvr
void deserialize(BufferReaderType &reader, MessageClass &msg)
Deserializes a message from a buffer, using a MessageClass
messages::ReplacementTreeFromServer treeOut
Message from server, updating/replacing the client's configuration.
A tag for use when the only field in a message is a string so the length prefix is unnecessary...
void serialize(BufferType &buf, MessageClass &msg)
Serializes a message into a buffer, using a MessageClass
const char * systemSender()
The sender type for system messages.
Definition: MessageKeys.cpp:37
const char * routingData()
Return the string identifying routing data messages.
Definition: MessageKeys.cpp:38
messages::AppStartupToServer appStartup
Message from client to server, notifying of app ID.
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 fromStructTimeval(TimeValue &dest, struct timeval const &src)
Convert a struct timeval to a TimeValue.
Definition: TimeValue.h:104
messages::ClientRouteToServer routeIn
Message from client, sending a single new/updated JSON routing directive in to the server...
messages::RoutesFromServer routesOut
Message from server to client, replacing all routes.
void sendPending()
Called from a component to send pending messages instead of waiting for next time.
Definition: BaseDevice.cpp:86
static shared_ptr< SystemComponent > create()
Factory method.
Parent & m_getParent()
Gets the parent - only call if m_hasParent() is true.
static const char * deviceName()
Get the special device name to be used with this component.
Json::Value pathTreeToJson(PathTree const &tree, bool keepNulls=false)
Serialize a path tree to a JSON array of objects, one for each node.
Header.
BaseDevice component, to be used only with the "OSVR" special device.
Header defining buffer types, with optional alignment dependent on Boost version. ...
BufferReader< ExternalBufferReadingWrapper< CharType > > readExternalBuffer(const CharType *buf, size_t len)
Constructs and returns a buffer reader for an externally-allocated buffer: it's on you to supply a va...
Definition: Buffer.h:198
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
A tag for use when the only field in a message is JSON so the length prefix is unnecessary.