blob: d5bafd68729065ae45cb9b18ae1682204545a8f6 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- 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 Zhua9a7e1d2013-02-25 18:29:07 -080020//
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 Zhua9a7e1d2013-02-25 18:29:07 -080033#include "connection.hpp"
34#include "connection_manager.hpp"
35#include "request_handler.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080036#include <boost/asio.hpp>
37#include <boost/noncopyable.hpp>
38#include <string>
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080039
40namespace http {
41namespace server {
42
43/// The top-level class of the HTTP server.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080044class server : private boost::noncopyable
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080045{
46public:
47 /// Construct the server to listen on the specified TCP address and port, and
48 /// serve up files from the given directory.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080049 explicit server(const std::string& address, const std::string& port, const std::string& doc_root);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080050
Zhenkai Zhu377cbeb2013-02-28 16:36:26 -080051 ~server();
52
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080053 /// Run the server's io_service loop.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 void
55 run();
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080056
Zhenkai Zhu907cbaf2013-02-27 22:29:23 -080057 /// Handle a request to stop the server.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 void
59 handle_stop();
Zhenkai Zhu907cbaf2013-02-27 22:29:23 -080060
Zhenkai Zhu377cbeb2013-02-28 16:36:26 -080061private:
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080062 /// Initiate an asynchronous accept operation.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080063 void
64 start_accept();
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080065
66 /// Handle completion of an asynchronous accept operation.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080067 void
68 handle_accept(const boost::system::error_code& e);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080069
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080070 /// 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 Afanasyevf8ff5e12013-07-11 13:57:32 -070074 // boost::asio::signal_set signals_;
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080075
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