blob: 2ac9384c1c6fd6edc3dde157cb2db72dabf8db1f [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento15b55052018-01-27 19:09:28 -05002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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"
Yukai Tu2d6d5632015-10-26 11:06:02 -070027#include "generic-link-service.hpp"
28#include "websocket-transport.hpp"
Davide Pesavento6ad890a2015-03-09 03:43:17 +010029#include "core/global-io.hpp"
Wentao Shang98733142014-09-17 12:13:57 -070030
Wentao Shang53df1632014-04-21 12:01:32 -070031namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040032namespace face {
Wentao Shang53df1632014-04-21 12:01:32 -070033
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(WebSocketChannel);
Wentao Shang53df1632014-04-21 12:01:32 -070035
Wentao Shang53df1632014-04-21 12:01:32 -070036WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint)
37 : m_localEndpoint(localEndpoint)
Davide Pesavento15b55052018-01-27 19:09:28 -050038 , m_pingInterval(10_s)
Wentao Shang53df1632014-04-21 12:01:32 -070039{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010040 setUri(FaceUri(m_localEndpoint, "ws"));
Davide Pesavento77911cc2017-04-08 22:12:30 -040041 NFD_LOG_CHAN_INFO("Creating channel");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010042
Yukai Tu2d6d5632015-10-26 11:06:02 -070043 // Be quiet
Wentao Shang53df1632014-04-21 12:01:32 -070044 m_server.clear_access_channels(websocketpp::log::alevel::all);
Yukai Tu2d6d5632015-10-26 11:06:02 -070045 m_server.clear_error_channels(websocketpp::log::elevel::all);
Wentao Shang53df1632014-04-21 12:01:32 -070046
Yukai Tu2d6d5632015-10-26 11:06:02 -070047 // Setup WebSocket server
48 m_server.init_asio(&getGlobalIoService());
Wentao Shang53df1632014-04-21 12:01:32 -070049 m_server.set_open_handler(bind(&WebSocketChannel::handleOpen, this, _1));
50 m_server.set_close_handler(bind(&WebSocketChannel::handleClose, this, _1));
Yukai Tu2d6d5632015-10-26 11:06:02 -070051 m_server.set_message_handler(bind(&WebSocketChannel::handleMessage, this, _1, _2));
Davide Pesavento6ad890a2015-03-09 03:43:17 +010052
53 // Detect disconnections using ping-pong messages
Yukai Tu2d6d5632015-10-26 11:06:02 -070054 m_server.set_pong_handler(bind(&WebSocketChannel::handlePong, this, _1));
55 m_server.set_pong_timeout_handler(bind(&WebSocketChannel::handlePongTimeout, this, _1));
Davide Pesavento6ad890a2015-03-09 03:43:17 +010056
Wentao Shang93ef6c92014-06-19 11:59:17 -040057 // Always set SO_REUSEADDR flag
58 m_server.set_reuse_addr(true);
Wentao Shang53df1632014-04-21 12:01:32 -070059}
60
Davide Pesavento6ad890a2015-03-09 03:43:17 +010061void
62WebSocketChannel::setPingInterval(time::milliseconds interval)
Wentao Shang53df1632014-04-21 12:01:32 -070063{
Yukai Tu2d6d5632015-10-26 11:06:02 -070064 BOOST_ASSERT(!m_server.is_listening());
65
Davide Pesavento6ad890a2015-03-09 03:43:17 +010066 m_pingInterval = interval;
Wentao Shang53df1632014-04-21 12:01:32 -070067}
68
69void
Wentao Shang98733142014-09-17 12:13:57 -070070WebSocketChannel::setPongTimeout(time::milliseconds timeout)
71{
Yukai Tu2d6d5632015-10-26 11:06:02 -070072 BOOST_ASSERT(!m_server.is_listening());
73
Wentao Shang98733142014-09-17 12:13:57 -070074 m_server.set_pong_timeout(static_cast<long>(timeout.count()));
75}
76
77void
Yukai Tu2d6d5632015-10-26 11:06:02 -070078WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl)
Wentao Shang98733142014-09-17 12:13:57 -070079{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010080 auto it = m_channelFaces.find(hdl);
81 if (it != m_channelFaces.end()) {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040082 static_cast<WebSocketTransport*>(it->second->getTransport())->handlePongTimeout();
Yukai Tu2d6d5632015-10-26 11:06:02 -070083 }
84 else {
Davide Pesavento77911cc2017-04-08 22:12:30 -040085 NFD_LOG_CHAN_WARN("Pong timeout on unknown transport");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010086 }
Wentao Shang98733142014-09-17 12:13:57 -070087}
88
89void
Yukai Tu2d6d5632015-10-26 11:06:02 -070090WebSocketChannel::handlePong(websocketpp::connection_hdl hdl)
Wentao Shang98733142014-09-17 12:13:57 -070091{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010092 auto it = m_channelFaces.find(hdl);
93 if (it != m_channelFaces.end()) {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040094 static_cast<WebSocketTransport*>(it->second->getTransport())->handlePong();
Yukai Tu2d6d5632015-10-26 11:06:02 -070095 }
96 else {
Davide Pesavento77911cc2017-04-08 22:12:30 -040097 NFD_LOG_CHAN_WARN("Pong received on unknown transport");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010098 }
Wentao Shang98733142014-09-17 12:13:57 -070099}
100
101void
Wentao Shang53df1632014-04-21 12:01:32 -0700102WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
103 websocket::Server::message_ptr msg)
104{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 auto it = m_channelFaces.find(hdl);
106 if (it != m_channelFaces.end()) {
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400107 static_cast<WebSocketTransport*>(it->second->getTransport())->receiveMessage(msg->get_payload());
Yukai Tu2d6d5632015-10-26 11:06:02 -0700108 }
109 else {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400110 NFD_LOG_CHAN_WARN("Message received on unknown transport");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100111 }
Wentao Shang53df1632014-04-21 12:01:32 -0700112}
113
114void
115WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
116{
Davide Pesavento77911cc2017-04-08 22:12:30 -0400117 NFD_LOG_CHAN_TRACE("Incoming connection from " << m_server.get_con_from_hdl(hdl)->get_remote_endpoint());
118
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400119 auto linkService = make_unique<GenericLinkService>();
120 auto transport = make_unique<WebSocketTransport>(hdl, ref(m_server), m_pingInterval);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700121 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Wentao Shang98733142014-09-17 12:13:57 -0700122
Davide Pesavento77911cc2017-04-08 22:12:30 -0400123 BOOST_ASSERT(m_channelFaces.count(hdl) == 0);
Yukai Tu2d6d5632015-10-26 11:06:02 -0700124 m_channelFaces[hdl] = face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700125 connectFaceClosedSignal(*face, [this, hdl] { m_channelFaces.erase(hdl); });
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100126
Yukai Tu2d6d5632015-10-26 11:06:02 -0700127 m_onFaceCreatedCallback(face);
Wentao Shang53df1632014-04-21 12:01:32 -0700128}
129
130void
131WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
132{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100133 auto it = m_channelFaces.find(hdl);
134 if (it != m_channelFaces.end()) {
Junxiao Shicde37ad2015-12-24 01:02:05 -0700135 it->second->close();
Yukai Tu2d6d5632015-10-26 11:06:02 -0700136 }
137 else {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400138 NFD_LOG_CHAN_WARN("Close on unknown transport");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100139 }
Wentao Shang53df1632014-04-21 12:01:32 -0700140}
141
Wentao Shang53df1632014-04-21 12:01:32 -0700142void
143WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated)
144{
Davide Pesavento77911cc2017-04-08 22:12:30 -0400145 if (isListening()) {
146 NFD_LOG_CHAN_WARN("Already listening");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100147 return;
148 }
Wentao Shang53df1632014-04-21 12:01:32 -0700149
150 m_onFaceCreatedCallback = onFaceCreated;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400151
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100152 m_server.listen(m_localEndpoint);
Wentao Shang53df1632014-04-21 12:01:32 -0700153 m_server.start_accept();
Davide Pesavento77911cc2017-04-08 22:12:30 -0400154 NFD_LOG_CHAN_DEBUG("Started listening");
Wentao Shang53df1632014-04-21 12:01:32 -0700155}
156
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400157} // namespace face
Wentao Shang53df1632014-04-21 12:01:32 -0700158} // namespace nfd