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 | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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" |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 27 | #include "core/global-io.hpp" |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | |
| 31 | NFD_LOG_INIT("WebSocketFace"); |
| 32 | |
| 33 | WebSocketFace::WebSocketFace(const FaceUri& remoteUri, const FaceUri& localUri, |
| 34 | websocketpp::connection_hdl hdl, |
| 35 | websocket::Server& server) |
| 36 | : Face(remoteUri, localUri) |
| 37 | , m_handle(hdl) |
| 38 | , m_server(server) |
| 39 | , m_closed(false) |
| 40 | { |
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 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 50 | this->emitSignal(onSendInterest, interest); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 51 | const Block& payload = interest.wireEncode(); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 52 | this->getMutableCounters().getNOutBytes() += payload.size(); |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 53 | |
| 54 | try { |
| 55 | m_server.send(m_handle, payload.wire(), payload.size(), |
| 56 | websocketpp::frame::opcode::binary); |
| 57 | } |
| 58 | catch (const websocketpp::lib::error_code& e) { |
| 59 | NFD_LOG_DEBUG("Failed to send Interest because: " << e |
| 60 | << "(" << e.message() << ")"); |
| 61 | } |
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 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 70 | this->emitSignal(onSendData, data); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 71 | const Block& payload = data.wireEncode(); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 72 | this->getMutableCounters().getNOutBytes() += payload.size(); |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 73 | |
| 74 | try { |
| 75 | m_server.send(m_handle, payload.wire(), payload.size(), |
| 76 | websocketpp::frame::opcode::binary); |
| 77 | } |
| 78 | catch (const websocketpp::lib::error_code& e) { |
| 79 | NFD_LOG_DEBUG("Failed to send Data because: " << e |
| 80 | << "(" << e.message() << ")"); |
| 81 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | WebSocketFace::close() |
| 86 | { |
| 87 | if (m_closed == false) |
| 88 | { |
| 89 | m_closed = true; |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 90 | scheduler::cancel(m_pingEventId); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 91 | websocketpp::lib::error_code ecode; |
| 92 | m_server.close(m_handle, websocketpp::close::status::normal, "closed by nfd", ecode); |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 93 | |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 94 | fail("Face closed"); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | WebSocketFace::handleReceive(const std::string& msg) |
| 100 | { |
| 101 | // Copy message into Face internal buffer |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 102 | if (msg.size() > ndn::MAX_NDN_PACKET_SIZE) |
Wentao Shang | 6990e4c | 2014-11-05 11:17:35 -0800 | [diff] [blame] | 103 | { |
| 104 | NFD_LOG_WARN("[id:" << this->getId() |
| 105 | << "] Received WebSocket message size [" |
| 106 | << msg.size() << "] is too big"); |
| 107 | return; |
| 108 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 109 | |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 110 | this->getMutableCounters().getNInBytes() += msg.size(); |
| 111 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 112 | // Try to parse message data |
| 113 | bool isOk = true; |
| 114 | Block element; |
| 115 | isOk = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size(), element); |
| 116 | if (!isOk) |
| 117 | { |
Wentao Shang | 6990e4c | 2014-11-05 11:17:35 -0800 | [diff] [blame] | 118 | NFD_LOG_WARN("[id:" << this->getId() |
| 119 | << "] Received invalid NDN packet of length [" |
| 120 | << msg.size() << "]"); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (!this->decodeAndDispatchInput(element)) |
| 125 | { |
| 126 | NFD_LOG_WARN("[id:" << this->getId() |
| 127 | << "] Received unrecognized block of type [" |
| 128 | << element.type() << "]"); |
| 129 | // ignore unknown packet and proceed |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } // namespace nfd |