NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
unix-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "common.hpp"
23 
24 #include "unix-transport.hpp"
25 #include "stream-transport.hpp"
26 
27 #include "../face.hpp"
28 #include "util/face-uri.hpp"
29 
30 namespace ndn {
31 
32 UnixTransport::UnixTransport(const std::string& unixSocket)
33  : m_unixSocket(unixSocket)
34 {
35 }
36 
38 {
39 }
40 
41 std::string
42 UnixTransport::getDefaultSocketName(const ConfigFile& config)
43 {
44  const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
45 
46  try
47  {
48  const util::FaceUri uri(parsed.get<std::string>("transport"));
49 
50  if (uri.getScheme() != "unix")
51  {
52  BOOST_THROW_EXCEPTION(Transport::Error("Cannot create UnixTransport from \"" +
53  uri.getScheme() + "\" URI"));
54  }
55 
56  if (!uri.getPath().empty())
57  {
58  return uri.getPath();
59  }
60  }
61  catch (const boost::property_tree::ptree_bad_path& error)
62  {
63  // no transport specified
64  }
65  catch (const boost::property_tree::ptree_bad_data& error)
66  {
67  BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
68  }
69  catch (const util::FaceUri::Error& error)
70  {
71  BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
72  }
73 
74  // Assume the default nfd.sock location.
75  return "/var/run/nfd.sock";
76 }
77 
78 shared_ptr<UnixTransport>
80 {
81  return make_shared<UnixTransport>(getDefaultSocketName(config));
82 }
83 
84 void
85 UnixTransport::connect(boost::asio::io_service& ioService,
86  const ReceiveCallback& receiveCallback)
87 {
88  if (!static_cast<bool>(m_impl)) {
89  Transport::connect(ioService, receiveCallback);
90 
91  m_impl = make_shared<Impl>(ref(*this), ref(ioService));
92  }
93 
94  m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
95 }
96 
97 void
99 {
100  BOOST_ASSERT(static_cast<bool>(m_impl));
101  m_impl->send(wire);
102 }
103 
104 void
105 UnixTransport::send(const Block& header, const Block& payload)
106 {
107  BOOST_ASSERT(static_cast<bool>(m_impl));
108  m_impl->send(header, payload);
109 }
110 
111 void
113 {
114  BOOST_ASSERT(static_cast<bool>(m_impl));
115  m_impl->close();
116  m_impl.reset();
117 }
118 
119 void
121 {
122  if (static_cast<bool>(m_impl)) {
123  m_impl->pause();
124  }
125 }
126 
127 void
129 {
130  BOOST_ASSERT(static_cast<bool>(m_impl));
131  m_impl->resume();
132 }
133 
134 }
virtual void connect(boost::asio::io_service &io_service, const ReceiveCallback &receiveCallback)
Connect transport.
Definition: transport.hpp:132
virtual void resume()
Copyright (c) 2011-2015 Regents of the University of California.
const std::string & getPath() const
get path
Definition: face-uri.hpp:130
virtual void close()
Close the connection.
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
virtual void pause()
const Parsed & getParsedConfiguration() const
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
function< void(const Block &wire)> ReceiveCallback
Definition: transport.hpp:42
boost::property_tree::ptree Parsed
Definition: config-file.hpp:48
virtual void send(const Block &wire)
Send block of data from.
const std::string & getScheme() const
get scheme (protocol)
Definition: face-uri.hpp:109
static shared_ptr< UnixTransport > create(const ConfigFile &config)
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.