blob: 027085c4555874f5499df538ec88714181b2755b [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"
Wentao Shang53df1632014-04-21 12:01:32 -070030#include "websocket-face.hpp"
31
32namespace nfd {
33
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020034namespace websocket {
35typedef boost::asio::ip::tcp::endpoint Endpoint;
36} // namespace websocket
37
Wentao Shang53df1632014-04-21 12:01:32 -070038/**
39 * \brief Class implementing WebSocket-based channel to create faces
Wentao Shang53df1632014-04-21 12:01:32 -070040 */
41class WebSocketChannel : public Channel
42{
43public:
44 /**
Wentao Shang53df1632014-04-21 12:01:32 -070045 * \brief Create WebSocket channel for the local endpoint
46 *
47 * To enable creation of faces upon incoming connections,
48 * one needs to explicitly call WebSocketChannel::listen method.
49 * The created socket is bound to the localEndpoint.
50 *
51 * \throw WebSocketChannel::Error if bind on the socket fails
52 */
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
59 * \param onFaceCreated Callback to notify successful creation of the face
60 *
61 * \throws WebSocketChannel::Error if called multiple times
62 */
63 void
64 listen(const FaceCreatedCallback& onFaceCreated);
65
66 /**
67 * \brief Get number of faces in the channel
68 */
69 size_t
70 size() const;
71
Wentao Shang93ef6c92014-06-19 11:59:17 -040072 bool
73 isListening() const;
74
Davide Pesavento6ad890a2015-03-09 03:43:17 +010075PUBLIC_WITH_TESTS_ELSE_PRIVATE:
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
79 void
80 setPongTimeout(time::milliseconds timeout);
81
Wentao Shang53df1632014-04-21 12:01:32 -070082private:
83 void
Wentao Shang98733142014-09-17 12:13:57 -070084 sendPing(websocketpp::connection_hdl hdl);
85
86 void
87 handlePong(websocketpp::connection_hdl hdl, std::string msg);
88
89 void
90 handlePongTimeout(websocketpp::connection_hdl hdl, std::string msg);
91
92 void
Wentao Shang53df1632014-04-21 12:01:32 -070093 handleMessage(websocketpp::connection_hdl hdl, websocket::Server::message_ptr msg);
94
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
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 std::map<websocketpp::connection_hdl, shared_ptr<WebSocketFace>,
106 std::owner_less<websocketpp::connection_hdl>> m_channelFaces;
107
Wentao Shang53df1632014-04-21 12:01:32 -0700108 /**
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100109 * Callback for face creation
Wentao Shang53df1632014-04-21 12:01:32 -0700110 */
111 FaceCreatedCallback m_onFaceCreatedCallback;
112
Wentao Shang53df1632014-04-21 12:01:32 -0700113 /**
114 * \brief If true, it means the function listen has already been called
115 */
116 bool m_isListening;
117
Wentao Shang98733142014-09-17 12:13:57 -0700118 time::milliseconds m_pingInterval;
Wentao Shang53df1632014-04-21 12:01:32 -0700119};
120
Wentao Shang93ef6c92014-06-19 11:59:17 -0400121inline bool
122WebSocketChannel::isListening() const
123{
124 return m_isListening;
125}
126
Wentao Shang53df1632014-04-21 12:01:32 -0700127} // namespace nfd
128
129#endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP