blob: cf3da37229e8d1067801fb0388f7127786d72bdb [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
Davide Pesavento8728a252015-11-06 04:01:22 +010059void
60WebSocketTransport::beforeChangePersistency(ndn::nfd::FacePersistency newPersistency)
Yukai Tu2d6d5632015-10-26 11:06:02 -070061{
62 if (newPersistency != ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
63 BOOST_THROW_EXCEPTION(
64 std::invalid_argument("WebSocketTransport supports only FACE_PERSISTENCY_ON_DEMAND"));
65 }
66}
67
68void
69WebSocketTransport::doSend(Transport::Packet&& packet)
70{
71 NFD_LOG_FACE_TRACE(__func__);
72
73 websocketpp::lib::error_code error;
74 m_server.send(m_handle, packet.packet.wire(), packet.packet.size(),
75 websocketpp::frame::opcode::binary, error);
76 if (error)
77 return processErrorCode(error);
78
79 NFD_LOG_FACE_TRACE("Successfully sent: " << packet.packet.size() << " bytes");
80}
81
82void
83WebSocketTransport::receiveMessage(const std::string& msg)
84{
85 NFD_LOG_FACE_TRACE("Received: " << msg.size() << " bytes");
86
87 bool isOk = false;
88 Block element;
89 std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size());
90 if (!isOk) {
91 NFD_LOG_FACE_WARN("Failed to parse message payload");
92 return;
93 }
94
95 this->receive(Transport::Packet(std::move(element)));
96}
97
98void
99WebSocketTransport::schedulePing()
100{
101 m_pingEventId = scheduler::schedule(m_pingInterval, bind(&WebSocketTransport::sendPing, this));
102}
103
104void
105WebSocketTransport::sendPing()
106{
107 NFD_LOG_FACE_TRACE(__func__);
108
109 websocketpp::lib::error_code error;
110 m_server.ping(m_handle, "NFD-WebSocket", error);
111 if (error)
112 return processErrorCode(error);
113
114 this->schedulePing();
115}
116
117void
118WebSocketTransport::handlePong()
119{
120 NFD_LOG_FACE_TRACE(__func__);
121}
122
123void
124WebSocketTransport::handlePongTimeout()
125{
126 NFD_LOG_FACE_WARN(__func__);
127 this->setState(TransportState::FAILED);
128 doClose();
129}
130
131void
132WebSocketTransport::processErrorCode(const websocketpp::lib::error_code& error)
133{
134 NFD_LOG_FACE_TRACE(__func__);
135
136 if (getState() == TransportState::CLOSING ||
137 getState() == TransportState::FAILED ||
138 getState() == TransportState::CLOSED)
139 // transport is shutting down, ignore any errors
140 return;
141
142 NFD_LOG_FACE_WARN("Send or ping operation failed: " << error.message());
143
144 this->setState(TransportState::FAILED);
145 doClose();
146}
147
148void
149WebSocketTransport::doClose()
150{
151 NFD_LOG_FACE_TRACE(__func__);
152
153 m_pingEventId.cancel();
154
155 // use the non-throwing variant and ignore errors, if any
156 websocketpp::lib::error_code error;
157 m_server.close(m_handle, websocketpp::close::status::normal, "closed by NFD", error);
158
159 this->setState(TransportState::CLOSED);
160}
161
162} // namespace face
163} // namespace nfd