Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 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 Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 24 | */ |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 25 | |
| 26 | #ifndef NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |
| 27 | #define NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |
| 28 | |
| 29 | #include "channel.hpp" |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 30 | #include "websocketpp.hpp" |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 32 | #include <map> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::websocket { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 35 | using Endpoint = boost::asio::ip::tcp::endpoint; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | } // namespace nfd::websocket |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 37 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 38 | namespace nfd::face { |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 39 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 40 | /** |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 41 | * \brief Class implementing a WebSocket-based channel to create faces. |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 42 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 43 | class WebSocketChannel final : public Channel |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 44 | { |
| 45 | public: |
| 46 | /** |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 47 | * \brief Create a WebSocket channel for the given \p localEndpoint. |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 48 | * |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 49 | * To enable the creation of faces upon incoming connections, one needs to |
| 50 | * explicitly call listen(). The created channel is bound to \p localEndpoint. |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 51 | */ |
| 52 | explicit |
| 53 | WebSocketChannel(const websocket::Endpoint& localEndpoint); |
| 54 | |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 55 | bool |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 56 | isListening() const final |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 57 | { |
| 58 | return m_server.is_listening(); |
| 59 | } |
| 60 | |
| 61 | size_t |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 62 | size() const final |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 63 | { |
| 64 | return m_channelFaces.size(); |
| 65 | } |
| 66 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 67 | /** |
| 68 | * \brief Enable listening on the local endpoint, accept connections, |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 69 | * and create faces when remote host makes a connection. |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 70 | * |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 71 | * \param onFaceCreated Callback to notify successful creation of a face |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 72 | */ |
| 73 | void |
| 74 | listen(const FaceCreatedCallback& onFaceCreated); |
| 75 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 76 | NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 77 | /** |
| 78 | * \pre listen() has not been invoked. |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 79 | */ |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 80 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 81 | setPingInterval(time::milliseconds interval); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 82 | |
Davide Pesavento | 9bf64cc | 2023-10-05 02:12:02 -0400 | [diff] [blame] | 83 | /** |
| 84 | * \pre listen() has not been invoked. |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 85 | */ |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 86 | void |
| 87 | setPongTimeout(time::milliseconds timeout); |
| 88 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 89 | void |
| 90 | handlePong(websocketpp::connection_hdl hdl); |
| 91 | |
| 92 | void |
| 93 | handlePongTimeout(websocketpp::connection_hdl hdl); |
| 94 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 95 | private: |
| 96 | void |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 97 | handleMessage(websocketpp::connection_hdl hdl, |
| 98 | websocket::Server::message_ptr msg); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 99 | |
| 100 | void |
| 101 | handleOpen(websocketpp::connection_hdl hdl); |
| 102 | |
| 103 | void |
| 104 | handleClose(websocketpp::connection_hdl hdl); |
| 105 | |
| 106 | private: |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 107 | const websocket::Endpoint m_localEndpoint; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 108 | websocket::Server m_server; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 109 | std::map<websocketpp::connection_hdl, shared_ptr<Face>, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 110 | std::owner_less<websocketpp::connection_hdl>> m_channelFaces; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 111 | FaceCreatedCallback m_onFaceCreatedCallback; |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 112 | time::milliseconds m_pingInterval; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 115 | } // namespace nfd::face |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 116 | |
| 117 | #endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |