blob: 7ffe530ee9e7fc110856a15cf6ecd3bb5245c240 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080026
27namespace ndn {
28
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080029TcpTransport::TcpTransport(const std::string& host, const std::string& port/* = "6363"*/)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080030 : m_host(host)
31 , m_port(port)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080032{
33}
34
35TcpTransport::~TcpTransport()
36{
37}
38
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000039void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080040TcpTransport::connect(boost::asio::io_service& ioService,
41 const ReceiveCallback& receiveCallback)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080042{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080043 if (!static_cast<bool>(m_impl)) {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080044 Transport::connect(ioService, receiveCallback);
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000045
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070046 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080047 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080048
49 boost::asio::ip::tcp::resolver::query query(m_host, m_port);
50 m_impl->connect(query);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080051}
52
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000053void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080054TcpTransport::send(const Block& wire)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080055{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070056 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080057 m_impl->send(wire);
58}
59
60void
61TcpTransport::send(const Block& header, const Block& payload)
62{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070063 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080064 m_impl->send(header, payload);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080065}
66
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000067void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080068TcpTransport::close()
69{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070070 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080071 m_impl->close();
Alexander Afanasyevbc5830a2014-07-11 15:02:38 -070072 m_impl.reset();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080073}
74
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000075void
76TcpTransport::pause()
77{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070078 if (static_cast<bool>(m_impl)) {
79 m_impl->pause();
80 }
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000081}
82
83void
84TcpTransport::resume()
85{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070086 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000087 m_impl->resume();
88}
89
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080090} // namespace ndn