Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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" |
| 27 | #include "core/global-io.hpp" |
| 28 | |
| 29 | namespace nfd { |
| 30 | namespace face { |
| 31 | |
| 32 | NFD_LOG_INCLASS_DEFINE(InternalForwarderTransport, "InternalForwarderTransport"); |
| 33 | |
| 34 | InternalForwarderTransport::InternalForwarderTransport( |
| 35 | const FaceUri& localUri, const FaceUri& remoteUri, |
| 36 | ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType) |
| 37 | { |
| 38 | this->setLocalUri(localUri); |
| 39 | this->setRemoteUri(remoteUri); |
| 40 | this->setScope(scope); |
| 41 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 42 | this->setLinkType(linkType); |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 43 | this->setMtu(MTU_UNLIMITED); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 44 | |
| 45 | NFD_LOG_FACE_INFO("Creating transport"); |
| 46 | } |
| 47 | |
| 48 | void |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame^] | 49 | InternalForwarderTransport::beforeChangePersistency(ndn::nfd::FacePersistency newPersistency) |
| 50 | { |
| 51 | if (newPersistency != ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
| 52 | BOOST_THROW_EXCEPTION( |
| 53 | std::invalid_argument("InternalForwarderTransport supports only FACE_PERSISTENCY_PERMANENT")); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 58 | InternalForwarderTransport::receiveFromLink(const Block& packet) |
| 59 | { |
| 60 | NFD_LOG_FACE_TRACE(__func__); |
| 61 | |
| 62 | Packet p; |
| 63 | p.packet = packet; |
| 64 | this->receive(std::move(p)); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | InternalForwarderTransport::doSend(Packet&& packet) |
| 69 | { |
| 70 | NFD_LOG_FACE_TRACE(__func__); |
| 71 | |
| 72 | this->emitSignal(afterSend, packet.packet); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | InternalForwarderTransport::doClose() |
| 77 | { |
| 78 | NFD_LOG_FACE_TRACE(__func__); |
| 79 | |
| 80 | this->setState(TransportState::CLOSED); |
| 81 | } |
| 82 | |
| 83 | NFD_LOG_INCLASS_DEFINE(InternalClientTransport, "InternalClientTransport"); |
| 84 | |
| 85 | static void |
| 86 | asyncReceive(InternalTransportBase* recipient, const Block& packet) |
| 87 | { |
| 88 | getGlobalIoService().post([packet, recipient] { |
| 89 | recipient->receiveFromLink(packet); |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | InternalClientTransport::connectToForwarder(InternalForwarderTransport* forwarderTransport) |
| 95 | { |
| 96 | NFD_LOG_DEBUG(__func__ << " " << forwarderTransport); |
| 97 | |
| 98 | m_fwToClientTransmitConn.disconnect(); |
| 99 | m_clientToFwTransmitConn.disconnect(); |
| 100 | m_fwTransportStateConn.disconnect(); |
| 101 | |
| 102 | if (forwarderTransport != nullptr) { |
| 103 | m_fwToClientTransmitConn = forwarderTransport->afterSend.connect(bind(&asyncReceive, this, _1)); |
| 104 | m_clientToFwTransmitConn = this->afterSend.connect(bind(&asyncReceive, forwarderTransport, _1)); |
| 105 | m_fwTransportStateConn = forwarderTransport->afterStateChange.connect( |
| 106 | [this] (TransportState oldState, TransportState newState) { |
| 107 | if (newState == TransportState::CLOSED) { |
| 108 | this->connectToForwarder(nullptr); |
| 109 | } |
| 110 | }); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | InternalClientTransport::receiveFromLink(const Block& packet) |
| 116 | { |
| 117 | if (m_receiveCallback) { |
| 118 | m_receiveCallback(packet); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | InternalClientTransport::send(const Block& wire) |
| 124 | { |
| 125 | this->emitSignal(afterSend, wire); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | InternalClientTransport::send(const Block& header, const Block& payload) |
| 130 | { |
| 131 | ndn::EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size()); |
| 132 | encoder.appendByteArray(header.wire(), header.size()); |
| 133 | encoder.appendByteArray(payload.wire(), payload.size()); |
| 134 | |
| 135 | this->send(encoder.block()); |
| 136 | } |
| 137 | |
| 138 | } // namespace face |
| 139 | } // namespace nfd |