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/bom/BomJSONImport.hpp>
23 #include <stdair/bom/BomJSONExport.hpp>
24 #include <stdair/service/Logger.hpp>
30 typedef unsigned int ServerPort_T;
34 const std::string K_AIRINV_DEFAULT_LOG_FILENAME (
"airinvServer.log");
37 const std::string K_AIRINV_DEFAULT_SERVER_PROTOCOL (
"tcp://");
40 const std::string K_AIRINV_DEFAULT_SERVER_ADDRESS (
"*");
43 const ServerPort_T K_AIRINV_DEFAULT_SERVER_PORT (5555);
63 const bool K_AIRINV_DEFAULT_BUILT_IN_INPUT =
false;
69 const bool K_AIRINV_DEFAULT_FOR_SCHEDULE =
false;
74 const int K_AIRINV_EARLY_RETURN_STATUS = 99;
91 template<
class T> std::ostream&
operator<< (std::ostream& os,
92 const std::vector<T>& v) {
93 std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout,
" "));
98 int readConfiguration (
int argc,
char* argv[], std::string& ioServerProtocol,
99 std::string& ioServerAddress, ServerPort_T& ioServerPort,
100 bool& ioIsBuiltin,
bool& ioIsForSchedule,
101 stdair::Filename_T& ioInventoryFilename,
102 stdair::Filename_T& ioScheduleInputFilename,
103 stdair::Filename_T& ioODInputFilename,
104 stdair::Filename_T& ioYieldInputFilename,
105 std::string& ioLogFilename) {
107 ioIsBuiltin = K_AIRINV_DEFAULT_BUILT_IN_INPUT;
110 ioIsForSchedule = K_AIRINV_DEFAULT_FOR_SCHEDULE;
113 boost::program_options::options_description
generic (
"Generic options");
114 generic.add_options()
115 (
"prefix",
"print installation prefix")
116 (
"version,v",
"print version string")
117 (
"help,h",
"produce help message");
122 boost::program_options::options_description config (
"Configuration");
125 "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")
127 "The BOM tree should be built from a schedule file (instead of from an inventory dump)")
129 boost::program_options::value< std::string >(&ioInventoryFilename)->default_value(K_AIRINV_DEFAULT_INVENTORY_FILENAME),
130 "(CVS) input file for the inventory")
132 boost::program_options::value< std::string >(&ioScheduleInputFilename)->default_value(K_AIRINV_DEFAULT_SCHEDULE_FILENAME),
133 "(CVS) input file for the schedule")
135 boost::program_options::value< std::string >(&ioODInputFilename)->default_value(K_AIRINV_DEFAULT_OND_FILENAME),
136 "(CVS) input file for the O&D")
138 boost::program_options::value< std::string >(&ioYieldInputFilename)->default_value(K_AIRINV_DEFAULT_YIELD_FILENAME),
139 "(CVS) input file for the yield")
141 boost::program_options::value< std::string >(&ioServerProtocol)->default_value(K_AIRINV_DEFAULT_SERVER_PROTOCOL),
144 boost::program_options::value< std::string >(&ioServerAddress)->default_value(K_AIRINV_DEFAULT_SERVER_ADDRESS),
147 boost::program_options::value< ServerPort_T >(&ioServerPort)->default_value(K_AIRINV_DEFAULT_SERVER_PORT),
150 boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_AIRINV_DEFAULT_LOG_FILENAME),
151 "Filename for the output logs")
156 boost::program_options::options_description hidden (
"Hidden options");
159 boost::program_options::value< std::vector<std::string> >(),
160 "Show the copyright (license)");
162 boost::program_options::options_description cmdline_options;
163 cmdline_options.add(
generic).add(config).add(hidden);
165 boost::program_options::options_description config_file_options;
166 config_file_options.add(config).add(hidden);
167 boost::program_options::options_description visible (
"Allowed options");
168 visible.add(
generic).add(config);
170 boost::program_options::positional_options_description p;
171 p.add (
"copyright", -1);
173 boost::program_options::variables_map vm;
174 boost::program_options::
175 store (boost::program_options::command_line_parser (argc, argv).
176 options (cmdline_options).positional(p).run(), vm);
178 std::ifstream ifs (
"airinvServer.cfg");
179 boost::program_options::store (parse_config_file (ifs, config_file_options),
181 boost::program_options::notify (vm);
183 if (vm.count (
"help")) {
184 std::cout << visible << std::endl;
185 return K_AIRINV_EARLY_RETURN_STATUS;
188 if (vm.count (
"version")) {
190 return K_AIRINV_EARLY_RETURN_STATUS;
193 if (vm.count (
"prefix")) {
194 std::cout <<
"Installation prefix: " <<
PREFIXDIR << std::endl;
195 return K_AIRINV_EARLY_RETURN_STATUS;
198 if (vm.count (
"protocol")) {
199 ioServerProtocol = vm[
"protocol"].as< std::string >();
200 std::cout <<
"Server protocol is: " << ioServerProtocol << std::endl;
203 if (vm.count (
"address")) {
204 ioServerAddress = vm[
"address"].as< std::string >();
205 std::cout <<
"Server address is: " << ioServerAddress << std::endl;
208 if (vm.count (
"port")) {
209 ioServerPort = vm[
"port"].as< ServerPort_T >();
210 std::cout <<
"Server port is: " << ioServerPort << std::endl;
213 if (vm.count (
"builtin")) {
216 const std::string isBuiltinStr = (ioIsBuiltin ==
true)?
"yes":
"no";
217 std::cout <<
"The BOM should be built-in? " << isBuiltinStr << std::endl;
219 if (vm.count (
"for_schedule")) {
220 ioIsForSchedule =
true;
222 const std::string isForScheduleStr = (ioIsForSchedule ==
true)?
"yes":
"no";
223 std::cout <<
"The BOM should be built from schedule? " << isForScheduleStr
226 if (ioIsBuiltin ==
false) {
228 if (ioIsForSchedule ==
false) {
230 if (vm.count (
"inventory")) {
231 ioInventoryFilename = vm[
"inventory"].as< std::string >();
232 std::cout <<
"Input inventory filename is: " << ioInventoryFilename
238 std::cerr <<
"Either one among the -b/--builtin, -i/--inventory or "
239 <<
" -f/--for_schedule and -s/--schedule options "
240 <<
"must be specified" << std::endl;
245 if (vm.count (
"schedule")) {
246 ioScheduleInputFilename = vm[
"schedule"].as< std::string >();
247 std::cout <<
"Input schedule filename is: " << ioScheduleInputFilename
253 std::cerr <<
"Either one among the -b/--builtin, -i/--inventory or "
254 <<
" -f/--for_schedule and -s/--schedule options "
255 <<
"must be specified" << std::endl;
258 if (vm.count (
"ond")) {
259 ioODInputFilename = vm[
"ond"].as< std::string >();
260 std::cout <<
"Input O&D filename is: " << ioODInputFilename << std::endl;
263 if (vm.count (
"yield")) {
264 ioYieldInputFilename = vm[
"yield"].as< std::string >();
265 std::cout <<
"Input yield filename is: " << ioYieldInputFilename << std::endl;
270 if (vm.count (
"log")) {
271 ioLogFilename = vm[
"log"].as< std::string >();
272 std::cout <<
"Log filename is: " << ioLogFilename << std::endl;
283 static std::string s_recv (zmq::socket_t& socket) {
284 zmq::message_t message;
285 socket.recv (&message);
287 return std::string (static_cast<char*> (message.data()), message.size());
293 static bool s_send (zmq::socket_t& socket,
const std::string&
string) {
294 zmq::message_t message (
string.size());
295 memcpy (message.data(),
string.data(),
string.size());
297 bool rc = socket.send (message);
303 int main (
int argc,
char* argv[]) {
306 std::string ioServerProtocol;
307 std::string ioServerAddress;
308 ServerPort_T ioServerPort;
316 stdair::Filename_T lInventoryFilename;
317 stdair::Filename_T lScheduleInputFilename;
318 stdair::Filename_T lODInputFilename;
319 stdair::Filename_T lYieldInputFilename;
322 stdair::Filename_T lLogFilename;
325 const int lOptionParserStatus =
326 readConfiguration (argc, argv, ioServerProtocol, ioServerAddress,
327 ioServerPort, isBuiltin, isForSchedule,
328 lInventoryFilename, lScheduleInputFilename,
329 lODInputFilename, lYieldInputFilename, lLogFilename);
331 if (lOptionParserStatus == K_AIRINV_EARLY_RETURN_STATUS) {
336 std::ofstream logOutputFile;
338 logOutputFile.open (lLogFilename.c_str());
339 logOutputFile.clear();
342 const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
346 STDAIR_LOG_DEBUG (
"Initialisation of the AirInv server");
349 if (isBuiltin ==
true) {
352 airinvService.buildSampleBom();
355 if (isForSchedule ==
true) {
357 AIRRAC::YieldFilePath lYieldFilePath (lYieldInputFilename);
358 airinvService.parseAndLoad (lScheduleInputFilename, lODInputFilename,
363 airinvService.parseAndLoad (lInventoryFilename);
368 std::ostringstream oZeroMQBindStream;
369 oZeroMQBindStream << ioServerProtocol << ioServerAddress
370 <<
":" << ioServerPort;
371 const std::string lZeroMQBindString (oZeroMQBindStream.str());
374 zmq::context_t context (1);
375 zmq::socket_t socket (context, ZMQ_REP);
376 socket.bind (lZeroMQBindString.c_str());
379 STDAIR_LOG_DEBUG (
"The AirInv server is ready to receive requests...");
385 const std::string& lFlightDateKeyJSONString = s_recv (socket);
388 STDAIR_LOG_DEBUG (
"Received: '" << lFlightDateKeyJSONString <<
"'");
391 stdair::AirlineCode_T lAirlineCode;
392 stdair::BomJSONImport::jsonImportInventoryKey (lFlightDateKeyJSONString,
396 stdair::FlightNumber_T lFlightNumber;
397 stdair::Date_T lDate;
398 stdair::BomJSONImport::jsonImportFlightDateKey (lFlightDateKeyJSONString,
399 lFlightNumber, lDate);
402 STDAIR_LOG_DEBUG (
"=> airline code = '" << lAirlineCode
403 <<
"', flight number = " << lFlightNumber
404 <<
"', departure date = '" << lDate <<
"'");
407 const std::string& lFlightDateCSVDump =
408 airinvService.csvDisplay (lAirlineCode, lFlightNumber, lDate);
409 STDAIR_LOG_DEBUG (std::endl << lFlightDateCSVDump);
412 const std::string& lFlightDateJSONDump =
413 airinvService.jsonExport (lAirlineCode, lFlightNumber, lDate);
416 STDAIR_LOG_DEBUG (
"Send: '" << lFlightDateJSONDump <<
"'");
419 s_send (socket, lFlightDateJSONDump);