tcp-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-2023 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 TcpTransport::TcpTransport(const std::string& host, const std::string& port)
34  : m_host(host)
35  , m_port(port)
36 {
37 }
38 
39 TcpTransport::~TcpTransport() = default;
40 
41 shared_ptr<TcpTransport>
42 TcpTransport::create(const std::string& uri)
43 {
44  auto [host, port] = getSocketHostAndPortFromUri(uri);
45  return make_shared<TcpTransport>(host, port);
46 }
47 
48 std::pair<std::string, std::string>
49 TcpTransport::getSocketHostAndPortFromUri(const std::string& uriString)
50 {
51  // Default host and port.
52  std::string host = "localhost";
53  std::string port = "6363";
54 
55  // Use host and port from the provided URI, if valid.
56  if (!uriString.empty()) {
57  try {
58  const FaceUri uri(uriString);
59 
60  const auto& scheme = uri.getScheme();
61  if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6") {
62  NDN_THROW(Error("Cannot create TcpTransport from \"" + scheme + "\" URI"));
63  }
64 
65  if (!uri.getHost().empty()) {
66  host = uri.getHost();
67  }
68  if (!uri.getPort().empty()) {
69  port = uri.getPort();
70  }
71  }
72  catch (const FaceUri::Error& error) {
73  NDN_THROW_NESTED(Error(error.what()));
74  }
75  }
76 
77  return {host, port};
78 }
79 
80 void
81 TcpTransport::connect(boost::asio::io_context& ioCtx, ReceiveCallback receiveCallback)
82 {
83  NDN_LOG_DEBUG("connect host=" << m_host << " port=" << m_port);
84 
85  if (m_impl == nullptr) {
86  Transport::connect(ioCtx, std::move(receiveCallback));
87  m_impl = make_shared<Impl>(*this, ioCtx);
88  }
89  m_impl->connect(m_host, m_port);
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
The underlying protocol and address used by a Face.
Definition: face-uri.hpp:47
A transport that uses a TCP socket for communication.
TcpTransport(const std::string &host, const std::string &port="6363")
void send(const Block &wire) override
Send a TLV block through the transport.
void pause() override
Pause the transport, canceling all pending operations.
static shared_ptr< TcpTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
~TcpTransport() override
void connect(boost::asio::io_context &ioCtx, ReceiveCallback receiveCallback) override
Asynchronously open the connection.
void close() override
Close the connection.
void resume() override
Resume the transport.
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
#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