blob: 6b493de31f67336d51bb3ddd1ecc4ff669d87cd9 [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 Pesavento15b55052018-01-27 19:09:28 -05003 * Copyright (c) 2014-2018, 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&
37WebSocketFactory::getId()
38{
39 static std::string id("websocket");
40 return id;
41}
42
Junxiao Shi0ba6d642017-07-17 00:53:22 +000043WebSocketFactory::WebSocketFactory(const CtorParams& params)
44 : ProtocolFactory(params)
45{
46}
Junxiao Shi3409cd32017-01-18 15:31:27 +000047
48void
49WebSocketFactory::processConfig(OptionalConfigSection configSection,
50 FaceSystem::ConfigContext& context)
51{
52 // websocket
53 // {
54 // listen yes
55 // port 9696
56 // enable_v4 yes
57 // enable_v6 yes
58 // }
59
60 bool wantListen = false;
61 uint16_t port = 9696;
62 bool enableV4 = true;
63 bool enableV6 = true;
64
65 if (configSection) {
66 wantListen = true;
67 for (const auto& pair : *configSection) {
68 const std::string& key = pair.first;
69
70 if (key == "listen") {
71 wantListen = ConfigFile::parseYesNo(pair, "face_system.websocket");
72 }
73 else if (key == "port") {
74 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.websocket");
75 }
76 else if (key == "enable_v4") {
77 enableV4 = ConfigFile::parseYesNo(pair, "face_system.websocket");
78 }
79 else if (key == "enable_v6") {
80 enableV6 = ConfigFile::parseYesNo(pair, "face_system.websocket");
81 }
82 else {
83 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.websocket." + key));
84 }
85 }
86 }
87
88 if (!enableV4 && !enableV6) {
89 BOOST_THROW_EXCEPTION(ConfigFile::Error(
90 "IPv4 and IPv6 WebSocket channels have been disabled. Remove face_system.websocket section "
91 "to disable WebSocket channels or enable at least one channel type."));
92 }
93
Davide Pesavento096f86a2018-11-10 19:51:45 -050094 if (context.isDryRun) {
95 return;
Junxiao Shi3409cd32017-01-18 15:31:27 +000096 }
97
Davide Pesavento096f86a2018-11-10 19:51:45 -050098 if (!wantListen) {
99 if (!m_channels.empty()) {
100 NFD_LOG_WARN("Cannot disable WebSocket channels after initialization");
Junxiao Shi3409cd32017-01-18 15:31:27 +0000101 }
Davide Pesavento096f86a2018-11-10 19:51:45 -0500102 return;
103 }
Junxiao Shi3409cd32017-01-18 15:31:27 +0000104
Davide Pesavento096f86a2018-11-10 19:51:45 -0500105 if (enableV4) {
106 websocket::Endpoint endpoint(ip::tcp::v4(), port);
107 auto v4Channel = this->createChannel(endpoint);
108 if (!v4Channel->isListening()) {
109 v4Channel->listen(this->addFace);
110 }
111 }
Junxiao Shi3409cd32017-01-18 15:31:27 +0000112
Davide Pesavento096f86a2018-11-10 19:51:45 -0500113 if (enableV6) {
114 websocket::Endpoint endpoint(ip::tcp::v6(), port);
115 auto v6Channel = this->createChannel(endpoint);
116 if (!v6Channel->isListening()) {
117 v6Channel->listen(this->addFace);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000118 }
119 }
120}
121
122void
Davide Pesavento15b55052018-01-27 19:09:28 -0500123WebSocketFactory::createFace(const CreateFaceRequest& req,
Junxiao Shi3409cd32017-01-18 15:31:27 +0000124 const FaceCreatedCallback& onCreated,
125 const FaceCreationFailedCallback& onFailure)
126{
127 onFailure(406, "Unsupported protocol");
128}
129
Wentao Shang53df1632014-04-21 12:01:32 -0700130shared_ptr<WebSocketChannel>
131WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
132{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400133 auto it = m_channels.find(endpoint);
134 if (it != m_channels.end())
135 return it->second;
Wentao Shang53df1632014-04-21 12:01:32 -0700136
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400137 auto channel = make_shared<WebSocketChannel>(endpoint);
Wentao Shang53df1632014-04-21 12:01:32 -0700138 m_channels[endpoint] = channel;
139
140 return channel;
141}
142
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200143std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600144WebSocketFactory::getChannels() const
145{
Junxiao Shi3409cd32017-01-18 15:31:27 +0000146 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600147}
148
Junxiao Shi3409cd32017-01-18 15:31:27 +0000149} // namespace face
Wentao Shang53df1632014-04-21 12:01:32 -0700150} // namespace nfd