Skip to content

Commit

Permalink
change library interface to expose json container as structure to exc…
Browse files Browse the repository at this point in the history
…hange data
  • Loading branch information
DennisOSRM committed Jan 6, 2015
1 parent b50a907 commit 4a63256
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 80 deletions.
6 changes: 3 additions & 3 deletions Library/OSRM.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
class OSRM_impl;
struct RouteParameters;

namespace http
namespace JSON
{
class Reply;
struct Object;
}

class OSRM
Expand All @@ -48,7 +48,7 @@ class OSRM
public:
explicit OSRM(ServerPaths paths, const bool use_shared_memory = false);
~OSRM();
void RunQuery(RouteParameters &route_parameters, http::Reply &reply);
int RunQuery(RouteParameters &route_parameters, JSON::Object &json_result);
};

#endif // OSRM_H
14 changes: 6 additions & 8 deletions Library/OSRM_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ namespace boost { namespace interprocess { class named_mutex; } }
#include "../Server/DataStructures/InternalDataFacade.h"
#include "../Server/DataStructures/SharedBarriers.h"
#include "../Server/DataStructures/SharedDataFacade.h"
//TODO: remove
#include "../Server/Http/Reply.h"
#include "../Util/make_unique.hpp"
#include "../Util/ProgramOptions.h"
#include "../Util/simple_logger.hpp"
Expand Down Expand Up @@ -104,13 +102,12 @@ void OSRM_impl::RegisterPlugin(BasePlugin *plugin)
plugin_map.emplace(plugin->GetDescriptor(), plugin);
}

void OSRM_impl::RunQuery(RouteParameters &route_parameters, http::Reply &reply)
int OSRM_impl::RunQuery(RouteParameters &route_parameters, JSON::Object &json_result)
{
const PluginMap::const_iterator &iter = plugin_map.find(route_parameters.service);

if (plugin_map.end() != iter)
{
reply.status = http::Reply::ok;
if (barrier)
{
// lock update pending
Expand All @@ -131,7 +128,7 @@ void OSRM_impl::RunQuery(RouteParameters &route_parameters, http::Reply &reply)
->CheckAndReloadFacade();
}

iter->second->HandleRequest(route_parameters, reply);
iter->second->HandleRequest(route_parameters, json_result);
if (barrier)
{
// lock query
Expand All @@ -148,10 +145,11 @@ void OSRM_impl::RunQuery(RouteParameters &route_parameters, http::Reply &reply)
barrier->no_running_queries_condition.notify_all();
}
}
return 200;
}
else
{
reply = http::Reply::StockReply(http::Reply::badRequest);
return 400;
}
}

Expand All @@ -164,7 +162,7 @@ OSRM::OSRM(ServerPaths paths, const bool use_shared_memory)

OSRM::~OSRM() { OSRM_pimpl_.reset(); }

void OSRM::RunQuery(RouteParameters &route_parameters, http::Reply &reply)
int OSRM::RunQuery(RouteParameters &route_parameters, JSON::Object &json_result)
{
OSRM_pimpl_->RunQuery(route_parameters, reply);
return OSRM_pimpl_->RunQuery(route_parameters, json_result);
}
3 changes: 2 additions & 1 deletion Library/OSRM_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class BasePlugin;
namespace http { class Reply; }
struct RouteParameters;

#include <osrm/json_container.hpp>
#include <osrm/server_paths.hpp>

#include "../data_structures/query_edge.hpp"
Expand All @@ -52,7 +53,7 @@ class OSRM_impl
OSRM_impl(ServerPaths paths, const bool use_shared_memory);
OSRM_impl(const OSRM_impl &) = delete;
virtual ~OSRM_impl();
void RunQuery(RouteParameters &route_parameters, http::Reply &reply);
int RunQuery(RouteParameters &route_parameters, JSON::Object &json_result);

