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