AirInv Logo  1.00.0
C++ Simulated Airline Inventory Management System library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
win_main.cpp
Go to the documentation of this file.
1 //
2 // win_main.cpp
3 // ~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #include <iostream>
12 #include <string>
13 #include <boost/asio.hpp>
14 #include <boost/bind.hpp>
15 #include <boost/function.hpp>
16 #include <boost/lexical_cast.hpp>
18 
19 #if defined(_WIN32)
20 
21 boost::function0<void> console_ctrl_function;
22 
23 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type) {
24  switch (ctrl_type) {
25  case CTRL_C_EVENT:
26  case CTRL_BREAK_EVENT:
27  case CTRL_CLOSE_EVENT:
28  case CTRL_SHUTDOWN_EVENT:
29  console_ctrl_function();
30  return TRUE;
31  default:
32  return FALSE;
33  }
34 }
35 
36 int main(int argc, char* argv[]) {
37 
38  try {
39 
40  // Check command line arguments.
41  if (argc != 5) {
42  std::cerr << "Usage: http_server <address> <port> <threads> <doc_root>\n";
43  std::cerr << " For IPv4, try:\n";
44  std::cerr << " http_server 0.0.0.0 80 1 .\n";
45  std::cerr << " For IPv6, try:\n";
46  std::cerr << " http_server 0::0 80 1 .\n";
47  return 1;
48  }
49 
50  // Initialise server.
51  std::size_t num_threads = boost::lexical_cast<std::size_t>(argv[3]);
52  AIRINV::AirInvServer s (argv[1], argv[2], argv[4], num_threads);
53 
54  // Set console control handler to allow server to be stopped.
55  console_ctrl_function = boost::bind(&AIRINV::AirInvServer::stop, &s);
56  SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
57 
58  // Run the server until stopped.
59  s.run();
60 
61  } catch (std::exception& e) {
62  std::cerr << "exception: " << e.what() << "\n";
63  }
64 
65  return 0;
66 }
67 #endif // defined(_WIN32)