Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "websocket-transport.hpp" |
| 27 | |
| 28 | namespace nfd { |
| 29 | namespace face { |
| 30 | |
| 31 | NFD_LOG_INIT("WebSocketTransport"); |
| 32 | |
| 33 | WebSocketTransport::WebSocketTransport(websocketpp::connection_hdl hdl, |
| 34 | websocket::Server& server, |
| 35 | time::milliseconds pingInterval) |
| 36 | : m_handle(hdl) |
| 37 | , m_server(server) |
| 38 | , m_pingInterval(pingInterval) |
| 39 | { |
| 40 | const auto& sock = m_server.get_con_from_hdl(hdl)->get_socket(); |
| 41 | this->setLocalUri(FaceUri(sock.local_endpoint(), "ws")); |
| 42 | this->setRemoteUri(FaceUri(sock.remote_endpoint(), "wsclient")); |
| 43 | |
| 44 | if (sock.local_endpoint().address().is_loopback() && |
| 45 | sock.remote_endpoint().address().is_loopback()) |
| 46 | this->setScope(ndn::nfd::FACE_SCOPE_LOCAL); |
| 47 | else |
| 48 | this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 49 | |
| 50 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 51 | this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 52 | this->setMtu(MTU_UNLIMITED); |
| 53 | |
| 54 | this->schedulePing(); |
| 55 | |
| 56 | NFD_LOG_FACE_INFO("Creating transport"); |
| 57 | } |
| 58 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 59 | void |
| 60 | WebSocketTransport::beforeChangePersistency(ndn::nfd::FacePersistency newPersistency) |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 61 | { |
| 62 | if (newPersistency != ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) { |
| 63 | BOOST_THROW_EXCEPTION( |
| 64 | std::invalid_argument("WebSocketTransport supports only FACE_PERSISTENCY_ON_DEMAND")); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | WebSocketTransport::doSend(Transport::Packet&& packet) |
| 70 | { |
| 71 | NFD_LOG_FACE_TRACE(__func__); |
| 72 | |
| 73 | websocketpp::lib::error_code error; |
| 74 | m_server.send(m_handle, packet.packet.wire(), packet.packet.size(), |
| 75 | websocketpp::frame::opcode::binary, error); |
| 76 | if (error) |
| 77 | return processErrorCode(error); |
| 78 | |
| 79 | NFD_LOG_FACE_TRACE("Successfully sent: " << packet.packet.size() << " bytes"); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | WebSocketTransport::receiveMessage(const std::string& msg) |
| 84 | { |
| 85 | NFD_LOG_FACE_TRACE("Received: " << msg.size() << " bytes"); |
| 86 | |
| 87 | bool isOk = false; |
| 88 | Block element; |
| 89 | std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size()); |
| 90 | if (!isOk) { |
| 91 | NFD_LOG_FACE_WARN("Failed to parse message payload"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | this->receive(Transport::Packet(std::move(element))); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | WebSocketTransport::schedulePing() |
| 100 | { |
| 101 | m_pingEventId = scheduler::schedule(m_pingInterval, bind(&WebSocketTransport::sendPing, this)); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | WebSocketTransport::sendPing() |
| 106 | { |
| 107 | NFD_LOG_FACE_TRACE(__func__); |
| 108 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 109 | ++this->nOutPings; |
| 110 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 111 | websocketpp::lib::error_code error; |
| 112 | m_server.ping(m_handle, "NFD-WebSocket", error); |
| 113 | if (error) |
| 114 | return processErrorCode(error); |
| 115 | |
| 116 | this->schedulePing(); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | WebSocketTransport::handlePong() |
| 121 | { |
| 122 | NFD_LOG_FACE_TRACE(__func__); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 123 | |
| 124 | ++this->nInPongs; |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | WebSocketTransport::handlePongTimeout() |
| 129 | { |
| 130 | NFD_LOG_FACE_WARN(__func__); |
| 131 | this->setState(TransportState::FAILED); |
| 132 | doClose(); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | WebSocketTransport::processErrorCode(const websocketpp::lib::error_code& error) |
| 137 | { |
| 138 | NFD_LOG_FACE_TRACE(__func__); |
| 139 | |
| 140 | if (getState() == TransportState::CLOSING || |
| 141 | getState() == TransportState::FAILED || |
| 142 | getState() == TransportState::CLOSED) |
| 143 | // transport is shutting down, ignore any errors |
| 144 | return; |
| 145 | |
| 146 | NFD_LOG_FACE_WARN("Send or ping operation failed: " << error.message()); |
| 147 | |
| 148 | this->setState(TransportState::FAILED); |
| 149 | doClose(); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | WebSocketTransport::doClose() |
| 154 | { |
| 155 | NFD_LOG_FACE_TRACE(__func__); |
| 156 | |
| 157 | m_pingEventId.cancel(); |
| 158 | |
| 159 | // use the non-throwing variant and ignore errors, if any |
| 160 | websocketpp::lib::error_code error; |
| 161 | m_server.close(m_handle, websocketpp::close::status::normal, "closed by NFD", error); |
| 162 | |
| 163 | this->setState(TransportState::CLOSED); |
| 164 | } |
| 165 | |
| 166 | } // namespace face |
| 167 | } // namespace nfd |