blob: 9ffa4eb8b56a9e0854095cf9723e0a49300da85f [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.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 Afanasyeveda3b7a2016-12-25 11:26:40 -080031#include "logging.h"
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080032#include <boost/bind.hpp>
33#include <signal.h>
34
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080035INIT_LOGGER("HttpServer");
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080036namespace http {
37namespace server {
38
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039server::server(const std::string& address, const std::string& port, const std::string& doc_root)
40 : io_service_()
41 ,
42 // signals_(io_service_),
43 acceptor_(io_service_)
44 , connection_manager_()
45 , new_connection_()
46 , request_handler_(doc_root)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080047{
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 Afanasyeveda3b7a2016-12-25 11:26:40 -080051 // 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 Zhua9a7e1d2013-02-25 18:29:07 -080057
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 Zhud9429222013-02-25 22:34:09 -080068
69 _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080070}
71
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080072server::~server()
73{
Zhenkai Zhu377cbeb2013-02-28 16:36:26 -080074 // handle_stop();
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080075}
76
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080077void
78server::run()
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080079{
80 // The io_service::run() call will block until all asynchronous operations
81 // have finished. While the server is running, there is always at least one
82 // asynchronous operation outstanding: the asynchronous accept call waiting
83 // for new incoming connections.
84 io_service_.run();
85}
86
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080087void
88server::start_accept()
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080089{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080090 new_connection_.reset(new connection(io_service_, connection_manager_, request_handler_));
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080091 acceptor_.async_accept(new_connection_->socket(),
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080092 boost::bind(&server::handle_accept, this, boost::asio::placeholders::error));
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080093}
94
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080095void
96server::handle_accept(const boost::system::error_code& e)
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080097{
98 // Check whether the server was stopped by a signal before this completion
99 // handler had a chance to run.
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 if (!acceptor_.is_open()) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800101 return;
102 }
103
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800104 if (!e) {
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800105 connection_manager_.start(new_connection_);
106 }
107
108 start_accept();
109}
110
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800111void
112server::handle_stop()
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -0800113{
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 Zhu377cbeb2013-02-28 16:36:26 -0800119 // 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 Zhua9a7e1d2013-02-25 18:29:07 -0800124}
125
126} // namespace server
127} // namespace http