Skip to content

Commit

Permalink
Passed ResultT down to engine plugins, so now they can form replies i…
Browse files Browse the repository at this point in the history
…n different formats.
  • Loading branch information
Denis Chaplygin committed Aug 26, 2019
1 parent f6f86b2 commit 75aadb0
Show file tree
Hide file tree
Showing 32 changed files with 136 additions and 99 deletions.
21 changes: 21 additions & 0 deletions include/engine/api/base_result.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef ENGINE_API_BASE_RESULT_HPP
#define ENGINE_API_BASE_RESULT_HPP

#include <mapbox/variant.hpp>

#include <string>

#include "util/json_container.hpp"

namespace osrm
{
namespace engine
{
namespace api
{
using ResultT = mapbox::util::variant<util::json::Object, std::string>;
} // ns api
} // ns engine
} // ns osrm

#endif
27 changes: 15 additions & 12 deletions include/engine/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ class EngineInterface
public:
virtual ~EngineInterface() = default;
virtual Status Route(const api::RouteParameters &parameters,
util::json::Object &result) const = 0;
api::ResultT &result) const = 0;
virtual Status Table(const api::TableParameters &parameters,
util::json::Object &result) const = 0;
api::ResultT &result) const = 0;
virtual Status Nearest(const api::NearestParameters &parameters,
util::json::Object &result) const = 0;
api::ResultT &result) const = 0;
virtual Status Trip(const api::TripParameters &parameters,
util::json::Object &result) const = 0;
api::ResultT &result) const = 0;
virtual Status Match(const api::MatchParameters &parameters,
util::json::Object &result) const = 0;
virtual Status Tile(const api::TileParameters &parameters, std::string &result) const = 0;
api::ResultT &result) const = 0;
virtual Status Tile(const api::TileParameters &parameters,
api::ResultT &result) const = 0;
};

template <typename Algorithm> class Engine final : public EngineInterface
Expand Down Expand Up @@ -90,35 +91,37 @@ template <typename Algorithm> class Engine final : public EngineInterface
virtual ~Engine() = default;

Status Route(const api::RouteParameters &params,
util::json::Object &result) const override final
api::ResultT &result) const override final
{
return route_plugin.HandleRequest(GetAlgorithms(params), params, result);
}

Status Table(const api::TableParameters &params,
util::json::Object &result) const override final
api::ResultT &result) const override final
{
return table_plugin.HandleRequest(GetAlgorithms(params), params, result);
}

Status Nearest(const api::NearestParameters &params,
util::json::Object &result) const override final
api::ResultT &result) const override final
{
return nearest_plugin.HandleRequest(GetAlgorithms(params), params, result);
}

Status Trip(const api::TripParameters &params, util::json::Object &result) const override final
Status Trip(const api::TripParameters &params,
api::ResultT &result) const override final
{
return trip_plugin.HandleRequest(GetAlgorithms(params), params, result);
}

Status Match(const api::MatchParameters &params,
util::json::Object &result) const override final
api::ResultT &result) const override final
{
return match_plugin.HandleRequest(GetAlgorithms(params), params, result);
}

Status Tile(const api::TileParameters &params, std::string &result) const override final
Status Tile(const api::TileParameters &params,
api::ResultT &result) const override final
{
return tile_plugin.HandleRequest(GetAlgorithms(params), params, result);
}
Expand Down
2 changes: 1 addition & 1 deletion include/engine/plugins/match.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MatchPlugin : public BasePlugin

Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::MatchParameters &parameters,
util::json::Object &json_result) const;
osrm::engine::api::ResultT &json_result) const;

private:
const int max_locations_map_matching;
Expand Down
2 changes: 1 addition & 1 deletion include/engine/plugins/nearest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NearestPlugin final : public BasePlugin

Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::NearestParameters &params,
util::json::Object &result) const;
osrm::engine::api::ResultT &result) const;

private:
const int max_results;
Expand Down
7 changes: 5 additions & 2 deletions include/engine/plugins/plugin_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BASE_PLUGIN_HPP

#include "engine/api/base_parameters.hpp"
#include "engine/api/base_result.hpp"
#include "engine/datafacade/datafacade_base.hpp"
#include "engine/phantom_node.hpp"
#include "engine/routing_algorithms.hpp"
Expand Down Expand Up @@ -39,7 +40,7 @@ class BasePlugin