private:
void RegisterPlugin(BasePlugin *plugin);
Expand Down
17 changes: 11 additions & 6 deletions Server/RequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../Util/json_renderer.hpp"
#include "../Util/simple_logger.hpp"
#include "../Util/string_util.hpp"
#include "../Util/xml_renderer.hpp"
#include "../typedefs.h"

#include <osrm/route_parameters.hpp>
Expand Down Expand Up @@ -84,13 +85,14 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply
auto iter = request.begin();
const bool result = boost::spirit::qi::parse(iter, request.end(), api_parser);

JSON::Object json_result;
// check if the was an error with the request
if (!result || (iter != request.end()))
{
reply = http::Reply::StockReply(http::Reply::badRequest);
reply.content.clear();
const auto position = std::distance(request.begin(), iter);
JSON::Object json_result;

json_result.values["status"] = 400;
std::string message = "Query string malformed close to position ";
message += cast::integral_to_string(position);
Expand All @@ -107,29 +109,32 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply
const std::string json_p = (route_parameters.jsonp_parameter + "(");
reply.content.insert(reply.content.end(), json_p.begin(), json_p.end());
}
routing_machine->RunQuery(route_parameters, reply);
if (!route_parameters.jsonp_parameter.empty())
{ // append brace to jsonp response
reply.content.push_back(')');
}
routing_machine->RunQuery(route_parameters, json_result);

// set headers
reply.headers.emplace_back("Content-Length", cast::integral_to_string(reply.content.size()));
if ("gpx" == route_parameters.output_format)
{ // gpx file
JSON::gpx_render(reply.content, json_result.values["route"]);
reply.headers.emplace_back("Content-Type", "application/gpx+xml; charset=UTF-8");
reply.headers.emplace_back("Content-Disposition", "attachment; filename=\"route.gpx\"");
}
else if (route_parameters.jsonp_parameter.empty())
{ // json file
JSON::render(reply.content, json_result);
reply.headers.emplace_back("Content-Type", "application/json; charset=UTF-8");
reply.headers.emplace_back("Content-Disposition", "inline; filename=\"response.json\"");
}
else
{ // jsonp
JSON::render(reply.content, json_result);
reply.headers.emplace_back("Content-Type", "text/javascript; charset=UTF-8");
reply.headers.emplace_back("Content-Disposition", "inline; filename=\"response.js\"");
}
if (!route_parameters.jsonp_parameter.empty())
{ // append brace to jsonp response
reply.content.push_back(')');
}
}
catch (const std::exception &e)
{
Expand Down
6 changes: 4 additions & 2 deletions descriptors/descriptor_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../data_structures/raw_route_data.hpp"
#include "../typedefs.h"

#include <osrm/json_container.hpp>

#include <string>
#include <unordered_map>
#include <vector>
Expand Down Expand Up @@ -77,8 +79,8 @@ template <class DataFacadeT> class BaseDescriptor
BaseDescriptor() {}
// Maybe someone can explain the pure virtual destructor thing to me (dennis)
virtual ~BaseDescriptor() {}
virtual void Run(const RawRouteData &raw_route, http::Reply &reply) = 0;
virtual void SetConfig(const DescriptorConfig &config) = 0;
virtual void Run(const RawRouteData &, JSON::Object &) = 0;
virtual void SetConfig(const DescriptorConfig &) = 0;
};

#endif // DESCRIPTOR_BASE_HPP
17 changes: 9 additions & 8 deletions descriptors/gpx_descriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ template <class DataFacadeT> class GPXDescriptor final : public BaseDescriptor<D
DescriptorConfig config;
DataFacadeT *facade;

void AddRoutePoint(const FixedPointCoordinate &coordinate, JSON::Array &json_result)
void AddRoutePoint(const FixedPointCoordinate &coordinate, JSON::Array &json_route)
{
JSON::Object json_lat;
JSON::Object json_lon;
Expand All @@ -59,35 +59,36 @@ template <class DataFacadeT> class GPXDescriptor final : public BaseDescriptor<D
json_row.values.push_back(json_lon);
JSON::Object entry;
entry.values["rtept"] = json_row;
json_result.values.push_back(entry);
json_route.values.push_back(entry);
}

