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