blob: 7fb6341253a0f07056570beff9f3aff34a4975e9 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev20d2c582014-01-26 15:32:51 -08002/**
Junxiao Shi25467942017-06-30 02:53:14 +00003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080020 */
21
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080022#include "tcp-transport.hpp"
Junxiao Shi446de3c2016-07-25 22:38:16 +000023#include "stream-transport-with-resolver-impl.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000024#include "net/face-uri.hpp"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080025
26namespace ndn {
27
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080028TcpTransport::TcpTransport(const std::string& host, const std::string& port/* = "6363"*/)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080029 : m_host(host)
30 , m_port(port)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080031{
32}
33
Junxiao Shi446de3c2016-07-25 22:38:16 +000034TcpTransport::~TcpTransport() = default;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080035
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070036shared_ptr<TcpTransport>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070037TcpTransport::create(const std::string& uri)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070038{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070039 const auto hostAndPort(getSocketHostAndPortFromUri(uri));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080040 return make_shared<TcpTransport>(hostAndPort.first, hostAndPort.second);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070041}
42
43std::pair<std::string, std::string>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070044TcpTransport::getSocketHostAndPortFromUri(const std::string& uriString)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070045{
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070046 std::string host = "localhost";
47 std::string port = "6363";
48
Alexander Afanasyev57e00362016-06-23 13:22:54 -070049 if (uriString.empty()) {
50 return {host, port};
51 }
52
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080053 try {
Junxiao Shi25467942017-06-30 02:53:14 +000054 const FaceUri uri(uriString);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070055
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080056 const std::string scheme = uri.getScheme();
57 if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6") {
Alexander Afanasyev57e00362016-06-23 13:22:54 -070058 BOOST_THROW_EXCEPTION(Error("Cannot create TcpTransport from \"" + scheme + "\" URI"));
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070059 }
60
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080061 if (!uri.getHost().empty()) {
62 host = uri.getHost();
63 }
64
65 if (!uri.getPort().empty()) {
66 port = uri.getPort();
67 }
68 }
Junxiao Shi25467942017-06-30 02:53:14 +000069 catch (const FaceUri::Error& error) {
Alexander Afanasyev57e00362016-06-23 13:22:54 -070070 BOOST_THROW_EXCEPTION(Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080071 }
72
73 return {host, port};
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070074}
75
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000076void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080077TcpTransport::connect(boost::asio::io_service& ioService,
78 const ReceiveCallback& receiveCallback)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080079{
Junxiao Shi446de3c2016-07-25 22:38:16 +000080 if (m_impl == nullptr) {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080081 Transport::connect(ioService, receiveCallback);
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000082
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070083 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080084 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080085
86 boost::asio::ip::tcp::resolver::query query(m_host, m_port);
87 m_impl->connect(query);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080088}
89
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000090void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080091TcpTransport::send(const Block& wire)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080092{
Junxiao Shi446de3c2016-07-25 22:38:16 +000093 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080094 m_impl->send(wire);
95}
96
97void
98TcpTransport::send(const Block& header, const Block& payload)
99{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000100 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800101 m_impl->send(header, payload);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800102}
103
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000104void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800105TcpTransport::close()
106{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000107 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800108 m_impl->close();
Alexander Afanasyevbc5830a2014-07-11 15:02:38 -0700109 m_impl.reset();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800110}
111
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000112void
113TcpTransport::pause()
114{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000115 if (m_impl != nullptr) {
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700116 m_impl->pause();
117 }
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000118}
119
120void
121TcpTransport::resume()
122{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000123 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000124 m_impl->resume();
125}
126
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800127} // namespace ndn