blob: 2ee41cd243f1e24092d36a2185d8e118564497ea [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento15b55052018-01-27 19:09:28 -05002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Wentao Shang53df1632014-04-21 12:01:32 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shia1937bf2014-11-06 11:43:40 -070024 */
Wentao Shang53df1632014-04-21 12:01:32 -070025
26#include "websocket-channel.hpp"
Davide Pesaventocb425e82019-07-14 21:48:22 -040027#include "face.hpp"
Yukai Tu2d6d5632015-10-26 11:06:02 -070028#include "generic-link-service.hpp"
29#include "websocket-transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040030#include "common/global.hpp"
Wentao Shang98733142014-09-17 12:13:57 -070031
Wentao Shang53df1632014-04-21 12:01:32 -070032namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040033namespace face {
Wentao Shang53df1632014-04-21 12:01:32 -070034
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(WebSocketChannel);
Wentao Shang53df1632014-04-21 12:01:32 -070036
Wentao Shang53df1632014-04-21 12:01:32 -070037WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint)
38 : m_localEndpoint(localEndpoint)
Davide Pesavento15b55052018-01-27 19:09:28 -050039 , m_pingInterval(10_s)
Wentao Shang53df1632014-04-21 12:01:32 -070040{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010041 setUri(FaceUri(m_localEndpoint, "ws"));
Davide Pesavento77911cc2017-04-08 22:12:30 -040042 NFD_LOG_CHAN_INFO("Creating channel");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010043
Yukai Tu2d6d5632015-10-26 11:06:02 -070044 // Be quiet
Wentao Shang53df1632014-04-21 12:01:32 -070045 m_server.clear_access_channels(websocketpp::log::alevel::all);
Yukai Tu2d6d5632015-10-26 11:06:02 -070046 m_server.clear_error_channels(websocketpp::log::elevel::all);
Wentao Shang53df1632014-04-21 12:01:32 -070047
Yukai Tu2d6d5632015-10-26 11:06:02 -070048 // Setup WebSocket server
49 m_server.init_asio(&getGlobalIoService());
Davide Pesavento096f86a2018-11-10 19:51:45 -050050 m_server.set_tcp_pre_bind_handler([isV6 = m_localEndpoint.address().is_v6()] (const auto& acceptor) {
51 if (isV6) {
52 acceptor->set_option(boost::asio::ip::v6_only(true));
53 }
54 return websocketpp::lib::error_code{};
55 });
Davide Pesavento412c9822021-07-02 00:21:05 -040056 m_server.set_open_handler(std::bind(&WebSocketChannel::handleOpen, this, _1));
57 m_server.set_close_handler(std::bind(&WebSocketChannel::handleClose, this, _1));
58 m_server.set_message_handler(std::bind(&WebSocketChannel::handleMessage, this, _1, _2));
Davide Pesavento6ad890a2015-03-09 03:43:17 +010059
60 // Detect disconnections using ping-pong messages
Davide Pesavento412c9822021-07-02 00:21:05 -040061 m_server.set_pong_handler(std::bind(&WebSocketChannel::handlePong, this, _1));
62 m_server.set_pong_timeout_handler(std::bind(&WebSocketChannel::handlePongTimeout, this, _1));
Davide Pesavento6ad890a2015-03-09 03:43:17 +010063
Wentao Shang93ef6c92014-06-19 11:59:17 -040064 // Always set SO_REUSEADDR flag
65 m_server.set_reuse_addr(true);
Wentao Shang53df1632014-04-21 12:01:32 -070066}
67
Davide Pesavento6ad890a2015-03-09 03:43:17 +010068void
69WebSocketChannel::setPingInterval(time::milliseconds interval)
Wentao Shang53df1632014-04-21 12:01:32 -070070{
Yukai Tu2d6d5632015-10-26 11:06:02 -070071 BOOST_ASSERT(!m_server.is_listening());
72
Davide Pesavento6ad890a2015-03-09 03:43:17 +010073 m_pingInterval = interval;
Wentao Shang53df1632014-04-21 12:01:32 -070074}
75
76void
Wentao Shang98733142014-09-17 12:13:57 -070077WebSocketChannel::setPongTimeout(time::milliseconds timeout)
78{
Yukai Tu2d6d5632015-10-26 11:06:02 -070079 BOOST_ASSERT(!m_server.is_listening());
80
Wentao Shang98733142014-09-17 12:13:57 -070081 m_server.set_pong_timeout(static_cast<long>(timeout.count()));
82}
83
84void
Yukai Tu2d6d5632015-10-26 11:06:02 -070085WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl)
Wentao Shang98733142014-09-17 12:13:57 -070086{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010087 auto it = m_channelFaces.find(hdl);
88 if (it != m_channelFaces.end()) {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040089 static_cast<WebSocketTransport*>(it->second->getTransport())->handlePongTimeout();
Yukai Tu2d6d5632015-10-26 11:06:02 -070090 }
91 else {
Davide Pesavento77911cc2017-04-08 22:12:30 -040092 NFD_LOG_CHAN_WARN("Pong timeout on unknown transport");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010093 }
Wentao Shang98733142014-09-17 12:13:57 -070094}
95
96void
Yukai Tu2d6d5632015-10-26 11:06:02 -070097WebSocketChannel::handlePong(websocketpp::connection_hdl hdl)
Wentao Shang98733142014-09-17 12:13:57 -070098{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010099 auto it = m_channelFaces.find(hdl);
100 if (it != m_channelFaces.end()) {
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400101 static_cast<WebSocketTransport*>(it->second->getTransport())->handlePong();
Yukai Tu2d6d5632015-10-26 11:06:02 -0700102 }
103 else {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400104 NFD_LOG_CHAN_WARN("Pong received on unknown transport");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 }
Wentao Shang98733142014-09-17 12:13:57 -0700106}
107
108void
Wentao Shang53df1632014-04-21 12:01:32 -0700109WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
110 websocket::Server::message_ptr msg)
111{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100112 auto it = m_channelFaces.find(hdl);
113 if (it != m_channelFaces.end()) {
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400114 static_cast<WebSocketTransport*>(it->second->getTransport())->receiveMessage(msg->get_payload());
Yukai Tu2d6d5632015-10-26 11:06:02 -0700115 }
116 else {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400117 NFD_LOG_CHAN_WARN("Message received on unknown transport");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100118 }
Wentao Shang53df1632014-04-21 12:01:32 -0700119}
120
121void
122WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
123{
Davide Pesavento77911cc2017-04-08 22:12:30 -0400124 NFD_LOG_CHAN_TRACE("Incoming connection from " << m_server.get_con_from_hdl(hdl)->get_remote_endpoint());
125
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400126 auto linkService = make_unique<GenericLinkService>();
Alexander Afanasyeva6bd7da2019-04-25 17:26:11 -0400127 auto transport = make_unique<WebSocketTransport>(hdl, m_server, m_pingInterval);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700128 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400129 face->setChannel(weak_from_this());
Wentao Shang98733142014-09-17 12:13:57 -0700130
Davide Pesavento77911cc2017-04-08 22:12:30 -0400131 BOOST_ASSERT(m_channelFaces.count(hdl) == 0);
Yukai Tu2d6d5632015-10-26 11:06:02 -0700132 m_channelFaces[hdl] = face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700133 connectFaceClosedSignal(*face, [this, hdl] { m_channelFaces.erase(hdl); });
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100134
Yukai Tu2d6d5632015-10-26 11:06:02 -0700135 m_onFaceCreatedCallback(face);
Wentao Shang53df1632014-04-21 12:01:32 -0700136}
137
138void
139WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
140{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100141 auto it = m_channelFaces.find(hdl);
142 if (it != m_channelFaces.end()) {
Junxiao Shicde37ad2015-12-24 01:02:05 -0700143 it->second->close();
Yukai Tu2d6d5632015-10-26 11:06:02 -0700144 }
145 else {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400146 NFD_LOG_CHAN_WARN("Close on unknown transport");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100147 }
Wentao Shang53df1632014-04-21 12:01:32 -0700148}
149
Wentao Shang53df1632014-04-21 12:01:32 -0700150void
151WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated)
152{
Davide Pesavento77911cc2017-04-08 22:12:30 -0400153 if (isListening()) {
154 NFD_LOG_CHAN_WARN("Already listening");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100155 return;
156 }
Wentao Shang53df1632014-04-21 12:01:32 -0700157
158 m_onFaceCreatedCallback = onFaceCreated;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400159
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100160 m_server.listen(m_localEndpoint);
Wentao Shang53df1632014-04-21 12:01:32 -0700161 m_server.start_accept();
Davide Pesavento77911cc2017-04-08 22:12:30 -0400162 NFD_LOG_CHAN_DEBUG("Started listening");
Wentao Shang53df1632014-04-21 12:01:32 -0700163}
164
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400165} // namespace face
Wentao Shang53df1632014-04-21 12:01:32 -0700166} // namespace nfd