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