2016-01-02 11:13:44 -05:00
|
|
|
#include "engine/osrm_impl.hpp"
|
2015-01-27 10:35:19 -05:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "engine/plugins/distance_table.hpp"
|
|
|
|
#include "engine/plugins/hello_world.hpp"
|
|
|
|
#include "engine/plugins/nearest.hpp"
|
|
|
|
#include "engine/plugins/timestamp.hpp"
|
|
|
|
#include "engine/plugins/trip.hpp"
|
|
|
|
#include "engine/plugins/viaroute.hpp"
|
|
|
|
#include "engine/plugins/match.hpp"
|
|
|
|
#include "engine/datafacade/datafacade_base.hpp"
|
|
|
|
#include "engine/datafacade/internal_datafacade.hpp"
|
|
|
|
#include "engine/datafacade/shared_barriers.hpp"
|
|
|
|
#include "engine/datafacade/shared_datafacade.hpp"
|
|
|
|
#include "util/make_unique.hpp"
|
|
|
|
#include "util/routed_options.hpp"
|
|
|
|
#include "util/simple_logger.hpp"
|
2015-01-27 10:35:19 -05:00
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <boost/interprocess/sync/named_condition.hpp>
|
|
|
|
#include <boost/interprocess/sync/scoped_lock.hpp>
|
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "osrm/libosrm_config.hpp"
|
|
|
|
#include "osrm/osrm.hpp"
|
2016-01-03 12:55:42 -05:00
|
|
|
#include "osrm/route_parameters.hpp"
|
2015-01-27 10:35:19 -05:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-01-05 06:04:04 -05:00
|
|
|
OSRM::OSRM_impl::OSRM_impl(LibOSRMConfig &lib_config)
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
|
|
|
if (lib_config.use_shared_memory)
|
|
|
|
{
|
|
|
|
barrier = osrm::make_unique<SharedBarriers>();
|
|
|
|
query_data_facade = new SharedDataFacade<QueryEdge::EdgeData>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// populate base path
|
|
|
|
populate_base_path(lib_config.server_paths);
|
|
|
|
query_data_facade = new InternalDataFacade<QueryEdge::EdgeData>(lib_config.server_paths);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following plugins handle all requests.
|
|
|
|
RegisterPlugin(new DistanceTablePlugin<BaseDataFacade<QueryEdge::EdgeData>>(
|
|
|
|
query_data_facade, lib_config.max_locations_distance_table));
|
|
|
|
RegisterPlugin(new HelloWorldPlugin());
|
|
|
|
RegisterPlugin(new NearestPlugin<BaseDataFacade<QueryEdge::EdgeData>>(query_data_facade));
|
2015-03-02 17:39:53 -05:00
|
|
|
RegisterPlugin(new MapMatchingPlugin<BaseDataFacade<QueryEdge::EdgeData>>(
|
|
|
|
query_data_facade, lib_config.max_locations_map_matching));
|
2015-01-27 10:35:19 -05:00
|
|
|
RegisterPlugin(new TimestampPlugin<BaseDataFacade<QueryEdge::EdgeData>>(query_data_facade));
|
2016-01-05 06:04:04 -05:00
|
|
|
RegisterPlugin(new ViaRoutePlugin<BaseDataFacade<QueryEdge::EdgeData>>(
|
|
|
|
query_data_facade, lib_config.max_locations_viaroute));
|
|
|
|
RegisterPlugin(new RoundTripPlugin<BaseDataFacade<QueryEdge::EdgeData>>(
|
|
|
|
query_data_facade, lib_config.max_locations_trip));
|
2015-01-27 10:35:19 -05:00
|
|
|
}
|
|
|
|
|
2015-12-15 13:25:26 -05:00
|
|
|
void OSRM::OSRM_impl::RegisterPlugin(BasePlugin *raw_plugin_ptr)
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
2015-12-14 16:00:20 -05:00
|
|
|
std::unique_ptr<BasePlugin> plugin_ptr(raw_plugin_ptr);
|
|
|
|
SimpleLogger().Write() << "loaded plugin: " << plugin_ptr->GetDescriptor();
|
|
|
|
plugin_map[plugin_ptr->GetDescriptor()] = std::move(plugin_ptr);
|
2015-01-27 10:35:19 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 06:04:04 -05:00
|
|
|
int OSRM::OSRM_impl::RunQuery(const RouteParameters &route_parameters,
|
|
|
|
osrm::json::Object &json_result)
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
|
|
|
const auto &plugin_iterator = plugin_map.find(route_parameters.service);
|
|
|
|
|
|
|
|
if (plugin_map.end() == plugin_iterator)
|
|
|
|
{
|
2015-12-16 22:14:06 -05:00
|
|
|
json_result.values["status_message"] = "Service not found";
|
2015-01-27 10:35:19 -05:00
|
|
|
return 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
increase_concurrent_query_count();
|
2015-12-16 22:14:06 -05:00
|
|
|
auto return_code = plugin_iterator->second->HandleRequest(route_parameters, json_result);
|
2015-01-27 10:35:19 -05:00
|
|
|
decrease_concurrent_query_count();
|
2015-12-17 10:45:15 -05:00
|
|
|
return static_cast<int>(return_code);
|
2015-01-27 10:35:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrease number of concurrent queries
|
2015-12-15 13:25:26 -05:00
|
|
|
void OSRM::OSRM_impl::decrease_concurrent_query_count()
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
|
|
|
if (!barrier)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// lock query
|
|
|
|
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> query_lock(
|
|
|
|
barrier->query_mutex);
|
|
|
|
|
|
|
|
// decrement query count
|
|
|
|
--(barrier->number_of_queries);
|
|
|
|
BOOST_ASSERT_MSG(0 <= barrier->number_of_queries, "invalid number of queries");
|
|
|
|
|
|
|
|
// notify all processes that were waiting for this condition
|
|
|
|
if (0 == barrier->number_of_queries)
|
|
|
|
{
|
|
|
|
barrier->no_running_queries_condition.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// increase number of concurrent queries
|
2015-12-15 13:25:26 -05:00
|
|
|
void OSRM::OSRM_impl::increase_concurrent_query_count()
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
|
|
|
if (!barrier)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock update pending
|
|
|
|
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> pending_lock(
|
|
|
|
barrier->pending_update_mutex);
|
|
|
|
|
|
|
|
// lock query
|
|
|
|
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> query_lock(
|
|
|
|
barrier->query_mutex);
|
|
|
|
|
|
|
|
// unlock update pending
|
|
|
|
pending_lock.unlock();
|
|
|
|
|
|
|
|
// increment query count
|
|
|
|
++(barrier->number_of_queries);
|
|
|
|
|
|
|
|
(static_cast<SharedDataFacade<QueryEdge::EdgeData> *>(query_data_facade))
|
|
|
|
->CheckAndReloadFacade();
|
|
|
|
}
|
|
|
|
|
|
|
|
// proxy code for compilation firewall
|
2015-12-15 13:25:26 -05:00
|
|
|
OSRM::OSRM(LibOSRMConfig &lib_config) : OSRM_pimpl_(osrm::make_unique<OSRM_impl>(lib_config)) {}
|
2015-01-27 10:35:19 -05:00
|
|
|
|
2015-12-15 13:25:26 -05:00
|
|
|
// needed because unique_ptr needs the size of OSRM_impl for delete
|
|
|
|
OSRM::~OSRM() {}
|
2015-01-27 10:35:19 -05:00
|
|
|
|
2015-12-14 16:00:20 -05:00
|
|
|
int OSRM::RunQuery(const RouteParameters &route_parameters, osrm::json::Object &json_result)
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
|
|
|
return OSRM_pimpl_->RunQuery(route_parameters, json_result);
|
|
|
|
}
|