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-2022, 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 namespace nfd::face {
30 
31 NFD_LOG_MEMBER_INIT(InternalForwarderTransport, InternalForwarderTransport);
32 NFD_LOG_MEMBER_INIT(InternalClientTransport, InternalClientTransport);
33 
34 InternalForwarderTransport::InternalForwarderTransport(const FaceUri& localUri, const FaceUri& remoteUri,
35  ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType)
36 {
37  this->setLocalUri(localUri);
38  this->setRemoteUri(remoteUri);
39  this->setScope(scope);
40  this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
41  this->setLinkType(linkType);
42  this->setMtu(MTU_UNLIMITED);
43 
44  NFD_LOG_FACE_DEBUG("Creating transport");
45 }
46 
47 void
49 {
50  getGlobalIoService().post([this, packet] {
51  NFD_LOG_FACE_TRACE("Received: " << packet.size() << " bytes");
52  receive(packet);
53  });
54 }
55 
56 void
57 InternalForwarderTransport::doSend(const Block& packet)
58 {
59  NFD_LOG_FACE_TRACE("Sending to " << m_peer);
60 
61  if (m_peer)
62  m_peer->receivePacket(packet);
63 }
64 
65 void
67 {
68  NFD_LOG_FACE_TRACE(__func__);
69 
71 }
72 
74 {
75  if (m_forwarder != nullptr) {
76  m_forwarder->setPeer(nullptr);
77  }
78 }
79 
80 void
82 {
83  NFD_LOG_DEBUG(__func__ << " " << forwarder);
84 
85  if (m_forwarder != nullptr) {
86  // disconnect from the old forwarder transport
87  m_forwarder->setPeer(nullptr);
88  m_fwTransportStateConn.disconnect();
89  }
90 
91  m_forwarder = forwarder;
92 
93  if (m_forwarder != nullptr) {
94  // connect to the new forwarder transport
95  m_forwarder->setPeer(this);
96  m_fwTransportStateConn = m_forwarder->afterStateChange.connect(
97  [this] (TransportState oldState, TransportState newState) {
98  if (newState == TransportState::CLOSED) {
99  connectToForwarder(nullptr);
100  }
101  });
102  }
103 }
104 
105 void
107 {
108  getGlobalIoService().post([this, packet] {
109  NFD_LOG_TRACE("Received: " << packet.size() << " bytes");
110  if (m_receiveCallback) {
111  m_receiveCallback(packet);
112  }
113  });
114 }
115 
116 void
118 {
119  NFD_LOG_TRACE("Sending to " << m_forwarder);
120 
121  if (m_forwarder)
122  m_forwarder->receivePacket(wire);
123 }
124 
125 } // 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.
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 setPeer(InternalTransportBase *peer)
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:346
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
Signals when the transport state changes.
Definition: transport.hpp:299
void setState(TransportState newState)
Set transport state.
Definition: transport.cpp:175
void setLocalUri(const FaceUri &uri) noexcept
Definition: transport.hpp:334
void setLinkType(ndn::nfd::LinkType linkType) noexcept
Definition: transport.hpp:352
void setRemoteUri(const FaceUri &uri) noexcept
Definition: transport.hpp:340
#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:92
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:36