2013-06-26 20:05:42 -04:00
|
|
|
/*
|
2013-10-14 07:42:28 -04:00
|
|
|
|
|
|
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
|
|
|
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
|
|
|
|
|
|
|
#include "OSRM.h"
|
|
|
|
|
2013-10-17 12:11:53 -04:00
|
|
|
OSRM::OSRM( const ServerPaths & server_paths, const bool use_shared_memory )
|
|
|
|
:
|
|
|
|
shm(
|
|
|
|
boost::interprocess::open_or_create,
|
|
|
|
"SharedBarriers",
|
|
|
|
boost::interprocess::read_write
|
|
|
|
),
|
|
|
|
use_shared_memory(use_shared_memory)
|
|
|
|
{
|
2013-09-20 12:30:47 -04:00
|
|
|
if( !use_shared_memory ) {
|
2013-10-16 07:40:40 -04:00
|
|
|
SimpleLogger().Write() << "loading data into internal memory";
|
2013-09-20 12:30:47 -04:00
|
|
|
query_data_facade = new InternalDataFacade<QueryEdge::EdgeData>(
|
2013-10-15 09:44:40 -04:00
|
|
|
server_paths
|
2013-09-17 12:37:08 -04:00
|
|
|
);
|
|
|
|
} else {
|
2013-10-17 12:11:53 -04:00
|
|
|
region = boost::interprocess::mapped_region(
|
|
|
|
shm, //What to map
|
|
|
|
boost::interprocess::read_write //Map it as read-write
|
|
|
|
);
|
|
|
|
shm.truncate( sizeof(SharedBarriers) );
|
|
|
|
barrier = static_cast<SharedBarriers *>( region.get_address() );
|
|
|
|
|
2013-10-16 07:40:40 -04:00
|
|
|
SimpleLogger().Write() << "loading data from shared memory";
|
2013-09-20 12:30:47 -04:00
|
|
|
query_data_facade = new SharedDataFacade<QueryEdge::EdgeData>(
|
2013-10-15 09:44:40 -04:00
|
|
|
server_paths
|
2013-09-20 12:30:47 -04:00
|
|
|
);
|
2013-09-17 12:37:08 -04:00
|
|
|
}
|
2013-09-19 12:55:19 -04:00
|
|
|
|
2013-10-17 12:11:53 -04:00
|
|
|
|
2013-09-19 12:55:19 -04:00
|
|
|
//The following plugins handle all requests.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
OSRM::~OSRM() {
|
2013-09-19 12:55:19 -04:00
|
|
|
BOOST_FOREACH(PluginMap::value_type & plugin_pointer, plugin_map) {
|
2013-06-26 20:05:42 -04:00
|
|
|
delete plugin_pointer.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSRM::RegisterPlugin(BasePlugin * plugin) {
|
2013-08-08 10:19:25 -04:00
|
|
|
SimpleLogger().Write() << "loaded plugin: " << plugin->GetDescriptor();
|
2013-09-19 12:55:19 -04:00
|
|
|
if( plugin_map.find(plugin->GetDescriptor()) != plugin_map.end() ) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void OSRM::RunQuery(RouteParameters & route_parameters, http::Reply & reply) {
|
2013-09-19 12:55:19 -04:00
|
|
|
const PluginMap::const_iterator & iter = plugin_map.find(
|
|
|
|
route_parameters.service
|
|
|
|
);
|
|
|
|
|
|
|
|
if(plugin_map.end() != iter) {
|
2013-06-26 20:05:42 -04:00
|
|
|
reply.status = http::Reply::ok;
|
2013-10-17 12:11:53 -04:00
|
|
|
if( use_shared_memory && barrier->update_ongoing ) {
|
|
|
|
//wait until we get the mutex and free it immediately
|
|
|
|
//TODO: increment semaphore of querying processes
|
|
|
|
boost::interprocess::scoped_lock<
|
|
|
|
boost::interprocess::interprocess_mutex
|
|
|
|
> lock(barrier->update_mutex);
|
|
|
|
}
|
2013-06-26 20:05:42 -04:00
|
|
|
iter->second->HandleRequest(route_parameters, reply );
|
|
|
|
} else {
|
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
}
|
|
|
|
}
|