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 | |
| 57 | try { |
| 58 | m_server.send(m_handle, payload.wire(), payload.size(), |
| 59 | websocketpp::frame::opcode::binary); |
| 60 | } |
| 61 | catch (const websocketpp::lib::error_code& e) { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 62 | NFD_LOG_FACE_WARN("Failed to send Interest: " << e << " (" << e.message() << ")"); |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 63 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
| 67 | WebSocketFace::sendData(const Data& data) |
| 68 | { |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 69 | if (m_closed) |
| 70 | return; |
| 71 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 72 | NFD_LOG_FACE_TRACE(__func__); |
| 73 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 74 | this->emitSignal(onSendData, data); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 75 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 76 | const Block& payload = data.wireEncode(); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 77 | this->getMutableCounters().getNOutBytes() += payload.size(); |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 78 | |
| 79 | try { |
| 80 | m_server.send(m_handle, payload.wire(), payload.size(), |
| 81 | websocketpp::frame::opcode::binary); |
| 82 | } |
| 83 | catch (const websocketpp::lib::error_code& e) { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 84 | NFD_LOG_FACE_WARN("Failed to send Data: " << e << " (" << e.message() << ")"); |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 85 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void |
| 89 | WebSocketFace::close() |
| 90 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 91 | if (m_closed) |
| 92 | return; |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 93 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 94 | NFD_LOG_FACE_INFO("Closing face"); |
| 95 | |
| 96 | m_closed = true; |
| 97 | scheduler::cancel(m_pingEventId); |
| 98 | websocketpp::lib::error_code ecode; |
| 99 | m_server.close(m_handle, websocketpp::close::status::normal, "closed by nfd", ecode); |
| 100 | |
| 101 | fail("Face closed"); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
| 105 | WebSocketFace::handleReceive(const std::string& msg) |
| 106 | { |
| 107 | // Copy message into Face internal buffer |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 108 | if (msg.size() > ndn::MAX_NDN_PACKET_SIZE) |
Wentao Shang | 6990e4c | 2014-11-05 11:17:35 -0800 | [diff] [blame] | 109 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 110 | 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] | 111 | return; |
| 112 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 113 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 114 | NFD_LOG_FACE_TRACE("Received: " << msg.size() << " bytes"); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 115 | this->getMutableCounters().getNInBytes() += msg.size(); |
| 116 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 117 | // Try to parse message data |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 118 | bool isOk = false; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 119 | Block element; |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 120 | std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), |
| 121 | msg.size()); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 122 | if (!isOk) |
| 123 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 124 | 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] | 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (!this->decodeAndDispatchInput(element)) |
| 129 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 130 | NFD_LOG_FACE_WARN("Received unrecognized TLV block of type " << element.type()); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 131 | // ignore unknown packet and proceed |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } // namespace nfd |