blob: f52b4481b1587e2c0c74331328bafeb0ae93bbef [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeva9369b42017-01-11 11:58:00 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08004 *
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.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 Afanasyevcaf61b12016-12-25 14:18:52 -080031#include "core/logging.hpp"
32
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080033#include <signal.h>
34
35namespace http {
36namespace server {
37
Alexander Afanasyevcaf61b12016-12-25 14:18:52 -080038using namespace ndn::chronoshare;
39
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080040_LOG_INIT(HttpServer);
Alexander Afanasyevcaf61b12016-12-25 14:18:52 -080041
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080042server::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 Zhua9a7e1d2013-02-25 18:29:07 -080050{
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 Afanasyeveda3b7a2016-12-25 11:26:40 -080054 // signals_.add(SIGINT);
55 // signals_.add(SIGTERM);
56 //#if defined(SIGQUIT)
57 // signals_.add(SIGQUIT);
58 //#endif // defined(SIGQUIT)
Alexander Afanasyevcaf61b12016-12-25 14:18:52 -080059 // signals_.async_wait(std::bind(&server::handle_stop, this));
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080060
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 Zhud9429222013-02-25 22:34:09 -080071
72 _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080073}
74
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080075server::~server()
76{
Zhenkai Zhu377cbeb2013-02-28 16:36:26 -080077 // handle_stop();
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080078}
79
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080080void
81server::run()
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080082{
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 Afanasyeveda3b7a2016-12-25 11:26:40 -080090void
91server::start_accept()
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080092{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080093 new_connection_.reset(new connection(io_service_, connection_manager_, request_handler_));
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080094 acceptor_.async_accept(new_connection_->socket(),
Alexander Afanasyevcaf61b12016-12-25 14:18:52 -080095 std::bind(&server::handle_accept, this, std::placeholders::_1));
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080096}
97
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080098void
99server::handle_accept(const boost::system::error_code& e)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800100{
101 // Check whether the server was stopped by a signal before this completion
102 // handler had a chance to run.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 if (!acceptor_.is_open()) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800104 return;
105 }
106
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 if (!e) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800108 connection_manager_.start(new_connection_);
109 }
110
111 start_accept();
112}
113
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800114void
115server::handle_stop()
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800116{
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 Zhu377cbeb2013-02-28 16:36:26 -0800122 // 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 Zhua9a7e1d2013-02-25 18:29:07 -0800127}
128
129} // namespace server
130} // namespace http