Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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" |
| 27 | |
| 28 | namespace nfd { |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 29 | namespace face { |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 31 | namespace ip = boost::asio::ip; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 33 | NFD_LOG_INIT(WebSocketFactory); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 34 | NFD_REGISTER_PROTOCOL_FACTORY(WebSocketFactory); |
| 35 | |
| 36 | const std::string& |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 37 | WebSocketFactory::getId() noexcept |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 38 | { |
| 39 | static std::string id("websocket"); |
| 40 | return id; |
| 41 | } |
| 42 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 43 | void |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 44 | WebSocketFactory::doProcessConfig(OptionalConfigSection configSection, |
| 45 | FaceSystem::ConfigContext& context) |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 46 | { |
| 47 | // websocket |
| 48 | // { |
| 49 | // listen yes |
| 50 | // port 9696 |
| 51 | // enable_v4 yes |
| 52 | // enable_v6 yes |
| 53 | // } |
| 54 | |
| 55 | bool wantListen = false; |
| 56 | uint16_t port = 9696; |
| 57 | bool enableV4 = true; |
| 58 | bool enableV6 = true; |
| 59 | |
| 60 | if (configSection) { |
| 61 | wantListen = true; |
| 62 | for (const auto& pair : *configSection) { |
| 63 | const std::string& key = pair.first; |
| 64 | |
| 65 | if (key == "listen") { |
| 66 | wantListen = ConfigFile::parseYesNo(pair, "face_system.websocket"); |
| 67 | } |
| 68 | else if (key == "port") { |
| 69 | port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.websocket"); |
| 70 | } |
| 71 | else if (key == "enable_v4") { |
| 72 | enableV4 = ConfigFile::parseYesNo(pair, "face_system.websocket"); |
| 73 | } |
| 74 | else if (key == "enable_v6") { |
| 75 | enableV6 = ConfigFile::parseYesNo(pair, "face_system.websocket"); |
| 76 | } |
| 77 | else { |
| 78 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.websocket." + key)); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (!enableV4 && !enableV6) { |
| 84 | BOOST_THROW_EXCEPTION(ConfigFile::Error( |
| 85 | "IPv4 and IPv6 WebSocket channels have been disabled. Remove face_system.websocket section " |
| 86 | "to disable WebSocket channels or enable at least one channel type.")); |
| 87 | } |
| 88 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 89 | if (context.isDryRun) { |
| 90 | return; |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 93 | if (!wantListen) { |
| 94 | if (!m_channels.empty()) { |
| 95 | NFD_LOG_WARN("Cannot disable WebSocket channels after initialization"); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 96 | } |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 97 | return; |
| 98 | } |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 99 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 100 | if (enableV4) { |
| 101 | websocket::Endpoint endpoint(ip::tcp::v4(), port); |
| 102 | auto v4Channel = this->createChannel(endpoint); |
| 103 | if (!v4Channel->isListening()) { |
| 104 | v4Channel->listen(this->addFace); |
| 105 | } |
| 106 | } |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 107 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 108 | if (enableV6) { |
| 109 | websocket::Endpoint endpoint(ip::tcp::v6(), port); |
| 110 | auto v6Channel = this->createChannel(endpoint); |
| 111 | if (!v6Channel->isListening()) { |
| 112 | v6Channel->listen(this->addFace); |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 117 | shared_ptr<WebSocketChannel> |
| 118 | WebSocketFactory::createChannel(const websocket::Endpoint& endpoint) |
| 119 | { |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 120 | auto it = m_channels.find(endpoint); |
| 121 | if (it != m_channels.end()) |
| 122 | return it->second; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 123 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 124 | auto channel = make_shared<WebSocketChannel>(endpoint); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 125 | m_channels[endpoint] = channel; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 126 | return channel; |
| 127 | } |
| 128 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 129 | std::vector<shared_ptr<const Channel>> |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 130 | WebSocketFactory::doGetChannels() const |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 131 | { |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 132 | return getChannelsFromMap(m_channels); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 133 | } |
| 134 | |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 135 | } // namespace face |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 136 | } // namespace nfd |