30 #include <json/value.h>
31 #include <json/reader.h>
32 #include <json/writer.h>
42 static inline Json::Value
43 parseRoutingDirective(std::string
const &routingDirective) {
46 if (!reader.parse(routingDirective, val)) {
47 throw std::runtime_error(
"Invalid JSON routing directive: " +
53 static inline std::string
54 getDestinationFromJson(Json::Value
const &routingDirective) {
55 return routingDirective.get(routing_keys::destination(),
"").asString();
57 static inline std::string toFastString(Json::Value
const &val) {
58 Json::FastWriter writer;
59 return writer.write(val);
62 static inline std::string
63 getSourceFromJson(Json::Value
const &routingDirective) {
64 return routingDirective.get(routing_keys::source(),
"")
72 Json::Value routesVal;
73 if (!reader.parse(routes, routesVal)) {
74 throw std::runtime_error(
"Invalid JSON routing directive array: " +
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));
85 auto route = parseRoutingDirective(routingDirective);
86 return m_addRoute(getDestinationFromJson(route), routingDirective);
90 Json::Value routes(Json::arrayValue);
91 for (
auto const &r : m_routingDirectives) {
92 routes.append(parseRoutingDirective(r));
95 return routes.toStyledString();
97 return toFastString(routes);
103 if (!route.empty()) {
104 Json::Value directive = parseRoutingDirective(route);
105 return getSourceFromJson(directive);
107 return std::string();
110 std::string RouteContainer::getSourceAt(
size_t i)
const {
111 return getSourceFromJson(
112 parseRoutingDirective(m_routingDirectives.at(i)));
115 std::string RouteContainer::getDestinationAt(
size_t i)
const {
116 return getDestinationFromJson(
117 parseRoutingDirective(m_routingDirectives.at(i)));
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);
128 if (it != end(m_routingDirectives)) {
131 return std::string();
136 return getDestinationFromJson(parseRoutingDirective(route));
139 return getSourceFromJson(parseRoutingDirective(route));
141 bool RouteContainer::m_addRoute(std::string
const &destination,
142 std::string
const &routingDirective) {
143 bool replaced =
false;
147 begin(m_routingDirectives),
148 end(m_routingDirectives), [&](std::string
const &directive) {
149 Json::Value candidate = parseRoutingDirective(directive);
150 bool match = (getDestinationFromJson(candidate) == destination);
155 }, routingDirective);
159 m_routingDirectives.push_back(routingDirective);
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.