blob: 8b99f3efb59095f5accaad42fa3a511eaa736e68 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shif9b880b2017-09-08 13:05:06 +00002/*
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"
Junxiao Shif9b880b2017-09-08 13:05:06 +000025#include "util/logger.hpp"
26
27NDN_LOG_INIT(ndn.TcpTransport);
28// DEBUG level: connect, close, pause, resume.
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080029
30namespace ndn {
31
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080032TcpTransport::TcpTransport(const std::string& host, const std::string& port/* = "6363"*/)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080033 : m_host(host)
34 , m_port(port)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080035{
36}
37
Junxiao Shi446de3c2016-07-25 22:38:16 +000038TcpTransport::~TcpTransport() = default;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080039
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070040shared_ptr<TcpTransport>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070041TcpTransport::create(const std::string& uri)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070042{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070043 const auto hostAndPort(getSocketHostAndPortFromUri(uri));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080044 return make_shared<TcpTransport>(hostAndPort.first, hostAndPort.second);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070045}
46
47std::pair<std::string, std::string>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070048TcpTransport::getSocketHostAndPortFromUri(const std::string& uriString)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070049{
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070050 std::string host = "localhost";
51 std::string port = "6363";
52
Alexander Afanasyev57e00362016-06-23 13:22:54 -070053 if (uriString.empty()) {
54 return {host, port};
55 }
56
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080057 try {
Junxiao Shi25467942017-06-30 02:53:14 +000058 const FaceUri uri(uriString);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070059
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080060 const std::string scheme = uri.getScheme();
61 if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6") {
Alexander Afanasyev57e00362016-06-23 13:22:54 -070062 BOOST_THROW_EXCEPTION(Error("Cannot create TcpTransport from \"" + scheme + "\" URI"));
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070063 }
64
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080065 if (!uri.getHost().empty()) {
66 host = uri.getHost();
67 }
68
69 if (!uri.getPort().empty()) {
70 port = uri.getPort();
71 }
72 }
Junxiao Shi25467942017-06-30 02:53:14 +000073 catch (const FaceUri::Error& error) {
Alexander Afanasyev57e00362016-06-23 13:22:54 -070074 BOOST_THROW_EXCEPTION(Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080075 }
76
77 return {host, port};
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070078}
79
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000080void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080081TcpTransport::connect(boost::asio::io_service& ioService,
82 const ReceiveCallback& receiveCallback)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080083{
Junxiao Shif9b880b2017-09-08 13:05:06 +000084 NDN_LOG_DEBUG("connect host=" << m_host << " port=" << m_port);
85
Junxiao Shi446de3c2016-07-25 22:38:16 +000086 if (m_impl == nullptr) {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080087 Transport::connect(ioService, receiveCallback);
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000088
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070089 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080090 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080091
92 boost::asio::ip::tcp::resolver::query query(m_host, m_port);
93 m_impl->connect(query);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080094}
95
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000096void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080097TcpTransport::send(const Block& wire)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080098{
Junxiao Shi446de3c2016-07-25 22:38:16 +000099 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800100 m_impl->send(wire);
101}
102
103void
104TcpTransport::send(const Block& header, const Block& payload)
105{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000106 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800107 m_impl->send(header, payload);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800108}
109
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000110void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800111TcpTransport::close()
112{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000113 BOOST_ASSERT(m_impl != nullptr);
Junxiao Shif9b880b2017-09-08 13:05:06 +0000114 NDN_LOG_DEBUG("close");
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800115 m_impl->close();
Alexander Afanasyevbc5830a2014-07-11 15:02:38 -0700116 m_impl.reset();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800117}
118
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000119void
120TcpTransport::pause()
121{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000122 if (m_impl != nullptr) {
Junxiao Shif9b880b2017-09-08 13:05:06 +0000123 NDN_LOG_DEBUG("pause");
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700124 m_impl->pause();
125 }
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000126}
127
128void
129TcpTransport::resume()
130{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000131 BOOST_ASSERT(m_impl != nullptr);
Junxiao Shif9b880b2017-09-08 13:05:06 +0000132 NDN_LOG_DEBUG("resume");
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000133 m_impl->resume();
134}
135
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800136} // namespace ndn