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 | 1e46be3 | 2015-01-08 20:18: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 | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 24 | */ |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 25 | |
| 26 | #include "websocket-channel.hpp" |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 27 | #include "core/global-io.hpp" |
| 28 | #include "core/scheduler.hpp" |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 29 | |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 30 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 31 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 32 | namespace nfd { |
| 33 | |
| 34 | NFD_LOG_INIT("WebSocketChannel"); |
| 35 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 36 | WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint) |
| 37 | : m_localEndpoint(localEndpoint) |
| 38 | , m_isListening(false) |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 39 | , m_pingInterval(10000) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 41 | setUri(FaceUri(m_localEndpoint, "ws")); |
| 42 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 43 | // Setup WebSocket server |
| 44 | m_server.clear_access_channels(websocketpp::log::alevel::all); |
| 45 | m_server.clear_error_channels(websocketpp::log::alevel::all); |
| 46 | |
| 47 | m_server.set_message_handler(bind(&WebSocketChannel::handleMessage, this, _1, _2)); |
| 48 | m_server.set_open_handler(bind(&WebSocketChannel::handleOpen, this, _1)); |
| 49 | m_server.set_close_handler(bind(&WebSocketChannel::handleClose, this, _1)); |
| 50 | m_server.init_asio(&getGlobalIoService()); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 51 | |
| 52 | // Detect disconnections using ping-pong messages |
| 53 | m_server.set_pong_handler(bind(&WebSocketChannel::handlePong, this, _1, _2)); |
| 54 | m_server.set_pong_timeout_handler(bind(&WebSocketChannel::handlePongTimeout, this, _1, _2)); |
| 55 | |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 56 | // Always set SO_REUSEADDR flag |
| 57 | m_server.set_reuse_addr(true); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 60 | void |
| 61 | WebSocketChannel::setPingInterval(time::milliseconds interval) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 62 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 63 | m_pingInterval = interval; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 67 | WebSocketChannel::setPongTimeout(time::milliseconds timeout) |
| 68 | { |
| 69 | m_server.set_pong_timeout(static_cast<long>(timeout.count())); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl, std::string msg) |
| 74 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 75 | auto it = m_channelFaces.find(hdl); |
| 76 | if (it != m_channelFaces.end()) { |
| 77 | NFD_LOG_TRACE(__func__ << ": " << it->second->getRemoteUri()); |
| 78 | it->second->close(); |
| 79 | m_channelFaces.erase(it); |
| 80 | } |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
| 84 | WebSocketChannel::handlePong(websocketpp::connection_hdl hdl, std::string msg) |
| 85 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 86 | auto it = m_channelFaces.find(hdl); |
| 87 | if (it != m_channelFaces.end()) { |
| 88 | NFD_LOG_TRACE("Pong from " << it->second->getRemoteUri()); |
| 89 | } |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 93 | WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl, |
| 94 | websocket::Server::message_ptr msg) |
| 95 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 96 | auto it = m_channelFaces.find(hdl); |
| 97 | if (it != m_channelFaces.end()) { |
| 98 | it->second->handleReceive(msg->get_payload()); |
| 99 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void |
| 103 | WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl) |
| 104 | { |
| 105 | std::string remote; |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 106 | try { |
| 107 | remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint(); |
| 108 | } |
| 109 | catch (const websocketpp::lib::error_code& e) { |
| 110 | NFD_LOG_WARN("Cannot get remote URI"); |
| 111 | websocketpp::lib::error_code error; |
| 112 | m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", error); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | auto face = make_shared<WebSocketFace>(FaceUri(remote), this->getUri(), |
| 117 | hdl, ref(m_server)); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 118 | m_onFaceCreatedCallback(face); |
| 119 | m_channelFaces[hdl] = face; |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 120 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 121 | // Schedule ping message |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 122 | scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval, |
| 123 | bind(&WebSocketChannel::sendPing, this, hdl)); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 124 | face->setPingEventId(pingEvent); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | WebSocketChannel::sendPing(websocketpp::connection_hdl hdl) |
| 129 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 130 | auto it = m_channelFaces.find(hdl); |
| 131 | if (it != m_channelFaces.end()) { |
| 132 | NFD_LOG_TRACE("Sending ping to " << it->second->getRemoteUri()); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 133 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 134 | try { |
| 135 | m_server.ping(hdl, "NFD-WebSocket"); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 136 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 137 | catch (const websocketpp::lib::error_code& e) { |
| 138 | NFD_LOG_WARN("Failed to ping " << it->second->getRemoteUri()); |
| 139 | it->second->close(); |
| 140 | m_channelFaces.erase(it); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | // Schedule next ping message |
| 145 | scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval, |
| 146 | bind(&WebSocketChannel::sendPing, this, hdl)); |
| 147 | it->second->setPingEventId(pingEvent); |
| 148 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void |
| 152 | WebSocketChannel::handleClose(websocketpp::connection_hdl hdl) |
| 153 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 154 | auto it = m_channelFaces.find(hdl); |
| 155 | if (it != m_channelFaces.end()) { |
| 156 | NFD_LOG_TRACE(__func__ << ": " << it->second->getRemoteUri()); |
| 157 | it->second->close(); |
| 158 | m_channelFaces.erase(it); |
| 159 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 162 | void |
| 163 | WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated) |
| 164 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 165 | if (isListening()) { |
| 166 | NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening"); |
| 167 | return; |
| 168 | } |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 169 | m_isListening = true; |
| 170 | |
| 171 | m_onFaceCreatedCallback = onFaceCreated; |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame^] | 172 | m_server.listen(m_localEndpoint); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 173 | m_server.start_accept(); |
| 174 | } |
| 175 | |
| 176 | size_t |
| 177 | WebSocketChannel::size() const |
| 178 | { |
| 179 | return m_channelFaces.size(); |
| 180 | } |
| 181 | |
| 182 | } // namespace nfd |