Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "internal-transport.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 29 | #include <boost/asio/post.hpp> |
| 30 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 31 | namespace nfd::face { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 33 | NFD_LOG_MEMBER_INIT(InternalForwarderTransport, InternalForwarderTransport); |
| 34 | NFD_LOG_MEMBER_INIT(InternalClientTransport, InternalClientTransport); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 36 | InternalForwarderTransport::InternalForwarderTransport(const FaceUri& localUri, const FaceUri& remoteUri, |
| 37 | ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 38 | { |
| 39 | this->setLocalUri(localUri); |
| 40 | this->setRemoteUri(remoteUri); |
| 41 | this->setScope(scope); |
| 42 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 43 | this->setLinkType(linkType); |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 44 | this->setMtu(MTU_UNLIMITED); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 45 | |
Davide Pesavento | a681a24 | 2019-03-29 23:48:27 -0400 | [diff] [blame] | 46 | NFD_LOG_FACE_DEBUG("Creating transport"); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 50 | InternalForwarderTransport::receivePacket(const Block& packet) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 51 | { |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 52 | boost::asio::post(getGlobalIoService(), [this, packet] { |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 53 | NFD_LOG_FACE_TRACE("Received: " << packet.size() << " bytes"); |
| 54 | receive(packet); |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 55 | }); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame] | 59 | InternalForwarderTransport::doSend(const Block& packet) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 60 | { |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 61 | NFD_LOG_FACE_TRACE("Sending to " << m_peer); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 62 | |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 63 | if (m_peer) |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 64 | m_peer->receivePacket(packet); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
| 68 | InternalForwarderTransport::doClose() |
| 69 | { |
| 70 | NFD_LOG_FACE_TRACE(__func__); |
| 71 | |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 72 | setState(TransportState::CLOSED); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 75 | InternalClientTransport::~InternalClientTransport() |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 76 | { |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 77 | if (m_forwarder != nullptr) { |
| 78 | m_forwarder->setPeer(nullptr); |
| 79 | } |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 83 | InternalClientTransport::connectToForwarder(InternalForwarderTransport* forwarder) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 84 | { |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 85 | NFD_LOG_DEBUG(__func__ << " " << forwarder); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 86 | |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 87 | if (m_forwarder != nullptr) { |
| 88 | // disconnect from the old forwarder transport |
| 89 | m_forwarder->setPeer(nullptr); |
| 90 | m_fwTransportStateConn.disconnect(); |
| 91 | } |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 92 | |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 93 | m_forwarder = forwarder; |
| 94 | |
| 95 | if (m_forwarder != nullptr) { |
| 96 | // connect to the new forwarder transport |
| 97 | m_forwarder->setPeer(this); |
| 98 | m_fwTransportStateConn = m_forwarder->afterStateChange.connect( |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 99 | [this] (TransportState oldState, TransportState newState) { |
| 100 | if (newState == TransportState::CLOSED) { |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 101 | connectToForwarder(nullptr); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 102 | } |
| 103 | }); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 108 | InternalClientTransport::receivePacket(const Block& packet) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 109 | { |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 110 | boost::asio::post(getGlobalIoService(), [this, packet] { |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 111 | NFD_LOG_TRACE("Received: " << packet.size() << " bytes"); |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 112 | if (m_receiveCallback) { |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 113 | m_receiveCallback(packet); |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 114 | } |
| 115 | }); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
| 119 | InternalClientTransport::send(const Block& wire) |
| 120 | { |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 121 | NFD_LOG_TRACE("Sending to " << m_forwarder); |
| 122 | |
| 123 | if (m_forwarder) |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 124 | m_forwarder->receivePacket(wire); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 127 | } // namespace nfd::face |