blob: f560ab3b8a7efd5a6a68197817473d7f6d132efa [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
34/**
35 * \brief Class implementing WebSocket-based channel to create faces
Wentao Shang53df1632014-04-21 12:01:32 -070036 */
37class WebSocketChannel : public Channel
38{
39public:
40 /**
Wentao Shang53df1632014-04-21 12:01:32 -070041 * \brief Create WebSocket channel for the local endpoint
42 *
43 * To enable creation of faces upon incoming connections,
44 * one needs to explicitly call WebSocketChannel::listen method.
45 * The created socket is bound to the localEndpoint.
46 *
47 * \throw WebSocketChannel::Error if bind on the socket fails
48 */
49 explicit
50 WebSocketChannel(const websocket::Endpoint& localEndpoint);
51
Wentao Shang53df1632014-04-21 12:01:32 -070052 /**
53 * \brief Enable listening on the local endpoint, accept connections,
54 * and create faces when remote host makes a connection
55 * \param onFaceCreated Callback to notify successful creation of the face
56 *
57 * \throws WebSocketChannel::Error if called multiple times
58 */
59 void
60 listen(const FaceCreatedCallback& onFaceCreated);
61
62 /**
63 * \brief Get number of faces in the channel
64 */
65 size_t
66 size() const;
67
Wentao Shang93ef6c92014-06-19 11:59:17 -040068 bool
69 isListening() const;
70
Davide Pesavento6ad890a2015-03-09 03:43:17 +010071PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Wentao Shang98733142014-09-17 12:13:57 -070072 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +010073 setPingInterval(time::milliseconds interval);
Wentao Shang98733142014-09-17 12:13:57 -070074
75 void
76 setPongTimeout(time::milliseconds timeout);
77
Wentao Shang53df1632014-04-21 12:01:32 -070078private:
79 void
Wentao Shang98733142014-09-17 12:13:57 -070080 sendPing(websocketpp::connection_hdl hdl);
81
82 void
83 handlePong(websocketpp::connection_hdl hdl, std::string msg);
84
85 void
86 handlePongTimeout(websocketpp::connection_hdl hdl, std::string msg);
87
88 void
Wentao Shang53df1632014-04-21 12:01:32 -070089 handleMessage(websocketpp::connection_hdl hdl, websocket::Server::message_ptr msg);
90
91 void
92 handleOpen(websocketpp::connection_hdl hdl);
93
94 void
95 handleClose(websocketpp::connection_hdl hdl);
96
97private:
98 websocket::Endpoint m_localEndpoint;
Wentao Shang53df1632014-04-21 12:01:32 -070099 websocket::Server m_server;
100
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100101 std::map<websocketpp::connection_hdl, shared_ptr<WebSocketFace>,
102 std::owner_less<websocketpp::connection_hdl>> m_channelFaces;
103
Wentao Shang53df1632014-04-21 12:01:32 -0700104 /**
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 * Callback for face creation
Wentao Shang53df1632014-04-21 12:01:32 -0700106 */
107 FaceCreatedCallback m_onFaceCreatedCallback;
108
Wentao Shang53df1632014-04-21 12:01:32 -0700109 /**
110 * \brief If true, it means the function listen has already been called
111 */
112 bool m_isListening;
113
Wentao Shang98733142014-09-17 12:13:57 -0700114 time::milliseconds m_pingInterval;
Wentao Shang53df1632014-04-21 12:01:32 -0700115};
116
Wentao Shang93ef6c92014-06-19 11:59:17 -0400117inline bool
118WebSocketChannel::isListening() const
119{
120 return m_isListening;
121}
122
Wentao Shang53df1632014-04-21 12:01:32 -0700123} // namespace nfd
124
125#endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP