OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
RouteContainer.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
28 
29 // Library/third-party includes
30 #include <json/value.h>
31 #include <json/reader.h>
32 #include <json/writer.h>
33 
34 // Standard includes
35 #include <stdexcept>
36 #include <functional>
37 #include <algorithm>
38 
39 namespace osvr {
40 namespace common {
41 
42  static inline Json::Value
43  parseRoutingDirective(std::string const &routingDirective) {
44  Json::Reader reader;
45  Json::Value val;
46  if (!reader.parse(routingDirective, val)) {
47  throw std::runtime_error("Invalid JSON routing directive: " +
48  routingDirective);
49  }
50  return val;
51  }
52 
53  static inline std::string
54  getDestinationFromJson(Json::Value const &routingDirective) {
55  return routingDirective.get(routing_keys::destination(), "").asString();
56  }
57  static inline std::string toFastString(Json::Value const &val) {
58  Json::FastWriter writer;
59  return writer.write(val);
60  }
61 
62  static inline std::string
63  getSourceFromJson(Json::Value const &routingDirective) {
64  return routingDirective.get(routing_keys::source(), "")
65  .toStyledString();
66  }
67 
69 
70  RouteContainer::RouteContainer(std::string const &routes) {
71  Json::Reader reader;
72  Json::Value routesVal;
73  if (!reader.parse(routes, routesVal)) {
74  throw std::runtime_error("Invalid JSON routing directive array: " +
75  routes);
76  }
77  for (Json::ArrayIndex i = 0, e = routesVal.size(); i < e; ++i) {
78  const Json::Value thisRoute = routesVal[i];
79  m_addRoute(getDestinationFromJson(thisRoute),
80  toFastString(thisRoute));
81  }
82  }
83 
84  bool RouteContainer::addRoute(std::string const &routingDirective) {
85  auto route = parseRoutingDirective(routingDirective);
86  return m_addRoute(getDestinationFromJson(route), routingDirective);
87  }
88 
89  std::string RouteContainer::getRoutes(bool styled) const {
90  Json::Value routes(Json::arrayValue);
91  for (auto const &r : m_routingDirectives) {
92  routes.append(parseRoutingDirective(r));
93  }
94  if (styled) {
95  return routes.toStyledString();
96  }
97  return toFastString(routes);
98  }
99 
100  std::string
101  RouteContainer::getSource(std::string const &destination) const {
102  auto route = getRouteForDestination(destination);
103  if (!route.empty()) {
104  Json::Value directive = parseRoutingDirective(route);
105  return getSourceFromJson(directive);
106  }
107  return std::string();
108  }
109 
110  std::string RouteContainer::getSourceAt(size_t i) const {
111  return getSourceFromJson(
112  parseRoutingDirective(m_routingDirectives.at(i)));
113  }
114 
115  std::string RouteContainer::getDestinationAt(size_t i) const {
116  return getDestinationFromJson(
117  parseRoutingDirective(m_routingDirectives.at(i)));
118  }
119 
121  std::string const &destination) const {
122  auto it = std::find_if(
123  begin(m_routingDirectives), end(m_routingDirectives),
124  [&](std::string const &directive) {
125  return (getDestinationFromJson(
126  parseRoutingDirective(directive)) == destination);
127  });
128  if (it != end(m_routingDirectives)) {
129  return *it;
130  }
131  return std::string();
132  }
133 
134  std::string
135  RouteContainer::getDestinationFromString(std::string const &route) {
136  return getDestinationFromJson(parseRoutingDirective(route));
137  }
138  std::string RouteContainer::getSourceFromString(std::string const &route) {
139  return getSourceFromJson(parseRoutingDirective(route));
140  }
141  bool RouteContainer::m_addRoute(std::string const &destination,
142  std::string const &routingDirective) {
143  bool replaced = false;
146  std::replace_if(
147  begin(m_routingDirectives),
148  end(m_routingDirectives), [&](std::string const &directive) {
149  Json::Value candidate = parseRoutingDirective(directive);
150  bool match = (getDestinationFromJson(candidate) == destination);
151  if (match) {
152  replaced = true;
153  }
154  return match;
155  }, routingDirective);
156 
158  if (!replaced) {
159  m_routingDirectives.push_back(routingDirective);
160  }
161  return !replaced;
162  }
163 } // namespace common
164 } // namespace osvr
static std::string getDestinationFromString(std::string const &route)
Gets the destination of a route, given the route in string format.
static std::string getSourceFromString(std::string const &route)
Gets the source of a route, given the route in string format.
RouteContainer()
Empty constructor.
std::string getRoutes(bool styled=false) const
Get a JSON array of all routing directives.
std::string getRouteForDestination(std::string const &destination) const
Get the full routing JSON string for a given destination path.
std::string getSource(std::string const &destination) const
Get the source JSON string for a given destination path.
bool addRoute(std::string const &routingDirective)
Register a JSON string as a routing directive.