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/>. |
| 24 | **/ |
| 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 | { |
| 40 | } |
| 41 | |
| 42 | |
| 43 | void |
| 44 | WebSocketFace::sendInterest(const Interest& interest) |
| 45 | { |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 46 | if (m_closed) |
| 47 | return; |
| 48 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 49 | this->onSendInterest(interest); |
| 50 | const Block& payload = interest.wireEncode(); |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 51 | |
| 52 | try { |
| 53 | m_server.send(m_handle, payload.wire(), payload.size(), |
| 54 | websocketpp::frame::opcode::binary); |
| 55 | } |
| 56 | catch (const websocketpp::lib::error_code& e) { |
| 57 | NFD_LOG_DEBUG("Failed to send Interest because: " << e |
| 58 | << "(" << e.message() << ")"); |
| 59 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void |
| 63 | WebSocketFace::sendData(const Data& data) |
| 64 | { |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 65 | if (m_closed) |
| 66 | return; |
| 67 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 68 | this->onSendData(data); |
| 69 | const Block& payload = data.wireEncode(); |
Wentao Shang | 3d36c2b | 2014-07-04 10:09:58 -0400 | [diff] [blame] | 70 | |
| 71 | try { |
| 72 | m_server.send(m_handle, payload.wire(), payload.size(), |
| 73 | websocketpp::frame::opcode::binary); |
| 74 | } |
| 75 | catch (const websocketpp::lib::error_code& e) { |
| 76 | NFD_LOG_DEBUG("Failed to send Data because: " << e |
| 77 | << "(" << e.message() << ")"); |
| 78 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
| 82 | WebSocketFace::close() |
| 83 | { |
| 84 | if (m_closed == false) |
| 85 | { |
| 86 | m_closed = true; |
| 87 | websocketpp::lib::error_code ecode; |
| 88 | 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] | 89 | |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 90 | fail("Face closed"); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | WebSocketFace::handleReceive(const std::string& msg) |
| 96 | { |
| 97 | // Copy message into Face internal buffer |
| 98 | BOOST_ASSERT(msg.size() <= MAX_NDN_PACKET_SIZE); |
| 99 | |
| 100 | // Try to parse message data |
| 101 | bool isOk = true; |
| 102 | Block element; |
| 103 | isOk = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size(), element); |
| 104 | if (!isOk) |
| 105 | { |
| 106 | NFD_LOG_TRACE("[id:" << this->getId() |
| 107 | << "] Received invalid NDN packet of length [" |
| 108 | << msg.size() << "]"); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if (!this->decodeAndDispatchInput(element)) |
| 113 | { |
| 114 | NFD_LOG_WARN("[id:" << this->getId() |
| 115 | << "] Received unrecognized block of type [" |
| 116 | << element.type() << "]"); |
| 117 | // ignore unknown packet and proceed |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | } // namespace nfd |