blob: 0fc7747afea736d484166577c5727a11d6e99a0c [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"
31#include <boost/bind.hpp>
32#include <signal.h>
Zhenkai Zhud9429222013-02-25 22:34:09 -080033#include "logging.h"
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080034
Zhenkai Zhud9429222013-02-25 22:34:09 -080035INIT_LOGGER ("HttpServer");
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080036namespace http {
37namespace server {
38
39server::server(const std::string& address, const std::string& port,
40 const std::string& doc_root)
41 : io_service_(),
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -070042 // signals_(io_service_),
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080043 acceptor_(io_service_),
44 connection_manager_(),
45 new_connection_(),
46 request_handler_(doc_root)
47{
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 Afanasyevf8ff5e12013-07-11 13:57:32 -070051// 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
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080077void server::run()
78{
79 // The io_service::run() call will block until all asynchronous operations
80 // have finished. While the server is running, there is always at least one
81 // asynchronous operation outstanding: the asynchronous accept call waiting
82 // for new incoming connections.
83 io_service_.run();
84}
85
86void server::start_accept()
87{
88 new_connection_.reset(new connection(io_service_,
89 connection_manager_, request_handler_));
90 acceptor_.async_accept(new_connection_->socket(),
91 boost::bind(&server::handle_accept, this,
92 boost::asio::placeholders::error));
93}
94
95void server::handle_accept(const boost::system::error_code& e)
96{
97 // Check whether the server was stopped by a signal before this completion
98 // handler had a chance to run.
99 if (!acceptor_.is_open())
100 {
101 return;
102 }
103
104 if (!e)
105 {
106 connection_manager_.start(new_connection_);
107 }
108
109 start_accept();
110}
111
112void server::handle_stop()
113{
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