NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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 
34 
37 {
38  this->setLocalUri(localUri);
39  this->setRemoteUri(remoteUri);
40  this->setScope(scope);
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, packet] {
52  NFD_LOG_FACE_TRACE("Received: " << packet.size() << " bytes");
53  receive(packet);
54  });
55 }
56 
57 void
58 InternalForwarderTransport::doSend(const Block& packet, const EndpointId&)
59 {
60  NFD_LOG_FACE_TRACE("Sending to " << m_peer);
61 
62  if (m_peer)
63  m_peer->receivePacket(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, packet] {
110  NFD_LOG_TRACE("Received: " << packet.size() << " bytes");
111  if (m_receiveCallback) {
112  m_receiveCallback(packet);
113  }
114  });
115 }
116 
117 void
119 {
120  NFD_LOG_TRACE("Sending to " << m_forwarder);
121 
122  if (m_forwarder)
123  m_forwarder->receivePacket(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
NFD_LOG_TRACE
#define NFD_LOG_TRACE
Definition: logger.hpp:37
nfd::face::TransportState
TransportState
Indicates the state of a transport.
Definition: transport.hpp:37
global.hpp
nfd::face::TransportState::CLOSED
@ CLOSED
the transport is closed, and can be safely deallocated
nfd::detail::SimulatorIo::post
void post(const std::function< void()> &callback)
Definition: global.cpp:35
nfd::face::MTU_UNLIMITED
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:91
nfd::face::Transport::setMtu
void setMtu(ssize_t mtu)
Definition: transport.hpp:448
nfd::face::Transport::setRemoteUri
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:406
nfd::face::NFD_LOG_MEMBER_INIT
NFD_LOG_MEMBER_INIT(InternalForwarderTransport, InternalForwarderTransport)
ndn::FaceUri
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:45
ndn::nfd::FACE_PERSISTENCY_PERMANENT
@ FACE_PERSISTENCY_PERMANENT
face is permanent
Definition: nfd-constants.hpp:49
nfd::face::InternalForwarderTransport::setPeer
void setPeer(InternalTransportBase *peer)
Definition: internal-transport.hpp:60
nfd::face::Transport::setScope
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:418
nfd::face::InternalClientTransport
Implements a client-side transport that can be paired with an InternalForwarderTransport.
Definition: internal-transport.hpp:85
nfd::face::Transport::setState
void setState(TransportState newState)
set transport state
Definition: transport.cpp:173
nfd::face::InternalClientTransport::receivePacket
void receivePacket(const Block &packet) final
Definition: internal-transport.cpp:107
nfd::face::InternalForwarderTransport::receivePacket
void receivePacket(const Block &packet) final
Definition: internal-transport.cpp:49
NFD_LOG_FACE_DEBUG
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
Definition: face-common.hpp:136
ndn::nfd::LinkType
LinkType
Definition: nfd-constants.hpp:57
ndn::Transport::m_receiveCallback
ReceiveCallback m_receiveCallback
Definition: transport.hpp:112
nfd::face::InternalClientTransport::connectToForwarder
void connectToForwarder(InternalForwarderTransport *forwarder)
Connect to a forwarder-side transport.
Definition: internal-transport.cpp:82
nfd::getGlobalIoService
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:49
nfd::face::InternalTransportBase::receivePacket
virtual void receivePacket(const Block &packet)=0
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
NFD_LOG_FACE_TRACE
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-common.hpp:133
nfd::face::InternalForwarderTransport::doClose
void doClose() final
performs Transport specific operations to close the transport
Definition: internal-transport.cpp:67
ndn::util::signal::ScopedConnection::disconnect
void disconnect()
Manually disconnect the connection.
Definition: scoped-connection.cpp:49
internal-transport.hpp
NFD_LOG_DEBUG
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
nfd::face::Transport::setLinkType
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:436
nfd::face::InternalForwarderTransport::InternalForwarderTransport
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)
Definition: internal-transport.cpp:35
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
nfd::face::InternalForwarderTransport
Implements a forwarder-side transport that can be paired with another transport.
Definition: internal-transport.hpp:51
nfd::face::Transport::setPersistency
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:150
ndn::Block::size
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:290
nfd::face::InternalClientTransport::~InternalClientTransport
~InternalClientTransport() final
Definition: internal-transport.cpp:74
nfd::face::EndpointId
uint64_t EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:65
nfd::face::Transport::receive
void receive(const Block &packet, const EndpointId &endpoint=0)
Pass a received link-layer packet to the upper layer for further processing.
Definition: transport.cpp:115
nfd::face::InternalClientTransport::send
void send(const Block &wire) final
send a TLV block through the transport
Definition: internal-transport.cpp:118
ndn::Block::wire
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition: block.cpp:281
ndn::nfd::FaceScope
FaceScope
Definition: nfd-constants.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38
nfd::face::Transport::setLocalUri
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:394
nfd::face::Transport::afterStateChange
signal::Signal< Transport, TransportState, TransportState > afterStateChange
signals when transport state changes
Definition: transport.hpp:244