All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
unix-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #include "common.hpp"
8 
9 #include "unix-transport.hpp"
10 #include "stream-transport.hpp"
11 
12 #include "../face.hpp"
13 
14 namespace ndn {
15 
16 UnixTransport::UnixTransport(const std::string& unixSocket)
17  : m_unixSocket(unixSocket)
18 {
19 }
20 
22 {
23 }
24 
25 std::string
27 {
28  const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
29  try
30  {
31  return parsed.get<std::string>("unix_socket");
32  }
33  catch (boost::property_tree::ptree_bad_path& error)
34  {
35  // unix_socket not present, continue
36  }
37  catch (boost::property_tree::ptree_bad_data& error)
38  {
39  throw ConfigFile::Error(error.what());
40  }
41 
42  // no unix_socket specified so the default socket name
43  // depends on the protocol we're using
44  try
45  {
46  const std::string protocol = parsed.get<std::string>("protocol");
47  if (protocol == "ndnd-tlv-0.7")
48  {
49  return "/tmp/.ndnd.sock";
50  }
51  }
52  catch (boost::property_tree::ptree_bad_path& error)
53  {
54  return "/var/run/nfd.sock";
55  }
56  catch (boost::property_tree::ptree_bad_data& error)
57  {
58  throw ConfigFile::Error(error.what());
59  }
60 
61  // A we made here, then there's no unix_socket specified in the configuration
62  // file. A protocol is present, but it's not ndnd.
63  // Assume the default nfd.sock location.
64  return "/var/run/nfd.sock";
65 }
66 
67 void
68 UnixTransport::connect(boost::asio::io_service& ioService,
69  const ReceiveCallback& receiveCallback)
70 {
71  if (!static_cast<bool>(m_impl)) {
72  Transport::connect(ioService, receiveCallback);
73 
74  m_impl = make_shared<Impl> (boost::ref(*this),
75  boost::ref(ioService));
76  }
77 
78  m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
79 }
80 
81 void
83 {
84  m_impl->send(wire);
85 }
86 
87 void
88 UnixTransport::send(const Block& header, const Block& payload)
89 {
90  m_impl->send(header, payload);
91 }
92 
93 void
95 {
96  m_impl->close();
97 }
98 
99 void
101 {
102  m_impl->pause();
103 }
104 
105 void
107 {
108  m_impl->resume();
109 }
110 
111 }
virtual void connect(boost::asio::io_service &io_service, const ReceiveCallback &receiveCallback)
Connect transport.
Definition: transport.hpp:116
ptr_lib::function< void(const Block &wire)> ReceiveCallback
Definition: transport.hpp:25
virtual void resume()
static std::string getDefaultSocketName(const ConfigFile &config)
Determine the default NFD unix socket.
virtual void close()
Close the connection.
virtual void pause()
const Parsed & getParsedConfiguration() const
Definition: config-file.hpp:94
Class representing wire element of the NDN packet.
Definition: block.hpp:26
boost::property_tree::ptree Parsed
Definition: config-file.hpp:31
virtual void send(const Block &wire)
Set data to the host.
UnixTransport(const std::string &unixSocket)
Create Unix transport based on the socket specified in a well-known configuration file or fallback to...
virtual void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
Connect transport.