NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2017 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 
22 #include "tcp-transport.hpp"
24 #include "net/face-uri.hpp"
25 #include "util/logger.hpp"
26 
28 // DEBUG level: connect, close, pause, resume.
29 
30 namespace ndn {
31 
32 TcpTransport::TcpTransport(const std::string& host, const std::string& port/* = "6363"*/)
33  : m_host(host)
34  , m_port(port)
35 {
36 }
37 
38 TcpTransport::~TcpTransport() = default;
39 
40 shared_ptr<TcpTransport>
41 TcpTransport::create(const std::string& uri)
42 {
43  const auto hostAndPort(getSocketHostAndPortFromUri(uri));
44  return make_shared<TcpTransport>(hostAndPort.first, hostAndPort.second);
45 }
46 
47 std::pair<std::string, std::string>
48 TcpTransport::getSocketHostAndPortFromUri(const std::string& uriString)
49 {
50  std::string host = "localhost";
51  std::string port = "6363";
52 
53  if (uriString.empty()) {
54  return {host, port};
55  }
56 
57  try {
58  const FaceUri uri(uriString);
59 
60  const std::string scheme = uri.getScheme();
61  if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6") {
62  BOOST_THROW_EXCEPTION(Error("Cannot create TcpTransport from \"" + scheme + "\" URI"));
63  }
64 
65  if (!uri.getHost().empty()) {
66  host = uri.getHost();
67  }
68 
69  if (!uri.getPort().empty()) {
70  port = uri.getPort();
71  }
72  }
73  catch (const FaceUri::Error& error) {
74  BOOST_THROW_EXCEPTION(Error(error.what()));
75  }
76 
77  return {host, port};
78 }
79 
80 void
81 TcpTransport::connect(boost::asio::io_service& ioService,
82  const ReceiveCallback& receiveCallback)
83 {
84  NDN_LOG_DEBUG("connect host=" << m_host << " port=" << m_port);
85 
86  if (m_impl == nullptr) {
87  Transport::connect(ioService, receiveCallback);
88 
89  m_impl = make_shared<Impl>(ref(*this), ref(ioService));
90  }
91 
92  boost::asio::ip::tcp::resolver::query query(m_host, m_port);
93  m_impl->connect(query);
94 }
95 
96 void
98 {
99  BOOST_ASSERT(m_impl != nullptr);
100  m_impl->send(wire);
101 }
102 
103 void
104 TcpTransport::send(const Block& header, const Block& payload)
105 {
106  BOOST_ASSERT(m_impl != nullptr);
107  m_impl->send(header, payload);
108 }
109 
110 void
112 {
113  BOOST_ASSERT(m_impl != nullptr);
114  NDN_LOG_DEBUG("close");
115  m_impl->close();
116  m_impl.reset();
117 }
118 
119 void
121 {
122  if (m_impl != nullptr) {
123  NDN_LOG_DEBUG("pause");
124  m_impl->pause();
125  }
126 }
127 
128 void
130 {
131  BOOST_ASSERT(m_impl != nullptr);
132  NDN_LOG_DEBUG("resume");
133  m_impl->resume();
134 }
135 
136 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
const std::string & getHost() const
get host (domain)
Definition: face-uri.hpp:120
void resume() override
resume the transport
void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback) override
asynchronously open the connection
const std::string & getPort() const
get port
Definition: face-uri.hpp:127
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
a transport using TCP socket
void send(const Block &wire) override
send a TLV block through the transport
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE std::string getSocketHostAndPortFromUri(const std::string &uri)
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:32
#define NDN_LOG_DEBUG(expression)
Definition: logger.hpp:35
static shared_ptr< TcpTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
void close() override
Close the connection.
const std::string & getScheme() const
get scheme (protocol)
Definition: face-uri.hpp:113
TcpTransport(const std::string &host, const std::string &port="6363")
void pause() override
pause the transport
function< void(const Block &wire)> ReceiveCallback
Definition: transport.hpp:47
virtual void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
asynchronously open the connection
Definition: transport.cpp:44
~TcpTransport() override