osrm-backend/routed.cpp

137 lines
3.9 KiB
C++
Raw Normal View History

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.
*/
2010-07-09 05:05:40 -04:00
#include "Library/OSRM.h"
#include "Server/ServerFactory.h"
#include "Util/BaseConfiguration.h"
#include "Util/InputFileUtil.h"
2012-08-27 11:40:59 -04:00
#include "Util/OpenMPWrapper.h"
2011-09-28 11:22:03 -04:00
#ifdef __linux__
#include "Util/LinuxStackTrace.h"
#include <sys/mman.h>
2011-09-28 11:22:03 -04:00
#endif
#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[]) {
#ifdef __linux__
if(!mlockall(MCL_CURRENT | MCL_FUTURE))
2012-05-08 05:14:48 -04:00
WARN("Process " << argv[0] << "could not be locked to RAM");
#endif
#ifdef __linux__
installCrashHandler(argv[0]);
2011-09-28 11:22:03 -04:00
#endif
2011-09-28 11:22:03 -04:00
// Bug - testing not necessary. testDataFiles also tries to open the first
// argv, which is the name of exec file
//if(testDataFiles(argc, argv)==false) {
//std::cerr << "[error] at least one data file name seems to be bogus!" << std::endl;
//exit(-1);
//}
try {
std::cout << "\n starting up engines, compile at " <<
__DATE__ << ", " __TIME__ << std::endl;
2011-09-28 11:22:03 -04:00
#ifndef _WIN32
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
BaseConfiguration serverConfig((argc > 1 ? argv[1] : "server.ini"));
OSRM routing_machine((argc > 1 ? argv[1] : "server.ini"));
Server * s = ServerFactory::CreateServer(serverConfig);
s->GetRequestHandlerPtr().RegisterRoutingMachine(&routing_machine);
boost::thread t(boost::bind(&Server::Run, s));
2011-09-28 11:22:03 -04:00
#ifndef _WIN32
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);
std::cout << "[server] running and waiting for requests" << std::endl;
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);
std::cout << "[server] running and waiting for requests" << std::endl;
2011-09-28 11:22:03 -04:00
s->Run();
#endif
std::cout << "[server] initiating shutdown" << std::endl;
s->Stop();
std::cout << "[server] stopping threads" << std::endl;
2013-03-03 12:05:36 -05:00
if(!t.timed_join(boost::posix_time::seconds(2))) {
// INFO("Threads did not finish within 2 seconds. Hard abort!");
}
std::cout << "[server] freeing objects" << std::endl;
delete s;
std::cout << "[server] shutdown completed" << std::endl;
} catch (std::exception& e) {
std::cerr << "[fatal error] exception: " << e.what() << std::endl;
}
#ifdef __linux__
munlockall();
#endif
return 0;
2010-07-09 05:05:40 -04:00
}