2013-06-26 20:05:42 -04:00
|
|
|
/*
|
2013-10-14 07:42:28 -04:00
|
|
|
|
2015-01-08 09:07:01 -05:00
|
|
|
Copyright (c) 2015, Project OSRM, Dennis Luxen, others
|
2013-10-14 07:42:28 -04:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2013-06-26 20:05:42 -04:00
|
|
|
|
2014-06-11 06:25:57 -04:00
|
|
|
namespace boost { namespace interprocess { class named_mutex; } }
|
|
|
|
|
2013-12-13 12:32:24 -05:00
|
|
|
#include "OSRM_impl.h"
|
2014-04-16 10:16:42 -04:00
|
|
|
#include "OSRM.h"
|
2013-06-26 20:05:42 -04:00
|
|
|
|
2014-11-28 09:13:29 -05:00
|
|
|
#include "../plugins/distance_table.hpp"
|
|
|
|
#include "../plugins/hello_world.hpp"
|
|
|
|
#include "../plugins/locate.hpp"
|
|
|
|
#include "../plugins/nearest.hpp"
|
|
|
|
#include "../plugins/timestamp.hpp"
|
|
|
|
#include "../plugins/viaroute.hpp"
|
2014-02-11 05:35:29 -05:00
|
|
|
#include "../Server/DataStructures/BaseDataFacade.h"
|
2013-12-08 13:38:13 -05:00
|
|
|
#include "../Server/DataStructures/InternalDataFacade.h"
|
2014-02-11 05:35:29 -05:00
|
|
|
#include "../Server/DataStructures/SharedBarriers.h"
|
2013-12-08 13:38:13 -05:00
|
|
|
#include "../Server/DataStructures/SharedDataFacade.h"
|
2014-09-24 07:17:55 -04:00
|
|
|
#include "../Util/make_unique.hpp"
|
2014-10-23 10:11:54 -04:00
|
|
|
#include "../Util/ProgramOptions.h"
|
2014-10-10 13:31:43 -04:00
|
|
|
#include "../Util/simple_logger.hpp"
|
2013-12-08 13:38:13 -05:00
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
2014-06-11 06:25:57 -04:00
|
|
|
#include <boost/interprocess/sync/named_condition.hpp>
|
|
|
|
#include <boost/interprocess/sync/scoped_lock.hpp>
|
|
|
|
|
2014-11-24 11:57:01 -05:00
|
|
|
#include <osrm/route_parameters.hpp>
|
|
|
|
|
2014-06-11 06:25:57 -04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2013-12-13 12:32:24 -05:00
|
|
|
|
2015-01-08 09:05:54 -05:00
|
|
|
OSRM_impl::OSRM_impl(libosrm_config &lib_config)
|
2013-10-17 12:11:53 -04:00
|
|
|
{
|
2015-01-08 09:05:54 -05:00
|
|
|
if (lib_config.use_shared_memory)
|
2014-03-17 10:19:24 -04:00
|
|
|
{
|
2014-10-08 13:30:42 -04:00
|
|
|
barrier = osrm::make_unique<SharedBarriers>();
|
2014-05-07 10:17:14 -04:00
|
|
|
query_data_facade = new SharedDataFacade<QueryEdge::EdgeData>();
|
2014-03-17 10:19:24 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-08 11:01:53 -04:00
|
|
|
// populate base path
|
2015-01-08 09:05:54 -05:00
|
|
|
populate_base_path(lib_config.server_paths);
|
|
|
|
query_data_facade = new InternalDataFacade<QueryEdge::EdgeData>(lib_config.server_paths);
|
2013-09-17 12:37:08 -04:00
|
|
|
}
|
2013-09-19 12:55:19 -04:00
|
|
|
|
2014-05-07 10:17:14 -04:00
|
|
|
// The following plugins handle all requests.
|
2014-12-23 09:34:54 -05:00
|
|
|
RegisterPlugin(new DistanceTablePlugin<BaseDataFacade<QueryEdge::EdgeData>>(query_data_facade,
|
2015-01-08 09:05:54 -05:00
|
|
|
lib_config.max_locations_distance_table));
|
2014-05-07 10:17:14 -04:00
|
|
|
RegisterPlugin(new HelloWorldPlugin());
|
|
|
|
RegisterPlugin(new LocatePlugin<BaseDataFacade<QueryEdge::EdgeData>>(query_data_facade));
|
|
|
|
RegisterPlugin(new NearestPlugin<BaseDataFacade<QueryEdge::EdgeData>>(query_data_facade));
|
|
|
|
RegisterPlugin(new TimestampPlugin<BaseDataFacade<QueryEdge::EdgeData>>(query_data_facade));
|
|
|
|
RegisterPlugin(new ViaRoutePlugin<BaseDataFacade<QueryEdge::EdgeData>>(query_data_facade));
|
2013-06-26 20:05:42 -04:00
|
|
|
}
|
|
|
|
|
2014-05-07 10:17:14 -04:00
|
|
|
OSRM_impl::~OSRM_impl()
|
|
|
|
{
|
2014-06-20 11:32:20 -04:00
|
|
|
delete query_data_facade;
|
2014-05-07 10:17:14 -04:00
|
|
|
for (PluginMap::value_type &plugin_pointer : plugin_map)
|
|
|
|
{
|
2013-06-26 20:05:42 -04:00
|
|
|
delete plugin_pointer.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 10:17:14 -04:00
|
|
|
void OSRM_impl::RegisterPlugin(BasePlugin *plugin)
|
|
|
|
{
|
|
|
|
SimpleLogger().Write() << "loaded plugin: " << plugin->GetDescriptor();
|
|
|
|
if (plugin_map.find(plugin->GetDescriptor()) != plugin_map.end())
|
|
|
|
{
|
2013-09-19 12:55:19 -04:00
|
|
|
delete plugin_map.find(plugin->GetDescriptor())->second;
|
2013-08-09 07:24:05 -04:00
|
|
|
}
|
2013-09-19 12:55:19 -04:00
|
|
|
plugin_map.emplace(plugin->GetDescriptor(), plugin);
|
2013-06-26 20:05:42 -04:00
|
|
|
}
|
|
|
|
|
2014-11-25 03:14:01 -05:00
|
|
|
int OSRM_impl::RunQuery(RouteParameters &route_parameters, JSON::Object &json_result)
|
2014-05-07 10:17:14 -04:00
|
|
|
{
|
2015-01-22 06:35:46 -05:00
|
|
|
const auto &plugin_iterator = plugin_map.find(route_parameters.service);
|
2013-09-19 12:55:19 -04:00
|
|
|
|
2015-01-22 06:35:46 -05:00
|
|
|
if (plugin_map.end() == plugin_iterator)
|
2014-05-07 10:17:14 -04:00
|
|
|
{
|
2015-01-22 06:35:46 -05:00
|
|
|
return 400;
|
2014-05-07 10:17:14 -04:00
|
|
|
}
|
2015-01-22 06:35:46 -05:00
|
|
|
|
|
|
|
increase_concurrent_query_count();
|
|
|
|
plugin_iterator->second->HandleRequest(route_parameters, json_result);
|
|
|
|
decrease_concurrent_query_count();
|
|
|
|
return 200;
|
2013-06-26 20:05:42 -04:00
|
|
|
}
|
2013-12-13 12:32:24 -05:00
|
|
|
|
2015-01-22 06:35:46 -05:00
|
|
|
// decrease number of concurrent queries
|
|
|
|
void OSRM_impl::decrease_concurrent_query_count()
|
|
|
|
{
|
|
|
|
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");
|
2013-12-13 12:32:24 -05:00
|
|
|
|
2015-01-22 06:35:46 -05:00
|
|
|
// 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
|
|
|
|
void OSRM_impl::increase_concurrent_query_count()
|
|
|
|
{
|
|
|
|
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-01-08 09:05:54 -05:00
|
|
|
OSRM::OSRM(libosrm_config &lib_config)
|
|
|
|
: OSRM_pimpl_(osrm::make_unique<OSRM_impl>(lib_config))
|
2014-05-07 10:17:14 -04:00
|
|
|
{
|
2013-12-13 12:32:24 -05:00
|
|
|
}
|
|
|
|
|
2014-09-24 07:17:55 -04:00
|
|
|
OSRM::~OSRM() { OSRM_pimpl_.reset(); }
|
2014-05-07 10:17:14 -04:00
|
|
|
|
2014-11-25 03:14:01 -05:00
|
|
|
int OSRM::RunQuery(RouteParameters &route_parameters, JSON::Object &json_result)
|
2014-05-07 10:17:14 -04:00
|
|
|
{
|
2014-11-25 03:14:01 -05:00
|
|
|
return OSRM_pimpl_->RunQuery(route_parameters, json_result);
|
2013-12-13 12:32:24 -05:00
|
|
|
}
|