AirInv Logo  0.1.2
C++ Simulated Airline Inventory Management System library
posix_main.cpp
Go to the documentation of this file.
00001 //
00002 // posix_main.cpp
00003 // ~~~~~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
00006 //
00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 
00011 #include <iostream>
00012 #include <string>
00013 #include <boost/asio.hpp>
00014 #include <boost/thread.hpp>
00015 #include <boost/bind.hpp>
00016 #include <boost/lexical_cast.hpp>
00017 #include <airinv/server/AirInvServer.hpp>
00018 
00019 #if !defined(_WIN32)
00020 
00021 #include <pthread.h>
00022 #include <signal.h>
00023 
00024 // //////////////// M A I N ///////////////////
00025 int main(int argc, char* argv[]) {
00026 
00027   try {
00028 
00029     // Check command line arguments.
00030     if (argc != 5) {
00031       std::cerr << "Usage: airinvServer <address> <port> <threads> <doc_root>"
00032                 << std::endl;
00033       std::cerr << "  For IPv4, try:" << std::endl;
00034       std::cerr << "    receiver 0.0.0.0 80 1 ." << std::endl;
00035       std::cerr << "  For IPv6, try:" << std::endl;
00036       std::cerr << "    receiver 0::0 80 1 ." << std::endl;
00037       return 1;
00038     }
00039 
00040     // Block all signals for background thread.
00041     sigset_t new_mask;
00042     sigfillset (&new_mask);
00043     sigset_t old_mask;
00044     pthread_sigmask (SIG_BLOCK, &new_mask, &old_mask);
00045 
00046     // Run server in background thread.
00047     std::size_t num_threads = boost::lexical_cast<std::size_t>(argv[3]);
00048     AIRINV::AirInvServer s (argv[1], argv[2], argv[4], num_threads);
00049     boost::thread t (boost::bind (&AIRINV::AirInvServer::run, &s));
00050 
00051     // Restore previous signals.
00052     pthread_sigmask (SIG_SETMASK, &old_mask, 0);
00053 
00054     // Wait for signal indicating time to shut down.
00055     sigset_t wait_mask;
00056     sigemptyset (&wait_mask);
00057     sigaddset (&wait_mask, SIGINT);
00058     sigaddset (&wait_mask, SIGQUIT);
00059     sigaddset (&wait_mask, SIGTERM);
00060     pthread_sigmask (SIG_BLOCK, &wait_mask, 0);
00061     int sig = 0;
00062     sigwait (&wait_mask, &sig);
00063 
00064     // Stop the server.
00065     s.stop();
00066     t.join();
00067 
00068   } catch (std::exception& e) {
00069     std::cerr << "exception: " << e.what() << "\n";
00070   }
00071 
00072   return 0;
00073 }
00074 
00075 #endif // !defined(_WIN32)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines