blob: 2905ae4dd5f1a7b96464f963284dac102bde74b7 [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
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
Junxiao Shi3409cd32017-01-18 15:31:27 +000033NFD_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 Shi3409cd32017-01-18 15:31:27 +000043
44void
45WebSocketFactory::processConfig(OptionalConfigSection configSection,
46 FaceSystem::ConfigContext& context)
47{
48 // websocket
49 // {
50 // listen yes
51 // port 9696
52 // enable_v4 yes
53 // enable_v6 yes
54 // }
55
56 bool wantListen = false;
57 uint16_t port = 9696;
58 bool enableV4 = true;
59 bool enableV6 = true;
60
61 if (configSection) {
62 wantListen = true;
63 for (const auto& pair : *configSection) {
64 const std::string& key = pair.first;
65
66 if (key == "listen") {
67 wantListen = ConfigFile::parseYesNo(pair, "face_system.websocket");
68 }
69 else if (key == "port") {
70 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.websocket");
71 }
72 else if (key == "enable_v4") {
73 enableV4 = ConfigFile::parseYesNo(pair, "face_system.websocket");
74 }
75 else if (key == "enable_v6") {
76 enableV6 = ConfigFile::parseYesNo(pair, "face_system.websocket");
77 }
78 else {
79 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.websocket." + key));
80 }
81 }
82 }
83
84 if (!enableV4 && !enableV6) {
85 BOOST_THROW_EXCEPTION(ConfigFile::Error(
86 "IPv4 and IPv6 WebSocket channels have been disabled. Remove face_system.websocket section "
87 "to disable WebSocket channels or enable at least one channel type."));
88 }
89
90 if (!enableV4 && enableV6) {
91 // websocketpp's IPv6 socket always accepts IPv4 connections.
92 BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
93 }
94
95 if (!context.isDryRun) {
96 if (!wantListen) {
97 if (!m_channels.empty()) {
98 NFD_LOG_WARN("Cannot close WebSocket channel after initialization");
99 }
100 return;
101 }
102
103 BOOST_ASSERT(enableV4);
104 websocket::Endpoint endpoint(enableV6 ? ip::tcp::v6() : ip::tcp::v4(), port);
105
106 auto channel = this->createChannel(endpoint);
107 if (!channel->isListening()) {
Junxiao Shi2d491752017-07-14 21:32:05 +0000108 channel->listen(this->addFace);
Junxiao Shi3409cd32017-01-18 15:31:27 +0000109 if (m_channels.size() > 1) {
110 NFD_LOG_WARN("Adding WebSocket channel for new endpoint; cannot close existing channels");
111 }
112 }
113 }
114}
115
116void
Eric Newberry78e32b02017-04-01 14:34:44 +0000117WebSocketFactory::createFace(const FaceUri& remoteUri,
118 const ndn::optional<FaceUri>& localUri,
Junxiao Shi3409cd32017-01-18 15:31:27 +0000119 ndn::nfd::FacePersistency persistency,
120 bool wantLocalFieldsEnabled,
121 const FaceCreatedCallback& onCreated,
122 const FaceCreationFailedCallback& onFailure)
123{
124 onFailure(406, "Unsupported protocol");
125}
126
Wentao Shang53df1632014-04-21 12:01:32 -0700127shared_ptr<WebSocketChannel>
128WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
129{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200130 auto channel = findChannel(endpoint);
131 if (channel)
Wentao Shang53df1632014-04-21 12:01:32 -0700132 return channel;
133
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700134 channel = make_shared<WebSocketChannel>(endpoint);
Wentao Shang53df1632014-04-21 12:01:32 -0700135 m_channels[endpoint] = channel;
136
137 return channel;
138}
139
140shared_ptr<WebSocketChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200141WebSocketFactory::createChannel(const std::string& localIp, const std::string& localPort)
Wentao Shang53df1632014-04-21 12:01:32 -0700142{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200143 websocket::Endpoint endpoint(ip::address::from_string(localIp),
144 boost::lexical_cast<uint16_t>(localPort));
Wentao Shang53df1632014-04-21 12:01:32 -0700145 return createChannel(endpoint);
146}
147
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200148std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600149WebSocketFactory::getChannels() const
150{
Junxiao Shi3409cd32017-01-18 15:31:27 +0000151 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600152}
153
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200154shared_ptr<WebSocketChannel>
155WebSocketFactory::findChannel(const websocket::Endpoint& endpoint) const
156{
157 auto i = m_channels.find(endpoint);
158 if (i != m_channels.end())
159 return i->second;
160 else
161 return nullptr;
162}
163
Junxiao Shi3409cd32017-01-18 15:31:27 +0000164} // namespace face
Wentao Shang53df1632014-04-21 12:01:32 -0700165} // namespace nfd