blob: 069f137046f7857bb74d4ecbe2d919fb9d0d07e2 [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2d491752017-07-14 21:32:05 +00002/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Chengyu Fan4381fb62015-01-14 11:37:04 -07004 * 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 Shang53df1632014-04-21 12:01:32 -070010 *
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 DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Wentao Shang53df1632014-04-21 12:01:32 -070025
26#include "websocket-factory.hpp"
27
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040028namespace nfd::face {
Wentao Shang53df1632014-04-21 12:01:32 -070029
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020030namespace ip = boost::asio::ip;
Wentao Shang53df1632014-04-21 12:01:32 -070031
Davide Pesaventoa3148082018-04-12 18:21:54 -040032NFD_LOG_INIT(WebSocketFactory);
Junxiao Shib47247d2017-01-24 15:09:16 +000033NFD_REGISTER_PROTOCOL_FACTORY(WebSocketFactory);
34
35const std::string&
Davide Pesaventod27841b2018-11-13 00:22:24 -050036WebSocketFactory::getId() noexcept
Junxiao Shib47247d2017-01-24 15:09:16 +000037{
38 static std::string id("websocket");
39 return id;
40}
41
Junxiao Shi3409cd32017-01-18 15:31:27 +000042void
Davide Pesaventod27841b2018-11-13 00:22:24 -050043WebSocketFactory::doProcessConfig(OptionalConfigSection configSection,
44 FaceSystem::ConfigContext& context)
Junxiao Shi3409cd32017-01-18 15:31:27 +000045{
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 Pesavento19779d82019-02-14 13:40:04 -050077 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.websocket." + key));
Junxiao Shi3409cd32017-01-18 15:31:27 +000078 }
79 }
80 }
81
82 if (!enableV4 && !enableV6) {
Davide Pesavento19779d82019-02-14 13:40:04 -050083 NDN_THROW(ConfigFile::Error(
Junxiao Shi3409cd32017-01-18 15:31:27 +000084 "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 Pesavento096f86a2018-11-10 19:51:45 -050088 if (context.isDryRun) {
89 return;
Junxiao Shi3409cd32017-01-18 15:31:27 +000090 }
91
Davide Pesavento096f86a2018-11-10 19:51:45 -050092 if (!wantListen) {
93 if (!m_channels.empty()) {
94 NFD_LOG_WARN("Cannot disable WebSocket channels after initialization");
Junxiao Shi3409cd32017-01-18 15:31:27 +000095 }
Davide Pesavento096f86a2018-11-10 19:51:45 -050096 return;
97 }
Junxiao Shi3409cd32017-01-18 15:31:27 +000098
Davide Pesavento096f86a2018-11-10 19:51:45 -050099 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 Shi3409cd32017-01-18 15:31:27 +0000106
Davide Pesavento096f86a2018-11-10 19:51:45 -0500107 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 Shi3409cd32017-01-18 15:31:27 +0000112 }
113 }
114}
115
Wentao Shang53df1632014-04-21 12:01:32 -0700116shared_ptr<WebSocketChannel>
117WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
118{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400119 auto it = m_channels.find(endpoint);
120 if (it != m_channels.end())
121 return it->second;
Wentao Shang53df1632014-04-21 12:01:32 -0700122
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400123 auto channel = make_shared<WebSocketChannel>(endpoint);
Wentao Shang53df1632014-04-21 12:01:32 -0700124 m_channels[endpoint] = channel;
Wentao Shang53df1632014-04-21 12:01:32 -0700125 return channel;
126}
127
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200128std::vector<shared_ptr<const Channel>>
Davide Pesaventod27841b2018-11-13 00:22:24 -0500129WebSocketFactory::doGetChannels() const
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600130{
Junxiao Shi3409cd32017-01-18 15:31:27 +0000131 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600132}
133
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400134} // namespace nfd::face