Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 4 | * 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 Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 24 | */ |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 25 | |
| 26 | #include "websocket-channel.hpp" |
Davide Pesavento | cb425e8 | 2019-07-14 21:48:22 -0400 | [diff] [blame] | 27 | #include "face.hpp" |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 28 | #include "generic-link-service.hpp" |
| 29 | #include "websocket-transport.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 30 | #include "common/global.hpp" |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 31 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 32 | namespace nfd { |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 33 | namespace face { |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 35 | NFD_LOG_INIT(WebSocketChannel); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 36 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 37 | WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint) |
| 38 | : m_localEndpoint(localEndpoint) |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 39 | , m_pingInterval(10_s) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 41 | setUri(FaceUri(m_localEndpoint, "ws")); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 42 | NFD_LOG_CHAN_INFO("Creating channel"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 43 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 44 | // Be quiet |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 45 | m_server.clear_access_channels(websocketpp::log::alevel::all); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 46 | m_server.clear_error_channels(websocketpp::log::elevel::all); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 47 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 48 | // Setup WebSocket server |
| 49 | m_server.init_asio(&getGlobalIoService()); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 50 | 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 Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 56 | 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 Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 59 | |
| 60 | // Detect disconnections using ping-pong messages |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 61 | 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 Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 63 | |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 64 | // Always set SO_REUSEADDR flag |
| 65 | m_server.set_reuse_addr(true); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 68 | void |
| 69 | WebSocketChannel::setPingInterval(time::milliseconds interval) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 70 | { |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 71 | BOOST_ASSERT(!m_server.is_listening()); |
| 72 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 73 | m_pingInterval = interval; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 77 | WebSocketChannel::setPongTimeout(time::milliseconds timeout) |
| 78 | { |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 79 | BOOST_ASSERT(!m_server.is_listening()); |
| 80 | |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 81 | m_server.set_pong_timeout(static_cast<long>(timeout.count())); |
| 82 | } |
| 83 | |
| 84 | void |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 85 | WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl) |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 86 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 87 | auto it = m_channelFaces.find(hdl); |
| 88 | if (it != m_channelFaces.end()) { |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 89 | static_cast<WebSocketTransport*>(it->second->getTransport())->handlePongTimeout(); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 90 | } |
| 91 | else { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 92 | NFD_LOG_CHAN_WARN("Pong timeout on unknown transport"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 93 | } |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 97 | WebSocketChannel::handlePong(websocketpp::connection_hdl hdl) |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 98 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 99 | auto it = m_channelFaces.find(hdl); |
| 100 | if (it != m_channelFaces.end()) { |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 101 | static_cast<WebSocketTransport*>(it->second->getTransport())->handlePong(); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 102 | } |
| 103 | else { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 104 | NFD_LOG_CHAN_WARN("Pong received on unknown transport"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 105 | } |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 109 | WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl, |
| 110 | websocket::Server::message_ptr msg) |
| 111 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 112 | auto it = m_channelFaces.find(hdl); |
| 113 | if (it != m_channelFaces.end()) { |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 114 | static_cast<WebSocketTransport*>(it->second->getTransport())->receiveMessage(msg->get_payload()); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 115 | } |
| 116 | else { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 117 | NFD_LOG_CHAN_WARN("Message received on unknown transport"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 118 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
| 122 | WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl) |
| 123 | { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 124 | NFD_LOG_CHAN_TRACE("Incoming connection from " << m_server.get_con_from_hdl(hdl)->get_remote_endpoint()); |
| 125 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 126 | auto linkService = make_unique<GenericLinkService>(); |
Alexander Afanasyev | a6bd7da | 2019-04-25 17:26:11 -0400 | [diff] [blame] | 127 | auto transport = make_unique<WebSocketTransport>(hdl, m_server, m_pingInterval); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 128 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 129 | face->setChannel(weak_from_this()); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 130 | |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 131 | BOOST_ASSERT(m_channelFaces.count(hdl) == 0); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 132 | m_channelFaces[hdl] = face; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 133 | connectFaceClosedSignal(*face, [this, hdl] { m_channelFaces.erase(hdl); }); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 134 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 135 | m_onFaceCreatedCallback(face); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void |
| 139 | WebSocketChannel::handleClose(websocketpp::connection_hdl hdl) |
| 140 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 141 | auto it = m_channelFaces.find(hdl); |
| 142 | if (it != m_channelFaces.end()) { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 143 | it->second->close(); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 144 | } |
| 145 | else { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 146 | NFD_LOG_CHAN_WARN("Close on unknown transport"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 147 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 150 | void |
| 151 | WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated) |
| 152 | { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 153 | if (isListening()) { |
| 154 | NFD_LOG_CHAN_WARN("Already listening"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 155 | return; |
| 156 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 157 | |
| 158 | m_onFaceCreatedCallback = onFaceCreated; |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 159 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 160 | m_server.listen(m_localEndpoint); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 161 | m_server.start_accept(); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 162 | NFD_LOG_CHAN_DEBUG("Started listening"); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 165 | } // namespace face |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 166 | } // namespace nfd |