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-2020, 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 /run/nfd.sock ; on Linux
50  // path /var/run/nfd.sock ; on other platforms
51  // }
52 
53  m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
54 
55  if (!configSection) {
56  if (!context.isDryRun && !m_channels.empty()) {
57  NFD_LOG_WARN("Cannot disable Unix channel after initialization");
58  }
59  return;
60  }
61 
62 #ifdef __linux__
63  std::string path = "/run/nfd.sock";
64 #else
65  std::string path = "/var/run/nfd.sock";
66 #endif // __linux__
67 
68  for (const auto& pair : *configSection) {
69  const std::string& key = pair.first;
70  const ConfigSection& value = pair.second;
71 
72  if (key == "path") {
73  path = value.get_value<std::string>();
74  }
75  else {
76  NDN_THROW(ConfigFile::Error("Unrecognized option face_system.unix." + key));
77  }
78  }
79 
80  if (context.isDryRun) {
81  return;
82  }
83 
84  auto channel = this->createChannel(path);
85  if (!channel->isListening()) {
86  channel->listen(this->addFace, nullptr);
87  }
88 }
89 
90 shared_ptr<UnixStreamChannel>
91 UnixStreamFactory::createChannel(const std::string& unixSocketPath)
92 {
93  boost::filesystem::path p(unixSocketPath);
94  p = boost::filesystem::canonical(p.parent_path()) / p.filename();
95  unix_stream::Endpoint endpoint(p.string());
96 
97  auto it = m_channels.find(endpoint);
98  if (it != m_channels.end())
99  return it->second;
100 
101  auto channel = make_shared<UnixStreamChannel>(endpoint, m_wantCongestionMarking);
102  m_channels[endpoint] = channel;
103  return channel;
104 }
105 
106 std::vector<shared_ptr<const Channel>>
107 UnixStreamFactory::doGetChannels() const
108 {
109  return getChannelsFromMap(m_channels);
110 }
111 
112 } // namespace face
113 } // namespace nfd
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory)
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
#define NDN_THROW(e)
Definition: exception.hpp:61
Protocol factory for stream-oriented Unix sockets.
FaceCreatedCallback addFace
callback when a new face is created
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
shared_ptr< UnixStreamChannel > createChannel(const std::string &unixSocketPath)
Create stream-oriented Unix channel using specified socket path.
boost::property_tree::ptree ConfigSection
a config file section
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:96
#define NFD_LOG_WARN
Definition: logger.hpp:40
boost::asio::local::stream_protocol::endpoint Endpoint
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
static const std::string & getId() noexcept