NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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 "common.hpp"
23 
24 #include "tcp-transport.hpp"
25 #include "stream-transport.hpp"
26 #include "util/face-uri.hpp"
27 
28 namespace ndn {
29 
30 TcpTransport::TcpTransport(const std::string& host, const std::string& port/* = "6363"*/)
31  : m_host(host)
32  , m_port(port)
33 {
34 }
35 
37 {
38 }
39 
40 shared_ptr<TcpTransport>
42 {
43  const auto hostAndPort(getDefaultSocketHostAndPort(config));
44  return make_shared<TcpTransport>(hostAndPort.first, hostAndPort.second);
45 }
46 
47 std::pair<std::string, std::string>
49 {
50  const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
51 
52  std::string host = "localhost";
53  std::string port = "6363";
54 
55  try {
56  const util::FaceUri uri(parsed.get<std::string>("transport", "tcp://" + host));
57 
58  const std::string scheme = uri.getScheme();
59  if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6") {
60  BOOST_THROW_EXCEPTION(Transport::Error("Cannot create TcpTransport from \"" +
61  scheme + "\" URI"));
62  }
63 
64  if (!uri.getHost().empty()) {
65  host = uri.getHost();
66  }
67 
68  if (!uri.getPort().empty()) {
69  port = uri.getPort();
70  }
71  }
72  catch (const util::FaceUri::Error& error) {
73  BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
74  }
75 
76  return {host, port};
77 }
78 
79 void
80 TcpTransport::connect(boost::asio::io_service& ioService,
81  const ReceiveCallback& receiveCallback)
82 {
83  if (!static_cast<bool>(m_impl)) {
84  Transport::connect(ioService, receiveCallback);
85 
86  m_impl = make_shared<Impl>(ref(*this), ref(ioService));
87  }
88 
89  boost::asio::ip::tcp::resolver::query query(m_host, m_port);
90  m_impl->connect(query);
91 }
92 
93 void
95 {
96  BOOST_ASSERT(static_cast<bool>(m_impl));
97  m_impl->send(wire);
98 }
99 
100 void
101 TcpTransport::send(const Block& header, const Block& payload)
102 {
103  BOOST_ASSERT(static_cast<bool>(m_impl));
104  m_impl->send(header, payload);
105 }
106 
107 void
109 {
110  BOOST_ASSERT(static_cast<bool>(m_impl));
111  m_impl->close();
112  m_impl.reset();
113 }
114 
115 void
117 {
118  if (static_cast<bool>(m_impl)) {
119  m_impl->pause();
120  }
121 }
122 
123 void
125 {
126  BOOST_ASSERT(static_cast<bool>(m_impl));
127  m_impl->resume();
128 }
129 
130 } // namespace ndn
virtual void connect(boost::asio::io_service &io_service, const ReceiveCallback &receiveCallback)
Connect transport.
Definition: transport.hpp:139
virtual void resume()
Copyright (c) 2011-2015 Regents of the University of California.
static shared_ptr< TcpTransport > create(const ConfigFile &config)
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
virtual void pause()
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
virtual void connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
Connect transport.
const std::string & getScheme() const
get scheme (protocol)
Definition: face-uri.hpp:109
virtual void send(const Block &wire)
Send block of data from wire through the transport.
function< void(const Block &wire)> ReceiveCallback
Definition: transport.hpp:49
const std::string & getPort() const
get port
Definition: face-uri.hpp:123
boost::property_tree::ptree Parsed
Definition: config-file.hpp:48
virtual void close()
Close the connection.
const std::string & getHost() const
get host (domain)
Definition: face-uri.hpp:116
TcpTransport(const std::string &host, const std::string &port="6363")
const Parsed & getParsedConfiguration() const
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE std::string getDefaultSocketHostAndPort(const ConfigFile &config)