NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
websocket-factory.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, Regents of the University of California,
4  * 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.
10  *
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/>.
24  */
25 
26 #include "websocket-factory.hpp"
27 
28 namespace nfd {
29 namespace face {
30 
31 namespace ip = boost::asio::ip;
32 
35 
36 const std::string&
38 {
39  static std::string id("websocket");
40  return id;
41 }
42 
43 void
44 WebSocketFactory::doProcessConfig(OptionalConfigSection configSection,
46 {
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 {
78  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.websocket." + key));
79  }
80  }
81  }
82 
83  if (!enableV4 && !enableV6) {
84  BOOST_THROW_EXCEPTION(ConfigFile::Error(
85  "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 
89  if (context.isDryRun) {
90  return;
91  }
92 
93  if (!wantListen) {
94  if (!m_channels.empty()) {
95  NFD_LOG_WARN("Cannot disable WebSocket channels after initialization");
96  }
97  return;
98  }
99 
100  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  }
107 
108  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);
113  }
114  }
115 }
116 
117 shared_ptr<WebSocketChannel>
119 {
120  auto it = m_channels.find(endpoint);
121  if (it != m_channels.end())
122  return it->second;
123 
124  auto channel = make_shared<WebSocketChannel>(endpoint);
125  m_channels[endpoint] = channel;
126  return channel;
127 }
128 
129 std::vector<shared_ptr<const Channel>>
130 WebSocketFactory::doGetChannels() const
131 {
132  return getChannelsFromMap(m_channels);
133 }
134 
135 } // namespace face
136 } // namespace nfd
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a config option that can be either "yes" or "no"
Definition: config-file.cpp:59
shared_ptr< WebSocketChannel > createChannel(const websocket::Endpoint &localEndpoint)
Create WebSocket-based channel using websocket::Endpoint.
#define NFD_LOG_WARN
Definition: logger.hpp:40
NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory)
static const std::string & getId() noexcept
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
Protocol factory for WebSocket.
FaceCreatedCallback addFace
callback when a new face is created
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:96
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
boost::asio::ip::tcp::endpoint Endpoint
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31