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