Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | f9b880b | 2017-09-08 13:05:06 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 22 | #include "tcp-transport.hpp" |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 23 | #include "stream-transport-with-resolver-impl.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 24 | #include "net/face-uri.hpp" |
Junxiao Shi | f9b880b | 2017-09-08 13:05:06 +0000 | [diff] [blame] | 25 | #include "util/logger.hpp" |
| 26 | |
| 27 | NDN_LOG_INIT(ndn.TcpTransport); |
| 28 | // DEBUG level: connect, close, pause, resume. |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 32 | TcpTransport::TcpTransport(const std::string& host, const std::string& port/* = "6363"*/) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 33 | : m_host(host) |
| 34 | , m_port(port) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 38 | TcpTransport::~TcpTransport() = default; |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 39 | |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 40 | shared_ptr<TcpTransport> |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 41 | TcpTransport::create(const std::string& uri) |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 42 | { |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 43 | const auto hostAndPort(getSocketHostAndPortFromUri(uri)); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 44 | return make_shared<TcpTransport>(hostAndPort.first, hostAndPort.second); |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | std::pair<std::string, std::string> |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 48 | TcpTransport::getSocketHostAndPortFromUri(const std::string& uriString) |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 49 | { |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 50 | std::string host = "localhost"; |
| 51 | std::string port = "6363"; |
| 52 | |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 53 | if (uriString.empty()) { |
| 54 | return {host, port}; |
| 55 | } |
| 56 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 57 | try { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 58 | const FaceUri uri(uriString); |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 60 | const std::string scheme = uri.getScheme(); |
| 61 | if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6") { |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 62 | BOOST_THROW_EXCEPTION(Error("Cannot create TcpTransport from \"" + scheme + "\" URI")); |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 65 | if (!uri.getHost().empty()) { |
| 66 | host = uri.getHost(); |
| 67 | } |
| 68 | |
| 69 | if (!uri.getPort().empty()) { |
| 70 | port = uri.getPort(); |
| 71 | } |
| 72 | } |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 73 | catch (const FaceUri::Error& error) { |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 74 | BOOST_THROW_EXCEPTION(Error(error.what())); |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | return {host, port}; |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 80 | void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 81 | TcpTransport::connect(boost::asio::io_service& ioService, |
| 82 | const ReceiveCallback& receiveCallback) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 83 | { |
Junxiao Shi | f9b880b | 2017-09-08 13:05:06 +0000 | [diff] [blame] | 84 | NDN_LOG_DEBUG("connect host=" << m_host << " port=" << m_port); |
| 85 | |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 86 | if (m_impl == nullptr) { |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 87 | Transport::connect(ioService, receiveCallback); |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 88 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 89 | m_impl = make_shared<Impl>(ref(*this), ref(ioService)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 90 | } |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 91 | |
| 92 | boost::asio::ip::tcp::resolver::query query(m_host, m_port); |
| 93 | m_impl->connect(query); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 96 | void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 97 | TcpTransport::send(const Block& wire) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 98 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 99 | BOOST_ASSERT(m_impl != nullptr); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 100 | m_impl->send(wire); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | TcpTransport::send(const Block& header, const Block& payload) |
| 105 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 106 | BOOST_ASSERT(m_impl != nullptr); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 107 | m_impl->send(header, payload); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 110 | void |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 111 | TcpTransport::close() |
| 112 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 113 | BOOST_ASSERT(m_impl != nullptr); |
Junxiao Shi | f9b880b | 2017-09-08 13:05:06 +0000 | [diff] [blame] | 114 | NDN_LOG_DEBUG("close"); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 115 | m_impl->close(); |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 116 | m_impl.reset(); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 119 | void |
| 120 | TcpTransport::pause() |
| 121 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 122 | if (m_impl != nullptr) { |
Junxiao Shi | f9b880b | 2017-09-08 13:05:06 +0000 | [diff] [blame] | 123 | NDN_LOG_DEBUG("pause"); |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 124 | m_impl->pause(); |
| 125 | } |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void |
| 129 | TcpTransport::resume() |
| 130 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 131 | BOOST_ASSERT(m_impl != nullptr); |
Junxiao Shi | f9b880b | 2017-09-08 13:05:06 +0000 | [diff] [blame] | 132 | NDN_LOG_DEBUG("resume"); |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 133 | m_impl->resume(); |
| 134 | } |
| 135 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 136 | } // namespace ndn |