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 | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 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/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 25 | |
| 26 | #include "websocket-factory.hpp" |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 30 | namespace face { |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 32 | namespace ip = boost::asio::ip; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 34 | NFD_LOG_INIT("WebSocketFactory"); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame^] | 35 | NFD_REGISTER_PROTOCOL_FACTORY(WebSocketFactory); |
| 36 | |
| 37 | const std::string& |
| 38 | WebSocketFactory::getId() |
| 39 | { |
| 40 | static std::string id("websocket"); |
| 41 | return id; |
| 42 | } |
| 43 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 44 | |
| 45 | void |
| 46 | WebSocketFactory::processConfig(OptionalConfigSection configSection, |
| 47 | FaceSystem::ConfigContext& context) |
| 48 | { |
| 49 | // websocket |
| 50 | // { |
| 51 | // listen yes |
| 52 | // port 9696 |
| 53 | // enable_v4 yes |
| 54 | // enable_v6 yes |
| 55 | // } |
| 56 | |
| 57 | bool wantListen = false; |
| 58 | uint16_t port = 9696; |
| 59 | bool enableV4 = true; |
| 60 | bool enableV6 = true; |
| 61 | |
| 62 | if (configSection) { |
| 63 | wantListen = true; |
| 64 | for (const auto& pair : *configSection) { |
| 65 | const std::string& key = pair.first; |
| 66 | |
| 67 | if (key == "listen") { |
| 68 | wantListen = ConfigFile::parseYesNo(pair, "face_system.websocket"); |
| 69 | } |
| 70 | else if (key == "port") { |
| 71 | port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.websocket"); |
| 72 | } |
| 73 | else if (key == "enable_v4") { |
| 74 | enableV4 = ConfigFile::parseYesNo(pair, "face_system.websocket"); |
| 75 | } |
| 76 | else if (key == "enable_v6") { |
| 77 | enableV6 = ConfigFile::parseYesNo(pair, "face_system.websocket"); |
| 78 | } |
| 79 | else { |
| 80 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.websocket." + key)); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (!enableV4 && !enableV6) { |
| 86 | BOOST_THROW_EXCEPTION(ConfigFile::Error( |
| 87 | "IPv4 and IPv6 WebSocket channels have been disabled. Remove face_system.websocket section " |
| 88 | "to disable WebSocket channels or enable at least one channel type.")); |
| 89 | } |
| 90 | |
| 91 | if (!enableV4 && enableV6) { |
| 92 | // websocketpp's IPv6 socket always accepts IPv4 connections. |
| 93 | BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel.")); |
| 94 | } |
| 95 | |
| 96 | if (!context.isDryRun) { |
| 97 | if (!wantListen) { |
| 98 | if (!m_channels.empty()) { |
| 99 | NFD_LOG_WARN("Cannot close WebSocket channel after initialization"); |
| 100 | } |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | BOOST_ASSERT(enableV4); |
| 105 | websocket::Endpoint endpoint(enableV6 ? ip::tcp::v6() : ip::tcp::v4(), port); |
| 106 | |
| 107 | auto channel = this->createChannel(endpoint); |
| 108 | if (!channel->isListening()) { |
| 109 | channel->listen(context.addFace); |
| 110 | if (m_channels.size() > 1) { |
| 111 | NFD_LOG_WARN("Adding WebSocket channel for new endpoint; cannot close existing channels"); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | WebSocketFactory::createFace(const FaceUri& uri, |
| 119 | ndn::nfd::FacePersistency persistency, |
| 120 | bool wantLocalFieldsEnabled, |
| 121 | const FaceCreatedCallback& onCreated, |
| 122 | const FaceCreationFailedCallback& onFailure) |
| 123 | { |
| 124 | onFailure(406, "Unsupported protocol"); |
| 125 | } |
| 126 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 127 | shared_ptr<WebSocketChannel> |
| 128 | WebSocketFactory::createChannel(const websocket::Endpoint& endpoint) |
| 129 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 130 | auto channel = findChannel(endpoint); |
| 131 | if (channel) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 132 | return channel; |
| 133 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 134 | channel = make_shared<WebSocketChannel>(endpoint); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 135 | m_channels[endpoint] = channel; |
| 136 | |
| 137 | return channel; |
| 138 | } |
| 139 | |
| 140 | shared_ptr<WebSocketChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 141 | WebSocketFactory::createChannel(const std::string& localIp, const std::string& localPort) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 142 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 143 | websocket::Endpoint endpoint(ip::address::from_string(localIp), |
| 144 | boost::lexical_cast<uint16_t>(localPort)); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 145 | return createChannel(endpoint); |
| 146 | } |
| 147 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 148 | std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 149 | WebSocketFactory::getChannels() const |
| 150 | { |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 151 | return getChannelsFromMap(m_channels); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 152 | } |
| 153 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 154 | shared_ptr<WebSocketChannel> |
| 155 | WebSocketFactory::findChannel(const websocket::Endpoint& endpoint) const |
| 156 | { |
| 157 | auto i = m_channels.find(endpoint); |
| 158 | if (i != m_channels.end()) |
| 159 | return i->second; |
| 160 | else |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 164 | } // namespace face |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 165 | } // namespace nfd |