blob: 7e7ac3e6a645fbc687ae0f38bce2b0a5eeccc72c [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/*
3 * Copyright (c) 2014-2018, 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"
27#include "core/global-io.hpp"
28
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
45 NFD_LOG_FACE_INFO("Creating transport");
46}
47
48void
Junxiao Shi6535f1e2015-10-08 13:02:18 -070049InternalForwarderTransport::receiveFromLink(const Block& packet)
50{
51 NFD_LOG_FACE_TRACE(__func__);
52
53 Packet p;
54 p.packet = packet;
55 this->receive(std::move(p));
56}
57
58void
59InternalForwarderTransport::doSend(Packet&& packet)
60{
61 NFD_LOG_FACE_TRACE(__func__);
62
63 this->emitSignal(afterSend, packet.packet);
64}
65
66void
67InternalForwarderTransport::doClose()
68{
69 NFD_LOG_FACE_TRACE(__func__);
70
71 this->setState(TransportState::CLOSED);
72}
73
Junxiao Shi6535f1e2015-10-08 13:02:18 -070074static void
75asyncReceive(InternalTransportBase* recipient, const Block& packet)
76{
77 getGlobalIoService().post([packet, recipient] {
78 recipient->receiveFromLink(packet);
79 });
80}
81
82void
83InternalClientTransport::connectToForwarder(InternalForwarderTransport* forwarderTransport)
84{
85 NFD_LOG_DEBUG(__func__ << " " << forwarderTransport);
86
87 m_fwToClientTransmitConn.disconnect();
88 m_clientToFwTransmitConn.disconnect();
89 m_fwTransportStateConn.disconnect();
90
91 if (forwarderTransport != nullptr) {
92 m_fwToClientTransmitConn = forwarderTransport->afterSend.connect(bind(&asyncReceive, this, _1));
93 m_clientToFwTransmitConn = this->afterSend.connect(bind(&asyncReceive, forwarderTransport, _1));
94 m_fwTransportStateConn = forwarderTransport->afterStateChange.connect(
95 [this] (TransportState oldState, TransportState newState) {
96 if (newState == TransportState::CLOSED) {
97 this->connectToForwarder(nullptr);
98 }
99 });
100 }
101}
102
103void
104InternalClientTransport::receiveFromLink(const Block& packet)
105{
106 if (m_receiveCallback) {
107 m_receiveCallback(packet);
108 }
109}
110
111void
112InternalClientTransport::send(const Block& wire)
113{
114 this->emitSignal(afterSend, wire);
115}
116
117void
118InternalClientTransport::send(const Block& header, const Block& payload)
119{
120 ndn::EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
121 encoder.appendByteArray(header.wire(), header.size());
122 encoder.appendByteArray(payload.wire(), payload.size());
123
124 this->send(encoder.block());
125}
126
127} // namespace face
128} // namespace nfd