Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / asio / example / cpp03 / http / server3 / server.cpp
1 //
2 // server.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 "server.hpp"
12 #include <boost/thread/thread.hpp>
13 #include <boost/bind.hpp>
14 #include <boost/shared_ptr.hpp>
15 #include <vector>
16
17 namespace http {
18 namespace server3 {
19
20 server::server(const std::string& address, const std::string& port,
21     const std::string& doc_root, std::size_t thread_pool_size)
22   : thread_pool_size_(thread_pool_size),
23     signals_(io_service_),
24     acceptor_(io_service_),
25     new_connection_(),
26     request_handler_(doc_root)
27 {
28   // Register to handle the signals that indicate when the server should exit.
29   // It is safe to register for the same signal multiple times in a program,
30   // provided all registration for the specified signal is made through Asio.
31   signals_.add(SIGINT);
32   signals_.add(SIGTERM);
33 #if defined(SIGQUIT)
34   signals_.add(SIGQUIT);
35 #endif // defined(SIGQUIT)
36   signals_.async_wait(boost::bind(&server::handle_stop, this));
37
38   // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
39   boost::asio::ip::tcp::resolver resolver(io_service_);
40   boost::asio::ip::tcp::resolver::query query(address, port);
41   boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
42   acceptor_.open(endpoint.protocol());
43   acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
44   acceptor_.bind(endpoint);
45   acceptor_.listen();
46
47   start_accept();
48 }
49
50 void server::run()
51 {
52   // Create a pool of threads to run all of the io_services.
53   std::vector<boost::shared_ptr<boost::thread> > threads;
54   for (std::size_t i = 0; i < thread_pool_size_; ++i)
55   {
56     boost::shared_ptr<boost::thread> thread(new boost::thread(
57           boost::bind(&boost::asio::io_service::run, &io_service_)));
58     threads.push_back(thread);
59   }
60
61   // Wait for all threads in the pool to exit.
62   for (std::size_t i = 0; i < threads.size(); ++i)
63     threads[i]->join();
64 }
65
66 void server::start_accept()
67 {
68   new_connection_.reset(new connection(io_service_, request_handler_));
69   acceptor_.async_accept(new_connection_->socket(),
70       boost::bind(&server::handle_accept, this,
71         boost::asio::placeholders::error));
72 }
73
74 void server::handle_accept(const boost::system::error_code& e)
75 {
76   if (!e)
77   {
78     new_connection_->start();
79   }
80
81   start_accept();
82 }
83
84 void server::handle_stop()
85 {
86   io_service_.stop();
87 }
88
89 } // namespace server3
90 } // namespace http