unix-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
24 
25 #include "ndn-cxx/face.hpp"
26 #include "ndn-cxx/net/face-uri.hpp"
27 #include "ndn-cxx/util/logger.hpp"
28 
30 // DEBUG level: connect, close, pause, resume.
31 
32 namespace ndn {
33 
34 UnixTransport::UnixTransport(const std::string& unixSocket)
35  : m_unixSocket(unixSocket)
36 {
37 }
38 
40 
41 std::string
42 UnixTransport::getSocketNameFromUri(const std::string& uriString)
43 {
44  // Assume the default nfd.sock location.
45 #ifdef __linux__
46  std::string path = "/run/nfd.sock";
47 #else
48  std::string path = "/var/run/nfd.sock";
49 #endif // __linux__
50 
51  if (uriString.empty()) {
52  return path;
53  }
54 
55  try {
56  const FaceUri uri(uriString);
57 
58  if (uri.getScheme() != "unix") {
59  NDN_THROW(Error("Cannot create UnixTransport from \"" + uri.getScheme() + "\" URI"));
60  }
61 
62  if (!uri.getPath().empty()) {
63  path = uri.getPath();
64  }
65  }
66  catch (const FaceUri::Error& error) {
67  NDN_THROW_NESTED(Error(error.what()));
68  }
69 
70  return path;
71 }
72 
73 shared_ptr<UnixTransport>
74 UnixTransport::create(const std::string& uri)
75 {
76  return make_shared<UnixTransport>(getSocketNameFromUri(uri));
77 }
78 
79 void
80 UnixTransport::connect(boost::asio::io_service& ioService, ReceiveCallback receiveCallback)
81 {
82  NDN_LOG_DEBUG("connect path=" << m_unixSocket);
83 
84  if (m_impl == nullptr) {
85  Transport::connect(ioService, std::move(receiveCallback));
86  m_impl = make_shared<Impl>(*this, ioService);
87  }
88 
89  m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
90 }
91 
92 void
94 {
95  BOOST_ASSERT(m_impl != nullptr);
96  m_impl->send(wire);
97 }
98 
99 void
101 {
102  BOOST_ASSERT(m_impl != nullptr);
103  NDN_LOG_DEBUG("close");
104  m_impl->close();
105  m_impl.reset();
106 }
107 
108 void
110 {
111  if (m_impl != nullptr) {
112  NDN_LOG_DEBUG("pause");
113  m_impl->pause();
114  }
115 }
116 
117 void
119 {
120  BOOST_ASSERT(m_impl != nullptr);
121  NDN_LOG_DEBUG("resume");
122  m_impl->resume();
123 }
124 
125 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
std::function< void(const Block &)> ReceiveCallback
Definition: transport.hpp:54
virtual void connect(boost::asio::io_service &ioService, ReceiveCallback receiveCallback)
Asynchronously open the connection.
Definition: transport.cpp:32
A transport using Unix stream socket.
void resume() override
Resume the transport.
static shared_ptr< UnixTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
void pause() override
Pause the transport, canceling all pending operations.
void send(const Block &wire) override
Send a TLV block through the transport.
~UnixTransport() override
void connect(boost::asio::io_service &ioService, ReceiveCallback receiveCallback) override
Asynchronously open the connection.
UnixTransport(const std::string &unixSocket)
void close() override
Close the connection.
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
#define NDN_THROW(e)
Definition: exception.hpp:61
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition: logger.hpp:254
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition: logger.hpp:163
Definition: data.cpp:25