unix-stream-factory.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "unix-stream-factory.hpp"
27 #include "core/logger.hpp"
28 
29 #include <boost/filesystem.hpp>
30 
31 namespace nfd {
32 namespace face {
33 
34 NFD_LOG_INIT("UnixStreamFactory");
35 NFD_REGISTER_PROTOCOL_FACTORY(UnixStreamFactory);
36 
37 const std::string&
39 {
40  static std::string id("unix");
41  return id;
42 }
43 
44 
45 void
48 {
49  // unix
50  // {
51  // path /var/run/nfd.sock
52  // }
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  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.unix." + key));
72  }
73  }
74 
75  if (!context.isDryRun) {
76  auto channel = this->createChannel(path);
77  if (!channel->isListening()) {
78  channel->listen(context.addFace, nullptr);
79  }
80  }
81 }
82 
83 void
84 UnixStreamFactory::createFace(const FaceUri& uri,
85  ndn::nfd::FacePersistency persistency,
86  bool wantLocalFieldsEnabled,
87  const FaceCreatedCallback& onCreated,
88  const FaceCreationFailedCallback& onFailure)
89 {
90  onFailure(406, "Unsupported protocol");
91 }
92 
93 shared_ptr<UnixStreamChannel>
94 UnixStreamFactory::createChannel(const std::string& unixSocketPath)
95 {
96  boost::filesystem::path p(unixSocketPath);
97  p = boost::filesystem::canonical(p.parent_path()) / p.filename();
98  unix_stream::Endpoint endpoint(p.string());
99 
100  auto channel = findChannel(endpoint);
101  if (channel)
102  return channel;
103 
104  channel = make_shared<UnixStreamChannel>(endpoint);
105  m_channels[endpoint] = channel;
106  return channel;
107 }
108 
109 std::vector<shared_ptr<const Channel>>
111 {
112  return getChannelsFromMap(m_channels);
113 }
114 
115 shared_ptr<UnixStreamChannel>
116 UnixStreamFactory::findChannel(const unix_stream::Endpoint& endpoint) const
117 {
118  auto i = m_channels.find(endpoint);
119  if (i != m_channels.end())
120  return i->second;
121  else
122  return nullptr;
123 }
124 
125 } // namespace face
126 } // namespace nfd
void createFace(const FaceUri &uri, ndn::nfd::FacePersistency persistency, bool wantLocalFieldsEnabled, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create Face using the supplied FaceUri.
#define NFD_REGISTER_PROTOCOL_FACTORY(PF)
registers a protocol factory
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:163
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:77
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...
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
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
Definition: channel.hpp:44
#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.
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when the face is created (as a response to incoming connec...
Definition: channel.hpp:38
boost::asio::local::stream_protocol::endpoint Endpoint
std::vector< shared_ptr< const Channel > > getChannels() const override
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context) override
process face_system.unix config section
static const std::string & getId()