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 
53 Transport::Packet::Packet(Block&& packet1)
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
131 Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
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
virtual void doClose()=0
performs Transport specific operations to close the transport
virtual ~Transport()
Definition: transport.cpp:71
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
Definition: nfd.hpp:35
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:74
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
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
Definition: face-log.hpp:77
ssize_t getMtu() const
Definition: transport.hpp:410
virtual void beforeChangePersistency(ndn::nfd::FacePersistency newPersistency)=0
invoked before persistency is changed
STL namespace.
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition: face.cpp:46
void setPersistency(ndn::nfd::FacePersistency persistency)
changes face persistency setting
Definition: transport.cpp:131
signal::Signal< Transport, TransportState, TransportState > afterStateChange
signals when transport state changes
Definition: transport.hpp:254
the lower part of a Face
Definition: transport.hpp:104
FaceUri getRemoteUri() const
Definition: transport.hpp:368
void receive(Packet &&packet)
receive a link-layer packet
Definition: transport.cpp:119
stores a packet along with the remote endpoint
Definition: transport.hpp:113
ByteCounter nInBytes
total incoming bytes
Definition: transport.hpp:82
the transport is being closed due to a failure
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
the transport is closed, and can be safely deallocated
PacketCounter nInPackets
count of incoming packets
Definition: transport.hpp:66
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:80
const Face * getFace() const
Definition: transport.hpp:332
generalization of a network interface
Definition: face.hpp:67
FaceId getId() const
Definition: face.hpp:222
the transport is requested to be closed
PacketCounter nOutPackets
count of outgoing packets
Definition: transport.hpp:73
void send(Packet &&packet)
send a link-layer packet
Definition: transport.cpp:99
void close()
request the transport to be closed
Definition: transport.cpp:86
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
TransportState getState() const
Definition: transport.hpp:423
FaceUri getLocalUri() const
Definition: transport.hpp:356
void setState(TransportState newState)
set transport state
Definition: transport.cpp:150
the transport is up
uint64_t FaceId
identifies a face
Definition: face.hpp:39
ByteCounter nOutBytes
total outgoing bytes
Definition: transport.hpp:90
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:42
void setFaceAndLinkService(Face &face, LinkService &service)
set Face and LinkService for Transport
Definition: transport.cpp:76
Transport()
constructor
Definition: transport.cpp:59
the transport is down temporarily, and is being recovered