Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, 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. |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 24 | */ |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 25 | |
| 26 | #ifndef NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |
| 27 | #define NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |
| 28 | |
| 29 | #include "channel.hpp" |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 30 | #include "websocketpp.hpp" |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 34 | namespace websocket { |
| 35 | typedef boost::asio::ip::tcp::endpoint Endpoint; |
| 36 | } // namespace websocket |
| 37 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 38 | /** |
| 39 | * \brief Class implementing WebSocket-based channel to create faces |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 40 | */ |
| 41 | class WebSocketChannel : public Channel |
| 42 | { |
| 43 | public: |
| 44 | /** |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 45 | * \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. |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 49 | * The created channel is bound to the localEndpoint. |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 50 | */ |
| 51 | explicit |
| 52 | WebSocketChannel(const websocket::Endpoint& localEndpoint); |
| 53 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 54 | /** |
| 55 | * \brief Enable listening on the local endpoint, accept connections, |
| 56 | * and create faces when remote host makes a connection |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 57 | * |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 58 | * \param onFaceCreated Callback to notify successful creation of a face |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 59 | */ |
| 60 | void |
| 61 | listen(const FaceCreatedCallback& onFaceCreated); |
| 62 | |
| 63 | /** |
| 64 | * \brief Get number of faces in the channel |
| 65 | */ |
| 66 | size_t |
| 67 | size() const; |
| 68 | |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 69 | bool |
| 70 | isListening() const; |
| 71 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 72 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 73 | /** \pre listen hasn't been invoked |
| 74 | */ |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 75 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 76 | setPingInterval(time::milliseconds interval); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 77 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 78 | /** \pre listen hasn't been invoked |
| 79 | */ |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 80 | void |
| 81 | setPongTimeout(time::milliseconds timeout); |
| 82 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 83 | void |
| 84 | handlePong(websocketpp::connection_hdl hdl); |
| 85 | |
| 86 | void |
| 87 | handlePongTimeout(websocketpp::connection_hdl hdl); |
| 88 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 89 | private: |
| 90 | void |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 91 | handleMessage(websocketpp::connection_hdl hdl, |
| 92 | websocket::Server::message_ptr msg); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 93 | |
| 94 | void |
| 95 | handleOpen(websocketpp::connection_hdl hdl); |
| 96 | |
| 97 | void |
| 98 | handleClose(websocketpp::connection_hdl hdl); |
| 99 | |
| 100 | private: |
| 101 | websocket::Endpoint m_localEndpoint; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 102 | websocket::Server m_server; |
| 103 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 104 | std::map<websocketpp::connection_hdl, shared_ptr<Face>, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 105 | std::owner_less<websocketpp::connection_hdl>> m_channelFaces; |
| 106 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 107 | FaceCreatedCallback m_onFaceCreatedCallback; |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 108 | time::milliseconds m_pingInterval; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 111 | inline bool |
| 112 | WebSocketChannel::isListening() const |
| 113 | { |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 114 | return m_server.is_listening(); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 117 | } // namespace nfd |
| 118 | |
| 119 | #endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |