blob: e5d598ab509b5ef04b665badbf0799e4a4418a81 [file] [log] [blame]
Junxiao Shi6535f1e2015-10-08 13:02:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
Davide Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi6535f1e2015-10-08 13:02:18 -07004 * 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 Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
Junxiao Shi6535f1e2015-10-08 13:02:18 -070028
29namespace nfd {
30namespace face {
31
Davide Pesaventoa3148082018-04-12 18:21:54 -040032NFD_LOG_MEMBER_INIT(InternalForwarderTransport, InternalForwarderTransport);
33NFD_LOG_MEMBER_INIT(InternalClientTransport, InternalClientTransport);
Junxiao Shi6535f1e2015-10-08 13:02:18 -070034
Davide Pesaventoa3148082018-04-12 18:21:54 -040035InternalForwarderTransport::InternalForwarderTransport(const FaceUri& localUri, const FaceUri& remoteUri,
36 ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType)
Junxiao Shi6535f1e2015-10-08 13:02:18 -070037{
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 Shi5b8a2b22015-10-22 17:20:44 -070043 this->setMtu(MTU_UNLIMITED);
Junxiao Shi6535f1e2015-10-08 13:02:18 -070044
Davide Pesaventoa681a242019-03-29 23:48:27 -040045 NFD_LOG_FACE_DEBUG("Creating transport");
Junxiao Shi6535f1e2015-10-08 13:02:18 -070046}
47
48void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040049InternalForwarderTransport::receivePacket(const Block& packet)
Junxiao Shi6535f1e2015-10-08 13:02:18 -070050{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040051 getGlobalIoService().post([this, packet] {
52 NFD_LOG_FACE_TRACE("Received: " << packet.size() << " bytes");
53 receive(packet);
Davide Pesavento284bd622019-03-31 02:10:02 -040054 });
Junxiao Shi6535f1e2015-10-08 13:02:18 -070055}
56
57void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040058InternalForwarderTransport::doSend(const Block& packet, const EndpointId&)
Junxiao Shi6535f1e2015-10-08 13:02:18 -070059{
Davide Pesavento284bd622019-03-31 02:10:02 -040060 NFD_LOG_FACE_TRACE("Sending to " << m_peer);
Junxiao Shi6535f1e2015-10-08 13:02:18 -070061
Davide Pesavento284bd622019-03-31 02:10:02 -040062 if (m_peer)
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040063 m_peer->receivePacket(packet);
Junxiao Shi6535f1e2015-10-08 13:02:18 -070064}
65
66void
67InternalForwarderTransport::doClose()
68{
69 NFD_LOG_FACE_TRACE(__func__);
70
Davide Pesavento284bd622019-03-31 02:10:02 -040071 setState(TransportState::CLOSED);
Junxiao Shi6535f1e2015-10-08 13:02:18 -070072}
73
Davide Pesavento284bd622019-03-31 02:10:02 -040074InternalClientTransport::~InternalClientTransport()
Junxiao Shi6535f1e2015-10-08 13:02:18 -070075{
Davide Pesavento284bd622019-03-31 02:10:02 -040076 if (m_forwarder != nullptr) {
77 m_forwarder->setPeer(nullptr);
78 }
Junxiao Shi6535f1e2015-10-08 13:02:18 -070079}
80
81void
Davide Pesavento284bd622019-03-31 02:10:02 -040082InternalClientTransport::connectToForwarder(InternalForwarderTransport* forwarder)
Junxiao Shi6535f1e2015-10-08 13:02:18 -070083{
Davide Pesavento284bd622019-03-31 02:10:02 -040084 NFD_LOG_DEBUG(__func__ << " " << forwarder);
Junxiao Shi6535f1e2015-10-08 13:02:18 -070085
Davide Pesavento284bd622019-03-31 02:10:02 -040086 if (m_forwarder != nullptr) {
87 // disconnect from the old forwarder transport
88 m_forwarder->setPeer(nullptr);
89 m_fwTransportStateConn.disconnect();
90 }
Junxiao Shi6535f1e2015-10-08 13:02:18 -070091
Davide Pesavento284bd622019-03-31 02:10:02 -040092 m_forwarder = forwarder;
93
94 if (m_forwarder != nullptr) {
95 // connect to the new forwarder transport
96 m_forwarder->setPeer(this);
97 m_fwTransportStateConn = m_forwarder->afterStateChange.connect(
Junxiao Shi6535f1e2015-10-08 13:02:18 -070098 [this] (TransportState oldState, TransportState newState) {
99 if (newState == TransportState::CLOSED) {
Davide Pesavento284bd622019-03-31 02:10:02 -0400100 connectToForwarder(nullptr);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700101 }
102 });
103 }
104}
105
106void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400107InternalClientTransport::receivePacket(const Block& packet)
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700108{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400109 getGlobalIoService().post([this, packet] {
110 NFD_LOG_TRACE("Received: " << packet.size() << " bytes");
Davide Pesavento284bd622019-03-31 02:10:02 -0400111 if (m_receiveCallback) {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400112 m_receiveCallback(packet);
Davide Pesavento284bd622019-03-31 02:10:02 -0400113 }
114 });
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700115}
116
117void
118InternalClientTransport::send(const Block& wire)
119{
Davide Pesavento284bd622019-03-31 02:10:02 -0400120 NFD_LOG_TRACE("Sending to " << m_forwarder);
121
122 if (m_forwarder)
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400123 m_forwarder->receivePacket(wire);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700124}
125
126void
127InternalClientTransport::send(const Block& header, const Block& payload)
128{
129 ndn::EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
130 encoder.appendByteArray(header.wire(), header.size());
131 encoder.appendByteArray(payload.wire(), payload.size());
132
Davide Pesavento284bd622019-03-31 02:10:02 -0400133 send(encoder.block());
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700134}
135
136} // namespace face
137} // namespace nfd