Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| 6 | * |
| 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| 19 | */ |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 20 | // |
| 21 | // server.cpp |
| 22 | // ~~~~~~~~~~ |
| 23 | // |
| 24 | // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
| 25 | // |
| 26 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 27 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 28 | // |
| 29 | |
| 30 | #include "server.hpp" |
| 31 | #include <boost/bind.hpp> |
| 32 | #include <signal.h> |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 33 | #include "logging.h" |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 34 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 35 | INIT_LOGGER ("HttpServer"); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 36 | namespace http { |
| 37 | namespace server { |
| 38 | |
| 39 | server::server(const std::string& address, const std::string& port, |
| 40 | const std::string& doc_root) |
| 41 | : io_service_(), |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 42 | // signals_(io_service_), |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 43 | acceptor_(io_service_), |
| 44 | connection_manager_(), |
| 45 | new_connection_(), |
| 46 | request_handler_(doc_root) |
| 47 | { |
| 48 | // Register to handle the signals that indicate when the server should exit. |
| 49 | // It is safe to register for the same signal multiple times in a program, |
| 50 | // provided all registration for the specified signal is made through Asio. |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 51 | // signals_.add(SIGINT); |
| 52 | // signals_.add(SIGTERM); |
| 53 | //#if defined(SIGQUIT) |
| 54 | // signals_.add(SIGQUIT); |
| 55 | //#endif // defined(SIGQUIT) |
| 56 | // signals_.async_wait(boost::bind(&server::handle_stop, this)); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 57 | |
| 58 | // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). |
| 59 | boost::asio::ip::tcp::resolver resolver(io_service_); |
| 60 | boost::asio::ip::tcp::resolver::query query(address, port); |
| 61 | boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query); |
| 62 | acceptor_.open(endpoint.protocol()); |
| 63 | acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); |
| 64 | acceptor_.bind(endpoint); |
| 65 | acceptor_.listen(); |
| 66 | |
| 67 | start_accept(); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 68 | |
| 69 | _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame] | 72 | server::~server() |
| 73 | { |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 74 | // handle_stop(); |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 77 | void server::run() |
| 78 | { |
| 79 | // The io_service::run() call will block until all asynchronous operations |
| 80 | // have finished. While the server is running, there is always at least one |
| 81 | // asynchronous operation outstanding: the asynchronous accept call waiting |
| 82 | // for new incoming connections. |
| 83 | io_service_.run(); |
| 84 | } |
| 85 | |
| 86 | void server::start_accept() |
| 87 | { |
| 88 | new_connection_.reset(new connection(io_service_, |
| 89 | connection_manager_, request_handler_)); |
| 90 | acceptor_.async_accept(new_connection_->socket(), |
| 91 | boost::bind(&server::handle_accept, this, |
| 92 | boost::asio::placeholders::error)); |
| 93 | } |
| 94 | |
| 95 | void server::handle_accept(const boost::system::error_code& e) |
| 96 | { |
| 97 | // Check whether the server was stopped by a signal before this completion |
| 98 | // handler had a chance to run. |
| 99 | if (!acceptor_.is_open()) |
| 100 | { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (!e) |
| 105 | { |
| 106 | connection_manager_.start(new_connection_); |
| 107 | } |
| 108 | |
| 109 | start_accept(); |
| 110 | } |
| 111 | |
| 112 | void server::handle_stop() |
| 113 | { |
| 114 | // The server is stopped by cancelling all outstanding asynchronous |
| 115 | // operations. Once all operations have finished the io_service::run() call |
| 116 | // will exit. |
| 117 | acceptor_.close(); |
| 118 | connection_manager_.stop_all(); |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 119 | // although they say io_service::run() would stop, but it didn't happen.. |
| 120 | // the thread join was blocking, waiting for io_service::run() to finish |
| 121 | // even after handle_stop() call.. |
| 122 | // so just force it quit, no harm here. |
| 123 | io_service_.stop(); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // namespace server |
| 127 | } // namespace http |