blob: 5dfe5169fd356180a2ccf5de7ee0fe6b32a54372 [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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
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
28namespace nfd {
29
30NFD_LOG_INIT("WebSocketFace");
31
32WebSocketFace::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
43void
44WebSocketFace::sendInterest(const Interest& interest)
45{
46 this->onSendInterest(interest);
47 const Block& payload = interest.wireEncode();
48 m_server.send(m_handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
49}
50
51void
52WebSocketFace::sendData(const Data& data)
53{
54 this->onSendData(data);
55 const Block& payload = data.wireEncode();
56 m_server.send(m_handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
57}
58
59void
60WebSocketFace::close()
61{
62 if (m_closed == false)
63 {
64 m_closed = true;
65 websocketpp::lib::error_code ecode;
66 m_server.close(m_handle, websocketpp::close::status::normal, "closed by nfd", ecode);
Alexander Afanasyev251f3c12014-06-02 18:39:58 +030067
68 onFail("Face closed");
Wentao Shang53df1632014-04-21 12:01:32 -070069 }
70}
71
72void
73WebSocketFace::handleReceive(const std::string& msg)
74{
75 // Copy message into Face internal buffer
76 BOOST_ASSERT(msg.size() <= MAX_NDN_PACKET_SIZE);
77
78 // Try to parse message data
79 bool isOk = true;
80 Block element;
81 isOk = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size(), element);
82 if (!isOk)
83 {
84 NFD_LOG_TRACE("[id:" << this->getId()
85 << "] Received invalid NDN packet of length ["
86 << msg.size() << "]");
87 return;
88 }
89
90 if (!this->decodeAndDispatchInput(element))
91 {
92 NFD_LOG_WARN("[id:" << this->getId()
93 << "] Received unrecognized block of type ["
94 << element.type() << "]");
95 // ignore unknown packet and proceed
96 }
97}
98
99} // namespace nfd