blob: 7997b1e73a38e3f4a1407546a315577fd605ba46 [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * 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.
Wentao Shang53df1632014-04-21 12:01:32 -070010 *
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 Shia1937bf2014-11-06 11:43:40 -070024 */
Wentao Shang53df1632014-04-21 12:01:32 -070025
26#include "websocket-channel.hpp"
Davide Pesavento6ad890a2015-03-09 03:43:17 +010027#include "core/global-io.hpp"
28#include "core/scheduler.hpp"
Wentao Shang53df1632014-04-21 12:01:32 -070029
Wentao Shang98733142014-09-17 12:13:57 -070030#include <boost/date_time/posix_time/posix_time.hpp>
31
Wentao Shang53df1632014-04-21 12:01:32 -070032namespace nfd {
33
34NFD_LOG_INIT("WebSocketChannel");
35
Wentao Shang53df1632014-04-21 12:01:32 -070036WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint)
37 : m_localEndpoint(localEndpoint)
38 , m_isListening(false)
Wentao Shang98733142014-09-17 12:13:57 -070039 , m_pingInterval(10000)
Wentao Shang53df1632014-04-21 12:01:32 -070040{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010041 setUri(FaceUri(m_localEndpoint, "ws"));
42
Wentao Shang53df1632014-04-21 12:01:32 -070043 // Setup WebSocket server
44 m_server.clear_access_channels(websocketpp::log::alevel::all);
45 m_server.clear_error_channels(websocketpp::log::alevel::all);
46
47 m_server.set_message_handler(bind(&WebSocketChannel::handleMessage, this, _1, _2));
48 m_server.set_open_handler(bind(&WebSocketChannel::handleOpen, this, _1));
49 m_server.set_close_handler(bind(&WebSocketChannel::handleClose, this, _1));
50 m_server.init_asio(&getGlobalIoService());
Davide Pesavento6ad890a2015-03-09 03:43:17 +010051
52 // Detect disconnections using ping-pong messages
53 m_server.set_pong_handler(bind(&WebSocketChannel::handlePong, this, _1, _2));
54 m_server.set_pong_timeout_handler(bind(&WebSocketChannel::handlePongTimeout, this, _1, _2));
55
Wentao Shang93ef6c92014-06-19 11:59:17 -040056 // Always set SO_REUSEADDR flag
57 m_server.set_reuse_addr(true);
Wentao Shang53df1632014-04-21 12:01:32 -070058}
59
Davide Pesavento6ad890a2015-03-09 03:43:17 +010060void
61WebSocketChannel::setPingInterval(time::milliseconds interval)
Wentao Shang53df1632014-04-21 12:01:32 -070062{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010063 m_pingInterval = interval;
Wentao Shang53df1632014-04-21 12:01:32 -070064}
65
66void
Wentao Shang98733142014-09-17 12:13:57 -070067WebSocketChannel::setPongTimeout(time::milliseconds timeout)
68{
69 m_server.set_pong_timeout(static_cast<long>(timeout.count()));
70}
71
72void
73WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl, std::string msg)
74{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010075 auto it = m_channelFaces.find(hdl);
76 if (it != m_channelFaces.end()) {
77 NFD_LOG_TRACE(__func__ << ": " << it->second->getRemoteUri());
78 it->second->close();
79 m_channelFaces.erase(it);
80 }
Wentao Shang98733142014-09-17 12:13:57 -070081}
82
83void
84WebSocketChannel::handlePong(websocketpp::connection_hdl hdl, std::string msg)
85{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010086 auto it = m_channelFaces.find(hdl);
87 if (it != m_channelFaces.end()) {
88 NFD_LOG_TRACE("Pong from " << it->second->getRemoteUri());
89 }
Wentao Shang98733142014-09-17 12:13:57 -070090}
91
92void
Wentao Shang53df1632014-04-21 12:01:32 -070093WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
94 websocket::Server::message_ptr msg)
95{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010096 auto it = m_channelFaces.find(hdl);
97 if (it != m_channelFaces.end()) {
98 it->second->handleReceive(msg->get_payload());
99 }
Wentao Shang53df1632014-04-21 12:01:32 -0700100}
101
102void
103WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
104{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 try {
Wentao Shangd397d772015-04-29 12:09:31 -0700106 std::string remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint();
107 auto face = make_shared<WebSocketFace>(FaceUri(remote), this->getUri(),
108 hdl, ref(m_server));
109 m_onFaceCreatedCallback(face);
110 m_channelFaces[hdl] = face;
111 // Schedule ping message
112 scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval,
113 bind(&WebSocketChannel::sendPing,
114 this, hdl));
115 face->setPingEventId(pingEvent);
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100116 }
Wentao Shangd397d772015-04-29 12:09:31 -0700117 catch (const FaceUri::Error& e) {
118 NFD_LOG_WARN(e.what());
119 websocketpp::lib::error_code ec;
120 m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", ec);
121 // ignore error on close
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100122 }
Wentao Shangd397d772015-04-29 12:09:31 -0700123 catch (const websocketpp::exception& e) {
124 NFD_LOG_WARN("Cannot get remote connection: " << e.what());
125 websocketpp::lib::error_code ec;
126 m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", ec);
127 // ignore error on close
128 }
Wentao Shang98733142014-09-17 12:13:57 -0700129}
130
131void
132WebSocketChannel::sendPing(websocketpp::connection_hdl hdl)
133{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100134 auto it = m_channelFaces.find(hdl);
135 if (it != m_channelFaces.end()) {
136 NFD_LOG_TRACE("Sending ping to " << it->second->getRemoteUri());
Wentao Shang98733142014-09-17 12:13:57 -0700137
Wentao Shangd397d772015-04-29 12:09:31 -0700138 websocketpp::lib::error_code ec;
139 m_server.ping(hdl, "NFD-WebSocket", ec);
140 if (ec)
141 {
142 NFD_LOG_WARN("Failed to ping " << it->second->getRemoteUri() << ": " << ec.message());
143 it->second->close();
144 m_channelFaces.erase(it);
145 return;
146 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100147
148 // Schedule next ping message
149 scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval,
Wentao Shangd397d772015-04-29 12:09:31 -0700150 bind(&WebSocketChannel::sendPing,
151 this, hdl));
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100152 it->second->setPingEventId(pingEvent);
153 }
Wentao Shang53df1632014-04-21 12:01:32 -0700154}
155
156void
157WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
158{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100159 auto it = m_channelFaces.find(hdl);
160 if (it != m_channelFaces.end()) {
161 NFD_LOG_TRACE(__func__ << ": " << it->second->getRemoteUri());
162 it->second->close();
163 m_channelFaces.erase(it);
164 }
Wentao Shang53df1632014-04-21 12:01:32 -0700165}
166
Wentao Shang53df1632014-04-21 12:01:32 -0700167void
168WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated)
169{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100170 if (isListening()) {
171 NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
172 return;
173 }
Wentao Shang53df1632014-04-21 12:01:32 -0700174 m_isListening = true;
175
176 m_onFaceCreatedCallback = onFaceCreated;
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100177 m_server.listen(m_localEndpoint);
Wentao Shang53df1632014-04-21 12:01:32 -0700178 m_server.start_accept();
179}
180
181size_t
182WebSocketChannel::size() const
183{
184 return m_channelFaces.size();
185}
186
187} // namespace nfd