$treeview $search $mathjax
AirInv Logo  1.00.1
$projectbrief
$projectbrief
$searchbox

win_main.cpp

Go to the documentation of this file.
00001 //
00002 // win_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/bind.hpp>
00015 #include <boost/function.hpp>
00016 #include <boost/lexical_cast.hpp>
00017 #include <airinv/server/AirInvServer.hpp>
00018 
00019 #if defined(_WIN32)
00020 
00021 boost::function0<void> console_ctrl_function;
00022 
00023 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type) {
00024   switch (ctrl_type) {
00025   case CTRL_C_EVENT:
00026   case CTRL_BREAK_EVENT:
00027   case CTRL_CLOSE_EVENT:
00028   case CTRL_SHUTDOWN_EVENT:
00029     console_ctrl_function();
00030     return TRUE;
00031   default:
00032     return FALSE;
00033   }
00034 }
00035 
00036 int main(int argc, char* argv[]) {
00037   
00038   try {
00039     
00040     // Check command line arguments.
00041     if (argc != 5) {
00042       std::cerr << "Usage: http_server <address> <port> <threads> <doc_root>\n";
00043       std::cerr << "  For IPv4, try:\n";
00044       std::cerr << "    http_server 0.0.0.0 80 1 .\n";
00045       std::cerr << "  For IPv6, try:\n";
00046       std::cerr << "    http_server 0::0 80 1 .\n";
00047       return 1;
00048     }
00049 
00050     // Initialise server.
00051     std::size_t num_threads = boost::lexical_cast<std::size_t>(argv[3]);
00052     AIRINV::AirInvServer s (argv[1], argv[2], argv[4], num_threads);
00053 
00054     // Set console control handler to allow server to be stopped.
00055     console_ctrl_function = boost::bind(&AIRINV::AirInvServer::stop, &s);
00056     SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
00057 
00058     // Run the server until stopped.
00059     s.run();
00060 
00061   } catch (std::exception& e) {
00062     std::cerr << "exception: " << e.what() << "\n";
00063   }
00064 
00065   return 0;
00066 }
00067 #endif // defined(_WIN32)