blob: a793a7c97a5b1c5462038194298fc89e68c46a9c [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento264af772021-02-09 21:48:24 -05002/*
Davide Pesavento9bf64cc2023-10-05 02:12:02 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Junxiao Shicde37ad2015-12-24 01:02: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 Shicde37ad2015-12-24 01:02:05 -070024 */
Wentao Shang53df1632014-04-21 12:01:32 -070025
26#ifndef NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP
27#define NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP
28
29#include "channel.hpp"
Yukai Tu2d6d5632015-10-26 11:06:02 -070030#include "websocketpp.hpp"
Wentao Shang53df1632014-04-21 12:01:32 -070031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::websocket {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040033using Endpoint = boost::asio::ip::tcp::endpoint;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034} // namespace nfd::websocket
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::face {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040037
Wentao Shang53df1632014-04-21 12:01:32 -070038/**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040039 * \brief Class implementing a WebSocket-based channel to create faces.
Wentao Shang53df1632014-04-21 12:01:32 -070040 */
Davide Pesavento3db98072021-03-09 23:03:27 -050041class WebSocketChannel final : public Channel
Wentao Shang53df1632014-04-21 12:01:32 -070042{
43public:
44 /**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040045 * \brief Create a WebSocket channel for the given \p localEndpoint.
Wentao Shang53df1632014-04-21 12:01:32 -070046 *
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040047 * To enable the creation of faces upon incoming connections, one needs to
48 * explicitly call listen(). The created channel is bound to \p localEndpoint.
Wentao Shang53df1632014-04-21 12:01:32 -070049 */
50 explicit
51 WebSocketChannel(const websocket::Endpoint& localEndpoint);
52
Davide Pesaventoc19408d2017-04-08 00:42:55 -040053 bool
Davide Pesavento3db98072021-03-09 23:03:27 -050054 isListening() const final
Davide Pesaventoc19408d2017-04-08 00:42:55 -040055 {
56 return m_server.is_listening();
57 }
58
59 size_t
Davide Pesavento3db98072021-03-09 23:03:27 -050060 size() const final
Davide Pesaventoc19408d2017-04-08 00:42:55 -040061 {
62 return m_channelFaces.size();
63 }
64
Wentao Shang53df1632014-04-21 12:01:32 -070065 /**
66 * \brief Enable listening on the local endpoint, accept connections,
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040067 * and create faces when remote host makes a connection.
Wentao Shang53df1632014-04-21 12:01:32 -070068 *
Yukai Tu2d6d5632015-10-26 11:06:02 -070069 * \param onFaceCreated Callback to notify successful creation of a face
Wentao Shang53df1632014-04-21 12:01:32 -070070 */
71 void
72 listen(const FaceCreatedCallback& onFaceCreated);
73
Davide Pesavento264af772021-02-09 21:48:24 -050074NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040075 /**
76 * \pre listen() has not been invoked.
Yukai Tu2d6d5632015-10-26 11:06:02 -070077 */
Wentao Shang98733142014-09-17 12:13:57 -070078 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +010079 setPingInterval(time::milliseconds interval);
Wentao Shang98733142014-09-17 12:13:57 -070080
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040081 /**
82 * \pre listen() has not been invoked.
Yukai Tu2d6d5632015-10-26 11:06:02 -070083 */
Wentao Shang98733142014-09-17 12:13:57 -070084 void
85 setPongTimeout(time::milliseconds timeout);
86
Yukai Tu2d6d5632015-10-26 11:06:02 -070087 void
88 handlePong(websocketpp::connection_hdl hdl);
89
90 void
91 handlePongTimeout(websocketpp::connection_hdl hdl);
92
Wentao Shang53df1632014-04-21 12:01:32 -070093private:
94 void
Yukai Tu2d6d5632015-10-26 11:06:02 -070095 handleMessage(websocketpp::connection_hdl hdl,
96 websocket::Server::message_ptr msg);
Wentao Shang53df1632014-04-21 12:01:32 -070097
98 void
99 handleOpen(websocketpp::connection_hdl hdl);
100
101 void
102 handleClose(websocketpp::connection_hdl hdl);
103
104private:
Davide Pesavento77911cc2017-04-08 22:12:30 -0400105 const websocket::Endpoint m_localEndpoint;
Wentao Shang53df1632014-04-21 12:01:32 -0700106 websocket::Server m_server;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700107 std::map<websocketpp::connection_hdl, shared_ptr<Face>,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100108 std::owner_less<websocketpp::connection_hdl>> m_channelFaces;
Wentao Shang53df1632014-04-21 12:01:32 -0700109 FaceCreatedCallback m_onFaceCreatedCallback;
Wentao Shang98733142014-09-17 12:13:57 -0700110 time::milliseconds m_pingInterval;
Wentao Shang53df1632014-04-21 12:01:32 -0700111};
112
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400113} // namespace nfd::face
Wentao Shang53df1632014-04-21 12:01:32 -0700114
115#endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP