NFD: Named Data Networking Forwarding Daemon 24.07-28-gdcc0e6e0
Loading...
Searching...
No Matches
websocket-factory.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2022, 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-factory.hpp"
27
28namespace nfd::face {
29
30namespace ip = boost::asio::ip;
31
32NFD_LOG_INIT(WebSocketFactory);
34
35const std::string&
37{
38 static std::string id("websocket");
39 return id;
40}
41
42void
43WebSocketFactory::doProcessConfig(OptionalConfigSection configSection,
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 {
77 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.websocket." + key));
78 }
79 }
80 }
81
82 if (!enableV4 && !enableV6) {
83 NDN_THROW(ConfigFile::Error(
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
88 if (context.isDryRun) {
89 return;
90 }
91
92 if (!wantListen) {
93 if (!m_channels.empty()) {
94 NFD_LOG_WARN("Cannot disable WebSocket channels after initialization");
95 }
96 return;
97 }
98
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 }
106
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);
112 }
113 }
114}
115
116shared_ptr<WebSocketChannel>
118{
119 auto it = m_channels.find(endpoint);
120 if (it != m_channels.end())
121 return it->second;
122
123 auto channel = make_shared<WebSocketChannel>(endpoint);
124 m_channels[endpoint] = channel;
125 return channel;
126}
127
128std::vector<shared_ptr<const Channel>>
129WebSocketFactory::doGetChannels() const
130{
131 return getChannelsFromMap(m_channels);
132}
133
134} // namespace nfd::face
static T parseNumber(const ConfigSection &node, const std::string &key, const std::string &sectionName)
Parse a numeric (integral or floating point) config option.
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
Parse a config option that can be either "yes" or "no".
Context for processing a config section in ProtocolFactory.
FaceCreatedCallback addFace
callback when a new face is created
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
Protocol factory for WebSocket.
static const std::string & getId() noexcept
shared_ptr< WebSocketChannel > createChannel(const websocket::Endpoint &localEndpoint)
Create WebSocket-based channel using websocket::Endpoint.
#define NFD_LOG_INIT(name)
Definition logger.hpp:31
#define NFD_LOG_WARN
Definition logger.hpp:40
boost::asio::ip::tcp::endpoint Endpoint
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
Registers a protocol factory.