2010-07-09 05:05:40 -04:00
|
|
|
/*
|
|
|
|
open source routing machine
|
|
|
|
Copyright (C) Dennis Luxen, 2010
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU AFFERO General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
or see http://www.gnu.org/licenses/agpl.txt.
|
2011-05-26 05:16:04 -04:00
|
|
|
*/
|
2010-07-09 05:05:40 -04:00
|
|
|
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2013-06-26 19:47:47 -04:00
|
|
|
#include "Library/OSRM.h"
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2013-06-26 19:47:47 -04:00
|
|
|
#include "Server/ServerFactory.h"
|
2012-04-14 14:07:30 -04:00
|
|
|
|
2013-08-19 16:41:46 -04:00
|
|
|
#include "Util/GitDescription.h"
|
2011-01-09 16:42:27 -05:00
|
|
|
#include "Util/InputFileUtil.h"
|
2012-08-27 11:40:59 -04:00
|
|
|
#include "Util/OpenMPWrapper.h"
|
2013-08-19 16:41:46 -04:00
|
|
|
#include "Util/ProgramOptions.h"
|
2013-08-08 08:17:01 -04:00
|
|
|
#include "Util/SimpleLogger.h"
|
2013-07-22 10:34:06 -04:00
|
|
|
#include "Util/UUID.h"
|
2011-09-28 11:22:03 -04:00
|
|
|
|
2013-06-26 19:47:47 -04:00
|
|
|
#ifdef __linux__
|
2011-07-28 08:16:36 -04:00
|
|
|
#include "Util/LinuxStackTrace.h"
|
2013-06-26 19:47:47 -04:00
|
|
|
#include <sys/mman.h>
|
2011-09-28 11:22:03 -04:00
|
|
|
#endif
|
|
|
|
|
2013-06-26 19:47:47 -04:00
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/date_time.hpp>
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-23 07:45:16 -04:00
|
|
|
int main (int argc, char * argv[]) {
|
2013-08-09 06:53:23 -04:00
|
|
|
try {
|
|
|
|
LogPolicy::GetInstance().Unmute();
|
2012-05-03 09:21:19 -04:00
|
|
|
#ifdef __linux__
|
2013-08-09 06:53:23 -04:00
|
|
|
if(!mlockall(MCL_CURRENT | MCL_FUTURE)) {
|
|
|
|
SimpleLogger().Write(logWARNING) << "Process " << argv[0] << "could not be locked to RAM";
|
|
|
|
}
|
2012-05-03 09:21:19 -04:00
|
|
|
#endif
|
2013-06-26 19:47:47 -04:00
|
|
|
#ifdef __linux__
|
2012-05-03 09:21:19 -04:00
|
|
|
|
2011-07-28 08:16:36 -04:00
|
|
|
installCrashHandler(argv[0]);
|
2011-09-28 11:22:03 -04:00
|
|
|
#endif
|
2013-08-19 16:41:46 -04:00
|
|
|
|
|
|
|
boost::unordered_map<const std::string,boost::filesystem::path> paths;
|
|
|
|
std::string ip_address;
|
|
|
|
int ip_port, requested_num_threads;
|
|
|
|
|
|
|
|
// declare a group of options that will be allowed only on command line
|
|
|
|
boost::program_options::options_description generic_options("Options");
|
|
|
|
generic_options.add_options()
|
|
|
|
("version,v", "Show version")
|
|
|
|
("help,h", "Show this help message")
|
|
|
|
("config,c", boost::program_options::value<boost::filesystem::path>(&paths["config"])->default_value("server.ini"),
|
2013-09-24 13:21:02 -04:00
|
|
|
"Path to a configuration file");
|
2013-08-19 16:41:46 -04:00
|
|
|
|
|
|
|
// declare a group of options that will be allowed both on command line and in config file
|
|
|
|
boost::program_options::options_description config_options("Configuration");
|
|
|
|
config_options.add_options()
|
2013-09-24 13:21:02 -04:00
|
|
|
("hsgrdata", boost::program_options::value<boost::filesystem::path>(&paths["hsgrdata"]),
|
2013-08-19 16:41:46 -04:00
|
|
|
"HSGR file")
|
2013-09-24 13:21:02 -04:00
|
|
|
("nodesdata", boost::program_options::value<boost::filesystem::path>(&paths["nodesdata"]),
|
2013-08-19 16:41:46 -04:00
|
|
|
"Nodes file")
|
2013-09-24 13:21:02 -04:00
|
|
|
("edgesdata", boost::program_options::value<boost::filesystem::path>(&paths["edgesdata"]),
|
2013-08-19 16:41:46 -04:00
|
|
|
"Edges file")
|
2013-09-24 13:21:02 -04:00
|
|
|
("ramindex", boost::program_options::value<boost::filesystem::path>(&paths["ramindex"]),
|
2013-08-19 16:41:46 -04:00
|
|
|
"RAM index file")
|
2013-09-24 13:21:02 -04:00
|
|
|
("fileindex", boost::program_options::value<boost::filesystem::path>(&paths["fileindex"]),
|
2013-08-19 16:41:46 -04:00
|
|
|
"File index file")
|
2013-09-24 13:21:02 -04:00
|
|
|
("namesdata", boost::program_options::value<boost::filesystem::path>(&paths["namesdata"]),
|
2013-08-19 16:41:46 -04:00
|
|
|
"Names file")
|
|
|
|
("timestamp", boost::program_options::value<boost::filesystem::path>(&paths["timestamp"]),
|
|
|
|
"Timestamp file")
|
|
|
|
("ip,i", boost::program_options::value<std::string>(&ip_address)->default_value("0.0.0.0"),
|
|
|
|
"IP address")
|
|
|
|
("port,p", boost::program_options::value<int>(&ip_port)->default_value(5000),
|
|
|
|
"IP Port")
|
2013-09-24 15:50:20 -04:00
|
|
|
("threads,t", boost::program_options::value<int>(&requested_num_threads)->default_value(8),
|
2013-08-19 16:41:46 -04:00
|
|
|
"Number of threads to use");
|
|
|
|
|
|
|
|
// hidden options, will be allowed both on command line and in config file, but will not be shown to the user
|
|
|
|
boost::program_options::options_description hidden_options("Hidden options");
|
|
|
|
hidden_options.add_options()
|
2013-09-24 13:21:02 -04:00
|
|
|
("base,b", boost::program_options::value<boost::filesystem::path>(&paths["base"]),
|
|
|
|
"base path to .osrm file, other wil be located in the same folder");
|
2013-08-19 16:41:46 -04:00
|
|
|
|
|
|
|
// positional option
|
|
|
|
boost::program_options::positional_options_description positional_options;
|
2013-09-24 13:21:02 -04:00
|
|
|
positional_options.add("base", 1);
|
2013-08-19 16:41:46 -04:00
|
|
|
|
|
|
|
// combine above options for parsing
|
|
|
|
boost::program_options::options_description cmdline_options;
|
|
|
|
cmdline_options.add(generic_options).add(config_options).add(hidden_options);
|
|
|
|
|
|
|
|
boost::program_options::options_description config_file_options;
|
|
|
|
config_file_options.add(config_options).add(hidden_options);
|
|
|
|
|
2013-09-24 13:21:02 -04:00
|
|
|
boost::program_options::options_description visible_options(boost::filesystem::basename(argv[0]) + " <base.osrm> [<options>]");
|
2013-08-19 16:41:46 -04:00
|
|
|
visible_options.add(generic_options).add(config_options);
|
|
|
|
|
|
|
|
// parse command line options
|
|
|
|
boost::program_options::variables_map option_variables;
|
|
|
|
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
|
|
|
|
options(cmdline_options).positional(positional_options).run(), option_variables);
|
|
|
|
|
|
|
|
if(option_variables.count("version")) {
|
|
|
|
SimpleLogger().Write() << g_GIT_DESCRIPTION;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(option_variables.count("help")) {
|
|
|
|
SimpleLogger().Write() << visible_options;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::program_options::notify(option_variables);
|
|
|
|
|
|
|
|
// parse config file
|
|
|
|
if(boost::filesystem::is_regular_file(paths["config"])) {
|
|
|
|
SimpleLogger().Write() << "Reading options from: " << paths["config"].c_str();
|
2013-09-24 15:50:20 -04:00
|
|
|
std::string config_str;
|
|
|
|
PrepareConfigFile( paths["config"], config_str );
|
|
|
|
std::stringstream config_stream( config_str );
|
|
|
|
boost::program_options::store(parse_config_file(config_stream, config_file_options), option_variables);
|
2013-08-19 16:41:46 -04:00
|
|
|
boost::program_options::notify(option_variables);
|
|
|
|
}
|
|
|
|
|
2013-09-24 13:21:02 -04:00
|
|
|
if(!option_variables.count("hsgrdata")) {
|
|
|
|
if(!option_variables.count("base")) {
|
|
|
|
SimpleLogger().Write(logWARNING) << "hsgrdata (or base) must be specified";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
paths["hsgrdata"] = std::string( paths["base"].c_str()) + ".hsgr";
|
2013-08-19 16:41:46 -04:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:21:02 -04:00
|
|
|
if(!option_variables.count("nodesdata")) {
|
|
|
|
if(!option_variables.count("base")) {
|
|
|
|
SimpleLogger().Write(logWARNING) << "nodesdata (or base) must be specified";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
paths["nodesdata"] = std::string( paths["base"].c_str()) + ".nodes";
|
2013-08-19 16:41:46 -04:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:21:02 -04:00
|
|
|
if(!option_variables.count("edgesdata")) {
|
|
|
|
if(!option_variables.count("base")) {
|
|
|
|
SimpleLogger().Write(logWARNING) << "edgesdata (or base) must be specified";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
paths["edgesdata"] = std::string( paths["base"].c_str()) + ".edges";
|
2013-08-19 16:41:46 -04:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:21:02 -04:00
|
|
|
if(!option_variables.count("ramindex")) {
|
|
|
|
if(!option_variables.count("base")) {
|
|
|
|
SimpleLogger().Write(logWARNING) << "ramindex (or base) must be specified";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
paths["ramindex"] = std::string( paths["base"].c_str()) + ".ramIndex";
|
2013-08-19 16:41:46 -04:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:21:02 -04:00
|
|
|
if(!option_variables.count("fileindex")) {
|
|
|
|
if(!option_variables.count("base")) {
|
|
|
|
SimpleLogger().Write(logWARNING) << "fileindex (or base) must be specified";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
paths["fileindex"] = std::string( paths["base"].c_str()) + ".fileIndex";
|
2013-08-19 16:41:46 -04:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:21:02 -04:00
|
|
|
if(!option_variables.count("namesdata")) {
|
|
|
|
if(!option_variables.count("base")) {
|
|
|
|
SimpleLogger().Write(logWARNING) << "namesdata (or base) must be specified";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
paths["namesdata"] = std::string( paths["base"].c_str()) + ".names";
|
2013-08-19 16:41:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!option_variables.count("timestamp")) {
|
2013-09-24 13:21:02 -04:00
|
|
|
if(!option_variables.count("base")) {
|
|
|
|
SimpleLogger().Write(logWARNING) << "timestamp (or base) must be specified";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
paths["timestamp"] = std::string( paths["base"].c_str()) + ".timestamp";
|
2013-08-19 16:41:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(1 > requested_num_threads) {
|
|
|
|
SimpleLogger().Write(logWARNING) << "Number of threads must be 1 or larger";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-11 17:25:22 -04:00
|
|
|
SimpleLogger().Write() <<
|
2013-08-19 16:41:46 -04:00
|
|
|
"starting up engines, " << g_GIT_DESCRIPTION << ", compiled at " << __DATE__ << ", " __TIME__;
|
|
|
|
|
2013-09-24 13:21:02 -04:00
|
|
|
SimpleLogger().Write() << "HSGR file:\t" << paths["hsgrdata"].c_str();
|
|
|
|
SimpleLogger().Write() << "Nodes file:\t" << paths["nodesdata"].c_str();
|
|
|
|
SimpleLogger().Write() << "Edges file:\t" << paths["edgesdata"].c_str();
|
|
|
|
SimpleLogger().Write() << "RAM file:\t" << paths["ramindex"].c_str();
|
|
|
|
SimpleLogger().Write() << "Index file:\t" << paths["fileindex"].c_str();
|
|
|
|
SimpleLogger().Write() << "Names file:\t" << paths["namesdata"].c_str();
|
2013-08-19 16:41:46 -04:00
|
|
|
SimpleLogger().Write() << "Timestamp file:\t" << paths["timestamp"].c_str();
|
|
|
|
SimpleLogger().Write() << "Threads:\t" << requested_num_threads;
|
|
|
|
SimpleLogger().Write() << "IP address:\t" << ip_address;
|
|
|
|
SimpleLogger().Write() << "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
|
|
|
|
2013-08-19 16:41:46 -04:00
|
|
|
OSRM routing_machine(paths);
|
|
|
|
Server * s = ServerFactory::CreateServer(ip_address,ip_port,requested_num_threads);
|
2013-06-26 19:47:47 -04:00
|
|
|
s->GetRequestHandlerPtr().RegisterRoutingMachine(&routing_machine);
|
2011-05-26 05:16:04 -04:00
|
|
|
|
|
|
|
boost::thread t(boost::bind(&Server::Run, s));
|
|
|
|
|
2011-09-28 11:22:03 -04:00
|
|
|
#ifndef _WIN32
|
2011-05-26 05:16:04 -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);
|
2011-11-16 12:10:51 -05:00
|
|
|
std::cout << "[server] running and waiting for requests" << std::endl;
|
2011-05-26 05:16:04 -04:00
|
|
|
sigwait(&wait_mask, &sig);
|
2011-09-28 11:22:03 -04:00
|
|
|
#else
|
|
|
|
// Set console control handler to allow server to be stopped.
|
|
|
|
console_ctrl_function = boost::bind(&Server::Stop, s);
|
|
|
|
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
|
2011-11-16 12:10:51 -05:00
|
|
|
std::cout << "[server] running and waiting for requests" << std::endl;
|
2011-09-28 11:22:03 -04:00
|
|
|
s->Run();
|
|
|
|
#endif
|
|
|
|
|
2012-12-15 06:31:49 -05:00
|
|
|
std::cout << "[server] initiating shutdown" << std::endl;
|
2011-05-26 05:16:04 -04:00
|
|
|
s->Stop();
|
2012-12-15 06:31:49 -05:00
|
|
|
std::cout << "[server] stopping threads" << std::endl;
|
2013-03-03 12:05:36 -05:00
|
|
|
|
|
|
|
if(!t.timed_join(boost::posix_time::seconds(2))) {
|
2013-08-08 08:17:01 -04:00
|
|
|
SimpleLogger().Write(logDEBUG) << "Threads did not finish within 2 seconds. Hard abort!";
|
2013-03-03 12:05:36 -05:00
|
|
|
}
|
|
|
|
|
2012-12-15 06:31:49 -05:00
|
|
|
std::cout << "[server] freeing objects" << std::endl;
|
2012-04-12 13:35:47 -04:00
|
|
|
delete s;
|
2012-12-15 06:31:49 -05:00
|
|
|
std::cout << "[server] shutdown completed" << std::endl;
|
2011-05-26 05:16:04 -04:00
|
|
|
} catch (std::exception& e) {
|
|
|
|
std::cerr << "[fatal error] exception: " << e.what() << std::endl;
|
|
|
|
}
|
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
|
|
|
}
|