NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
26 #include "internal-transport.hpp"
27 #include "core/global-io.hpp"
28 
29 namespace nfd {
30 namespace face {
31 
32 NFD_LOG_INCLASS_DEFINE(InternalForwarderTransport, "InternalForwarderTransport");
33 
35  const FaceUri& localUri, const FaceUri& remoteUri,
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  if (newPersistency != ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
52  BOOST_THROW_EXCEPTION(
53  std::invalid_argument("InternalForwarderTransport supports only FACE_PERSISTENCY_PERMANENT"));
54  }
55 }
56 
57 void
59 {
60  NFD_LOG_FACE_TRACE(__func__);
61 
62  Packet p;
63  p.packet = packet;
64  this->receive(std::move(p));
65 }
66 
67 void
68 InternalForwarderTransport::doSend(Packet&& packet)
69 {
70  NFD_LOG_FACE_TRACE(__func__);
71 
72  this->emitSignal(afterSend, packet.packet);
73 }
74 
75 void
77 {
78  NFD_LOG_FACE_TRACE(__func__);
79 
81 }
82 
83 NFD_LOG_INCLASS_DEFINE(InternalClientTransport, "InternalClientTransport");
84 
85 static void
86 asyncReceive(InternalTransportBase* recipient, const Block& packet)
87 {
88  getGlobalIoService().post([packet, recipient] {
89  recipient->receiveFromLink(packet);
90  });
91 }
92 
93 void
95 {
96  NFD_LOG_DEBUG(__func__ << " " << forwarderTransport);
97 
98  m_fwToClientTransmitConn.disconnect();
99  m_clientToFwTransmitConn.disconnect();
100  m_fwTransportStateConn.disconnect();
101 
102  if (forwarderTransport != nullptr) {
103  m_fwToClientTransmitConn = forwarderTransport->afterSend.connect(bind(&asyncReceive, this, _1));
104  m_clientToFwTransmitConn = this->afterSend.connect(bind(&asyncReceive, forwarderTransport, _1));
105  m_fwTransportStateConn = forwarderTransport->afterStateChange.connect(
106  [this] (TransportState oldState, TransportState newState) {
107  if (newState == TransportState::CLOSED) {
108  this->connectToForwarder(nullptr);
109  }
110  });
111  }
112 }
113 
114 void
116 {
117  if (m_receiveCallback) {
118  m_receiveCallback(packet);
119  }
120 }
121 
122 void
124 {
125  this->emitSignal(afterSend, wire);
126 }
127 
128 void
129 InternalClientTransport::send(const Block& header, const Block& payload)
130 {
131  ndn::EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
132  encoder.appendByteArray(header.wire(), header.size());
133  encoder.appendByteArray(payload.wire(), payload.size());
134 
135  this->send(encoder.block());
136 }
137 
138 } // namespace face
139 } // namespace nfd
virtual void beforeChangePersistency(ndn::nfd::FacePersistency newPersistency) override
invoked before persistency is changed
implements a client-side transport that can be paired with another
static void asyncReceive(InternalTransportBase *recipient, const Block &packet)
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:74
void connectToForwarder(InternalForwarderTransport *forwarderTransport)
connect to a forwarder-side transport
void setPersistency(ndn::nfd::FacePersistency persistency)
changes face persistency setting
Definition: transport.cpp:131
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:95
TransportState
indicates the state of a transport
Definition: transport.hpp:41
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:374
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
signal::Signal< InternalTransportBase, Block > afterSend
stores a packet along with the remote endpoint
Definition: transport.hpp:113
const uint8_t * wire() const
Definition: block.cpp:495
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:55
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:404
virtual void receiveFromLink(const Block &packet)=0
causes the transport to receive a link-layer packet
abstracts a transport that can be paired with another
void setMtu(ssize_t mtu)
Definition: transport.hpp:416
virtual void receiveFromLink(const Block &packet) override
causes the transport to receive a link-layer packet
void send(Packet &&packet)
send a link-layer packet
Definition: transport.cpp:99
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:386
#define emitSignal(...)
(implementation detail)
Definition: signal-emit.hpp:76
size_t size() const
Definition: block.cpp:504
implements a forwarder-side transport that can be paired with another
EncodingImpl< EncoderTag > EncodingBuffer
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:80
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:254
the transport is closed, and can be safely deallocated
void post(const std::function< void()> &callback)
Definition: global-io.cpp:34
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:362
size_t appendByteArray(const uint8_t *array, size_t length)
Append a byte array array of length length.
Definition: encoder.cpp:135
virtual void receiveFromLink(const Block &packet) override
causes the transport to receive a link-layer packet
virtual void doClose() override
performs Transport specific operations to close the transport
void setState(TransportState newState)
set transport state
Definition: transport.cpp:150
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:119
virtual void send(const Block &wire) override
send a TLV block through the transport
Block packet
the packet as a TLV block
Definition: transport.hpp:124
#define NFD_LOG_INCLASS_DEFINE(cls, name)
Definition: logger.hpp:39