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-2019 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  std::string path = "/var/run/nfd.sock";
46 
47  if (uriString.empty()) {
48  return path;
49  }
50 
51  try {
52  const FaceUri uri(uriString);
53 
54  if (uri.getScheme() != "unix") {
55  NDN_THROW(Error("Cannot create UnixTransport from \"" + uri.getScheme() + "\" URI"));
56  }
57 
58  if (!uri.getPath().empty()) {
59  path = uri.getPath();
60  }
61  }
62  catch (const FaceUri::Error& error) {
63  NDN_THROW_NESTED(Error(error.what()));
64  }
65 
66  return path;
67 }
68 
69 shared_ptr<UnixTransport>
70 UnixTransport::create(const std::string& uri)
71 {
72  return make_shared<UnixTransport>(getSocketNameFromUri(uri));
73 }
74 
75 void
76 UnixTransport::connect(boost::asio::io_service& ioService,
77  const ReceiveCallback& receiveCallback)
78 {
79  NDN_LOG_DEBUG("connect path=" << m_unixSocket);
80 
81  if (m_impl == nullptr) {
82  Transport::connect(ioService, receiveCallback);
83 
84  m_impl = make_shared<Impl>(ref(*this), ref(ioService));
85  }
86 
87  m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
88 }
89 
90 void
92 {
93  BOOST_ASSERT(m_impl != nullptr);
94  m_impl->send(wire);
95 }
96 
97 void
98 UnixTransport::send(const Block& header, const Block& payload)
99 {
100  BOOST_ASSERT(m_impl != nullptr);
101  m_impl->send(header, payload);
102 }
103 
104 void
106 {
107  BOOST_ASSERT(m_impl != nullptr);
108  NDN_LOG_DEBUG("close");
109  m_impl->close();
110  m_impl.reset();
111 }
112 
113 void
115 {
116  if (m_impl != nullptr) {
117  NDN_LOG_DEBUG("pause");
118  m_impl->pause();
119  }
120 }
121 
122 void
124 {
125  BOOST_ASSERT(m_impl != nullptr);
126  NDN_LOG_DEBUG("resume");
127  m_impl->resume();
128 }
129 
130 } // namespace ndn
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
a transport using Unix stream socket
Definition: data.cpp:26
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition: logger.hpp:262
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition: logger.hpp:163
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.
#define NDN_THROW(e)
Definition: exception.hpp:61
void pause() override
pause the transport
void resume() override
resume the transport
~UnixTransport() override
void close() override
Close the connection.
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
const std::string & getScheme() const
get scheme (protocol)
Definition: face-uri.hpp:109
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:46
virtual void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
asynchronously open the connection
Definition: transport.cpp:39
const std::string & getPath() const
get path
Definition: face-uri.hpp:130