blob: 99153f7f420f625b13592416bee276175cf70006 [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento8fd15e62017-04-06 19:58:54 -04003 * Copyright (c) 2014-2017, 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
32namespace nfd {
33
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020034namespace websocket {
35typedef boost::asio::ip::tcp::endpoint Endpoint;
36} // namespace websocket
37
Davide Pesavento8fd15e62017-04-06 19:58:54 -040038namespace face {
39
Wentao Shang53df1632014-04-21 12:01:32 -070040/**
41 * \brief Class implementing WebSocket-based channel to create faces
Wentao Shang53df1632014-04-21 12:01:32 -070042 */
43class WebSocketChannel : public Channel
44{
45public:
46 /**
Wentao Shang53df1632014-04-21 12:01:32 -070047 * \brief Create WebSocket channel for the local endpoint
48 *
49 * To enable creation of faces upon incoming connections,
50 * one needs to explicitly call WebSocketChannel::listen method.
Yukai Tu2d6d5632015-10-26 11:06:02 -070051 * The created channel is bound to the localEndpoint.
Wentao Shang53df1632014-04-21 12:01:32 -070052 */
53 explicit
54 WebSocketChannel(const websocket::Endpoint& localEndpoint);
55
Wentao Shang53df1632014-04-21 12:01:32 -070056 /**
57 * \brief Enable listening on the local endpoint, accept connections,
58 * and create faces when remote host makes a connection
Wentao Shang53df1632014-04-21 12:01:32 -070059 *
Yukai Tu2d6d5632015-10-26 11:06:02 -070060 * \param onFaceCreated Callback to notify successful creation of a face
Wentao Shang53df1632014-04-21 12:01:32 -070061 */
62 void
63 listen(const FaceCreatedCallback& onFaceCreated);
64
65 /**
66 * \brief Get number of faces in the channel
67 */
68 size_t
69 size() const;
70
Wentao Shang93ef6c92014-06-19 11:59:17 -040071 bool
72 isListening() const;
73
Davide Pesavento6ad890a2015-03-09 03:43:17 +010074PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yukai Tu2d6d5632015-10-26 11:06:02 -070075 /** \pre listen hasn't been invoked
76 */
Wentao Shang98733142014-09-17 12:13:57 -070077 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +010078 setPingInterval(time::milliseconds interval);
Wentao Shang98733142014-09-17 12:13:57 -070079
Yukai Tu2d6d5632015-10-26 11:06:02 -070080 /** \pre listen hasn't been invoked
81 */
Wentao Shang98733142014-09-17 12:13:57 -070082 void
83 setPongTimeout(time::milliseconds timeout);
84
Yukai Tu2d6d5632015-10-26 11:06:02 -070085 void
86 handlePong(websocketpp::connection_hdl hdl);
87
88 void
89 handlePongTimeout(websocketpp::connection_hdl hdl);
90
Wentao Shang53df1632014-04-21 12:01:32 -070091private:
92 void
Yukai Tu2d6d5632015-10-26 11:06:02 -070093 handleMessage(websocketpp::connection_hdl hdl,
94 websocket::Server::message_ptr msg);
Wentao Shang53df1632014-04-21 12:01:32 -070095
96 void
97 handleOpen(websocketpp::connection_hdl hdl);
98
99 void
100 handleClose(websocketpp::connection_hdl hdl);
101
102private:
103 websocket::Endpoint m_localEndpoint;
Wentao Shang53df1632014-04-21 12:01:32 -0700104 websocket::Server m_server;
105
Junxiao Shicde37ad2015-12-24 01:02:05 -0700106 std::map<websocketpp::connection_hdl, shared_ptr<Face>,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100107 std::owner_less<websocketpp::connection_hdl>> m_channelFaces;
108
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
Wentao Shang93ef6c92014-06-19 11:59:17 -0400113inline bool
114WebSocketChannel::isListening() const
115{
Yukai Tu2d6d5632015-10-26 11:06:02 -0700116 return m_server.is_listening();
Wentao Shang93ef6c92014-06-19 11:59:17 -0400117}
118
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400119} // namespace face
Wentao Shang53df1632014-04-21 12:01:32 -0700120} // namespace nfd
121
122#endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP