blob: 4fbed812774d68edb97215c4edda0052b9c88f45 [file] [log] [blame]
Yukai Tu2d6d5632015-10-26 11:06:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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.
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-transport.hpp"
27
28namespace nfd {
29namespace face {
30
31NFD_LOG_INIT("WebSocketTransport");
32
33WebSocketTransport::WebSocketTransport(websocketpp::connection_hdl hdl,
34 websocket::Server& server,
35 time::milliseconds pingInterval)
36 : m_handle(hdl)
37 , m_server(server)
38 , m_pingInterval(pingInterval)
39{
40 const auto& sock = m_server.get_con_from_hdl(hdl)->get_socket();
41 this->setLocalUri(FaceUri(sock.local_endpoint(), "ws"));
42 this->setRemoteUri(FaceUri(sock.remote_endpoint(), "wsclient"));
43
44 if (sock.local_endpoint().address().is_loopback() &&
45 sock.remote_endpoint().address().is_loopback())
46 this->setScope(ndn::nfd::FACE_SCOPE_LOCAL);
47 else
48 this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
49
50 this->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
51 this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
52 this->setMtu(MTU_UNLIMITED);
53
54 this->schedulePing();
55
56 NFD_LOG_FACE_INFO("Creating transport");
57}
58
59void WebSocketTransport::beforeChangePersistency(ndn::nfd::FacePersistency newPersistency)
60{
61 if (newPersistency != ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
62 BOOST_THROW_EXCEPTION(
63 std::invalid_argument("WebSocketTransport supports only FACE_PERSISTENCY_ON_DEMAND"));
64 }
65}
66
67void
68WebSocketTransport::doSend(Transport::Packet&& packet)
69{
70 NFD_LOG_FACE_TRACE(__func__);
71
72 websocketpp::lib::error_code error;
73 m_server.send(m_handle, packet.packet.wire(), packet.packet.size(),
74 websocketpp::frame::opcode::binary, error);
75 if (error)
76 return processErrorCode(error);
77
78 NFD_LOG_FACE_TRACE("Successfully sent: " << packet.packet.size() << " bytes");
79}
80
81void
82WebSocketTransport::receiveMessage(const std::string& msg)
83{
84 NFD_LOG_FACE_TRACE("Received: " << msg.size() << " bytes");
85
86 bool isOk = false;
87 Block element;
88 std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size());
89 if (!isOk) {
90 NFD_LOG_FACE_WARN("Failed to parse message payload");
91 return;
92 }
93
94 this->receive(Transport::Packet(std::move(element)));
95}
96
97void
98WebSocketTransport::schedulePing()
99{
100 m_pingEventId = scheduler::schedule(m_pingInterval, bind(&WebSocketTransport::sendPing, this));
101}
102
103void
104WebSocketTransport::sendPing()
105{
106 NFD_LOG_FACE_TRACE(__func__);
107
108 websocketpp::lib::error_code error;
109 m_server.ping(m_handle, "NFD-WebSocket", error);
110 if (error)
111 return processErrorCode(error);
112
113 this->schedulePing();
114}
115
116void
117WebSocketTransport::handlePong()
118{
119 NFD_LOG_FACE_TRACE(__func__);
120}
121
122void
123WebSocketTransport::handlePongTimeout()
124{
125 NFD_LOG_FACE_WARN(__func__);
126 this->setState(TransportState::FAILED);
127 doClose();
128}
129
130void
131WebSocketTransport::processErrorCode(const websocketpp::lib::error_code& error)
132{
133 NFD_LOG_FACE_TRACE(__func__);
134
135 if (getState() == TransportState::CLOSING ||
136 getState() == TransportState::FAILED ||
137 getState() == TransportState::CLOSED)
138 // transport is shutting down, ignore any errors
139 return;
140
141 NFD_LOG_FACE_WARN("Send or ping operation failed: " << error.message());
142
143 this->setState(TransportState::FAILED);
144 doClose();
145}
146
147void
148WebSocketTransport::doClose()
149{
150 NFD_LOG_FACE_TRACE(__func__);
151
152 m_pingEventId.cancel();
153
154 // use the non-throwing variant and ignore errors, if any
155 websocketpp::lib::error_code error;
156 m_server.close(m_handle, websocketpp::close::status::normal, "closed by NFD", error);
157
158 this->setState(TransportState::CLOSED);
159}
160
161} // namespace face
162} // namespace nfd