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-2019, 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 {
30 namespace face {
31 
32 NFD_LOG_MEMBER_INIT(InternalForwarderTransport, InternalForwarderTransport);
33 NFD_LOG_MEMBER_INIT(InternalClientTransport, InternalClientTransport);
34 
35 InternalForwarderTransport::InternalForwarderTransport(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);
43  this->setMtu(MTU_UNLIMITED);
44 
45  NFD_LOG_FACE_DEBUG("Creating transport");
46 }
47 
48 void
50 {
51  getGlobalIoService().post([this, pkt = std::move(packet)] () mutable {
52  NFD_LOG_FACE_TRACE("Received: " << pkt.size() << " bytes");
53  receive(Packet{std::move(pkt)});
54  });
55 }
56 
57 void
58 InternalForwarderTransport::doSend(Packet&& packet)
59 {
60  NFD_LOG_FACE_TRACE("Sending to " << m_peer);
61 
62  if (m_peer)
63  m_peer->receivePacket(std::move(packet.packet));
64 }
65 
66 void
68 {
69  NFD_LOG_FACE_TRACE(__func__);
70 
72 }
73 
75 {
76  if (m_forwarder != nullptr) {
77  m_forwarder->setPeer(nullptr);
78  }
79 }
80 
81 void
83 {
84  NFD_LOG_DEBUG(__func__ << " " << forwarder);
85 
86  if (m_forwarder != nullptr) {
87  // disconnect from the old forwarder transport
88  m_forwarder->setPeer(nullptr);
89  m_fwTransportStateConn.disconnect();
90  }
91 
92  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(
98  [this] (TransportState oldState, TransportState newState) {
99  if (newState == TransportState::CLOSED) {
100  connectToForwarder(nullptr);
101  }
102  });
103  }
104 }
105 
106 void
108 {
109  getGlobalIoService().post([this, pkt = std::move(packet)] {
110  NFD_LOG_TRACE("Received: " << pkt.size() << " bytes");
111  if (m_receiveCallback) {
112  m_receiveCallback(pkt);
113  }
114  });
115 }
116 
117 void
119 {
120  NFD_LOG_TRACE("Sending to " << m_forwarder);
121 
122  if (m_forwarder)
123  m_forwarder->receivePacket(Block{wire});
124 }
125 
126 void
127 InternalClientTransport::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 
133  send(encoder.block());
134 }
135 
136 } // namespace face
137 } // namespace nfd
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:421
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:79
Implements a forwarder-side transport that can be paired with another transport.
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:445
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:100
TransportState
indicates the state of a transport
Definition: transport.hpp:46
void setMtu(ssize_t mtu)
Definition: transport.hpp:475
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
Definition: face-log.hpp:82
void receivePacket(Block &&packet) final
#define NFD_LOG_TRACE
Definition: logger.hpp:37
void receive(Packet &&packet)
receive a link-layer packet
Definition: transport.cpp:120
stores a packet along with the remote endpoint
Definition: transport.hpp:122
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)
virtual void receivePacket(Block &&packet)=0
void doClose() final
performs Transport specific operations to close the transport
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void connectToForwarder(InternalForwarderTransport *forwarder)
Connect to a forwarder-side transport.
the transport is closed, and can be safely deallocated
void send(const Block &wire) final
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:463
void setPeer(InternalTransportBase *peer)
void send(Packet &&packet)
send a link-layer packet
Definition: transport.cpp:100
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:154
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
#define NFD_LOG_MEMBER_INIT(cls, name)
Definition: logger.hpp:34
void setState(TransportState newState)
set transport state
Definition: transport.cpp:177
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:433
void receivePacket(Block &&packet) final
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:36