OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ClientContext.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 "GetJSONStringFromTree.h"
29 #include <osvr/Util/Verbosity.h>
30 
31 // Library/third-party includes
32 #include <boost/assert.hpp>
33 
34 // Standard includes
35 #include <algorithm>
36 
38 using ::osvr::common::ClientInterface;
39 using ::osvr::common::ClientContextDeleter;
40 using ::osvr::make_shared;
41 
42 namespace osvr {
43 namespace common {
45  auto del = ctx->getDeleter();
46  (*del)(ctx);
47  }
48 } // namespace common
49 } // namespace osvr
50 
51 static const auto CLIENT_LOG_PREFIX = "Client: ";
52 static const auto OSVR_LIBS_CLIENT_LOG_PREFIX = "OSVR: ";
53 static const auto OSVR_LIBS_CLIENT_LOG_SUFFIX = "";
54 
56  const char appId[],
57  osvr::common::ClientInterfaceFactory const &interfaceFactory,
58  osvr::common::ClientContextDeleter del)
59  : m_appId(appId), m_clientInterfaceFactory(interfaceFactory),
60  m_deleter(del),
61  m_logger(osvr::util::log::make_logger(
62  OSVR_LIBS_CLIENT_LOG_PREFIX + m_appId + OSVR_LIBS_CLIENT_LOG_SUFFIX)),
63  m_clientLogger(
64  osvr::util::log::make_logger(CLIENT_LOG_PREFIX + m_appId)) {
65  m_logger->info() << "OSVR client context initialized for " << m_appId;
66  m_logger->flush();
67 }
68 
69 osvr::util::log::LoggerPtr const &OSVR_ClientContextObject::logger() const {
70  return m_logger;
71 }
72 
74  ClientContextDeleter del)
76  appId, osvr::common::getStandardClientInterfaceFactory(), del) {}
77 
79  m_logger->info() << "OSVR client context shut down for " << m_appId;
80  m_logger->flush();
81 }
82 
83 std::string const &OSVR_ClientContextObject::getAppId() const {
84  return m_appId;
85 }
86 
88  m_update();
89  for (auto const &iface : m_interfaces) {
90  iface->update();
91  }
92 }
93 
95  auto ret = m_clientInterfaceFactory(*this, path);
96  if (!ret) {
97  return ret;
98  }
99  m_handleNewInterface(ret);
100  m_interfaces.push_back(ret);
101  return ret;
102 }
103 
106  ClientInterfacePtr ret;
107  if (!iface) {
108  return ret;
109  }
110  auto it = std::find_if(begin(m_interfaces), end(m_interfaces),
111  [&](ClientInterfacePtr const &ptr) {
112  if (ptr.get() == iface) {
113  ret = ptr;
114  return true;
115  }
116  return false;
117  });
118  BOOST_ASSERT_MSG(
119  (it == end(m_interfaces)) == (!ret),
120  "We should have a pointer if and only if we have the iterator");
121  if (ret) {
122  // Erase it from our list
123  m_interfaces.erase(it);
124  // Notify the derived class if desired
125  m_handleReleasingInterface(ret);
126  }
127  return ret;
128 }
129 
130 std::string
131 OSVR_ClientContextObject::getStringParameter(std::string const &path) const {
132  return getJSONStringFromTree(getPathTree(), path);
133 }
134 
136  return m_getPathTree();
137 }
138 
139 void OSVR_ClientContextObject::sendRoute(std::string const &route) {
140  m_sendRoute(route);
141 }
142 
144  return m_ownedObjects.release(obj);
145 }
146 
149  return m_getRoomToWorldTransform();
150 }
151 
153  osvr::common::Transform const &xform) {
154  m_setRoomToWorldTransform(xform);
155 }
156 
157 ClientContextDeleter OSVR_ClientContextObject::getDeleter() const {
158  return m_deleter;
159 }
160 
161 bool OSVR_ClientContextObject::getStatus() const { return m_getStatus(); }
162 
163 void OSVR_ClientContextObject::log(osvr::util::log::LogLevel severity,
164  const char *message) {
165  m_clientLogger->log(severity, message);
166 }
167 
168 bool OSVR_ClientContextObject::m_getStatus() const {
169  // by default, assume we are started up.
170  return true;
171 }
172 
173 void OSVR_ClientContextObject::m_handleNewInterface(
175  // by default do nothing
176 }
177 
178 void OSVR_ClientContextObject::m_handleReleasingInterface(
180  // by default do nothing
181 }
bool getStatus() const
Returns true if we are started up and fully connected (path tree received, etc.)
shared_ptr< ClientInterface > ClientInterfacePtr
Pointer for holding ClientInterface objects safely.
std::string getStringParameter(std::string const &path) const
Gets a string parameter value.
A tree representation, with path/url syntax, of the known OSVR system.
Definition: PathTree.h:43
osvr::common::Transform const & getRoomToWorldTransform() const
Gets the transform from room space to world space.
void sendRoute(std::string const &route)
Sends a JSON route/transform object to the server.
osvr::common::ClientInterfacePtr getInterface(const char path[])
Creates an interface object for the given path. The context retains shared ownership.
osvr::common::ClientInterfacePtr releaseInterface(osvr::common::ClientInterface *iface)
Searches through this context to determine if the passed interface object has been retained...
bool release(void *ptr)
Releases the indicated smart pointer in our ownership, if we have it.
std::string const & getAppId() const
Accessor for app ID.
void setRoomToWorldTransform(osvr::common::Transform const &xform)
Sets the transform from room space to world space.
OSVR_ClientContextObject(const char appId[], osvr::common::ClientContextDeleter del)
Constructor for derived class use only.
bool releaseObject(void *obj)
Frees some object whose lifetime is controlled by the client context.
osvr::common::PathTree const & getPathTree() const
Accessor for the path tree.
virtual ~OSVR_ClientContextObject()
Destructor.
void log(osvr::util::log::LogLevel severity, const char *message)
Logs a message from the client.
std::function< ClientInterfacePtr(ClientContext &, const char[])> ClientInterfaceFactory
A factory function type taking the client context and path, and returning a ClientInterfacePtr. The ClientContext will handle notifying its internals about the new interface before returning it.
ClientInterfaceFactory getStandardClientInterfaceFactory()
Returns a client interface factory suitable for standard client use.
Internal, configured header file for verbosity macros.
osvr::util::log::LoggerPtr const & logger() const
Provides logger access for related internal classes.
void deleteContext(ClientContext *ctx)
Use the stored deleter to appropriately delete the client context.
void update()
System-wide update method.
Spatial transformation, consisting of both pre and post components.
Definition: Transform.h:44
osvr::common::ClientContextDeleter getDeleter() const
Returns the specialized deleter for this object.