|
| 1 | +#include <functional> |
| 2 | +#include <string> |
| 3 | +#include <stdexcept> |
| 4 | +#include <vector> |
| 5 | +#include <unordered_map> |
| 6 | +#include <cstdint> |
| 7 | +#include <sstream> |
| 8 | +#include <boost/property_tree/ptree.hpp> |
| 9 | +#include <boost/property_tree/info_parser.hpp> |
| 10 | + |
| 11 | +#include <prime_server/prime_server.hpp> |
| 12 | +#include <prime_server/http_protocol.hpp> |
| 13 | +using namespace prime_server; |
| 14 | + |
| 15 | +#include <valhalla/midgard/logging.h> |
| 16 | +#include <valhalla/baldr/json.h> |
| 17 | +#include <valhalla/baldr/location.h> |
| 18 | +#include <valhalla/baldr/pathlocation.h> |
| 19 | +#include <valhalla/baldr/graphreader.h> |
| 20 | +#include <valhalla/sif/costfactory.h> |
| 21 | +#include <valhalla/sif/autocost.h> |
| 22 | +#include <valhalla/sif/bicyclecost.h> |
| 23 | +#include <valhalla/sif/pedestriancost.h> |
| 24 | + |
| 25 | +#include "thor/service.h" |
| 26 | +#include "thor/trippathbuilder.h" |
| 27 | +#include "thor/pathalgorithm.h" |
| 28 | + |
| 29 | +using namespace valhalla; |
| 30 | +using namespace valhalla::midgard; |
| 31 | +using namespace valhalla::baldr; |
| 32 | +using namespace valhalla::sif; |
| 33 | + |
| 34 | + |
| 35 | +namespace { |
| 36 | + //TODO: throw this in the header to make it testable? |
| 37 | + class thor_worker_t { |
| 38 | + public: |
| 39 | + thor_worker_t(const boost::property_tree::ptree& config):config(config), |
| 40 | + origin(PointLL()), destination(PointLL()), reader(config.get_child("mjolnir.hierarchy")) { |
| 41 | + // Register edge/node costing methods |
| 42 | + factory.Register("auto", sif::CreateAutoCost); |
| 43 | + factory.Register("auto_shorter", sif::CreateAutoShorterCost); |
| 44 | + factory.Register("bicycle", sif::CreateBicycleCost); |
| 45 | + factory.Register("pedestrian", sif::CreatePedestrianCost); |
| 46 | + } |
| 47 | + worker_t::result_t work(const std::list<zmq::message_t>& job, void* request_info) { |
| 48 | + try{ |
| 49 | + //get some info about what we need to do |
| 50 | + std::string request_str(static_cast<const char*>(job.front().data()), job.front().size()); |
| 51 | + std::stringstream stream(request_str); |
| 52 | + boost::property_tree::ptree request; |
| 53 | + boost::property_tree::read_info(stream, request); |
| 54 | + init_request(request); |
| 55 | + |
| 56 | + //find a path |
| 57 | + auto path_edges = path_algorithm.GetBestPath(origin, destination, reader, cost); |
| 58 | + if (path_edges.size() == 0) { |
| 59 | + if (cost->AllowMultiPass()) { |
| 60 | + LOG_INFO("Try again with relaxed hierarchy limits"); |
| 61 | + path_algorithm.Clear(); |
| 62 | + cost->RelaxHierarchyLimits(16.0f); |
| 63 | + path_edges = path_algorithm.GetBestPath(origin, destination, reader, cost); |
| 64 | + } |
| 65 | + } |
| 66 | + if (path_edges.size() == 0) { |
| 67 | + cost->DisableHighwayTransitions(); |
| 68 | + path_edges = path_algorithm.GetBestPath(origin, destination, reader, cost); |
| 69 | + if (path_edges.size() == 0) { |
| 70 | + throw std::runtime_error("No path could be found for input"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Form output information based on path edges |
| 75 | + auto trip_path = thor::TripPathBuilder::Build(reader, path_edges, origin, destination); |
| 76 | + path_algorithm.Clear(); |
| 77 | + locations.clear(); |
| 78 | + |
| 79 | + //pass it on |
| 80 | + worker_t::result_t result{true}; |
| 81 | + result.messages.emplace_back(std::move(request_str)); //the original request |
| 82 | + result.messages.emplace_back(trip_path.SerializeAsString()); //the protobuf path |
| 83 | + return result; |
| 84 | + } |
| 85 | + catch(const std::exception& e) { |
| 86 | + worker_t::result_t result{false}; |
| 87 | + http_response_t response(400, "Bad Request", e.what()); |
| 88 | + response.from_info(static_cast<http_request_t::info_t*>(request_info)); |
| 89 | + result.messages.emplace_back(response.to_string()); |
| 90 | + return result; |
| 91 | + } |
| 92 | + } |
| 93 | + void init_request(const boost::property_tree::ptree& request) { |
| 94 | + //we require locations |
| 95 | + try { |
| 96 | + for(const auto& location : request.get_child("locations")) |
| 97 | + locations.push_back(baldr::Location::FromPtree(location.second)); |
| 98 | + if(locations.size() < 2) |
| 99 | + throw; |
| 100 | + //TODO: bail if this is too many |
| 101 | + } |
| 102 | + catch(...) { |
| 103 | + throw std::runtime_error("insufficiently specified required parameter 'locations'"); |
| 104 | + } |
| 105 | + |
| 106 | + //we require correlated locations |
| 107 | + try { |
| 108 | + auto origin_pt = request.get_child("origin"); |
| 109 | + origin = PathLocation::FromPtree(locations, origin_pt); |
| 110 | + auto destination_pt = request.get_child("destination"); |
| 111 | + destination = PathLocation::FromPtree(locations, destination_pt); |
| 112 | + } |
| 113 | + catch(...) { |
| 114 | + throw std::runtime_error("path computation requires graph correlated locations"); |
| 115 | + } |
| 116 | + |
| 117 | + // Parse out the type of route - this provides the costing method to use |
| 118 | + std::string costing; |
| 119 | + try { |
| 120 | + costing = request.get<std::string>("costing"); |
| 121 | + } |
| 122 | + catch(...) { |
| 123 | + throw std::runtime_error("No edge/node costing provided"); |
| 124 | + } |
| 125 | + |
| 126 | + // Get the costing options. Get the base options from the config and the |
| 127 | + // options for the specified costing method |
| 128 | + std::string method_options = "costing_options." + costing; |
| 129 | + boost::property_tree::ptree config_costing = config.get_child(method_options); |
| 130 | + const auto& request_costing = request.get_child_optional(method_options); |
| 131 | + if (request_costing) { |
| 132 | + // If the request has any options for this costing type, merge the 2 |
| 133 | + // costing options - override any config options that are in the request. |
| 134 | + // and add any request options not in the config. |
| 135 | + for (const auto& r : *request_costing) { |
| 136 | + config_costing.put_child(r.first, r.second); |
| 137 | + } |
| 138 | + } |
| 139 | + cost = factory.Create(costing, config_costing); |
| 140 | + } |
| 141 | + protected: |
| 142 | + boost::property_tree::ptree config; |
| 143 | + std::vector<Location> locations; |
| 144 | + PathLocation origin, destination; |
| 145 | + sif::CostFactory<sif::DynamicCost> factory; |
| 146 | + valhalla::sif::cost_ptr_t cost; |
| 147 | + valhalla::baldr::GraphReader reader; |
| 148 | + valhalla::thor::PathAlgorithm path_algorithm; |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +namespace valhalla { |
| 153 | + namespace thor { |
| 154 | + void run_service(const boost::property_tree::ptree& config) { |
| 155 | + //gets requests from thor proxy |
| 156 | + auto upstream_endpoint = config.get<std::string>("thor.service.proxy") + "_out"; |
| 157 | + //sends them on to odin |
| 158 | + auto downstream_endpoint = config.get<std::string>("odin.service.proxy") + "_in"; |
| 159 | + //or returns just location information back to the server |
| 160 | + auto loopback_endpoint = config.get<std::string>("httpd.service.loopback"); |
| 161 | + |
| 162 | + //listen for requests |
| 163 | + zmq::context_t context; |
| 164 | + prime_server::worker_t worker(context, upstream_endpoint, downstream_endpoint, loopback_endpoint, |
| 165 | + std::bind(&thor_worker_t::work, thor_worker_t(config), std::placeholders::_1, std::placeholders::_2)); |
| 166 | + worker.work(); |
| 167 | + |
| 168 | + //TODO: should we listen for SIGINT and terminate gracefully/exit(0)? |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments