internal-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, 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 "common/global.hpp"
28 
29 #include <boost/asio/post.hpp>
30 
31 namespace nfd::face {
32 
33 NFD_LOG_MEMBER_INIT(InternalForwarderTransport, InternalForwarderTransport);
34 NFD_LOG_MEMBER_INIT(InternalClientTransport, InternalClientTransport);
35 
36 InternalForwarderTransport::InternalForwarderTransport(const FaceUri& localUri, const FaceUri& remoteUri,
37  ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType)
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);
44  this->setMtu(MTU_UNLIMITED);
45 
46  NFD_LOG_FACE_DEBUG("Creating transport");
47 }
48 
49 void
51 {
52  boost::asio::post(getGlobalIoService(), [this, packet] {
53  NFD_LOG_FACE_TRACE("Received: " << packet.size() << " bytes");
54  receive(packet);
55  });
56 }
57 
58 void
59 InternalForwarderTransport::doSend(const Block& packet)
60 {
61  NFD_LOG_FACE_TRACE("Sending to " << m_peer);
62 
63  if (m_peer)
64  m_peer->receivePacket(packet);
65 }
66 
67 void
69 {
70  NFD_LOG_FACE_TRACE(__func__);
71 
73 }
74 
76 {
77  if (m_forwarder != nullptr) {
78  m_forwarder->setPeer(nullptr);
79  }
80 }
81 
82 void
84 {
85  NFD_LOG_DEBUG(__func__ << " " << forwarder);
86 
87  if (m_forwarder != nullptr) {
88  // disconnect from the old forwarder transport
89  m_forwarder->setPeer(nullptr);
90  m_fwTransportStateConn.disconnect();
91  }
92 
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(
99  [this] (TransportState oldState, TransportState newState) {
100  if (newState == TransportState::CLOSED) {
101  connectToForwarder(nullptr);
102  }
103  });
104  }
105 }
106 
107 void
109 {
110  boost::asio::post(getGlobalIoService(), [this, packet] {
111  NFD_LOG_TRACE("Received: " << packet.size() << " bytes");
112  if (m_receiveCallback) {
113  m_receiveCallback(packet);
114  }
115  });
116 }
117 
118 void
120 {
121  NFD_LOG_TRACE("Sending to " << m_forwarder);
122 
123  if (m_forwarder)
124  m_forwarder->receivePacket(wire);
125 }
126 
127 } // namespace nfd::face
void connectToForwarder(InternalForwarderTransport *forwarder)
Connect to a forwarder-side transport.
void receivePacket(const Block &packet) final
void send(const Block &block) final
Implements a forwarder-side transport that can be paired with another transport.
void setPeer(InternalTransportBase *peer) noexcept
InternalForwarderTransport(const FaceUri &localUri=FaceUri("internal://"), const FaceUri &remoteUri=FaceUri("internal://"), ndn::nfd::FaceScope scope=ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LinkType linkType=ndn::nfd::LINK_TYPE_POINT_TO_POINT)
void doClose() final
Performs Transport specific operations to close the transport.
void receivePacket(const Block &packet) final
virtual void receivePacket(const Block &packet)=0
void setScope(ndn::nfd::FaceScope scope) noexcept
Definition: transport.hpp:355
void receive(const Block &packet, const EndpointId &endpoint={})
Pass a received link-layer packet to the upper layer for further processing.
Definition: transport.cpp:101
void setPersistency(ndn::nfd::FacePersistency newPersistency)
Changes the persistency setting of the transport.
Definition: transport.cpp:152
void setMtu(ssize_t mtu) noexcept
Definition: transport.cpp:114
signal::Signal< Transport, TransportState, TransportState > afterStateChange
Called when the transport state changes.
Definition: transport.hpp:308
void setState(TransportState newState)
Set transport state.
Definition: transport.cpp:175
void setLocalUri(const FaceUri &uri) noexcept
Definition: transport.hpp:343
void setLinkType(ndn::nfd::LinkType linkType) noexcept
Definition: transport.hpp:361
void setRemoteUri(const FaceUri &uri) noexcept
Definition: transport.hpp:349
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_MEMBER_INIT(cls, name)
Definition: logger.hpp:34
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
#define NFD_LOG_TRACE
Definition: logger.hpp:37
TransportState
Indicates the state of a transport.
Definition: transport.hpp:37
@ CLOSED
the transport is closed, and can be safely deallocated
constexpr ssize_t MTU_UNLIMITED
Indicates that the transport has no limit on payload size.
Definition: transport.hpp:101
boost::asio::io_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.
Definition: global.cpp:36