blob: 1898993965b129995174695440d63a30e3fbb7d9 [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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"
Junxiao Shi3409cd32017-01-18 15:31:27 +000027#include "core/logger.hpp"
Wentao Shang53df1632014-04-21 12:01:32 -070028
29namespace nfd {
Junxiao Shi3409cd32017-01-18 15:31:27 +000030namespace face {
Wentao Shang53df1632014-04-21 12:01:32 -070031
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020032namespace ip = boost::asio::ip;
Wentao Shang53df1632014-04-21 12:01:32 -070033
Junxiao Shi3409cd32017-01-18 15:31:27 +000034NFD_LOG_INIT("WebSocketFactory");
35
36void
37WebSocketFactory::processConfig(OptionalConfigSection configSection,
38 FaceSystem::ConfigContext& context)
39{
40 // websocket
41 // {
42 // listen yes
43 // port 9696
44 // enable_v4 yes
45 // enable_v6 yes
46 // }
47
48 bool wantListen = false;
49 uint16_t port = 9696;
50 bool enableV4 = true;
51 bool enableV6 = true;
52
53 if (configSection) {
54 wantListen = true;
55 for (const auto& pair : *configSection) {
56 const std::string& key = pair.first;
57
58 if (key == "listen") {
59 wantListen = ConfigFile::parseYesNo(pair, "face_system.websocket");
60 }
61 else if (key == "port") {
62 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.websocket");
63 }
64 else if (key == "enable_v4") {
65 enableV4 = ConfigFile::parseYesNo(pair, "face_system.websocket");
66 }
67 else if (key == "enable_v6") {
68 enableV6 = ConfigFile::parseYesNo(pair, "face_system.websocket");
69 }
70 else {
71 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.websocket." + key));
72 }
73 }
74 }
75
76 if (!enableV4 && !enableV6) {
77 BOOST_THROW_EXCEPTION(ConfigFile::Error(
78 "IPv4 and IPv6 WebSocket channels have been disabled. Remove face_system.websocket section "
79 "to disable WebSocket channels or enable at least one channel type."));
80 }
81
82 if (!enableV4 && enableV6) {
83 // websocketpp's IPv6 socket always accepts IPv4 connections.
84 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
85 }
86
87 if (!context.isDryRun) {
88 if (!wantListen) {
89 if (!m_channels.empty()) {
90 NFD_LOG_WARN("Cannot close WebSocket channel after initialization");
91 }
92 return;
93 }
94
95 BOOST_ASSERT(enableV4);
96 websocket::Endpoint endpoint(enableV6 ? ip::tcp::v6() : ip::tcp::v4(), port);
97
98 auto channel = this->createChannel(endpoint);
99 if (!channel->isListening()) {
100 channel->listen(context.addFace);
101 if (m_channels.size() > 1) {
102 NFD_LOG_WARN("Adding WebSocket channel for new endpoint; cannot close existing channels");
103 }
104 }
105 }
106}
107
108void
109WebSocketFactory::createFace(const FaceUri& uri,
110 ndn::nfd::FacePersistency persistency,
111 bool wantLocalFieldsEnabled,
112 const FaceCreatedCallback& onCreated,
113 const FaceCreationFailedCallback& onFailure)
114{
115 onFailure(406, "Unsupported protocol");
116}
117
Wentao Shang53df1632014-04-21 12:01:32 -0700118shared_ptr<WebSocketChannel>
119WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
120{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200121 auto channel = findChannel(endpoint);
122 if (channel)
Wentao Shang53df1632014-04-21 12:01:32 -0700123 return channel;
124
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700125 channel = make_shared<WebSocketChannel>(endpoint);
Wentao Shang53df1632014-04-21 12:01:32 -0700126 m_channels[endpoint] = channel;
127
128 return channel;
129}
130
131shared_ptr<WebSocketChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200132WebSocketFactory::createChannel(const std::string& localIp, const std::string& localPort)
Wentao Shang53df1632014-04-21 12:01:32 -0700133{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200134 websocket::Endpoint endpoint(ip::address::from_string(localIp),
135 boost::lexical_cast<uint16_t>(localPort));
Wentao Shang53df1632014-04-21 12:01:32 -0700136 return createChannel(endpoint);
137}
138
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200139std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600140WebSocketFactory::getChannels() const
141{
Junxiao Shi3409cd32017-01-18 15:31:27 +0000142 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600143}
144
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200145shared_ptr<WebSocketChannel>
146WebSocketFactory::findChannel(const websocket::Endpoint& endpoint) const
147{
148 auto i = m_channels.find(endpoint);
149 if (i != m_channels.end())
150 return i->second;
151 else
152 return nullptr;
153}
154
Junxiao Shi3409cd32017-01-18 15:31:27 +0000155} // namespace face
Wentao Shang53df1632014-04-21 12:01:32 -0700156} // namespace nfd