2010-07-09 05:05:40 -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.
|
|
|
|
|
|
|
|
*/
|
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"
|
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__
|
|
|
|
#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>
|
2014-03-25 09:54:52 -04:00
|
|
|
// #include <boost/date_time.hpp>
|
2013-06-26 19:47:47 -04:00
|
|
|
#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
|
|
|
|
|
2014-03-20 04:53:05 -04:00
|
|
|
int main (int argc, const char * argv[])
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-08-09 06:53:23 -04:00
|
|
|
LogPolicy::GetInstance().Unmute();
|
2014-03-20 04:53:05 -04:00
|
|
|
|
2014-03-25 09:54:52 -04:00
|
|
|
bool use_shared_memory = false, trial = false;
|
2013-08-19 16:41:46 -04:00
|
|
|
std::string ip_address;
|
2014-03-18 05:51:36 -04:00
|
|
|
int ip_port, requested_thread_num;
|
2013-08-19 16:41:46 -04:00
|
|
|
|
2013-10-12 09:19:59 -04:00
|
|
|
ServerPaths server_paths;
|
2014-03-25 09:54:52 -04:00
|
|
|
|
|
|
|
const unsigned init_result = GenerateServerProgramOptions(argc, argv, server_paths, ip_address, ip_port, requested_thread_num, use_shared_memory, trial);
|
|
|
|
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__
|
|
|
|
const int lock_flags = (MCL_CURRENT | MCL_FUTURE);
|
|
|
|
if (-1 == mlockall(lock_flags))
|
|
|
|
{
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write(logWARNING) << "Process " << argv[0] << " could not be locked to RAM";
|
2014-03-20 04:53:05 -04:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-12 09:19:59 -04:00
|
|
|
SimpleLogger().Write() <<
|
|
|
|
"starting up engines, " << g_GIT_DESCRIPTION << ", " <<
|
|
|
|
"compiled at " << __DATE__ << ", " __TIME__;
|
2013-08-19 16:41:46 -04:00
|
|
|
|
2014-03-20 04:53:05 -04:00
|
|
|
if(use_shared_memory)
|
|
|
|
{
|
2013-10-30 06:47:48 -04:00
|
|
|
SimpleLogger().Write(logDEBUG) << "Loading from shared memory";
|
2014-03-20 04:53:05 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write() << "HSGR file:\t" << server_paths["hsgrdata"];
|
|
|
|
SimpleLogger().Write(logDEBUG) << "Nodes file:\t" << server_paths["nodesdata"];
|
|
|
|
SimpleLogger().Write(logDEBUG) << "Edges file:\t" << server_paths["edgesdata"];
|
|
|
|
SimpleLogger().Write(logDEBUG) << "RAM file:\t" << server_paths["ramindex"];
|
|
|
|
SimpleLogger().Write(logDEBUG) << "Index file:\t" << server_paths["fileindex"];
|
|
|
|
SimpleLogger().Write(logDEBUG) << "Names file:\t" << server_paths["namesdata"];
|
|
|
|
SimpleLogger().Write(logDEBUG) << "Timestamp file:\t" << server_paths["timestamp"];
|
|
|
|
SimpleLogger().Write(logDEBUG) << "Threads:\t" << requested_thread_num;
|
|
|
|
SimpleLogger().Write(logDEBUG) << "IP address:\t" << ip_address;
|
|
|
|
SimpleLogger().Write(logDEBUG) << "IP port:\t" << ip_port;
|
2013-10-30 06:47:48 -04:00
|
|
|
}
|
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
|
|
|
|
2014-03-18 05:51:36 -04:00
|
|
|
OSRM osrm_lib(server_paths, use_shared_memory);
|
|
|
|
Server * routing_server = ServerFactory::CreateServer(
|
2013-10-12 09:19:59 -04:00
|
|
|
ip_address,
|
|
|
|
ip_port,
|
2014-03-18 05:51:36 -04:00
|
|
|
requested_thread_num
|
2013-10-12 09:19:59 -04:00
|
|
|
);
|
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-03-25 09:54:52 -04:00
|
|
|
if( trial )
|
|
|
|
{
|
|
|
|
SimpleLogger().Write() << "trial run, quitting after successful initialization";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-21 19:06:35 -04:00
|
|
|
boost::thread server_thread(boost::bind(&Server::Run, routing_server));
|
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.
|
|
|
|
console_ctrl_function = boost::bind(&Server::Stop, routing_server);
|
|
|
|
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
|
|
|
|
|
|
|
if (!server_thread.timed_join(boost::posix_time::seconds(2)))
|
|
|
|
{
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write(logDEBUG) << "Threads did not finish within 2 seconds. Hard abort!";
|
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-03-18 05:51:36 -04:00
|
|
|
delete routing_server;
|
2014-03-25 09:54:52 -04:00
|
|
|
SimpleLogger().Write() << "shutdown completed";
|
2014-03-18 05:51:36 -04:00
|
|
|
}
|
2014-03-25 09:54:52 -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
|
|
|
}
|