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