Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 1 | /* -*- 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 Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 30 | #include "websocket-face.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | /** |
| 35 | * \brief Class implementing WebSocket-based channel to create faces |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 36 | */ |
| 37 | class WebSocketChannel : public Channel |
| 38 | { |
| 39 | public: |
| 40 | /** |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 41 | * \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 Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 52 | /** |
| 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 Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 68 | bool |
| 69 | isListening() const; |
| 70 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 71 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 72 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 73 | setPingInterval(time::milliseconds interval); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 74 | |
| 75 | void |
| 76 | setPongTimeout(time::milliseconds timeout); |
| 77 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 78 | private: |
| 79 | void |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 80 | 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 Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 89 | 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 | |
| 97 | private: |
| 98 | websocket::Endpoint m_localEndpoint; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 99 | websocket::Server m_server; |
| 100 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 101 | std::map<websocketpp::connection_hdl, shared_ptr<WebSocketFace>, |
| 102 | std::owner_less<websocketpp::connection_hdl>> m_channelFaces; |
| 103 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 104 | /** |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 105 | * Callback for face creation |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 106 | */ |
| 107 | FaceCreatedCallback m_onFaceCreatedCallback; |
| 108 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 109 | /** |
| 110 | * \brief If true, it means the function listen has already been called |
| 111 | */ |
| 112 | bool m_isListening; |
| 113 | |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 114 | time::milliseconds m_pingInterval; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 117 | inline bool |
| 118 | WebSocketChannel::isListening() const |
| 119 | { |
| 120 | return m_isListening; |
| 121 | } |
| 122 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 123 | } // namespace nfd |
| 124 | |
| 125 | #endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP |