OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
JointClientContext.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
26 #include "JointClientContext.h"
33 #include <osvr/Util/Verbosity.h>
36 #include <osvr/Server/Server.h>
37 
38 // Library/third-party includes
39 #include <json/value.h>
40 
41 // Standard includes
42 #include <unordered_set>
43 #include <thread>
44 
45 namespace osvr {
46 namespace client {
47 
48  static const auto HOST = "localhost";
49  //static const std::chrono::milliseconds STARTUP_CONNECT_TIMEOUT(200);
50  //static const std::chrono::milliseconds STARTUP_TREE_TIMEOUT(1000);
51  //static const std::chrono::milliseconds STARTUP_LOOP_SLEEP(1);
52 
54  common::ClientContextDeleter del)
55  : ::OSVR_ClientContextObject(appId, del),
56  m_ifaceMgr(m_pathTreeOwner, m_factory,
57  *static_cast<common::ClientContext *>(this)) {
58 
60  populateRemoteHandlerFactory(m_factory, m_vrpnConns);
61 
63  auto conn = connection::Connection::createLoopbackConnection();
64 
66  m_mainConn = static_cast<vrpn_Connection *>(std::get<0>(conn));
67  m_vrpnConns.addConnection(m_mainConn, HOST);
68  BOOST_ASSERT(!m_vrpnConns.empty());
69 
71  m_server = server::Server::createNonListening(std::get<1>(conn));
72 
73  std::string sysDeviceName =
74  std::string(common::SystemComponent::deviceName()) + "@" + HOST;
75 
77  m_systemDevice = common::createClientDevice(sysDeviceName, m_mainConn);
78  m_systemComponent =
79  m_systemDevice->addComponent(common::SystemComponent::create());
81  DedupJsonFunction;
82 
83  using DedupJsonFunction =
85  m_systemComponent->registerReplaceTreeHandler(
86  DedupJsonFunction([&](Json::Value nodes) {
87 
88  OSVR_DEV_VERBOSE("Got updated path tree, processing");
89 
90  // Tree observers will handle destruction/creation of remote
91  // handlers.
92  m_pathTreeOwner.replaceTree(nodes);
93  }));
94  }
95 
96  JointClientContext::~JointClientContext() {}
97 
98  void JointClientContext::m_update() {
100  m_server->update();
101 
103  m_vrpnConns.updateAll();
104 
106  m_systemDevice->update();
108  m_ifaceMgr.updateHandlers();
109  }
110 
111  void JointClientContext::m_sendRoute(std::string const &route) {
112  m_systemComponent->sendClientRouteUpdate(route);
113  m_update();
114  }
115 
116  void JointClientContext::m_handleNewInterface(
117  common::ClientInterfacePtr const &iface) {
118  m_ifaceMgr.addInterface(iface);
119  }
120 
121  void JointClientContext::m_handleReleasingInterface(
122  common::ClientInterfacePtr const &iface) {
123  m_ifaceMgr.releaseInterface(iface);
124  }
125 
126  bool JointClientContext::m_getStatus() const {
128  return bool(m_pathTreeOwner);
129  }
130 
131  common::PathTree const &JointClientContext::m_getPathTree() const {
132  return m_pathTreeOwner.get();
133  }
134 } // namespace client
135 } // namespace osvr
void replaceTree(Json::Value const &nodes)
Replace the entirety of the path tree from the given serialized array of nodes.
Header.
shared_ptr< ClientInterface > ClientInterfacePtr
Pointer for holding ClientInterface objects safely.
BaseDevicePtr createClientDevice(std::string const &name, vrpn_ConnectionPtr const &conn)
Factory function for a bare client device with no components/interfaces registered by default...
Header including PathTree.h and all additional headers needed to define related types.
Header declaring osvr::server::Server.
static OSVR_SERVER_EXPORT ServerPtr createNonListening(connection::ConnectionPtr const &conn)
Create a server object with a provided connection with a provided connection that has been asserted t...
Definition: Server.cpp:65
JointClientContext(const char appId[], common::ClientContextDeleter del)
static shared_ptr< SystemComponent > create()
Factory method.
void addInterface(common::ClientInterfacePtr const &iface, bool verboseFailure=true)
Internal, configured header file for verbosity macros.
static const char * deviceName()
Get the special device name to be used with this component.
void updateHandlers()
run update on all remote handlers
A wrapper for a unary function that only calls its contained function when the input has changed...
PathTree & get()
Access the path tree object itself.
Definition: PathTreeOwner.h:73