bool CheckAlgorithms(const api::BaseParameters &params,
const RoutingAlgorithmsInterface &algorithms,
util::json::Object &result) const
osrm::engine::api::ResultT &result) const
{
if (algorithms.IsValid())
{
Expand All @@ -64,8 +65,10 @@ class BasePlugin

Status Error(const std::string &code,
const std::string &message,
util::json::Object &json_result) const
osrm::engine::api::ResultT &result) const
{
result = util::json::Object();
auto& json_result = result.get<util::json::Object>();
json_result.values["code"] = code;
json_result.values["message"] = message;
return Status::Error;
Expand Down
2 changes: 1 addition & 1 deletion include/engine/plugins/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TablePlugin final : public BasePlugin

Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::TableParameters &params,
util::json::Object &result) const;
osrm::engine::api::ResultT &result) const;

private:
const int max_locations_distance_table;
Expand Down
2 changes: 1 addition & 1 deletion include/engine/plugins/tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TilePlugin final : public BasePlugin
public:
Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::TileParameters &parameters,
std::string &pbf_buffer) const;
osrm::engine::api::ResultT &pbf_buffer) const;
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/engine/plugins/trip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TripPlugin final : public BasePlugin

Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::TripParameters &parameters,
util::json::Object &json_result) const;
osrm::engine::api::ResultT &json_result) const;
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/engine/plugins/viaroute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ViaRoutePlugin final : public BasePlugin

Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::RouteParameters &route_parameters,
util::json::Object &json_result) const;
osrm::engine::api::ResultT &json_result) const;
};
}
}
Expand Down
13 changes: 7 additions & 6 deletions include/osrm/osrm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "osrm/osrm_fwd.hpp"
#include "osrm/status.hpp"
#include "engine/api/base_result.hpp"

#include <memory>
#include <string>
Expand Down Expand Up @@ -83,7 +84,7 @@ class OSRM final
* \return Status indicating success for the query or failure
* \see Status, RouteParameters and json::Object
*/
Status Route(const RouteParameters &parameters, json::Object &result) const;
Status Route(const RouteParameters &parameters, osrm::engine::api::ResultT &result) const;

/**
* Distance tables for coordinates.
Expand All @@ -92,7 +93,7 @@ class OSRM final
* \return Status indicating success for the query or failure
* \see Status, TableParameters and json::Object
*/
Status Table(const TableParameters &parameters, json::Object &result) const;
Status Table(const TableParameters &parameters, osrm::engine::api::ResultT &result) const;

/**
* Nearest street segment for coordinate.
Expand All @@ -101,7 +102,7 @@ class OSRM final
* \return Status indicating success for the query or failure
* \see Status, NearestParameters and json::Object
*/
Status Nearest(const NearestParameters &parameters, json::Object &result) const;
Status Nearest(const NearestParameters &parameters, osrm::engine::api::ResultT &result) const;

/**
* Trip: shortest round trip between coordinates.
Expand All @@ -110,7 +111,7 @@ class OSRM final
* \return Status indicating success for the query or failure
* \see Status, TripParameters and json::Object
*/
Status Trip(const TripParameters &parameters, json::Object &result) const;
Status Trip(const TripParameters &parameters, osrm::engine::api::ResultT &result) const;

/**
* Match: snaps noisy coordinate traces to the road network
Expand All @@ -119,7 +120,7 @@ class OSRM final
* \return Status indicating success for the query or failure
* \see Status, MatchParameters and json::Object
*/
Status Match(const MatchParameters &parameters, json::Object &result) const;
Status Match(const MatchParameters &parameters, osrm::engine::api::ResultT &result) const;

/**
* Tile: vector tiles with internal graph representation
Expand All @@ -128,7 +129,7 @@ class OSRM final
* \return Status indicating success for the query or failure
* \see Status, TileParameters and json::Object
*/
Status Tile(const TileParameters &parameters, std::string &result) const;
Status Tile(const TileParameters &parameters, osrm::engine::api::ResultT &result) const;

