AirInv Logo  1.00.0
C++ Simulated Airline Inventory Management System library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
posix_main.cpp
Go to the documentation of this file.
1 //
2 // posix_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/thread.hpp>
15 #include <boost/bind.hpp>
16 #include <boost/lexical_cast.hpp>
18 
19 #if !defined(_WIN32)
20 
21 #include <pthread.h>
22 #include <signal.h>
23 
24 // //////////////// M A I N ///////////////////
25 int main(int argc, char* argv[]) {
26 
27  try {
28 
29  // Check command line arguments.
30  if (argc != 5) {
31  std::cerr << "Usage: airinvServer <address> <port> <threads> <doc_root>"
32  << std::endl;
33  std::cerr << " For IPv4, try:" << std::endl;
34  std::cerr << " receiver 0.0.0.0 80 1 ." << std::endl;
35  std::cerr << " For IPv6, try:" << std::endl;
36  std::cerr << " receiver 0::0 80 1 ." << std::endl;
37  return 1;
38  }
39 
40  // Block all signals for background thread.
41  sigset_t new_mask;
42  sigfillset (&new_mask);
43  sigset_t old_mask;
44  pthread_sigmask (SIG_BLOCK, &new_mask, &old_mask);
45 
46  // Run server in background thread.
47  std::size_t num_threads = boost::lexical_cast<std::size_t>(argv[3]);
48  AIRINV::AirInvServer s (argv[1], argv[2], argv[4], num_threads);
49  boost::thread t (boost::bind (&AIRINV::AirInvServer::run, &s));
50 
51  // Restore previous signals.
52  pthread_sigmask (SIG_SETMASK, &old_mask, 0);
53 
54  // Wait for signal indicating time to shut down.
55  sigset_t wait_mask;
56  sigemptyset (&wait_mask);
57  sigaddset (&wait_mask, SIGINT);
58  sigaddset (&wait_mask, SIGQUIT);
59  sigaddset (&wait_mask, SIGTERM);
60  pthread_sigmask (SIG_BLOCK, &wait_mask, 0);
61  int sig = 0;
62  sigwait (&wait_mask, &sig);
63 
64  // Stop the server.
65  s.stop();
66  t.join();
67 
68  } catch (std::exception& e) {
69  std::cerr << "exception: " << e.what() << "\n";
70  }
71 
72  return 0;
73 }
74 
75 #endif // !defined(_WIN32)