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-2024 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/net/face-uri.hpp"
26 #include "ndn-cxx/util/logger.hpp"
27 
29 // DEBUG level: connect, close, pause, resume.
30 
31 namespace ndn {
32 
33 UnixTransport::UnixTransport(const std::string& unixSocket)
34  : m_unixSocket(unixSocket)
35 {
36 }
37 
39 
40 std::string
41 UnixTransport::getSocketNameFromUri(const std::string& uriString)
42 {
43  // Use path from the provided URI, if valid.
44  if (!uriString.empty()) {
45  try {
46  const FaceUri uri(uriString);
47  if (uri.getScheme() != "unix") {
48  NDN_THROW(Error("Cannot create UnixTransport from \"" + uri.getScheme() + "\" URI"));
49  }
50  if (!uri.getPath().empty()) {
51  return uri.getPath();
52  }
53  }
54  catch (const FaceUri::Error& error) {
55  NDN_THROW_NESTED(Error(error.what()));
56  }
57  }
58 
59  // Otherwise, use the default nfd.sock location.
60  return
61 #ifdef __linux__
62  "/run/nfd/nfd.sock";
63 #else
64  "/var/run/nfd/nfd.sock";
65 #endif // __linux__
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_context& ioCtx, ReceiveCallback receiveCallback)
76 {
77  NDN_LOG_DEBUG("connect path=" << m_unixSocket);
78 
79  if (m_impl == nullptr) {
80  Transport::connect(ioCtx, std::move(receiveCallback));
81  m_impl = make_shared<Impl>(*this, ioCtx);
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
96 {
97  BOOST_ASSERT(m_impl != nullptr);
98  NDN_LOG_DEBUG("close");
99  m_impl->close();
100  m_impl.reset();
101 }
102 
103 void
105 {
106  if (m_impl != nullptr) {
107  NDN_LOG_DEBUG("pause");
108  m_impl->pause();
109  }
110 }
111 
112 void
114 {
115  BOOST_ASSERT(m_impl != nullptr);
116  NDN_LOG_DEBUG("resume");
117  m_impl->resume();
118 }
119 
120 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
The underlying protocol and address used by a Face.
Definition: face-uri.hpp:47
std::function< void(const Block &)> ReceiveCallback
Definition: transport.hpp:54
virtual void connect(boost::asio::io_context &ioCtx, ReceiveCallback receiveCallback)
Asynchronously open the connection.
Definition: transport.cpp:32
A transport that uses a Unix stream socket for communication.
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
UnixTransport(const std::string &unixSocket)
void connect(boost::asio::io_context &ioCtx, ReceiveCallback receiveCallback) override
Asynchronously open the connection.
void close() override
Close the connection.
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:65
#define NDN_THROW(e)
Definition: exception.hpp:56
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition: logger.hpp:260
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition: logger.hpp:169
Definition: data.cpp:25