15 #include <boost/program_options.hpp>
16 #include <boost/tokenizer.hpp>
20 #include <stdair/basic/BasLogParams.hpp>
21 #include <stdair/basic/BasDBParams.hpp>
22 #include <stdair/service/Logger.hpp>
23 #include <stdair/stdair_json.hpp>
25 #include <airinv/config/airinv-paths.hpp>
29 typedef unsigned int ServerPort_T;
33 const std::string K_AIRINV_DEFAULT_LOG_FILENAME (
"airinvServer.log");
36 const std::string K_AIRINV_DEFAULT_SERVER_PROTOCOL (
"tcp://");
39 const std::string K_AIRINV_DEFAULT_SERVER_ADDRESS (
"*");
42 const ServerPort_T K_AIRINV_DEFAULT_SERVER_PORT (5555);
57 const std::string K_AIRINV_DEFAULT_FF_DISUTILITY_INPUT_FILENAME (
STDAIR_SAMPLE_DIR
68 const bool K_AIRINV_DEFAULT_BUILT_IN_INPUT =
false;
74 const bool K_AIRINV_DEFAULT_FOR_SCHEDULE =
false;
79 const int K_AIRINV_EARLY_RETURN_STATUS = 99;
96 template<
class T> std::ostream&
operator<< (std::ostream& os,
97 const std::vector<T>& v) {
98 std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout,
" "));
103 int readConfiguration (
int argc,
char* argv[], std::string& ioServerProtocol,
104 std::string& ioServerAddress, ServerPort_T& ioServerPort,
105 bool& ioIsBuiltin,
bool& ioIsForSchedule,
106 stdair::Filename_T& ioInventoryFilename,
107 stdair::Filename_T& ioScheduleInputFilename,
108 stdair::Filename_T& ioODInputFilename,
109 stdair::Filename_T& ioFRAT5Filename,
110 stdair::Filename_T& ioFFDisutilityFilename,
111 stdair::Filename_T& ioYieldInputFilename,
112 std::string& ioLogFilename) {
114 ioIsBuiltin = K_AIRINV_DEFAULT_BUILT_IN_INPUT;
117 ioIsForSchedule = K_AIRINV_DEFAULT_FOR_SCHEDULE;
120 boost::program_options::options_description
generic (
"Generic options");
121 generic.add_options()
122 (
"prefix",
"print installation prefix")
123 (
"version,v",
"print version string")
124 (
"help,h",
"produce help message");
129 boost::program_options::options_description config (
"Configuration");
132 "The sample BOM tree can be either built-in or parsed from an input file. That latter must then be given with the -i/--inventory or -s/--schedule option")
134 "The BOM tree should be built from a schedule file (instead of from an inventory dump)")
136 boost::program_options::value< std::string >(&ioInventoryFilename)->default_value(K_AIRINV_DEFAULT_INVENTORY_FILENAME),
137 "(CVS) input file for the inventory")
139 boost::program_options::value< std::string >(&ioScheduleInputFilename)->default_value(K_AIRINV_DEFAULT_SCHEDULE_FILENAME),
140 "(CVS) input file for the schedule")
142 boost::program_options::value< std::string >(&ioODInputFilename)->default_value(K_AIRINV_DEFAULT_OND_FILENAME),
143 "(CVS) input file for the O&D")
145 boost::program_options::value< std::string >(&ioFRAT5Filename)->default_value(K_AIRINV_DEFAULT_FRAT5_INPUT_FILENAME),
146 "(CSV) input file for the FRAT5 Curve")
148 boost::program_options::value< std::string >(&ioFFDisutilityFilename)->default_value(K_AIRINV_DEFAULT_FF_DISUTILITY_INPUT_FILENAME),
149 "(CSV) input file for the FF disutility Curve")
151 boost::program_options::value< std::string >(&ioYieldInputFilename)->default_value(K_AIRINV_DEFAULT_YIELD_FILENAME),
152 "(CVS) input file for the yield")
154 boost::program_options::value< std::string >(&ioServerProtocol)->default_value(K_AIRINV_DEFAULT_SERVER_PROTOCOL),
157 boost::program_options::value< std::string >(&ioServerAddress)->default_value(K_AIRINV_DEFAULT_SERVER_ADDRESS),
160 boost::program_options::value< ServerPort_T >(&ioServerPort)->default_value(K_AIRINV_DEFAULT_SERVER_PORT),
163 boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_AIRINV_DEFAULT_LOG_FILENAME),
164 "Filename for the output logs")
169 boost::program_options::options_description hidden (
"Hidden options");
172 boost::program_options::value< std::vector<std::string> >(),
173 "Show the copyright (license)");
175 boost::program_options::options_description cmdline_options;
176 cmdline_options.add(
generic).add(config).add(hidden);
178 boost::program_options::options_description config_file_options;
179 config_file_options.add(config).add(hidden);
180 boost::program_options::options_description visible (
"Allowed options");
181 visible.add(
generic).add(config);
183 boost::program_options::positional_options_description p;
184 p.add (
"copyright", -1);
186 boost::program_options::variables_map vm;
187 boost::program_options::
188 store (boost::program_options::command_line_parser (argc, argv).
189 options (cmdline_options).positional(p).run(), vm);
191 std::ifstream ifs (
"airinvServer.cfg");
192 boost::program_options::store (parse_config_file (ifs, config_file_options),
194 boost::program_options::notify (vm);
196 if (vm.count (
"help")) {
197 std::cout << visible << std::endl;
198 return K_AIRINV_EARLY_RETURN_STATUS;
201 if (vm.count (
"version")) {
203 return K_AIRINV_EARLY_RETURN_STATUS;
206 if (vm.count (
"prefix")) {
207 std::cout <<
"Installation prefix: " <<
PREFIXDIR << std::endl;
208 return K_AIRINV_EARLY_RETURN_STATUS;
211 if (vm.count (
"protocol")) {
212 ioServerProtocol = vm[
"protocol"].as< std::string >();
213 std::cout <<
"Server protocol is: " << ioServerProtocol << std::endl;
216 if (vm.count (
"address")) {
217 ioServerAddress = vm[
"address"].as< std::string >();
218 std::cout <<
"Server address is: " << ioServerAddress << std::endl;
221 if (vm.count (
"port")) {
222 ioServerPort = vm[
"port"].as< ServerPort_T >();
223 std::cout <<
"Server port is: " << ioServerPort << std::endl;
226 if (vm.count (
"builtin")) {
229 const std::string isBuiltinStr = (ioIsBuiltin ==
true)?
"yes":
"no";
230 std::cout <<
"The BOM should be built-in? " << isBuiltinStr << std::endl;
232 if (vm.count (
"for_schedule")) {
233 ioIsForSchedule =
true;
235 const std::string isForScheduleStr = (ioIsForSchedule ==
true)?
"yes":
"no";
236 std::cout <<
"The BOM should be built from schedule? " << isForScheduleStr
239 if (ioIsBuiltin ==
false) {
241 if (ioIsForSchedule ==
false) {
243 if (vm.count (
"inventory")) {
244 ioInventoryFilename = vm[
"inventory"].as< std::string >();
245 std::cout <<
"Input inventory filename is: " << ioInventoryFilename
251 std::cerr <<
"Either one among the -b/--builtin, -i/--inventory or "
252 <<
" -f/--for_schedule and -s/--schedule options "
253 <<
"must be specified" << std::endl;
258 if (vm.count (
"schedule")) {
259 ioScheduleInputFilename = vm[
"schedule"].as< std::string >();
260 std::cout <<
"Input schedule filename is: " << ioScheduleInputFilename
266 std::cerr <<
"Either one among the -b/--builtin, -i/--inventory or "
267 <<
" -f/--for_schedule and -s/--schedule options "
268 <<
"must be specified" << std::endl;
271 if (vm.count (
"ond")) {
272 ioODInputFilename = vm[
"ond"].as< std::string >();
273 std::cout <<
"Input O&D filename is: " << ioODInputFilename << std::endl;
276 if (vm.count (
"frat5")) {
277 ioFRAT5Filename = vm[
"frat5"].as< std::string >();
278 std::cout <<
"FRAT5 input filename is: " << ioFRAT5Filename << std::endl;
282 if (vm.count (
"ff_disutility")) {
283 ioFFDisutilityFilename = vm[
"ff_disutility"].as< std::string >();
284 std::cout <<
"FF disutility input filename is: "
285 << ioFFDisutilityFilename << std::endl;
289 if (vm.count (
"yield")) {
290 ioYieldInputFilename = vm[
"yield"].as< std::string >();
291 std::cout <<
"Input yield filename is: " << ioYieldInputFilename << std::endl;
296 if (vm.count (
"log")) {
297 ioLogFilename = vm[
"log"].as< std::string >();
298 std::cout <<
"Log filename is: " << ioLogFilename << std::endl;
309 static std::string s_recv (zmq::socket_t& socket) {
310 zmq::message_t message;
311 socket.recv (&message);
313 return std::string (static_cast<char*> (message.data()), message.size());
319 static bool s_send (zmq::socket_t& socket,
const std::string&
string) {
320 zmq::message_t message (
string.size());
321 memcpy (message.data(),
string.data(),
string.size());
323 bool rc = socket.send (message);
329 int main (
int argc,
char* argv[]) {
332 std::string ioServerProtocol;
333 std::string ioServerAddress;
334 ServerPort_T ioServerPort;
342 stdair::Filename_T lInventoryFilename;
343 stdair::Filename_T lScheduleInputFilename;
344 stdair::Filename_T lODInputFilename;
345 stdair::Filename_T lFRAT5InputFilename;
346 stdair::Filename_T lFFDisutilityInputFilename;
347 stdair::Filename_T lYieldInputFilename;
350 stdair::Filename_T lLogFilename;
353 const int lOptionParserStatus =
354 readConfiguration (argc, argv, ioServerProtocol, ioServerAddress,
355 ioServerPort, isBuiltin, isForSchedule,
356 lInventoryFilename, lScheduleInputFilename,
357 lODInputFilename, lFRAT5InputFilename,
358 lFFDisutilityInputFilename, lYieldInputFilename,
361 if (lOptionParserStatus == K_AIRINV_EARLY_RETURN_STATUS) {
366 std::ofstream logOutputFile;
368 logOutputFile.open (lLogFilename.c_str());
369 logOutputFile.clear();
372 const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
376 STDAIR_LOG_DEBUG (
"Initialisation of the AirInv server");
379 if (isBuiltin ==
true) {
382 airinvService.buildSampleBom();
385 if (isForSchedule ==
true) {
387 stdair::ScheduleFilePath lScheduleFilePath (lScheduleInputFilename);
388 stdair::ODFilePath lODFilePath (lODInputFilename);
389 stdair::FRAT5FilePath lFRAT5FilePath (lFRAT5InputFilename);
390 stdair::FFDisutilityFilePath lFFDisutilityFilePath (lFFDisutilityInputFilename);
391 AIRRAC::YieldFilePath lYieldFilePath (lYieldInputFilename);
392 airinvService.parseAndLoad (lScheduleFilePath, lODFilePath,
393 lFRAT5FilePath, lFFDisutilityFilePath,
399 airinvService.parseAndLoad (lInventoryFilePath);
404 std::ostringstream oZeroMQBindStream;
405 oZeroMQBindStream << ioServerProtocol << ioServerAddress
406 <<
":" << ioServerPort;
407 const std::string lZeroMQBindString (oZeroMQBindStream.str());
410 zmq::context_t context (1);
411 zmq::socket_t socket (context, ZMQ_REP);
412 socket.bind (lZeroMQBindString.c_str());
415 STDAIR_LOG_DEBUG (
"The AirInv server is ready to receive requests...");
421 const std::string& lReceivedString = s_recv (socket);
424 STDAIR_LOG_DEBUG (
"Received: '" << lReceivedString <<
"'");
426 const stdair::JSONString lJSONCommandString (lReceivedString);
427 const std::string& lJSONDump =
428 airinvService.jsonHandler (lJSONCommandString);
431 STDAIR_LOG_DEBUG (
"Send: '" << lJSONDump <<
"'");
434 s_send (socket, lJSONDump);