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-2018, 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 "core/global-io.hpp"
28 
29 namespace nfd {
30 namespace face {
31 
32 NFD_LOG_MEMBER_INIT(InternalForwarderTransport, InternalForwarderTransport);
33 NFD_LOG_MEMBER_INIT(InternalClientTransport, InternalClientTransport);
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_INFO("Creating transport");
46 }
47 
48 void
50 {
51  NFD_LOG_FACE_TRACE(__func__);
52 
53  Packet p;
54  p.packet = packet;
55  this->receive(std::move(p));
56 }
57 
58 void
59 InternalForwarderTransport::doSend(Packet&& packet)
60 {
61  NFD_LOG_FACE_TRACE(__func__);
62 
63  this->emitSignal(afterSend, packet.packet);
64 }
65 
66 void
68 {
69  NFD_LOG_FACE_TRACE(__func__);
70 
72 }
73 
74 static void
75 asyncReceive(InternalTransportBase* recipient, const Block& packet)
76 {
77  getGlobalIoService().post([packet, recipient] {
78  recipient->receiveFromLink(packet);
79  });
80 }
81 
82 void
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 
103 void
105 {
106  if (m_receiveCallback) {
107  m_receiveCallback(packet);
108  }
109 }
110 
111 void
113 {
114  this->emitSignal(afterSend, wire);
115 }
116 
117 void
118 InternalClientTransport::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
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:154
static void asyncReceive(InternalTransportBase *recipient, const Block &packet)
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:79
void connectToForwarder(InternalForwarderTransport *forwarderTransport)
connect to a forwarder-side transport
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:96
TransportState
indicates the state of a transport
Definition: transport.hpp:42
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:433
signal::Signal< InternalTransportBase, Block > afterSend
stores a packet along with the remote endpoint
Definition: transport.hpp:122
#define emitSignal(...)
(implementation detail)
Definition: emit.hpp:76
const uint8_t * wire() const
Get pointer to encoded wire.
Definition: block.cpp:289
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:463
virtual void receiveFromLink(const Block &packet)=0
causes the transport to receive a link-layer packet
ReceiveCallback m_receiveCallback
Definition: transport.hpp:117
abstracts a transport that can be paired with another
void setMtu(ssize_t mtu)
Definition: transport.hpp:475
void receiveFromLink(const Block &packet) override
causes the transport to receive a link-layer packet
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:445
size_t size() const
Get size of encoded wire, including Type-Length-Value.
Definition: block.cpp:298
implements a forwarder-side transport that can be paired with another
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:85
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
signal::Signal< Transport, TransportState, TransportState > afterStateChange
signals when transport state changes
Definition: transport.hpp:274
the transport is closed, and can be safely deallocated
void post(const std::function< void()> &callback)
Definition: global-io.cpp:34
NFD_LOG_MEMBER_INIT(InternalForwarderTransport, InternalForwarderTransport)
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:421
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
size_t appendByteArray(const uint8_t *array, size_t length)
Append a byte array array of length length.
Definition: encoder.cpp:128
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
void receiveFromLink(const Block &packet) override
causes the transport to receive a link-layer packet
void disconnect()
Manually disconnect the connection.
void doClose() override
performs Transport specific operations to close the transport
void setState(TransportState newState)
set transport state
Definition: transport.cpp:177
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 receive(Packet &&packet)
receive a link-layer packet
Definition: transport.cpp:120
void send(const Block &wire) override
send a TLV block through the transport
EncodingImpl< EncoderTag > EncodingBuffer
Block packet
the packet as a TLV block
Definition: transport.hpp:133