blob: 49e5740246c6bc2cc61c2c52e468d266dd6df52f [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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
28namespace nfd {
Junxiao Shi3409cd32017-01-18 15:31:27 +000029namespace face {
Wentao Shang53df1632014-04-21 12:01:32 -070030
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020031namespace ip = boost::asio::ip;
Wentao Shang53df1632014-04-21 12:01:32 -070032
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(WebSocketFactory);
Junxiao Shib47247d2017-01-24 15:09:16 +000034NFD_REGISTER_PROTOCOL_FACTORY(WebSocketFactory);
35
36const std::string&
Davide Pesaventod27841b2018-11-13 00:22:24 -050037WebSocketFactory::getId() noexcept
Junxiao Shib47247d2017-01-24 15:09:16 +000038{
39 static std::string id("websocket");
40 return id;
41}
42
Junxiao Shi3409cd32017-01-18 15:31:27 +000043void
Davide Pesaventod27841b2018-11-13 00:22:24 -050044WebSocketFactory::doProcessConfig(OptionalConfigSection configSection,
45 FaceSystem::ConfigContext& context)
Junxiao Shi3409cd32017-01-18 15:31:27 +000046{
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 {
Davide Pesavento19779d82019-02-14 13:40:04 -050078 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.websocket." + key));
Junxiao Shi3409cd32017-01-18 15:31:27 +000079 }
80 }
81 }
82
83 if (!enableV4 && !enableV6) {
Davide Pesavento19779d82019-02-14 13:40:04 -050084 NDN_THROW(ConfigFile::Error(
Junxiao Shi3409cd32017-01-18 15:31:27 +000085 "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 Pesavento096f86a2018-11-10 19:51:45 -050089 if (context.isDryRun) {
90 return;
Junxiao Shi3409cd32017-01-18 15:31:27 +000091 }
92
Davide Pesavento096f86a2018-11-10 19:51:45 -050093 if (!wantListen) {
94 if (!m_channels.empty()) {
95 NFD_LOG_WARN("Cannot disable WebSocket channels after initialization");
Junxiao Shi3409cd32017-01-18 15:31:27 +000096 }
Davide Pesavento096f86a2018-11-10 19:51:45 -050097 return;
98 }
Junxiao Shi3409cd32017-01-18 15:31:27 +000099
Davide Pesavento096f86a2018-11-10 19:51:45 -0500100 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 Shi3409cd32017-01-18 15:31:27 +0000107
Davide Pesavento096f86a2018-11-10 19:51:45 -0500108 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 Shi3409cd32017-01-18 15:31:27 +0000113 }
114 }
115}
116
Wentao Shang53df1632014-04-21 12:01:32 -0700117shared_ptr<WebSocketChannel>
118WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
119{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400120 auto it = m_channels.find(endpoint);
121 if (it != m_channels.end())
122 return it->second;
Wentao Shang53df1632014-04-21 12:01:32 -0700123
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400124 auto channel = make_shared<WebSocketChannel>(endpoint);
Wentao Shang53df1632014-04-21 12:01:32 -0700125 m_channels[endpoint] = channel;
Wentao Shang53df1632014-04-21 12:01:32 -0700126 return channel;
127}
128
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200129std::vector<shared_ptr<const Channel>>
Davide Pesaventod27841b2018-11-13 00:22:24 -0500130WebSocketFactory::doGetChannels() const
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600131{
Junxiao Shi3409cd32017-01-18 15:31:27 +0000132 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600133}
134
Junxiao Shi3409cd32017-01-18 15:31:27 +0000135} // namespace face
Wentao Shang53df1632014-04-21 12:01:32 -0700136} // namespace nfd