blob: a793a7c97a5b1c5462038194298fc89e68c46a9c [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP
#define NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP
#include "channel.hpp"
#include "websocketpp.hpp"
namespace nfd::websocket {
using Endpoint = boost::asio::ip::tcp::endpoint;
} // namespace nfd::websocket
namespace nfd::face {
/**
* \brief Class implementing a WebSocket-based channel to create faces.
*/
class WebSocketChannel final : public Channel
{
public:
/**
* \brief Create a WebSocket channel for the given \p localEndpoint.
*
* To enable the creation of faces upon incoming connections, one needs to
* explicitly call listen(). The created channel is bound to \p localEndpoint.
*/
explicit
WebSocketChannel(const websocket::Endpoint& localEndpoint);
bool
isListening() const final
{
return m_server.is_listening();
}
size_t
size() const final
{
return m_channelFaces.size();
}
/**
* \brief Enable listening on the local endpoint, accept connections,
* and create faces when remote host makes a connection.
*
* \param onFaceCreated Callback to notify successful creation of a face
*/
void
listen(const FaceCreatedCallback& onFaceCreated);
NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
/**
* \pre listen() has not been invoked.
*/
void
setPingInterval(time::milliseconds interval);
/**
* \pre listen() has not been invoked.
*/
void
setPongTimeout(time::milliseconds timeout);
void
handlePong(websocketpp::connection_hdl hdl);
void
handlePongTimeout(websocketpp::connection_hdl hdl);
private:
void
handleMessage(websocketpp::connection_hdl hdl,
websocket::Server::message_ptr msg);
void
handleOpen(websocketpp::connection_hdl hdl);
void
handleClose(websocketpp::connection_hdl hdl);
private:
const websocket::Endpoint m_localEndpoint;
websocket::Server m_server;
std::map<websocketpp::connection_hdl, shared_ptr<Face>,
std::owner_less<websocketpp::connection_hdl>> m_channelFaces;
FaceCreatedCallback m_onFaceCreatedCallback;
time::milliseconds m_pingInterval;
};
} // namespace nfd::face
#endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP