NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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 "transport.hpp"
27 #include "face.hpp"
28 
29 namespace nfd {
30 namespace face {
31 
33 
34 const ssize_t Transport::MIN_MTU;
35 
36 std::ostream&
37 operator<<(std::ostream& os, TransportState state)
38 {
39  switch (state) {
40  case TransportState::UP:
41  return os << "UP";
43  return os << "DOWN";
45  return os << "CLOSING";
47  return os << "FAILED";
49  return os << "CLOSED";
50  default:
51  return os << "NONE";
52  }
53 }
54 
56  : packet(std::move(packet1))
57  , remoteEndpoint(0)
58 {
59 }
60 
62  : m_face(nullptr)
63  , m_service(nullptr)
64  , m_scope(ndn::nfd::FACE_SCOPE_NONE)
65  , m_persistency(ndn::nfd::FACE_PERSISTENCY_NONE)
66  , m_linkType(ndn::nfd::LINK_TYPE_NONE)
67  , m_mtu(MTU_INVALID)
68  , m_sendQueueCapacity(QUEUE_UNSUPPORTED)
69  , m_state(TransportState::UP)
70  , m_expirationTime(time::steady_clock::TimePoint::max())
71 {
72 }
73 
74 Transport::~Transport() = default;
75 
76 void
78 {
79  BOOST_ASSERT(m_face == nullptr);
80  BOOST_ASSERT(m_service == nullptr);
81 
82  m_face = &face;
83  m_service = &service;
84 }
85 
86 void
88 {
89  if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
90  return;
91  }
92 
94  this->doClose();
95  // warning: don't access any members after this:
96  // the Transport may be deallocated if doClose changes state to CLOSED
97 }
98 
99 void
101 {
102  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
103  packet.packet.size() <= static_cast<size_t>(this->getMtu()));
104 
105  TransportState state = this->getState();
106  if (state != TransportState::UP && state != TransportState::DOWN) {
107  NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
108  return;
109  }
110 
111  if (state == TransportState::UP) {
112  ++this->nOutPackets;
113  this->nOutBytes += packet.packet.size();
114  }
115 
116  this->doSend(std::move(packet));
117 }
118 
119 void
121 {
122  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
123  packet.packet.size() <= static_cast<size_t>(this->getMtu()));
124 
125  ++this->nInPackets;
126  this->nInBytes += packet.packet.size();
127 
128  m_service->receivePacket(std::move(packet));
129 }
130 
131 bool
133 {
134  // not changing, or setting initial persistency in subclass constructor
135  if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
136  return true;
137  }
138 
139  if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
140  NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
141  return false;
142  }
143 
144  return this->canChangePersistencyToImpl(newPersistency);
145 }
146 
147 bool
149 {
150  return false;
151 }
152 
153 void
155 {
156  BOOST_ASSERT(canChangePersistencyTo(newPersistency));
157 
158  if (m_persistency == newPersistency) {
159  return;
160  }
161 
162  auto oldPersistency = m_persistency;
163  m_persistency = newPersistency;
164 
165  if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
166  NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
167  this->afterChangePersistency(oldPersistency);
168  }
169 }
170 
171 void
173 {
174 }
175 
176 void
178 {
179  if (m_state == newState) {
180  return;
181  }
182 
183  bool isValid = false;
184  switch (m_state) {
185  case TransportState::UP:
186  isValid = newState == TransportState::DOWN ||
187  newState == TransportState::CLOSING ||
188  newState == TransportState::FAILED;
189  break;
191  isValid = newState == TransportState::UP ||
192  newState == TransportState::CLOSING ||
193  newState == TransportState::FAILED;
194  break;
197  isValid = newState == TransportState::CLOSED;
198  break;
199  default:
200  break;
201  }
202 
203  if (!isValid) {
204  BOOST_THROW_EXCEPTION(std::runtime_error("invalid state transition"));
205  }
206 
207  NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
208 
209  TransportState oldState = m_state;
210  m_state = newState;
211  afterStateChange(oldState, newState);
212  // warning: don't access any members after this:
213  // the Transport may be deallocated in the signal handler if newState is CLOSED
214 }
215 
216 std::ostream&
217 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
218 {
219  const Transport& transport = flh.obj;
220  const Face* face = transport.getFace();
221  FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
222 
223  os << "[id=" << faceId << ",local=" << transport.getLocalUri()
224  << ",remote=" << transport.getRemoteUri() << "] ";
225  return os;
226 }
227 
228 } // namespace face
229 } // namespace nfd
virtual void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
invoked after the persistency has been changed
Definition: transport.cpp:172
const ssize_t QUEUE_UNSUPPORTED
indicates that the transport does not support reading the queue capacity/length
Definition: transport.hpp:104
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:154
void setFaceAndLinkService(Face &face, LinkService &service)
set Face and LinkService for Transport
Definition: transport.cpp:77
Copyright (c) 2011-2015 Regents of the University of California.
the upper part of a Face
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:79
generalization of a network interface
Definition: face.hpp:67
std::ostream & operator<<(std::ostream &os, const Face &face)
Definition: ndn-common.hpp:89
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
const ssize_t MTU_INVALID
(for internal use) indicates MTU field is unset
Definition: transport.hpp:100
virtual bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
invoked by canChangePersistencyTo to perform the check
Definition: transport.cpp:148
static constexpr ssize_t MIN_MTU
minimum MTU that may be set on a transport
Definition: transport.hpp:374
stores a packet along with the remote endpoint
Definition: transport.hpp:122
PacketCounter nOutPackets
count of outgoing packets
Definition: transport.hpp:74
void close()
request the transport to be closed
Definition: transport.cpp:87
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
const Face * getFace() const
Definition: transport.hpp:391
ByteCounter nOutBytes
total outgoing bytes
Definition: transport.hpp:91
FaceUri getRemoteUri() const
Definition: transport.hpp:427
void send(Packet &&packet)
send a link-layer packet
Definition: transport.cpp:100
the transport is being closed due to a failure
bool canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
check whether the face persistency can be changed to newPersistency
Definition: transport.cpp:132
ByteCounter nInBytes
total incoming bytes
Definition: transport.hpp:83
FaceUri getLocalUri() const
Definition: transport.hpp:415
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:85
TransportState getState() const
Definition: transport.hpp:494
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 receivePacket(Transport::Packet &&packet)
performs LinkService specific operations to receive a lower-layer packet
FaceId getId() const
Definition: face.hpp:233
ssize_t getMtu() const
Definition: transport.hpp:469
the transport is being closed gracefully, either by the peer or by a call to close()
void setState(TransportState newState)
set transport state
Definition: transport.cpp:177
the transport is up and can transmit packets
for internal use by FaceLogging macros
Definition: face-log.hpp:43
uint64_t FaceId
identifies a face
Definition: face.hpp:39
void receive(Packet &&packet)
receive a link-layer packet
Definition: transport.cpp:120
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
PacketCounter nInPackets
count of incoming packets
Definition: transport.hpp:67
virtual void doClose()=0
performs Transport specific operations to close the transport
the lower part of a Face
Definition: transport.hpp:113
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:42
Transport()
constructor
Definition: transport.cpp:61
the transport is temporarily down, and is being recovered