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 
72 {
73 }
74 
75 void
77 {
78  BOOST_ASSERT(m_face == nullptr);
79  BOOST_ASSERT(m_service == nullptr);
80 
81  m_face = &face;
82  m_service = &service;
83 }
84 
85 void
87 {
88  if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
89  return;
90  }
91 
93  this->doClose();
94  // warning: don't access any fields after this:
95  // the Transport may be deallocated if doClose changes state to CLOSED
96 }
97 
98 void
100 {
101  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
102  packet.packet.size() <= static_cast<size_t>(this->getMtu()));
103 
104  TransportState state = this->getState();
105  if (state != TransportState::UP && state != TransportState::DOWN) {
106  NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
107  return;
108  }
109 
110  if (state == TransportState::UP) {
111  ++this->nOutPackets;
112  this->nOutBytes += packet.packet.size();
113  }
114 
115  this->doSend(std::move(packet));
116 }
117 
118 void
120 {
121  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
122  packet.packet.size() <= static_cast<size_t>(this->getMtu()));
123 
124  ++this->nInPackets;
125  this->nInBytes += packet.packet.size();
126 
127  m_service->receivePacket(std::move(packet));
128 }
129 
130 void
132 {
133  if (m_persistency == newPersistency) {
134  return;
135  }
136 
137  if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
138  throw std::runtime_error("invalid persistency transition");
139  }
140 
141  if (m_persistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
142  this->beforeChangePersistency(newPersistency);
143  NFD_LOG_FACE_DEBUG("setPersistency " << m_persistency << " -> " << newPersistency);
144  }
145 
146  m_persistency = newPersistency;
147 }
148 
149 void
151 {
152  if (m_state == newState) {
153  return;
154  }
155 
156  bool isValid = false;
157  switch (m_state) {
158  case TransportState::UP:
159  isValid = newState == TransportState::DOWN ||
160  newState == TransportState::CLOSING ||
161  newState == TransportState::FAILED;
162  break;
164  isValid = newState == TransportState::UP ||
165  newState == TransportState::CLOSING ||
166  newState == TransportState::FAILED;
167  break;
170  isValid = newState == TransportState::CLOSED;
171  break;
172  default:
173  break;
174  }
175 
176  if (!isValid) {
177  throw std::runtime_error("invalid state transition");
178  }
179 
180  NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
181 
182  TransportState oldState = m_state;
183  m_state = newState;
184  afterStateChange(oldState, newState);
185  // warning: don't access any fields after this:
186  // the Transport may be deallocated in the signal handler if newState is CLOSED
187 }
188 
189 std::ostream&
190 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
191 {
192  const Transport& transport = flh.obj;
193  const Face* face = transport.getFace();
194  FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
195 
196  os << "[id=" << faceId << ",local=" << transport.getLocalUri()
197  << ",remote=" << transport.getRemoteUri() << "] ";
198  return os;
199 }
200 
201 } // namespace face
202 } // namespace nfd
void setFaceAndLinkService(Face &face, LinkService &service)
set Face and LinkService for Transport
Definition: transport.cpp:76
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:74
generalization of a network interface
Definition: face.hpp:67
std::ostream & operator<<(std::ostream &os, const Face &face)
Definition: ndn-common.hpp:89
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
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
PacketCounter nOutPackets
count of outgoing packets
Definition: transport.hpp:73
STL namespace.
void close()
request the transport to be closed
Definition: transport.cpp:86
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
const Face * getFace() const
Definition: transport.hpp:332
ByteCounter nOutBytes
total outgoing bytes
Definition: transport.hpp:90
FaceUri getRemoteUri() const
Definition: transport.hpp:368
void send(Packet &&packet)
send a link-layer packet
Definition: transport.cpp:99
the transport is being closed due to a failure
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
Definition: face-log.hpp:77
ByteCounter nInBytes
total incoming bytes
Definition: transport.hpp:82
FaceUri getLocalUri() const
Definition: transport.hpp:356
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:80
TransportState getState() const
Definition: transport.hpp:423
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
virtual void beforeChangePersistency(ndn::nfd::FacePersistency newPersistency)=0
invoked before persistency is changed
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:229
ssize_t getMtu() const
Definition: transport.hpp:410
the transport is requested to be closed
void setState(TransportState newState)
set transport state
Definition: transport.cpp:150
the transport is up
virtual ~Transport()
Definition: transport.cpp:71
uint64_t FaceId
identifies a face
Definition: face.hpp:39
void receive(Packet &&packet)
receive a link-layer packet
Definition: transport.cpp:119
#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
the transport is down temporarily, and is being recovered