2010-07-09 05:05:40 -04:00
|
|
|
/*
|
|
|
|
|
2015-01-05 08:32:04 -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.
|
|
|
|
|
|
|
|
*/
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2013-06-26 19:47:47 -04:00
|
|
|
#include "Library/OSRM.h"
|
2014-08-20 05:24:29 -04:00
|
|
|
#include "Server/Server.h"
|
2015-01-05 08:32:04 -05:00
|
|
|
#include "Util/git_sha.hpp"
|
2013-08-19 16:41:46 -04:00
|
|
|
#include "Util/ProgramOptions.h"
|
2014-10-10 13:31:43 -04:00
|
|
|
#include "Util/simple_logger.hpp"
|
2011-09-28 11:22:03 -04:00
|
|
|
|
2013-06-26 19:47:47 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/mman.h>
|
2011-09-28 11:22:03 -04:00
|
|
|
#endif
|
|
|
|
|
2014-05-13 04:03:37 -04:00
|
|
|
#include <cstdlib>
|
2013-06-26 19:47:47 -04:00
|
|
|
|
2014-05-08 13:40:29 -04:00
|
|
|
#include <signal.h>
|
2013-06-26 19:47:47 -04:00
|
|
|
|
2014-05-13 04:03:37 -04:00
|
|
|
#include <chrono>
|
|
|
|
#include <future>
|
2013-06-26 19:47:47 -04:00
|
|
|
#include <iostream>
|
2014-05-13 04:03:37 -04:00
|
|
|
#include <thread>
|
2010-07-09 05:05:40 -04:00
|
|
|
|
2011-09-28 11:22:03 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
boost::function0<void> console_ctrl_function;
|
|
|
|
|
|
|
|
BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
|
|
|
|
{
|
2014-05-08 13:40:29 -04:00
|
|
|
switch (ctrl_type)
|
|
|
|
{
|
|
|
|
case CTRL_C_EVENT:
|
|
|
|
case CTRL_BREAK_EVENT:
|
|
|
|
case CTRL_CLOSE_EVENT:
|
|
|
|
case CTRL_SHUTDOWN_EVENT:
|
|
|
|
console_ctrl_function();
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-09-28 11:22:03 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-05-08 13:40:29 -04:00
|
|
|
int main(int argc, const char *argv[])
|
2014-03-20 04:53:05 -04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-08-09 06:53:23 -04:00
|
|
|
LogPolicy::GetInstance().Unmute();
|
2014-03-20 04:53:05 -04:00
|
|
|
|
2015-01-08 09:05:54 -05:00
|
|
|
bool trial_run = false;
|
2013-08-19 16:41:46 -04:00
|
|
|
std::string ip_address;
|
2015-01-08 09:05:54 -05:00
|
|
|
int ip_port, requested_thread_num;
|
2013-08-19 16:41:46 -04:00
|
|
|
|
2015-01-08 09:05:54 -05:00
|
|
|
libosrm_config lib_config;
|
2014-03-25 09:54:52 -04:00
|
|
|
|
2014-04-28 06:52:06 -04:00
|
|
|
const unsigned init_result = GenerateServerProgramOptions(argc,
|
|
|
|
argv,
|
2015-01-08 09:05:54 -05:00
|
|
|
lib_config.server_paths,
|
2014-04-28 06:52:06 -04:00
|
|
|
ip_address,
|
|
|
|
ip_port,
|
|
|
|
requested_thread_num,
|
2015-01-08 09:05:54 -05:00
|
|
|
lib_config.use_shared_memory,
|
2014-11-27 06:09:07 -05:00
|
|
|
trial_run,
|
2015-01-08 09:05:54 -05:00
|
|
|
lib_config.max_locations_distance_table);
|
2014-03-25 09:54:52 -04:00
|
|
|
if (init_result == INIT_OK_DO_NOT_START_ENGINE)
|
|
|
|
{
|
2013-08-19 16:41:46 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-25 09:54:52 -04:00
|
|
|
if (init_result == INIT_FAILED)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-08-19 16:41:46 -04:00
|
|
|
|
2014-03-20 04:53:05 -04:00
|
|
|
#ifdef __linux__
|
2014-04-28 06:50:44 -04:00
|
|
|
const int lock_flags = MCL_CURRENT | MCL_FUTURE;
|
2014-03-20 04:53:05 -04:00
|
|
|
if (-1 == mlockall(lock_flags))
|
|
|
|
{
|
2014-04-28 06:52:55 -04:00
|
|
|
SimpleLogger().Write(logWARNING) << argv[0] << " could not be locked to RAM";
|
2014-03-20 04:53:05 -04:00
|
|
|
}
|
|
|
|
#endif
|
2014-10-17 04:56:16 -04:00
|
|
|
SimpleLogger().Write() << "starting up engines, " << g_GIT_DESCRIPTION;
|
2013-08-19 16:41:46 -04:00
|
|
|
|
2015-01-08 09:05:54 -05:00
|
|
|
if (lib_config.use_shared_memory)
|
2014-03-20 04:53:05 -04:00
|
|
|
{
|
2013-10-30 06:47:48 -04:00
|
|
|
SimpleLogger().Write(logDEBUG) << "Loading from shared memory";
|
2014-03-20 04:53:05 -04:00
|
|
|
}
|
2014-10-08 11:01:53 -04:00
|
|
|
|
|
|
|
SimpleLogger().Write(logDEBUG) << "Threads:\t" << requested_thread_num;
|
|
|
|
SimpleLogger().Write(logDEBUG) << "IP address:\t" << ip_address;
|
|
|
|
SimpleLogger().Write(logDEBUG) << "IP port:\t" << ip_port;
|
2011-09-28 11:22:03 -04:00
|
|
|
#ifndef _WIN32
|
2011-05-26 05:16:04 -04:00
|
|
|
int sig = 0;
|
|
|
|
sigset_t new_mask;
|
|
|
|
sigset_t old_mask;
|
|
|
|
sigfillset(&new_mask);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
|
2011-09-28 11:22:03 -04:00
|
|
|
#endif
|
2011-05-26 05:16:04 -04:00
|
|
|
|
2015-01-08 09:05:54 -05:00
|
|
|
OSRM osrm_lib(lib_config);
|
2014-08-20 05:24:29 -04:00
|
|
|
auto routing_server =
|
|
|
|
Server::CreateServer(ip_address, ip_port, requested_thread_num);
|
2013-10-01 10:48:40 -04:00
|
|
|
|
2014-03-18 05:51:36 -04:00
|
|
|
routing_server->GetRequestHandlerPtr().RegisterRoutingMachine(&osrm_lib);
|
2011-05-26 05:16:04 -04:00
|
|
|
|
2014-05-08 13:40:29 -04:00
|
|
|
if (trial_run)
|
2014-03-25 09:54:52 -04:00
|
|
|
{
|
|
|
|
SimpleLogger().Write() << "trial run, quitting after successful initialization";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-05 11:02:33 -04:00
|
|
|
std::packaged_task<int()> server_task([&]()->int{ routing_server->Run(); return 0; });
|
2014-05-13 04:03:37 -04:00
|
|
|
auto future = server_task.get_future();
|
|
|
|
std::thread server_thread(std::move(server_task));
|
2011-05-26 05:16:04 -04:00
|
|
|
|
2011-09-28 11:22:03 -04:00
|
|
|
#ifndef _WIN32
|
2014-03-21 19:06:35 -04:00
|
|
|
sigset_t wait_mask;
|
|
|
|
pthread_sigmask(SIG_SETMASK, &old_mask, 0);
|
|
|
|
sigemptyset(&wait_mask);
|
|
|
|
sigaddset(&wait_mask, SIGINT);
|
|
|
|
sigaddset(&wait_mask, SIGQUIT);
|
|
|
|
sigaddset(&wait_mask, SIGTERM);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write() << "running and waiting for requests";
|
2014-03-21 19:06:35 -04:00
|
|
|
sigwait(&wait_mask, &sig);
|
2011-09-28 11:22:03 -04:00
|
|
|
#else
|
2014-03-21 19:06:35 -04:00
|
|
|
// Set console control handler to allow server to be stopped.
|
2014-05-08 13:40:29 -04:00
|
|
|
console_ctrl_function = std::bind(&Server::Stop, routing_server);
|
2014-03-21 19:06:35 -04:00
|
|
|
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write() << "running and waiting for requests";
|
2014-03-21 19:06:35 -04:00
|
|
|
routing_server->Run();
|
2011-09-28 11:22:03 -04:00
|
|
|
#endif
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write() << "initiating shutdown";
|
2014-03-21 19:06:35 -04:00
|
|
|
routing_server->Stop();
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write() << "stopping threads";
|
2014-03-21 19:06:35 -04:00
|
|
|
|
2014-05-13 04:03:37 -04:00
|
|
|
auto status = future.wait_for(std::chrono::seconds(2));
|
|
|
|
|
2014-08-07 13:19:21 -04:00
|
|
|
if (status == std::future_status::ready)
|
2014-03-21 19:06:35 -04:00
|
|
|
{
|
2014-08-07 13:19:21 -04:00
|
|
|
server_thread.join();
|
2014-05-13 04:03:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-07 13:19:21 -04:00
|
|
|
SimpleLogger().Write(logWARNING) << "Didn't exit within 2 seconds. Hard abort!";
|
|
|
|
server_task.reset(); // just kill it
|
2014-03-21 19:06:35 -04:00
|
|
|
}
|
2013-03-03 12:05:36 -05:00
|
|
|
}
|
|
|
|
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write() << "freeing objects";
|
2014-08-20 05:24:29 -04:00
|
|
|
routing_server.reset();
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write() << "shutdown completed";
|
2014-03-18 05:51:36 -04:00
|
|
|
}
|
2014-05-08 13:40:29 -04:00
|
|
|
catch (const std::exception &e)
|
2014-03-18 05:51:36 -04:00
|
|
|
{
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write(logWARNING) << "exception: " << e.what();
|
|
|
|
return 1;
|
2011-05-26 05:16:04 -04:00
|
|
|
}
|
2012-05-03 09:21:19 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
munlockall();
|
|
|
|
#endif
|
|
|
|
|
2011-05-26 05:16:04 -04:00
|
|
|
return 0;
|
2010-07-09 05:05:40 -04:00
|
|
|
}
|