blob: 20952abe87ee59ba3828ebb996d74bbfa47625e1 [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/**
Alexander Afanasyev57e00362016-06-23 13:22:54 -07003 * Copyright (c) 2013-2016 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 "common.hpp"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080023
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080024#include "tcp-transport.hpp"
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080025#include "stream-transport.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070026#include "util/face-uri.hpp"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080027
28namespace ndn {
29
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080030TcpTransport::TcpTransport(const std::string& host, const std::string& port/* = "6363"*/)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080031 : m_host(host)
32 , m_port(port)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080033{
34}
35
36TcpTransport::~TcpTransport()
37{
38}
39
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 {
Alexander Afanasyev57e00362016-06-23 13:22:54 -070058 const util::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 }
73 catch (const util::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{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080084 if (!static_cast<bool>(m_impl)) {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080085 Transport::connect(ioService, receiveCallback);
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000086
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070087 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080088 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080089
90 boost::asio::ip::tcp::resolver::query query(m_host, m_port);
91 m_impl->connect(query);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080092}
93
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000094void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080095TcpTransport::send(const Block& wire)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080096{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070097 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080098 m_impl->send(wire);
99}
100
101void
102TcpTransport::send(const Block& header, const Block& payload)
103{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700104 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800105 m_impl->send(header, payload);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800106}
107
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000108void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800109TcpTransport::close()
110{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700111 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800112 m_impl->close();
Alexander Afanasyevbc5830a2014-07-11 15:02:38 -0700113 m_impl.reset();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800114}
115
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000116void
117TcpTransport::pause()
118{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700119 if (static_cast<bool>(m_impl)) {
120 m_impl->pause();
121 }
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000122}
123
124void
125TcpTransport::resume()
126{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700127 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000128 m_impl->resume();
129}
130
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800131} // namespace ndn