Add table service

This commit is contained in:
Patrick Niklaus 2016-02-16 19:04:09 +01:00
parent c127aaae6b
commit 277829c280
3 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,34 @@
#ifndef SERVER_SERVICE_TABLE_SERVICE_HPP
#define SERVER_SERVICE_TABLE_SERVICE_HPP
#include "server/service/base_service.hpp"
#include "engine/status.hpp"
#include "util/coordinate.hpp"
#include "osrm/osrm.hpp"
#include <string>
#include <vector>
namespace osrm
{
namespace server
{
namespace service
{
class TableService final : public BaseService
{
public:
TableService(OSRM& routing_machine) : BaseService(routing_machine) {}
virtual engine::Status RunQuery(std::vector<util::FixedPointCoordinate> coordinates,
std::string &options,
util::json::Object &json_result) final override;
virtual unsigned GetVersion() final override { return 1; }
};
}
}
}
#endif

View File

@ -0,0 +1,82 @@
#include "server/service/table_service.hpp"
#include "engine/api/route_parameters.hpp"
#include "server/api/parameters_parser.hpp"
#include "util/json_container.hpp"
#include <boost/format.hpp>
namespace osrm
{
namespace server
{
namespace service
{
namespace {
const constexpr char PARAMETER_SIZE_MISMATCH_MSG[] = "Number of elements in %1% size %2% does not match coordinate size %3%";
template<typename ParamT>
bool constrainParamSize(const char* msg_template, const char* name, const ParamT& param, const std::size_t target_size, std::string& help)
{
if (param.size() > 0 && param.size() != target_size)
{
help = (boost::format(msg_template) % name % param.size() % target_size).str();
return true;
}
return false;
}
std::string getWrongOptionHelp(const engine::api::TableParameters& parameters)
{
std::string help;
const auto coord_size = parameters.coordinates.size();
const bool param_size_mismatch = constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "hints", parameters.hints, coord_size, help)
|| constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "bearings", parameters.bearings, coord_size, help)
|| constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "radiuses", parameters.radiuses, coord_size, help);
if (!param_size_mismatch && parameters.coordinates.size() < 2)
{
help = "Number of coordinates needs to be at least two.";
}
return help;
}
} // anon. ns
engine::Status TableService::RunQuery(std::vector<util::FixedPointCoordinate> coordinates,
std::string &options,
util::json::Object &json_result)
{
auto options_iterator = options.begin();
auto parameters = api::parseParameters<engine::api::TableParameters>(options_iterator, options.end());
if (!parameters || options_iterator != options.end())
{
const auto position = std::distance(options.begin(), options_iterator);
json_result.values["code"] = "invalid-options";
json_result.values["message"] =
"Options string malformed close to position " + std::to_string(position);
return engine::Status::Error;
}
BOOST_ASSERT(parameters);
parameters->coordinates = std::move(coordinates);
if (!parameters->IsValid())
{
json_result.values["code"] = "invalid-options";
json_result.values["message"] = getWrongOptionHelp(*parameters);
return engine::Status::Error;
}
BOOST_ASSERT(parameters->IsValid());
return BaseService::routing_machine.Table(*parameters, json_result);
}
}
}
}

View File

@ -1,6 +1,7 @@
#include "server/service_handler.hpp" #include "server/service_handler.hpp"
#include "server/service/route_service.hpp" #include "server/service/route_service.hpp"
#include "server/service/table_service.hpp"
#include "server/api/parsed_url.hpp" #include "server/api/parsed_url.hpp"
#include "util/json_util.hpp" #include "util/json_util.hpp"
@ -13,6 +14,7 @@ namespace server
ServiceHandler::ServiceHandler(osrm::EngineConfig &config) : routing_machine(config) ServiceHandler::ServiceHandler(osrm::EngineConfig &config) : routing_machine(config)
{ {
service_map["route"] = util::make_unique<service::RouteService>(routing_machine); service_map["route"] = util::make_unique<service::RouteService>(routing_machine);
service_map["table"] = util::make_unique<service::TableService>(routing_machine);
} }
engine::Status ServiceHandler::RunQuery(api::ParsedURL parsed_url, util::json::Object &json_result) engine::Status ServiceHandler::RunQuery(api::ParsedURL parsed_url, util::json::Object &json_result)