NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
unix-stream-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-2019, 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 "unix-stream-factory.hpp"
27 
28 #include <boost/filesystem.hpp>
29 
30 namespace nfd {
31 namespace face {
32 
35 
36 const std::string&
38 {
39  static std::string id("unix");
40  return id;
41 }
42 
43 void
44 UnixStreamFactory::doProcessConfig(OptionalConfigSection configSection,
46 {
47  // unix
48  // {
49  // path /var/run/nfd.sock
50  // }
51 
52  m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
53 
54  if (!configSection) {
55  if (!context.isDryRun && !m_channels.empty()) {
56  NFD_LOG_WARN("Cannot disable Unix channel after initialization");
57  }
58  return;
59  }
60 
61  std::string path = "/var/run/nfd.sock";
62 
63  for (const auto& pair : *configSection) {
64  const std::string& key = pair.first;
65  const ConfigSection& value = pair.second;
66 
67  if (key == "path") {
68  path = value.get_value<std::string>();
69  }
70  else {
71  NDN_THROW(ConfigFile::Error("Unrecognized option face_system.unix." + key));
72  }
73  }
74 
75  if (context.isDryRun) {
76  return;
77  }
78 
79  auto channel = this->createChannel(path);
80  if (!channel->isListening()) {
81  channel->listen(this->addFace, nullptr);
82  }
83 }
84 
85 shared_ptr<UnixStreamChannel>
86 UnixStreamFactory::createChannel(const std::string& unixSocketPath)
87 {
88  boost::filesystem::path p(unixSocketPath);
89  p = boost::filesystem::canonical(p.parent_path()) / p.filename();
90  unix_stream::Endpoint endpoint(p.string());
91 
92  auto it = m_channels.find(endpoint);
93  if (it != m_channels.end())
94  return it->second;
95 
96  auto channel = make_shared<UnixStreamChannel>(endpoint, m_wantCongestionMarking);
97  m_channels[endpoint] = channel;
98  return channel;
99 }
100 
101 std::vector<shared_ptr<const Channel>>
102 UnixStreamFactory::doGetChannels() const
103 {
104  return getChannelsFromMap(m_channels);
105 }
106 
107 } // namespace face
108 } // namespace nfd
nfd::face::UnixStreamFactory::getId
static const std::string & getId() noexcept
Definition: unix-stream-factory.cpp:37
nfd::face::UnixStreamFactory
Protocol factory for stream-oriented Unix sockets.
Definition: unix-stream-factory.hpp:38
nfd::face::NFD_REGISTER_PROTOCOL_FACTORY
NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory)
NFD_LOG_WARN
#define NFD_LOG_WARN
Definition: logger.hpp:40
nfd::OptionalConfigSection
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::face::ProtocolFactory::addFace
FaceCreatedCallback addFace
callback when a new face is created
Definition: protocol-factory.hpp:239
nfd::face::FaceSystem::ConfigContext::generalConfig
GeneralConfig generalConfig
Definition: face-system.hpp:99
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
nfd::ConfigSection
boost::property_tree::ptree ConfigSection
a config file section
Definition: ndn-l3-protocol.hpp:39
nfd::face::UnixStreamFactory::createChannel
shared_ptr< UnixStreamChannel > createChannel(const std::string &unixSocketPath)
Create stream-oriented Unix channel using specified socket path.
Definition: unix-stream-factory.cpp:86
nfd::face::FaceSystem::ConfigContext::isDryRun
bool isDryRun
Definition: face-system.hpp:100
nfd::face::FaceSystem::GeneralConfig::wantCongestionMarking
bool wantCongestionMarking
Definition: face-system.hpp:91
nfd::unix_stream::Endpoint
boost::asio::local::stream_protocol::endpoint Endpoint
Definition: unix-stream-channel.hpp:34
nfd::face::ProtocolFactory::getChannelsFromMap
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
Definition: protocol-factory.hpp:171
unix-stream-factory.hpp
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
nfd::face::FaceSystem::ConfigContext
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:97