public:
explicit GPXDescriptor(DataFacadeT *facade) : facade(facade) {}

void SetConfig(const DescriptorConfig &c) final { config = c; }

void Run(const RawRouteData &raw_route, http::Reply &reply) final
void Run(const RawRouteData &raw_route, JSON::Object &json_result) final
{
JSON::Array json_result;
JSON::Array json_route;
if (raw_route.shortest_path_length != INVALID_EDGE_WEIGHT)
{
AddRoutePoint(raw_route.segment_end_coordinates.front().source_phantom.location,
json_result);
json_route);

for (const std::vector<PathData> &path_data_vector : raw_route.unpacked_path_segments)
{
for (const PathData &path_data : path_data_vector)
{
const FixedPointCoordinate current_coordinate =
facade->GetCoordinateOfNode(path_data.node);
AddRoutePoint(current_coordinate, json_result);
AddRoutePoint(current_coordinate, json_route);
}
}
AddRoutePoint(raw_route.segment_end_coordinates.back().target_phantom.location,
json_result);
json_route);
}
JSON::gpx_render(reply.content, json_result);
// JSON::gpx_render(reply.content, json_route);
json_result.values["route"] = json_route;
}
};
#endif // GPX_DESCRIPTOR_HPP
21 changes: 10 additions & 11 deletions descriptors/json_descriptor.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Copyright (c) 2014, Project OSRM, Dennis Luxen, others
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
Expand Down Expand Up @@ -99,15 +99,14 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
return added_element_count;
}

void Run(const RawRouteData &raw_route, http::Reply &reply) final
void Run(const RawRouteData &raw_route, JSON::Object &json_result) final
{
JSON::Object json_result;
if (INVALID_EDGE_WEIGHT == raw_route.shortest_path_length)
{
// We do not need to do much, if there is no route ;-)
json_result.values["status"] = 207;
json_result.values["status_message"] = "Cannot find route between points";
JSON::render(reply.content, json_result);
// JSON::render(reply.content, json_result);
return;
}

Expand Down Expand Up @@ -296,10 +295,10 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
json_result.values["hint_data"] = json_hint_object;

// render the content to the output array
TIMER_START(route_render);
JSON::render(reply.content, json_result);
TIMER_STOP(route_render);
SimpleLogger().Write(logDEBUG) << "rendering took: " << TIMER_MSEC(route_render);
// TIMER_START(route_render);
// JSON::render(reply.content, json_result);
// TIMER_STOP(route_render);
// SimpleLogger().Write(logDEBUG) << "rendering took: " << TIMER_MSEC(route_render);
}

// TODO: reorder parameters
Expand Down Expand Up @@ -356,7 +355,7 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
json_instruction_row.values.push_back(
cast::integral_to_string(static_cast<unsigned>(segment.length)) + "m");
const double bearing_value = (segment.bearing / 10.);
json_instruction_row.values.push_back(Bearing::Get(bearing_value));
json_instruction_row.values.push_back(Azimuth::Get(bearing_value));
json_instruction_row.values.push_back(
static_cast<unsigned>(round(bearing_value)));
json_instruction_row.values.push_back(segment.travel_mode);
Expand Down Expand Up @@ -386,10 +385,10 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
json_last_instruction_row.values.push_back(necessary_segments_running_index - 1);
json_last_instruction_row.values.push_back(0);
json_last_instruction_row.values.push_back("0m");
json_last_instruction_row.values.push_back(Bearing::Get(0.0));
json_last_instruction_row.values.push_back(Azimuth::Get(0.0));
json_last_instruction_row.values.push_back(0.);
json_instruction_array.values.push_back(json_last_instruction_row);
}
};