private:
std::unique_ptr<engine::EngineInterface> engine_;
Expand Down
4 changes: 1 addition & 3 deletions include/server/service/base_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ namespace service
class BaseService
{
public:
using ResultT = mapbox::util::variant<util::json::Object, std::string>;

BaseService(OSRM &routing_machine) : routing_machine(routing_machine) {}
virtual ~BaseService() = default;

virtual engine::Status
RunQuery(std::size_t prefix_length, std::string &query, ResultT &result) = 0;
RunQuery(std::size_t prefix_length, std::string &query, osrm::engine::api::ResultT &result) = 0;

virtual unsigned GetVersion() = 0;

Expand Down
2 changes: 1 addition & 1 deletion include/server/service/match_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MatchService final : public BaseService
MatchService(OSRM &routing_machine) : BaseService(routing_machine) {}

engine::Status
RunQuery(std::size_t prefix_length, std::string &query, ResultT &result) final override;
RunQuery(std::size_t prefix_length, std::string &query, osrm::engine::api::ResultT &result) final override;

unsigned GetVersion() final override { return 1; }
};
Expand Down
2 changes: 1 addition & 1 deletion include/server/service/nearest_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NearestService final : public BaseService
NearestService(OSRM &routing_machine) : BaseService(routing_machine) {}

engine::Status
RunQuery(std::size_t prefix_length, std::string &query, ResultT &result) final override;
RunQuery(std::size_t prefix_length, std::string &query, osrm::engine::api::ResultT &result) final override;

unsigned GetVersion() final override { return 1; }
};
Expand Down
2 changes: 1 addition & 1 deletion include/server/service/route_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RouteService final : public BaseService
RouteService(OSRM &routing_machine) : BaseService(routing_machine) {}

engine::Status
RunQuery(std::size_t prefix_length, std::string &query, ResultT &result) final override;
RunQuery(std::size_t prefix_length, std::string &query, osrm::engine::api::ResultT &result) final override;

unsigned GetVersion() final override { return 1; }
};
Expand Down
2 changes: 1 addition & 1 deletion include/server/service/table_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TableService final : public BaseService
TableService(OSRM &routing_machine) : BaseService(routing_machine) {}

engine::Status
RunQuery(std::size_t prefix_length, std::string &query, ResultT &result) final override;
RunQuery(std::size_t prefix_length, std::string &query, osrm::engine::api::ResultT &result) final override;

unsigned GetVersion() final override { return 1; }
};
Expand Down
2 changes: 1 addition & 1 deletion include/server/service/tile_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TileService final : public BaseService
TileService(OSRM &routing_machine) : BaseService(routing_machine) {}

engine::Status
RunQuery(std::size_t prefix_length, std::string &query, ResultT &result) final override;
RunQuery(std::size_t prefix_length, std::string &query, osrm::engine::api::ResultT &result) final override;

unsigned GetVersion() final override { return 1; }
};
Expand Down
2 changes: 1 addition & 1 deletion include/server/service/trip_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TripService final : public BaseService
TripService(OSRM &routing_machine) : BaseService(routing_machine) {}

engine::Status
RunQuery(std::size_t prefix_length, std::string &query, ResultT &result) final override;
RunQuery(std::size_t prefix_length, std::string &query, osrm::engine::api::ResultT &result) final override;

unsigned GetVersion() final override { return 1; }
};
Expand Down
5 changes: 3 additions & 2 deletions include/server/service_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "server/service/base_service.hpp"

#include "osrm/osrm.hpp"
#include "engine/api/base_api.hpp"

#include <unordered_map>

Expand All @@ -28,14 +29,14 @@ class ServiceHandlerInterface
public:
virtual ~ServiceHandlerInterface() {}
virtual engine::Status RunQuery(api::ParsedURL parsed_url,
service::BaseService::ResultT &result) = 0;
osrm::engine::api::ResultT &result) = 0;
};

class ServiceHandler final : public ServiceHandlerInterface
{
public:
ServiceHandler(osrm::EngineConfig &config);
using ResultT = service::BaseService::ResultT;
using ResultT = osrm::engine::api::ResultT;

virtual engine::Status RunQuery(api::ParsedURL parsed_url, ResultT &result) override;

Expand Down
Loading

0 comments on commit 75aadb0

Please sign in to comment.