blob: 6a875631477869a685c853aceeeed5cd960eca29 [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/*
Junxiao Shi3409cd32017-01-18 15:31:27 +00003 * Copyright (c) 2014-2017, 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
Yanbiao Liafb20592017-08-03 16:20:46 -070028#include <ndn-cxx/net/address-converter.hpp>
29
Wentao Shang53df1632014-04-21 12:01:32 -070030namespace nfd {
Junxiao Shi3409cd32017-01-18 15:31:27 +000031namespace face {
Wentao Shang53df1632014-04-21 12:01:32 -070032
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020033namespace ip = boost::asio::ip;
Wentao Shang53df1632014-04-21 12:01:32 -070034
Junxiao Shi3409cd32017-01-18 15:31:27 +000035NFD_LOG_INIT("WebSocketFactory");
Junxiao Shib47247d2017-01-24 15:09:16 +000036NFD_REGISTER_PROTOCOL_FACTORY(WebSocketFactory);
37
38const std::string&
39WebSocketFactory::getId()
40{
41 static std::string id("websocket");
42 return id;
43}
44
Junxiao Shi0ba6d642017-07-17 00:53:22 +000045WebSocketFactory::WebSocketFactory(const CtorParams& params)
46 : ProtocolFactory(params)
47{
48}
Junxiao Shi3409cd32017-01-18 15:31:27 +000049
50void
51WebSocketFactory::processConfig(OptionalConfigSection configSection,
52 FaceSystem::ConfigContext& context)
53{
54 // websocket
55 // {
56 // listen yes
57 // port 9696
58 // enable_v4 yes
59 // enable_v6 yes
60 // }
61
62 bool wantListen = false;
63 uint16_t port = 9696;
64 bool enableV4 = true;
65 bool enableV6 = true;
66
67 if (configSection) {
68 wantListen = true;
69 for (const auto& pair : *configSection) {
70 const std::string& key = pair.first;
71
72 if (key == "listen") {
73 wantListen = ConfigFile::parseYesNo(pair, "face_system.websocket");
74 }
75 else if (key == "port") {
76 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.websocket");
77 }
78 else if (key == "enable_v4") {
79 enableV4 = ConfigFile::parseYesNo(pair, "face_system.websocket");
80 }
81 else if (key == "enable_v6") {
82 enableV6 = ConfigFile::parseYesNo(pair, "face_system.websocket");
83 }
84 else {
85 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.websocket." + key));
86 }
87 }
88 }
89
90 if (!enableV4 && !enableV6) {
91 BOOST_THROW_EXCEPTION(ConfigFile::Error(
92 "IPv4 and IPv6 WebSocket channels have been disabled. Remove face_system.websocket section "
93 "to disable WebSocket channels or enable at least one channel type."));
94 }
95
96 if (!enableV4 && enableV6) {
97 // websocketpp's IPv6 socket always accepts IPv4 connections.
98 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
99 }
100
101 if (!context.isDryRun) {
102 if (!wantListen) {
103 if (!m_channels.empty()) {
104 NFD_LOG_WARN("Cannot close WebSocket channel after initialization");
105 }
106 return;
107 }
108
109 BOOST_ASSERT(enableV4);
110 websocket::Endpoint endpoint(enableV6 ? ip::tcp::v6() : ip::tcp::v4(), port);
111
112 auto channel = this->createChannel(endpoint);
113 if (!channel->isListening()) {
Junxiao Shi2d491752017-07-14 21:32:05 +0000114 channel->listen(this->addFace);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000115 if (m_channels.size() > 1) {
116 NFD_LOG_WARN("Adding WebSocket channel for new endpoint; cannot close existing channels");
117 }
118 }
119 }
120}
121
122void
Eric Newberry944f38b2017-07-20 20:54:22 -0400123WebSocketFactory::createFace(const CreateFaceParams& params,
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 Pesavento1d7e7af2015-10-10 23:54:08 +0200133 auto channel = findChannel(endpoint);
134 if (channel)
Wentao Shang53df1632014-04-21 12:01:32 -0700135 return channel;
136
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700137 channel = make_shared<WebSocketChannel>(endpoint);
Wentao Shang53df1632014-04-21 12:01:32 -0700138 m_channels[endpoint] = channel;
139
140 return channel;
141}
142
143shared_ptr<WebSocketChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200144WebSocketFactory::createChannel(const std::string& localIp, const std::string& localPort)
Wentao Shang53df1632014-04-21 12:01:32 -0700145{
Yanbiao Liafb20592017-08-03 16:20:46 -0700146 websocket::Endpoint endpoint(ndn::ip::addressFromString(localIp),
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200147 boost::lexical_cast<uint16_t>(localPort));
Wentao Shang53df1632014-04-21 12:01:32 -0700148 return createChannel(endpoint);
149}
150
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200151std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600152WebSocketFactory::getChannels() const
153{
Junxiao Shi3409cd32017-01-18 15:31:27 +0000154 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600155}
156
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200157shared_ptr<WebSocketChannel>
158WebSocketFactory::findChannel(const websocket::Endpoint& endpoint) const
159{
160 auto i = m_channels.find(endpoint);
161 if (i != m_channels.end())
162 return i->second;
163 else
164 return nullptr;
165}
166
Junxiao Shi3409cd32017-01-18 15:31:27 +0000167} // namespace face
Wentao Shang53df1632014-04-21 12:01:32 -0700168} // namespace nfd