Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / asio / example / cpp03 / http / server4 / main.cpp
1 //
2 // main.cpp
3 // ~~~~~~~~
4 //
5 // Copyright (c) 2003-2014 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 <boost/asio.hpp>
13 #include <boost/bind.hpp>
14 #include <signal.h>
15 #include "server.hpp"
16 #include "file_handler.hpp"
17
18 int main(int argc, char* argv[])
19 {
20   try
21   {
22     // Check command line arguments.
23     if (argc != 4)
24     {
25       std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
26       std::cerr << "  For IPv4, try:\n";
27       std::cerr << "    receiver 0.0.0.0 80 .\n";
28       std::cerr << "  For IPv6, try:\n";
29       std::cerr << "    receiver 0::0 80 .\n";
30       return 1;
31     }
32
33     boost::asio::io_service io_service;
34
35     // Launch the initial server coroutine.
36     http::server4::server(io_service, argv[1], argv[2],
37         http::server4::file_handler(argv[3]))();
38
39     // Wait for signals indicating time to shut down.
40     boost::asio::signal_set signals(io_service);
41     signals.add(SIGINT);
42     signals.add(SIGTERM);
43 #if defined(SIGQUIT)
44     signals.add(SIGQUIT);
45 #endif // defined(SIGQUIT)
46     signals.async_wait(boost::bind(
47           &boost::asio::io_service::stop, &io_service));
48
49     // Run the server.
50     io_service.run();
51   }
52   catch (std::exception& e)
53   {
54     std::cerr << "exception: " << e.what() << "\n";
55   }
56
57   return 0;
58 }