2013-08-10 07:29:24 -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-08-10 07:29:24 -04:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-08-19 16:41:46 -04:00
|
|
|
#ifndef PROGAM_OPTIONS_H
|
|
|
|
#define PROGAM_OPTIONS_H
|
|
|
|
|
2013-10-12 09:19:59 -04:00
|
|
|
#include "GitDescription.h"
|
2013-08-20 04:04:58 -04:00
|
|
|
#include "OSRMException.h"
|
2013-10-12 09:19:59 -04:00
|
|
|
#include "SimpleLogger.h"
|
2013-08-20 04:04:58 -04:00
|
|
|
|
|
|
|
#include <boost/any.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
2013-08-10 07:29:24 -04:00
|
|
|
#include <boost/program_options.hpp>
|
2013-09-24 15:50:20 -04:00
|
|
|
#include <boost/regex.hpp>
|
2013-10-12 09:19:59 -04:00
|
|
|
#include <boost/unordered_map.hpp>
|
2013-08-10 07:29:24 -04:00
|
|
|
|
2013-10-12 09:19:59 -04:00
|
|
|
#include <fstream>
|
2013-08-20 04:04:58 -04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-08-10 07:29:24 -04:00
|
|
|
|
2013-10-12 09:19:59 -04:00
|
|
|
typedef boost::unordered_map<
|
|
|
|
const std::string,
|
|
|
|
boost::filesystem::path
|
|
|
|
> ServerPaths;
|
|
|
|
|
2013-08-20 04:04:58 -04:00
|
|
|
namespace boost {
|
2013-08-10 07:29:24 -04:00
|
|
|
namespace filesystem {
|
2013-08-20 04:04:58 -04:00
|
|
|
// Validator for boost::filesystem::path, that verifies that the file
|
|
|
|
// exists. The validate() function must be defined in the same namespace
|
|
|
|
// as the target type, (boost::filesystem::path in this case), otherwise
|
2013-10-21 05:46:17 -04:00
|
|
|
// it is not called
|
2013-10-13 08:13:08 -04:00
|
|
|
inline void validate(
|
2013-08-20 04:04:58 -04:00
|
|
|
boost::any & v,
|
|
|
|
const std::vector<std::string> & values,
|
|
|
|
boost::filesystem::path *,
|
|
|
|
int
|
|
|
|
) {
|
|
|
|
boost::program_options::validators::check_first_occurrence(v);
|
|
|
|
const std::string & input_string =
|
|
|
|
boost::program_options::validators::get_single_string(values);
|
2013-08-10 07:29:24 -04:00
|
|
|
if(boost::filesystem::is_regular_file(input_string)) {
|
|
|
|
v = boost::any(boost::filesystem::path(input_string));
|
|
|
|
} else {
|
2013-11-04 20:06:02 -05:00
|
|
|
throw OSRMException(input_string + " not found");
|
2013-08-10 07:29:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-19 16:41:46 -04:00
|
|
|
|
2013-10-21 05:46:17 -04:00
|
|
|
// support old capitalized option names by down-casing them with a regex replace
|
2013-10-12 09:19:59 -04:00
|
|
|
inline void PrepareConfigFile(
|
|
|
|
const boost::filesystem::path& path,
|
|
|
|
std::string& output
|
|
|
|
) {
|
2013-10-21 05:46:17 -04:00
|
|
|
std::ifstream config_stream( path.string().c_str() );
|
2013-10-21 06:00:52 -04:00
|
|
|
std::string input_str(
|
2013-10-12 09:19:59 -04:00
|
|
|
(std::istreambuf_iterator<char>(config_stream)),
|
|
|
|
std::istreambuf_iterator<char>()
|
|
|
|
);
|
2013-09-24 15:50:20 -04:00
|
|
|
boost::regex regex( "^([^=]*)" ); //match from start of line to '='
|
|
|
|
std::string format( "\\L$1\\E" ); //replace with downcased substring
|
2013-10-21 06:00:52 -04:00
|
|
|
output = boost::regex_replace( input_str, regex, format );
|
2013-09-24 15:50:20 -04:00
|
|
|
}
|
|
|
|
|
2013-10-12 09:19:59 -04:00
|
|
|
|
|
|
|
// generate boost::program_options object for the routing part
|
2013-10-13 08:13:08 -04:00
|
|
|
inline bool GenerateServerProgramOptions(
|
2013-10-12 09:19:59 -04:00
|
|
|
const int argc,
|
|
|
|
const char * argv[],
|
|
|
|
ServerPaths & paths,
|
|
|
|
std::string & ip_address,
|
|
|
|
int & ip_port,
|
2013-10-15 11:06:45 -04:00
|
|
|
int & requested_num_threads,
|
|
|
|
bool & use_shared_memory
|
2013-10-12 09:19:59 -04:00
|
|
|
) {
|
|
|
|
|
|
|
|
// 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"),
|
|
|
|
"Path to a configuration file"
|
|
|
|
);
|
|
|
|
|
|
|
|
// declare a group of options that will be allowed both on command line
|
|
|
|
// as well as in a config file
|
|
|
|
boost::program_options::options_description config_options("Configuration");
|
|
|
|
config_options.add_options()
|
|
|
|
(
|
|
|
|
"hsgrdata",
|
|
|
|
boost::program_options::value<boost::filesystem::path>(&paths["hsgrdata"]),
|
|
|
|
".hsgr file"
|
|
|
|
)
|
|
|
|
(
|
|
|
|
"nodesdata",
|
|
|
|
boost::program_options::value<boost::filesystem::path>(&paths["nodesdata"]),
|
|
|
|
".nodes file"
|
|
|
|
)
|
|
|
|
(
|
|
|
|
"edgesdata",
|
|
|
|
boost::program_options::value<boost::filesystem::path>(&paths["edgesdata"]),
|
|
|
|
".edges file")
|
|
|
|
(
|
|
|
|
"ramindex",
|
|
|
|
boost::program_options::value<boost::filesystem::path>(&paths["ramindex"]),
|
|
|
|
".ramIndex file")
|
|
|
|
(
|
|
|
|
"fileindex",
|
|
|
|
boost::program_options::value<boost::filesystem::path>(&paths["fileindex"]),
|
|
|
|
"File index file")
|
|
|
|
(
|
|
|
|
"namesdata",
|
|
|
|
boost::program_options::value<boost::filesystem::path>(&paths["namesdata"]),
|
|
|
|
".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),
|
|
|
|
"TCP/IP port"
|
|
|
|
)
|
|
|
|
(
|
|
|
|
"threads,t",
|
|
|
|
boost::program_options::value<int>(&requested_num_threads)->default_value(8),
|
|
|
|
"Number of threads to use"
|
2013-10-15 11:06:45 -04:00
|
|
|
)
|
|
|
|
(
|
|
|
|
"sharedmemory,s",
|
|
|
|
boost::program_options::value<bool>(&use_shared_memory)->default_value(false),
|
|
|
|
"Load data from shared memory"
|
2013-10-12 09:19:59 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
(
|
|
|
|
"base,b",
|
|
|
|
boost::program_options::value<boost::filesystem::path>(&paths["base"]),
|
|
|
|
"base path to .osrm file"
|
|
|
|
);
|
|
|
|
|
|
|
|
// positional option
|
|
|
|
boost::program_options::positional_options_description positional_options;
|
|
|
|
positional_options.add("base", 1);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
boost::program_options::options_description visible_options(
|
|
|
|
boost::filesystem::basename(argv[0]) + " <base.osrm> [<options>]"
|
|
|
|
);
|
|
|
|
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 false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(option_variables.count("help")) {
|
|
|
|
SimpleLogger().Write() << visible_options;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::program_options::notify(option_variables);
|
|
|
|
|
|
|
|
// parse config file
|
2013-11-08 09:47:32 -05:00
|
|
|
ServerPaths::iterator path_iterator = paths.find("config");
|
2013-10-16 07:27:44 -04:00
|
|
|
if(
|
|
|
|
path_iterator != paths.end() &&
|
|
|
|
boost::filesystem::is_regular_file(path_iterator->second) &&
|
|
|
|
!option_variables.count("base")
|
|
|
|
) {
|
2013-10-12 09:19:59 -04:00
|
|
|
SimpleLogger().Write() <<
|
2013-10-13 08:13:08 -04:00
|
|
|
"Reading options from: " << path_iterator->second.string();
|
2013-10-12 09:19:59 -04:00
|
|
|
std::string config_str;
|
|
|
|
PrepareConfigFile( paths["config"], config_str );
|
|
|
|
std::stringstream config_stream( config_str );
|
2013-10-13 08:13:08 -04:00
|
|
|
boost::program_options::store(
|
|
|
|
parse_config_file(config_stream, config_file_options),
|
|
|
|
option_variables
|
|
|
|
);
|
2013-10-12 09:19:59 -04:00
|
|
|
boost::program_options::notify(option_variables);
|
|
|
|
}
|
|
|
|
|
2013-11-01 10:24:13 -04:00
|
|
|
if( !use_shared_memory && option_variables.count("base") ) {
|
2013-11-26 13:41:55 -05:00
|
|
|
path_iterator = paths.find("base");
|
|
|
|
BOOST_ASSERT( paths.end() != path_iterator );
|
|
|
|
std::string base_string = path_iterator->second.string();
|
|
|
|
|
2013-10-31 09:05:06 -04:00
|
|
|
path_iterator = paths.find("hsgrdata");
|
|
|
|
if(
|
2013-11-01 10:24:13 -04:00
|
|
|
path_iterator != paths.end() &&
|
|
|
|
!boost::filesystem::is_regular_file(path_iterator->second)
|
2013-10-31 09:05:06 -04:00
|
|
|
) {
|
2013-11-08 09:47:32 -05:00
|
|
|
path_iterator->second = base_string + ".hsgr";
|
2013-10-12 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 09:05:06 -04:00
|
|
|
path_iterator = paths.find("nodesdata");
|
|
|
|
if(
|
2013-11-01 10:24:13 -04:00
|
|
|
path_iterator != paths.end() &&
|
|
|
|
!boost::filesystem::is_regular_file(path_iterator->second)
|
2013-10-31 09:05:06 -04:00
|
|
|
) {
|
2013-11-08 09:47:32 -05:00
|
|
|
path_iterator->second = base_string + ".nodes";
|
2013-10-12 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 09:05:06 -04:00
|
|
|
path_iterator = paths.find("edgesdata");
|
|
|
|
if(
|
2013-11-01 10:24:13 -04:00
|
|
|
path_iterator != paths.end() &&
|
|
|
|
!boost::filesystem::is_regular_file(path_iterator->second)
|
2013-10-31 09:05:06 -04:00
|
|
|
) {
|
2013-11-08 09:47:32 -05:00
|
|
|
path_iterator->second = base_string + ".edges";
|
2013-10-12 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 09:05:06 -04:00
|
|
|
path_iterator = paths.find("ramindex");
|
|
|
|
if(
|
2013-11-01 10:24:13 -04:00
|
|
|
path_iterator != paths.end() &&
|
|
|
|
!boost::filesystem::is_regular_file(path_iterator->second)
|
2013-10-31 09:05:06 -04:00
|
|
|
) {
|
2013-11-08 09:47:32 -05:00
|
|
|
path_iterator->second = base_string + ".ramIndex";
|
2013-10-12 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 09:05:06 -04:00
|
|
|
path_iterator = paths.find("fileindex");
|
|
|
|
if(
|
2013-11-01 10:24:13 -04:00
|
|
|
path_iterator != paths.end() &&
|
|
|
|
!boost::filesystem::is_regular_file(path_iterator->second)
|
2013-10-31 09:05:06 -04:00
|
|
|
) {
|
2013-11-08 09:47:32 -05:00
|
|
|
path_iterator->second = base_string + ".fileIndex";
|
2013-10-12 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 09:05:06 -04:00
|
|
|
path_iterator = paths.find("namesdata");
|
|
|
|
if(
|
2013-11-01 10:24:13 -04:00
|
|
|
path_iterator != paths.end() &&
|
|
|
|
!boost::filesystem::is_regular_file(path_iterator->second)
|
2013-10-31 09:05:06 -04:00
|
|
|
) {
|
2013-11-08 09:47:32 -05:00
|
|
|
path_iterator->second = base_string + ".names";
|
2013-10-12 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 09:05:06 -04:00
|
|
|
path_iterator = paths.find("timestamp");
|
|
|
|
if(
|
2013-11-01 10:24:13 -04:00
|
|
|
path_iterator != paths.end() &&
|
|
|
|
!boost::filesystem::is_regular_file(path_iterator->second)
|
2013-10-31 09:05:06 -04:00
|
|
|
) {
|
2013-11-08 09:47:32 -05:00
|
|
|
path_iterator->second = base_string + ".timestamp";
|
2013-10-12 09:19:59 -04:00
|
|
|
}
|
|
|
|
}
|
2013-11-01 10:24:13 -04:00
|
|
|
|
|
|
|
if( 1 > requested_num_threads ) {
|
2013-10-12 09:19:59 -04:00
|
|
|
throw OSRMException("Number of threads must be a positive number");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-19 16:41:46 -04:00
|
|
|
#endif /* PROGRAM_OPTIONS_H */
|