| 85 | static const std::chrono::milliseconds STARTUP_LOOP_SLEEP(1); |
| 86 | |
| 87 | PureClientContext::PureClientContext(const char appId[], const char host[], |
| 88 | common::ClientContextDeleter del) |
| 89 | : ::OSVR_ClientContextObject(appId, del), m_host(host), |
| 90 | m_ifaceMgr(m_pathTreeOwner, m_factory, |
| 91 | *static_cast<common::ClientContext *>(this)) { |
| 92 | |
| 93 | if (!m_network.isUp()) { |
| 94 | throw std::runtime_error("Network error: " + m_network.getError()); |
| 95 | } |
| 96 | |
| 97 | /// Create all the remote handler factories. |
| 98 | populateRemoteHandlerFactory(m_factory, m_vrpnConns); |
| 99 | |
| 100 | std::string sysDeviceName = |
| 101 | std::string(common::SystemComponent::deviceName()) + "@" + host; |
| 102 | m_mainConn = m_vrpnConns.getConnection( |
| 103 | common::SystemComponent::deviceName(), host); |
| 104 | |
| 105 | /// Create the system client device. |
| 106 | m_systemDevice = common::createClientDevice(sysDeviceName, m_mainConn); |
| 107 | m_systemComponent = |
| 108 | m_systemDevice->addComponent(common::SystemComponent::create()); |
| 109 | using DedupJsonFunction = |
| 110 | common::DeduplicatingFunctionWrapper<Json::Value const &>; |
| 111 | m_systemComponent->registerReplaceTreeHandler( |
| 112 | DedupJsonFunction([&](Json::Value nodes) { |
| 113 | |
| 114 | OSVR_DEV_VERBOSE("Got updated path tree, processing"); |
| 115 | // Replace localhost before we even convert the json to a tree. |
| 116 | // replace the @localhost with the correct host name |
| 117 | // in case we are a remote client, otherwise the connection |
| 118 | // would fail |
| 119 | replaceLocalhostServers(nodes, m_host); |
| 120 | |
| 121 | // Tree observers will handle destruction/creation of remote |
| 122 | // handlers. |
| 123 | m_pathTreeOwner.replaceTree(nodes); |
| 124 | })); |
| 125 | |
| 126 | typedef std::chrono::system_clock clock; |
| 127 | auto begin = clock::now(); |
| 128 | |
| 129 | // Spin the update to get a connection |
| 130 | auto connEnd = begin + STARTUP_CONNECT_TIMEOUT; |
| 131 | while (clock::now() < connEnd && !m_gotConnection) { |
| 132 | m_update(); |
| 133 | std::this_thread::sleep_for(STARTUP_LOOP_SLEEP); |
| 134 | } |
| 135 | if (!m_gotConnection) { |
| 136 | OSVR_DEV_VERBOSE( |
| 137 | "Could not connect to OSVR server in the timeout period " |
| 138 | "allotted of " |
| 139 | << std::chrono::duration_cast<std::chrono::milliseconds>( |
| 140 | STARTUP_CONNECT_TIMEOUT) |
| 141 | .count() |
| 142 | << "ms"); |
| 143 | return; // Bail early if we don't even have a connection |
| 144 | } |
nothing calls this directly
no test coverage detected