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.hpp |
| 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 | #ifndef HTTP_SERVER_HPP |
| 31 | #define HTTP_SERVER_HPP |
| 32 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 33 | #include "connection.hpp" |
| 34 | #include "connection_manager.hpp" |
| 35 | #include "request_handler.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 36 | #include <boost/asio.hpp> |
| 37 | #include <boost/noncopyable.hpp> |
| 38 | #include <string> |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 39 | |
| 40 | namespace http { |
| 41 | namespace server { |
| 42 | |
| 43 | /// The top-level class of the HTTP server. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 44 | class server : private boost::noncopyable |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 45 | { |
| 46 | public: |
| 47 | /// Construct the server to listen on the specified TCP address and port, and |
| 48 | /// serve up files from the given directory. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 49 | explicit server(const std::string& address, const std::string& port, const std::string& doc_root); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 50 | |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 51 | ~server(); |
| 52 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 53 | /// Run the server's io_service loop. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 54 | void |
| 55 | run(); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 56 | |
Zhenkai Zhu | 907cbaf | 2013-02-27 22:29:23 -0800 | [diff] [blame] | 57 | /// Handle a request to stop the server. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 58 | void |
| 59 | handle_stop(); |
Zhenkai Zhu | 907cbaf | 2013-02-27 22:29:23 -0800 | [diff] [blame] | 60 | |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 61 | private: |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 62 | /// Initiate an asynchronous accept operation. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 63 | void |
| 64 | start_accept(); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 65 | |
| 66 | /// Handle completion of an asynchronous accept operation. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 67 | void |
| 68 | handle_accept(const boost::system::error_code& e); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 69 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 70 | /// The io_service used to perform asynchronous operations. |
| 71 | boost::asio::io_service io_service_; |
| 72 | |
| 73 | /// The signal_set is used to register for process termination notifications. |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 74 | // boost::asio::signal_set signals_; |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 75 | |
| 76 | /// Acceptor used to listen for incoming connections. |
| 77 | boost::asio::ip::tcp::acceptor acceptor_; |
| 78 | |
| 79 | /// The connection manager which owns all live connections. |
| 80 | connection_manager connection_manager_; |
| 81 | |
| 82 | /// The next connection to be accepted. |
| 83 | connection_ptr new_connection_; |
| 84 | |
| 85 | /// The handler for all incoming requests. |
| 86 | request_handler request_handler_; |
| 87 | }; |
| 88 | |
| 89 | } // namespace server |
| 90 | } // namespace http |
| 91 | |
| 92 | #endif // HTTP_SERVER_HPP |