blob: 11b74cbd9c8111cdca3b7a43b0122f48bfabc0d1 [file] [log] [blame]
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -08001//
2// server.cpp
3// ~~~~~~~~~~
4//
5// Copyright (c) 2003-2012 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/bind.hpp>
13#include <signal.h>
Zhenkai Zhud9429222013-02-25 22:34:09 -080014#include "logging.h"
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080015
Zhenkai Zhud9429222013-02-25 22:34:09 -080016INIT_LOGGER ("HttpServer");
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080017namespace http {
18namespace server {
19
20server::server(const std::string& address, const std::string& port,
21 const std::string& doc_root)
22 : io_service_(),
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -070023 // signals_(io_service_),
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080024 acceptor_(io_service_),
25 connection_manager_(),
26 new_connection_(),
27 request_handler_(doc_root)
28{
29 // Register to handle the signals that indicate when the server should exit.
30 // It is safe to register for the same signal multiple times in a program,
31 // provided all registration for the specified signal is made through Asio.
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -070032// signals_.add(SIGINT);
33// signals_.add(SIGTERM);
34//#if defined(SIGQUIT)
35// signals_.add(SIGQUIT);
36//#endif // defined(SIGQUIT)
37// signals_.async_wait(boost::bind(&server::handle_stop, this));
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080038
39 // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
40 boost::asio::ip::tcp::resolver resolver(io_service_);
41 boost::asio::ip::tcp::resolver::query query(address, port);
42 boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
43 acceptor_.open(endpoint.protocol());
44 acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
45 acceptor_.bind(endpoint);
46 acceptor_.listen();
47
48 start_accept();
Zhenkai Zhud9429222013-02-25 22:34:09 -080049
50 _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080051}
52
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080053server::~server()
54{
Zhenkai Zhu377cbeb2013-02-28 16:36:26 -080055 // handle_stop();
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080056}
57
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080058void server::run()
59{
60 // The io_service::run() call will block until all asynchronous operations
61 // have finished. While the server is running, there is always at least one
62 // asynchronous operation outstanding: the asynchronous accept call waiting
63 // for new incoming connections.
64 io_service_.run();
65}
66
67void server::start_accept()
68{
69 new_connection_.reset(new connection(io_service_,
70 connection_manager_, request_handler_));
71 acceptor_.async_accept(new_connection_->socket(),
72 boost::bind(&server::handle_accept, this,
73 boost::asio::placeholders::error));
74}
75
76void server::handle_accept(const boost::system::error_code& e)
77{
78 // Check whether the server was stopped by a signal before this completion
79 // handler had a chance to run.
80 if (!acceptor_.is_open())
81 {
82 return;
83 }
84
85 if (!e)
86 {
87 connection_manager_.start(new_connection_);
88 }
89
90 start_accept();
91}
92
93void server::handle_stop()
94{
95 // The server is stopped by cancelling all outstanding asynchronous
96 // operations. Once all operations have finished the io_service::run() call
97 // will exit.
98 acceptor_.close();
99 connection_manager_.stop_all();
Zhenkai Zhu377cbeb2013-02-28 16:36:26 -0800100 // although they say io_service::run() would stop, but it didn't happen..
101 // the thread join was blocking, waiting for io_service::run() to finish
102 // even after handle_stop() call..
103 // so just force it quit, no harm here.
104 io_service_.stop();
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800105}
106
107} // namespace server
108} // namespace http