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" |
Alexander Afanasyev | caf61b1 | 2016-12-25 14:18:52 -0800 | [diff] [blame] | 31 | #include "core/logging.hpp" |
| 32 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 33 | #include <signal.h> |
| 34 | |
| 35 | namespace http { |
| 36 | namespace server { |
| 37 | |
Alexander Afanasyev | caf61b1 | 2016-12-25 14:18:52 -0800 | [diff] [blame] | 38 | using namespace ndn::chronoshare; |
| 39 | |
| 40 | INIT_LOGGER("HttpServer"); |
| 41 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 42 | server::server(const std::string& address, const std::string& port, const std::string& doc_root) |
| 43 | : io_service_() |
| 44 | , |
| 45 | // signals_(io_service_), |
| 46 | acceptor_(io_service_) |
| 47 | , connection_manager_() |
| 48 | , new_connection_() |
| 49 | , request_handler_(doc_root) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 50 | { |
| 51 | // Register to handle the signals that indicate when the server should exit. |
| 52 | // It is safe to register for the same signal multiple times in a program, |
| 53 | // provided all registration for the specified signal is made through Asio. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 54 | // signals_.add(SIGINT); |
| 55 | // signals_.add(SIGTERM); |
| 56 | //#if defined(SIGQUIT) |
| 57 | // signals_.add(SIGQUIT); |
| 58 | //#endif // defined(SIGQUIT) |
Alexander Afanasyev | caf61b1 | 2016-12-25 14:18:52 -0800 | [diff] [blame] | 59 | // signals_.async_wait(std::bind(&server::handle_stop, this)); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 60 | |
| 61 | // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). |
| 62 | boost::asio::ip::tcp::resolver resolver(io_service_); |
| 63 | boost::asio::ip::tcp::resolver::query query(address, port); |
| 64 | boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query); |
| 65 | acceptor_.open(endpoint.protocol()); |
| 66 | acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); |
| 67 | acceptor_.bind(endpoint); |
| 68 | acceptor_.listen(); |
| 69 | |
| 70 | start_accept(); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 71 | |
| 72 | _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame] | 75 | server::~server() |
| 76 | { |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 77 | // handle_stop(); |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 80 | void |
| 81 | server::run() |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 82 | { |
| 83 | // The io_service::run() call will block until all asynchronous operations |
| 84 | // have finished. While the server is running, there is always at least one |
| 85 | // asynchronous operation outstanding: the asynchronous accept call waiting |
| 86 | // for new incoming connections. |
| 87 | io_service_.run(); |
| 88 | } |
| 89 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 90 | void |
| 91 | server::start_accept() |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 92 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | new_connection_.reset(new connection(io_service_, connection_manager_, request_handler_)); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 94 | acceptor_.async_accept(new_connection_->socket(), |
Alexander Afanasyev | caf61b1 | 2016-12-25 14:18:52 -0800 | [diff] [blame] | 95 | std::bind(&server::handle_accept, this, std::placeholders::_1)); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 98 | void |
| 99 | server::handle_accept(const boost::system::error_code& e) |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 100 | { |
| 101 | // Check whether the server was stopped by a signal before this completion |
| 102 | // handler had a chance to run. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 103 | if (!acceptor_.is_open()) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 107 | if (!e) { |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 108 | connection_manager_.start(new_connection_); |
| 109 | } |
| 110 | |
| 111 | start_accept(); |
| 112 | } |
| 113 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 114 | void |
| 115 | server::handle_stop() |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 116 | { |
| 117 | // The server is stopped by cancelling all outstanding asynchronous |
| 118 | // operations. Once all operations have finished the io_service::run() call |
| 119 | // will exit. |
| 120 | acceptor_.close(); |
| 121 | connection_manager_.stop_all(); |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 122 | // although they say io_service::run() would stop, but it didn't happen.. |
| 123 | // the thread join was blocking, waiting for io_service::run() to finish |
| 124 | // even after handle_stop() call.. |
| 125 | // so just force it quit, no harm here. |
| 126 | io_service_.stop(); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | } // namespace server |
| 130 | } // namespace http |