NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
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_state(TransportState::UP)
67  , m_expirationTime(time::steady_clock::TimePoint::max())
68 {
69 }
70 
71 Transport::~Transport() = default;
72 
73 void
75 {
76  BOOST_ASSERT(m_face == nullptr);
77  BOOST_ASSERT(m_service == nullptr);
78 
79  m_face = &face;
80  m_service = &service;
81 }
82 
83 void
85 {
86  if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
87  return;
88  }
89 
91  this->doClose();
92  // warning: don't access any members after this:
93  // the Transport may be deallocated if doClose changes state to CLOSED
94 }
95 
96 void
98 {
99  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
100  packet.packet.size() <= static_cast<size_t>(this->getMtu()));
101 
102  TransportState state = this->getState();
103  if (state != TransportState::UP && state != TransportState::DOWN) {
104  NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
105  return;
106  }
107 
108  if (state == TransportState::UP) {
109  ++this->nOutPackets;
110  this->nOutBytes += packet.packet.size();
111  }
112 
113  this->doSend(std::move(packet));
114 }
115 
116 void
118 {
119  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
120  packet.packet.size() <= static_cast<size_t>(this->getMtu()));
121 
122  ++this->nInPackets;
123  this->nInBytes += packet.packet.size();
124 
125  m_service->receivePacket(std::move(packet));
126 }
127 
128 bool
130 {
131  // not changing, or setting initial persistency in subclass constructor
132  if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
133  return true;
134  }
135 
136  if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
137  NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
138  return false;
139  }
140 
141  return this->canChangePersistencyToImpl(newPersistency);
142 }
143 
144 bool
146 {
147  return false;
148 }
149 
150 void
152 {
153  BOOST_ASSERT(canChangePersistencyTo(newPersistency));
154 
155  if (m_persistency == newPersistency) {
156  return;
157  }
158 
159  auto oldPersistency = m_persistency;
160  m_persistency = newPersistency;
161 
162  if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
163  NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
164  this->afterChangePersistency(oldPersistency);
165  }
166 }
167 
168 void
170 {
171 }
172 
173 void
175 {
176  if (m_state == newState) {
177  return;
178  }
179 
180  bool isValid = false;
181  switch (m_state) {
182  case TransportState::UP:
183  isValid = newState == TransportState::DOWN ||
184  newState == TransportState::CLOSING ||
185  newState == TransportState::FAILED;
186  break;
188  isValid = newState == TransportState::UP ||
189  newState == TransportState::CLOSING ||
190  newState == TransportState::FAILED;
191  break;
194  isValid = newState == TransportState::CLOSED;
195  break;
196  default:
197  break;
198  }
199 
200  if (!isValid) {
201  BOOST_THROW_EXCEPTION(std::runtime_error("invalid state transition"));
202  }
203 
204  NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
205 
206  TransportState oldState = m_state;
207  m_state = newState;
208  afterStateChange(oldState, newState);
209  // warning: don't access any members after this:
210  // the Transport may be deallocated in the signal handler if newState is CLOSED
211 }
212 
213 std::ostream&
214 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
215 {
216  const Transport& transport = flh.obj;
217  const Face* face = transport.getFace();
218  FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
219 
220  os << "[id=" << faceId << ",local=" << transport.getLocalUri()
221  << ",remote=" << transport.getRemoteUri() << "] ";
222  return os;
223 }
224 
225 } // namespace face
226 } // namespace nfd
virtual void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
invoked after the persistency has been changed
Definition: transport.cpp:169
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:151
void setFaceAndLinkService(Face &face, LinkService &service)
set Face and LinkService for Transport
Definition: transport.cpp:74
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:95
TransportState
indicates the state of a transport
Definition: transport.hpp:41
const ssize_t MTU_INVALID
(for internal use) indicates MTU field is unset
Definition: transport.hpp:99
stores a packet along with the remote endpoint
Definition: transport.hpp:113
FaceUri getLocalUri() const
Definition: transport.hpp:378
PacketCounter nOutPackets
count of outgoing packets
Definition: transport.hpp:73
STL namespace.
void close()
request the transport to be closed
Definition: transport.cpp:84
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
ByteCounter nOutBytes
total outgoing bytes
Definition: transport.hpp:90
const Face * getFace() const
Definition: transport.hpp:354
void send(Packet &&packet)
send a link-layer packet
Definition: transport.cpp:97
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:129
ssize_t getMtu() const
Definition: transport.hpp:432
ByteCounter nInBytes
total incoming bytes
Definition: transport.hpp:82
#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:265
the transport is closed, and can be safely deallocated
void receivePacket(Transport::Packet &&packet)
performs LinkService specific operations to receive a lower-layer packet
FaceUri getRemoteUri() const
Definition: transport.hpp:390
the transport is being closed gracefully, either by the peer or by a call to close() ...
virtual bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
invoked by canChangePersistencyTo to perform the check
Definition: transport.cpp:145
TransportState getState() const
Definition: transport.hpp:445
void setState(TransportState newState)
set transport state
Definition: transport.cpp:174
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:117
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
PacketCounter nInPackets
count of incoming packets
Definition: transport.hpp:66
virtual void doClose()=0
performs Transport specific operations to close the transport
the lower part of a Face
Definition: transport.hpp:104
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:42
Transport()
constructor
Definition: transport.cpp:59
FaceId getId() const
Definition: face.hpp:229
the transport is temporarily down, and is being recovered