blob: afde1e001a0a33136bb0eebfbae538ef7554c5a0 [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 Afanasyev9d158f02015-02-17 21:30:19 -08003 * Copyright (c) 2013-2015 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>
41TcpTransport::create(const ConfigFile& config)
42{
43 const auto hostAndPort(getDefaultSocketHostAndPort(config));
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>
48TcpTransport::getDefaultSocketHostAndPort(const ConfigFile& config)
49{
50 const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080051
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070052 std::string host = "localhost";
53 std::string port = "6363";
54
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080055 try {
56 const util::FaceUri uri(parsed.get<std::string>("transport", "tcp://" + host));
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070057
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080058 const std::string scheme = uri.getScheme();
59 if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6") {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070060 BOOST_THROW_EXCEPTION(Transport::Error("Cannot create TcpTransport from \"" +
61 scheme + "\" URI"));
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070062 }
63
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080064 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) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070073 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080074 }
75
76 return {host, port};
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070077}
78
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000079void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080080TcpTransport::connect(boost::asio::io_service& ioService,
81 const ReceiveCallback& receiveCallback)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080082{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080083 if (!static_cast<bool>(m_impl)) {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080084 Transport::connect(ioService, receiveCallback);
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000085
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070086 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080087 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080088
89 boost::asio::ip::tcp::resolver::query query(m_host, m_port);
90 m_impl->connect(query);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080091}
92
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000093void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080094TcpTransport::send(const Block& wire)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080095{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070096 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080097 m_impl->send(wire);
98}
99
100void
101TcpTransport::send(const Block& header, const Block& payload)
102{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700103 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800104 m_impl->send(header, payload);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800105}
106
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000107void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800108TcpTransport::close()
109{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700110 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800111 m_impl->close();
Alexander Afanasyevbc5830a2014-07-11 15:02:38 -0700112 m_impl.reset();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800113}
114
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000115void
116TcpTransport::pause()
117{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700118 if (static_cast<bool>(m_impl)) {
119 m_impl->pause();
120 }
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000121}
122
123void
124TcpTransport::resume()
125{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700126 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000127 m_impl->resume();
128}
129
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800130} // namespace ndn