unix-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "unix-transport.hpp"
24 
25 #include "../face.hpp"
26 #include "util/face-uri.hpp"
27 
28 namespace ndn {
29 
30 UnixTransport::UnixTransport(const std::string& unixSocket)
31  : m_unixSocket(unixSocket)
32 {
33 }
34 
36 {
37 }
38 
39 std::string
40 UnixTransport::getSocketNameFromUri(const std::string& uriString)
41 {
42  // Assume the default nfd.sock location.
43  std::string path = "/var/run/nfd.sock";
44 
45  if (uriString.empty()) {
46  return path;
47  }
48 
49  try {
50  const util::FaceUri uri(uriString);
51 
52  if (uri.getScheme() != "unix") {
53  BOOST_THROW_EXCEPTION(Error("Cannot create UnixTransport from \"" +
54  uri.getScheme() + "\" URI"));
55  }
56 
57  if (!uri.getPath().empty()) {
58  path = uri.getPath();
59  }
60  }
61  catch (const util::FaceUri::Error& error) {
62  BOOST_THROW_EXCEPTION(Error(error.what()));
63  }
64 
65  return path;
66 }
67 
68 shared_ptr<UnixTransport>
69 UnixTransport::create(const std::string& uri)
70 {
71  return make_shared<UnixTransport>(getSocketNameFromUri(uri));
72 }
73 
74 void
75 UnixTransport::connect(boost::asio::io_service& ioService,
76  const ReceiveCallback& receiveCallback)
77 {
78  if (m_impl == nullptr) {
79  Transport::connect(ioService, receiveCallback);
80 
81  m_impl = make_shared<Impl>(ref(*this), ref(ioService));
82  }
83 
84  m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
85 }
86 
87 void
89 {
90  BOOST_ASSERT(m_impl != nullptr);
91  m_impl->send(wire);
92 }
93 
94 void
95 UnixTransport::send(const Block& header, const Block& payload)
96 {
97  BOOST_ASSERT(m_impl != nullptr);
98  m_impl->send(header, payload);
99 }
100 
101 void
103 {
104  BOOST_ASSERT(m_impl != nullptr);
105  m_impl->close();
106  m_impl.reset();
107 }
108 
109 void
111 {
112  if (m_impl != nullptr) {
113  m_impl->pause();
114  }
115 }
116 
117 void
119 {
120  BOOST_ASSERT(m_impl != nullptr);
121  m_impl->resume();
122 }
123 
124 } // namespace ndn
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
void send(const Block &wire) override
send a TLV block through the transport
static shared_ptr< UnixTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
void pause() override
pause the transport
void resume() override
resume the transport
~UnixTransport() override
void close() override
Close the connection.
void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback) override
asynchronously open the connection
UnixTransport(const std::string &unixSocket)
function< void(const Block &wire)> ReceiveCallback
Definition: transport.hpp:52
virtual void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
asynchronously open the connection
Definition: transport.cpp:44