websocket-channel.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, 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 #ifndef NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP
27 #define NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP
28 
29 #include "channel.hpp"
30 #include "websocketpp.hpp"
31 
32 #include <map>
33 
34 namespace nfd::websocket {
35 using Endpoint = boost::asio::ip::tcp::endpoint;
36 } // namespace nfd::websocket
37 
38 namespace nfd::face {
39 
43 class WebSocketChannel final : public Channel
44 {
45 public:
52  explicit
53  WebSocketChannel(const websocket::Endpoint& localEndpoint);
54 
55  bool
56  isListening() const final
57  {
58  return m_server.is_listening();
59  }
60 
61  size_t
62  size() const final
63  {
64  return m_channelFaces.size();
65  }
66 
73  void
74  listen(const FaceCreatedCallback& onFaceCreated);
75 
80  void
81  setPingInterval(time::milliseconds interval);
82 
86  void
87  setPongTimeout(time::milliseconds timeout);
88 
89  void
90  handlePong(websocketpp::connection_hdl hdl);
91 
92  void
93  handlePongTimeout(websocketpp::connection_hdl hdl);
94 
95 private:
96  void
97  handleMessage(websocketpp::connection_hdl hdl,
98  websocket::Server::message_ptr msg);
99 
100  void
101  handleOpen(websocketpp::connection_hdl hdl);
102 
103  void
104  handleClose(websocketpp::connection_hdl hdl);
105 
106 private:
107  const websocket::Endpoint m_localEndpoint;
108  websocket::Server m_server;
109  std::map<websocketpp::connection_hdl, shared_ptr<Face>,
110  std::owner_less<websocketpp::connection_hdl>> m_channelFaces;
111  FaceCreatedCallback m_onFaceCreatedCallback;
112  time::milliseconds m_pingInterval;
113 };
114 
115 } // namespace nfd::face
116 
117 #endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP
Represents a channel that listens on a local endpoint.
Definition: channel.hpp:43
Class implementing a WebSocket-based channel to create faces.
bool isListening() const final
Returns whether the channel is listening.
void listen(const FaceCreatedCallback &onFaceCreated)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
WebSocketChannel(const websocket::Endpoint &localEndpoint)
Create a WebSocket channel for the given localEndpoint.
size_t size() const final
Returns the number of faces in the channel.
#define NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:41
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:90
websocketpp::server< websocketpp::config::asio > Server
Definition: websocketpp.hpp:47
boost::asio::ip::tcp::endpoint Endpoint