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 | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 24 | */ |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 25 | |
| 26 | #include "websocket-face.hpp" |
| 27 | |
| 28 | namespace nfd { |
| 29 | |
| 30 | NFD_LOG_INIT("WebSocketFace"); |
| 31 | |
| 32 | WebSocketFace::WebSocketFace(const FaceUri& remoteUri, const FaceUri& localUri, |
| 33 | websocketpp::connection_hdl hdl, |
| 34 | websocket::Server& server) |
| 35 | : Face(remoteUri, localUri) |
| 36 | , m_handle(hdl) |
| 37 | , m_server(server) |
| 38 | , m_closed(false) |
| 39 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 40 | NFD_LOG_FACE_INFO("Creating face"); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 41 | this->setOnDemand(true); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 44 | void |
| 45 | WebSocketFace::sendInterest(const Interest& interest) |
| 46 | { |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 47 | if (m_closed) |
| 48 | return; |
| 49 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 50 | NFD_LOG_FACE_TRACE(__func__); |
| 51 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 52 | this->emitSignal(onSendInterest, interest); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 53 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 54 | const Block& payload = interest.wireEncode(); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 55 | this->getMutableCounters().getNOutBytes() += payload.size(); |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 56 | |
Wentao Shang | d397d77 | 2015-04-29 12:09:31 -0700 | [diff] [blame] | 57 | websocketpp::lib::error_code ec; |
| 58 | m_server.send(m_handle, payload.wire(), payload.size(), |
| 59 | websocketpp::frame::opcode::binary, ec); |
| 60 | if (ec) |
| 61 | NFD_LOG_FACE_WARN("Failed to send Interest: " << ec.message()); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
| 65 | WebSocketFace::sendData(const Data& data) |
| 66 | { |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 67 | if (m_closed) |
| 68 | return; |
| 69 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 70 | NFD_LOG_FACE_TRACE(__func__); |
| 71 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 72 | this->emitSignal(onSendData, data); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 73 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 74 | const Block& payload = data.wireEncode(); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 75 | this->getMutableCounters().getNOutBytes() += payload.size(); |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 76 | |
Wentao Shang | d397d77 | 2015-04-29 12:09:31 -0700 | [diff] [blame] | 77 | websocketpp::lib::error_code ec; |
| 78 | m_server.send(m_handle, payload.wire(), payload.size(), |
| 79 | websocketpp::frame::opcode::binary, ec); |
| 80 | if (ec) |
| 81 | NFD_LOG_FACE_WARN("Failed to send Data: " << ec.message()); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | WebSocketFace::close() |
| 86 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 87 | if (m_closed) |
| 88 | return; |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 89 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 90 | NFD_LOG_FACE_INFO("Closing face"); |
| 91 | |
| 92 | m_closed = true; |
| 93 | scheduler::cancel(m_pingEventId); |
Wentao Shang | d397d77 | 2015-04-29 12:09:31 -0700 | [diff] [blame] | 94 | websocketpp::lib::error_code ec; |
| 95 | m_server.close(m_handle, websocketpp::close::status::normal, "closed by nfd", ec); |
| 96 | // ignore error on close |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 97 | fail("Face closed"); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | WebSocketFace::handleReceive(const std::string& msg) |
| 102 | { |
| 103 | // Copy message into Face internal buffer |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 104 | if (msg.size() > ndn::MAX_NDN_PACKET_SIZE) |
Wentao Shang | 6990e4c | 2014-11-05 11:17:35 -0800 | [diff] [blame] | 105 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 106 | NFD_LOG_FACE_WARN("Received WebSocket message is too big (" << msg.size() << " bytes)"); |
Wentao Shang | 6990e4c | 2014-11-05 11:17:35 -0800 | [diff] [blame] | 107 | return; |
| 108 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 109 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 110 | NFD_LOG_FACE_TRACE("Received: " << msg.size() << " bytes"); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 111 | this->getMutableCounters().getNInBytes() += msg.size(); |
| 112 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 113 | // Try to parse message data |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 114 | bool isOk = false; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 115 | Block element; |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 116 | std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), |
| 117 | msg.size()); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 118 | if (!isOk) |
| 119 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 120 | NFD_LOG_FACE_WARN("Received block is invalid or too large to process"); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (!this->decodeAndDispatchInput(element)) |
| 125 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 126 | NFD_LOG_FACE_WARN("Received unrecognized TLV block of type " << element.type()); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 127 | // ignore unknown packet and proceed |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | } // namespace nfd |