blob: 47eda8c336d65ad71b1d6b8b40b1fbe76241681c [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 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"
Yukai Tu2d6d5632015-10-26 11:06:02 -070030#include "lp-face-wrapper.hpp"
31#include "websocketpp.hpp"
Wentao Shang53df1632014-04-21 12:01:32 -070032
33namespace nfd {
34
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020035namespace websocket {
36typedef boost::asio::ip::tcp::endpoint Endpoint;
37} // namespace websocket
38
Wentao Shang53df1632014-04-21 12:01:32 -070039/**
40 * \brief Class implementing WebSocket-based channel to create faces
Wentao Shang53df1632014-04-21 12:01:32 -070041 */
42class WebSocketChannel : public Channel
43{
44public:
45 /**
Wentao Shang53df1632014-04-21 12:01:32 -070046 * \brief Create WebSocket channel for the local endpoint
47 *
48 * To enable creation of faces upon incoming connections,
49 * one needs to explicitly call WebSocketChannel::listen method.
Yukai Tu2d6d5632015-10-26 11:06:02 -070050 * The created channel is bound to the localEndpoint.
Wentao Shang53df1632014-04-21 12:01:32 -070051 */
52 explicit
53 WebSocketChannel(const websocket::Endpoint& localEndpoint);
54
Wentao Shang53df1632014-04-21 12:01:32 -070055 /**
56 * \brief Enable listening on the local endpoint, accept connections,
57 * and create faces when remote host makes a connection
Wentao Shang53df1632014-04-21 12:01:32 -070058 *
Yukai Tu2d6d5632015-10-26 11:06:02 -070059 * \param onFaceCreated Callback to notify successful creation of a face
Wentao Shang53df1632014-04-21 12:01:32 -070060 */
61 void
62 listen(const FaceCreatedCallback& onFaceCreated);
63
64 /**
65 * \brief Get number of faces in the channel
66 */
67 size_t
68 size() const;
69
Wentao Shang93ef6c92014-06-19 11:59:17 -040070 bool
71 isListening() const;
72
Davide Pesavento6ad890a2015-03-09 03:43:17 +010073PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yukai Tu2d6d5632015-10-26 11:06:02 -070074 /** \pre listen hasn't been invoked
75 */
Wentao Shang98733142014-09-17 12:13:57 -070076 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +010077 setPingInterval(time::milliseconds interval);
Wentao Shang98733142014-09-17 12:13:57 -070078
Yukai Tu2d6d5632015-10-26 11:06:02 -070079 /** \pre listen hasn't been invoked
80 */
Wentao Shang98733142014-09-17 12:13:57 -070081 void
82 setPongTimeout(time::milliseconds timeout);
83
Yukai Tu2d6d5632015-10-26 11:06:02 -070084 void
85 handlePong(websocketpp::connection_hdl hdl);
86
87 void
88 handlePongTimeout(websocketpp::connection_hdl hdl);
89
Wentao Shang53df1632014-04-21 12:01:32 -070090private:
91 void
Yukai Tu2d6d5632015-10-26 11:06:02 -070092 handleMessage(websocketpp::connection_hdl hdl,
93 websocket::Server::message_ptr msg);
Wentao Shang53df1632014-04-21 12:01:32 -070094
95 void
96 handleOpen(websocketpp::connection_hdl hdl);
97
98 void
99 handleClose(websocketpp::connection_hdl hdl);
100
101private:
102 websocket::Endpoint m_localEndpoint;
Wentao Shang53df1632014-04-21 12:01:32 -0700103 websocket::Server m_server;
104
Yukai Tu2d6d5632015-10-26 11:06:02 -0700105 std::map<websocketpp::connection_hdl, shared_ptr<face::LpFaceWrapper>,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100106 std::owner_less<websocketpp::connection_hdl>> m_channelFaces;
107
Wentao Shang53df1632014-04-21 12:01:32 -0700108 FaceCreatedCallback m_onFaceCreatedCallback;
Wentao Shang98733142014-09-17 12:13:57 -0700109 time::milliseconds m_pingInterval;
Wentao Shang53df1632014-04-21 12:01:32 -0700110};
111
Wentao Shang93ef6c92014-06-19 11:59:17 -0400112inline bool
113WebSocketChannel::isListening() const
114{
Yukai Tu2d6d5632015-10-26 11:06:02 -0700115 return m_server.is_listening();
Wentao Shang93ef6c92014-06-19 11:59:17 -0400116}
117
Wentao Shang53df1632014-04-21 12:01:32 -0700118} // namespace nfd
119
120#endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP