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 | |
| 29 | namespace nfd { |
| 30 | |
| 31 | NFD_LOG_INIT("WebSocketChannel"); |
| 32 | |
| 33 | using namespace boost::asio; |
| 34 | |
| 35 | WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint) |
| 36 | : m_localEndpoint(localEndpoint) |
| 37 | , m_isListening(false) |
| 38 | { |
| 39 | // Setup WebSocket server |
| 40 | m_server.clear_access_channels(websocketpp::log::alevel::all); |
| 41 | m_server.clear_error_channels(websocketpp::log::alevel::all); |
| 42 | |
| 43 | m_server.set_message_handler(bind(&WebSocketChannel::handleMessage, this, _1, _2)); |
| 44 | m_server.set_open_handler(bind(&WebSocketChannel::handleOpen, this, _1)); |
| 45 | m_server.set_close_handler(bind(&WebSocketChannel::handleClose, this, _1)); |
| 46 | m_server.init_asio(&getGlobalIoService()); |
| 47 | |
| 48 | this->setUri(FaceUri(localEndpoint, "ws")); |
| 49 | } |
| 50 | |
| 51 | WebSocketChannel::~WebSocketChannel() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl, |
| 57 | websocket::Server::message_ptr msg) |
| 58 | { |
| 59 | ChannelFaceMap::iterator it = m_channelFaces.find(hdl); |
| 60 | if (it != m_channelFaces.end()) |
| 61 | { |
| 62 | it->second->handleReceive(msg->get_payload()); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl) |
| 68 | { |
| 69 | std::string remote; |
| 70 | try |
| 71 | { |
| 72 | remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint(); |
| 73 | } |
| 74 | catch (websocketpp::lib::error_code ec) |
| 75 | { |
| 76 | NFD_LOG_DEBUG("handleOpen: cannot get remote uri"); |
| 77 | websocketpp::lib::error_code ecode; |
| 78 | m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", ecode); |
| 79 | } |
| 80 | shared_ptr<WebSocketFace> face = make_shared<WebSocketFace>(FaceUri(remote), this->getUri(), |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 81 | hdl, ref(m_server)); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 82 | m_onFaceCreatedCallback(face); |
| 83 | m_channelFaces[hdl] = face; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | WebSocketChannel::handleClose(websocketpp::connection_hdl hdl) |
| 88 | { |
| 89 | ChannelFaceMap::iterator it = m_channelFaces.find(hdl); |
| 90 | if (it != m_channelFaces.end()) |
| 91 | { |
| 92 | NFD_LOG_DEBUG("handleClose: remove client"); |
| 93 | m_channelFaces.erase(it); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void |
| 99 | WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated) |
| 100 | { |
| 101 | if (m_isListening) |
| 102 | { |
| 103 | throw Error("Listen already called on this channel"); |
| 104 | } |
| 105 | m_isListening = true; |
| 106 | |
| 107 | m_onFaceCreatedCallback = onFaceCreated; |
| 108 | |
| 109 | try |
| 110 | { |
| 111 | m_server.listen(m_localEndpoint); |
| 112 | } |
| 113 | catch (websocketpp::lib::error_code ec) |
| 114 | { |
| 115 | throw Error("Failed to listen on local endpoint"); |
| 116 | } |
| 117 | |
| 118 | m_server.start_accept(); |
| 119 | } |
| 120 | |
| 121 | size_t |
| 122 | WebSocketChannel::size() const |
| 123 | { |
| 124 | return m_channelFaces.size(); |
| 125 | } |
| 126 | |
| 127 | } // namespace nfd |