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-2022, 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 namespace nfd::websocket {
33 using Endpoint = boost::asio::ip::tcp::endpoint;
34 } // namespace nfd::websocket
35 
36 namespace nfd::face {
37 
41 class WebSocketChannel final : public Channel
42 {
43 public:
51  explicit
52  WebSocketChannel(const websocket::Endpoint& localEndpoint);
53 
54  bool
55  isListening() const final
56  {
57  return m_server.is_listening();
58  }
59 
60  size_t
61  size() const final
62  {
63  return m_channelFaces.size();
64  }
65 
72  void
73  listen(const FaceCreatedCallback& onFaceCreated);
74 
78  void
79  setPingInterval(time::milliseconds interval);
80 
83  void
84  setPongTimeout(time::milliseconds timeout);
85 
86  void
87  handlePong(websocketpp::connection_hdl hdl);
88 
89  void
90  handlePongTimeout(websocketpp::connection_hdl hdl);
91 
92 private:
93  void
94  handleMessage(websocketpp::connection_hdl hdl,
95  websocket::Server::message_ptr msg);
96 
97  void
98  handleOpen(websocketpp::connection_hdl hdl);
99 
100  void
101  handleClose(websocketpp::connection_hdl hdl);
102 
103 private:
104  const websocket::Endpoint m_localEndpoint;
105  websocket::Server m_server;
106  std::map<websocketpp::connection_hdl, shared_ptr<Face>,
107  std::owner_less<websocketpp::connection_hdl>> m_channelFaces;
108  FaceCreatedCallback m_onFaceCreatedCallback;
109  time::milliseconds m_pingInterval;
110 };
111 
112 } // namespace nfd::face
113 
114 #endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP
Represents a channel that listens on a local endpoint.
Definition: channel.hpp:41
Class implementing 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 WebSocket channel for the local endpoint.
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:88
websocketpp::server< websocketpp::config::asio > Server
Definition: websocketpp.hpp:47
boost::asio::ip::tcp::endpoint Endpoint