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-2017, 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 
33 NFD_LOG_INIT("UnixStreamFactory");
34 NFD_REGISTER_PROTOCOL_FACTORY(UnixStreamFactory);
35 
36 const std::string&
38 {
39  static std::string id("unix");
40  return id;
41 }
42 
44  : ProtocolFactory(params)
45 {
46 }
47 
48 void
51 {
52  // unix
53  // {
54  // path /var/run/nfd.sock
55  // }
56 
57  if (!configSection) {
58  if (!context.isDryRun && !m_channels.empty()) {
59  NFD_LOG_WARN("Cannot disable unix channel after initialization");
60  }
61  return;
62  }
63 
64  std::string path = "/var/run/nfd.sock";
65 
66  for (const auto& pair : *configSection) {
67  const std::string& key = pair.first;
68  const ConfigSection& value = pair.second;
69 
70  if (key == "path") {
71  path = value.get_value<std::string>();
72  }
73  else {
74  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.unix." + key));
75  }
76  }
77 
78  if (!context.isDryRun) {
79  auto channel = this->createChannel(path);
80  if (!channel->isListening()) {
81  channel->listen(this->addFace, nullptr);
82  }
83  }
84 }
85 
86 void
88  const FaceCreatedCallback& onCreated,
89  const FaceCreationFailedCallback& onFailure)
90 {
91  onFailure(406, "Unsupported protocol");
92 }
93 
94 shared_ptr<UnixStreamChannel>
95 UnixStreamFactory::createChannel(const std::string& unixSocketPath)
96 {
97  boost::filesystem::path p(unixSocketPath);
98  p = boost::filesystem::canonical(p.parent_path()) / p.filename();
99  unix_stream::Endpoint endpoint(p.string());
100 
101  auto channel = findChannel(endpoint);
102  if (channel)
103  return channel;
104 
105  channel = make_shared<UnixStreamChannel>(endpoint);
106  m_channels[endpoint] = channel;
107  return channel;
108 }
109 
110 std::vector<shared_ptr<const Channel>>
112 {
113  return getChannelsFromMap(m_channels);
114 }
115 
116 shared_ptr<UnixStreamChannel>
117 UnixStreamFactory::findChannel(const unix_stream::Endpoint& endpoint) const
118 {
119  auto i = m_channels.find(endpoint);
120  if (i != m_channels.end())
121  return i->second;
122  else
123  return nullptr;
124 }
125 
126 } // namespace face
127 } // namespace nfd
FaceCreatedCallback addFace
callback when a new face is created
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
registers a protocol factory
void createFace(const CreateFaceParams &params, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create face using the supplied parameters.
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:163
std::vector< shared_ptr< const Channel > > getChannels() const override
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:85
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
UnixStreamFactory(const CtorParams &params)
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
Definition: channel.hpp:44
Provides support for an underlying protocol.
Parameters to ProtocolFactory constructor.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
shared_ptr< UnixStreamChannel > createChannel(const std::string &unixSocketPath)
Create stream-oriented Unix channel using specified socket path.
boost::asio::local::stream_protocol::endpoint Endpoint
Parameters to ProtocolFactory::createFace.
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context) override
process face_system.unix config section
static const std::string & getId()
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:35