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