#endif /* JSON_DESCRIPTOR_HPP */
#endif /* JSON_DESCRIPTOR_H_ */
14 changes: 6 additions & 8 deletions plugins/distance_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ template <class DataFacadeT> class DistanceTablePlugin final : public BasePlugin

const std::string GetDescriptor() const final { return descriptor_string; }

void HandleRequest(const RouteParameters &route_parameters, http::Reply &reply) final
int HandleRequest(const RouteParameters &route_parameters, JSON::Object &json_result) final
{
if (!check_all_coordinates(route_parameters.coordinates))
{
reply = http::Reply::StockReply(http::Reply::badRequest);
return;
return 400;
}

const bool checksum_OK = (route_parameters.check_sum == facade->GetCheckSum());
Expand Down Expand Up @@ -103,11 +102,9 @@ template <class DataFacadeT> class DistanceTablePlugin final : public BasePlugin

if (!result_table)
{
reply = http::Reply::StockReply(http::Reply::badRequest);
return;
return 400;
}

JSON::Object json_object;
JSON::Array json_array;
const auto number_of_locations = phantom_node_vector.size();
for (const auto row : osrm::irange<std::size_t>(0, number_of_locations))
Expand All @@ -118,8 +115,9 @@ template <class DataFacadeT> class DistanceTablePlugin final : public BasePlugin
json_row.values.insert(json_row.values.end(), row_begin_iterator, row_end_iterator);
json_array.values.push_back(json_row);
}
json_object.values["distance_table"] = json_array;
JSON::render(reply.content, json_object);
json_result.values["distance_table"] = json_array;
// JSON::render(reply.content, json_object);
return 200;
}

private:
Expand Down
8 changes: 2 additions & 6 deletions plugins/hello_world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ class HelloWorldPlugin final : public BasePlugin
virtual ~HelloWorldPlugin() {}
const std::string GetDescriptor() const final { return descriptor_string; }

void HandleRequest(const RouteParameters &routeParameters, http::Reply &reply) final
int HandleRequest(const RouteParameters &routeParameters, JSON::Object &json_result) final
{
reply.status = http::Reply::ok;

JSON::Object json_result;
std::string temp_string;
json_result.values["title"] = "Hello World";

Expand Down Expand Up @@ -98,8 +95,7 @@ class HelloWorldPlugin final : public BasePlugin
++counter;
}
json_result.values["hints"] = json_hints;

JSON::render(reply.content, json_result);
return 200;
}

private:
Expand Down
10 changes: 3 additions & 7 deletions plugins/locate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,14 @@ template <class DataFacadeT> class LocatePlugin final : public BasePlugin
explicit LocatePlugin(DataFacadeT *facade) : descriptor_string("locate"), facade(facade) {}
const std::string GetDescriptor() const final { return descriptor_string; }

void HandleRequest(const RouteParameters &route_parameters, http::Reply &reply) final
int HandleRequest(const RouteParameters &route_parameters, JSON::Object &json_result) final
{
// check number of parameters
if (route_parameters.coordinates.empty() || !route_parameters.coordinates.front().is_valid())
{
reply = http::Reply::StockReply(http::Reply::badRequest);
return;
return 400;
}

JSON::Object json_result;
FixedPointCoordinate result;
if (!facade->LocateClosestEndPointForCoordinate(route_parameters.coordinates.front(),
result))
Expand All @@ -62,15 +60,13 @@ template <class DataFacadeT> class LocatePlugin final : public BasePlugin
}
else
{
reply.status = http::Reply::ok;
json_result.values["status"] = 0;
JSON::Array json_coordinate;
json_coordinate.values.push_back(result.lat / COORDINATE_PRECISION);
json_coordinate.values.push_back(result.lon / COORDINATE_PRECISION);
json_result.values["mapped_coordinate"] = json_coordinate;
}

JSON::render(reply.content, json_result);
return 200;
}

private:
Expand Down
Loading

0 comments on commit 4a63256

Please sign in to comment.