transport.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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 #ifndef NFD_DAEMON_FACE_TRANSPORT_HPP
27 #define NFD_DAEMON_FACE_TRANSPORT_HPP
28 
29 #include "face-common.hpp"
30 #include "common/counter.hpp"
31 
32 namespace nfd::face {
33 
37 enum class TransportState {
38  NONE,
39  UP,
40  DOWN,
41  CLOSING,
42  FAILED,
43  CLOSED
44 };
45 
46 std::ostream&
47 operator<<(std::ostream& os, TransportState state);
48 
54 {
55 public:
63 
70 
79 
87 };
88 
92 inline constexpr ssize_t MTU_UNLIMITED = -1;
93 
97 inline constexpr ssize_t MTU_INVALID = -2;
98 
102 inline constexpr ssize_t QUEUE_UNSUPPORTED = -1;
103 
107 inline constexpr ssize_t QUEUE_ERROR = -2;
108 
113 class Transport : protected virtual TransportCounters, noncopyable
114 {
115 public:
120 
130 
131  virtual
133 
134 public:
139  void
140  setFaceAndLinkService(Face& face, LinkService& service) noexcept;
141 
145  const Face*
146  getFace() const noexcept
147  {
148  return m_face;
149  }
150 
154  const LinkService*
155  getLinkService() const noexcept
156  {
157  return m_service;
158  }
159 
163  LinkService*
164  getLinkService() noexcept
165  {
166  return m_service;
167  }
168 
169  virtual const Counters&
170  getCounters() const
171  {
172  return *this;
173  }
174 
175 public: // upper interface
184  void
185  close();
186 
192  void
193  send(const Block& packet);
194 
195 public: // static properties
199  FaceUri
200  getLocalUri() const noexcept
201  {
202  return m_localUri;
203  }
204 
208  FaceUri
209  getRemoteUri() const noexcept
210  {
211  return m_remoteUri;
212  }
213 
217  ndn::nfd::FaceScope
218  getScope() const noexcept
219  {
220  return m_scope;
221  }
222 
226  ndn::nfd::FacePersistency
227  getPersistency() const noexcept
228  {
229  return m_persistency;
230  }
231 
241  bool
242  canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const;
243 
247  void
248  setPersistency(ndn::nfd::FacePersistency newPersistency);
249 
253  ndn::nfd::LinkType
254  getLinkType() const noexcept
255  {
256  return m_linkType;
257  }
258 
269  ssize_t
270  getMtu() const noexcept
271  {
272  return m_mtu;
273  }
274 
280  ssize_t
281  getSendQueueCapacity() const noexcept
282  {
283  return m_sendQueueCapacity;
284  }
285 
286 public: // dynamic properties
291  getState() const noexcept
292  {
293  return m_state;
294  }
295 
299  signal::Signal<Transport, TransportState/*old*/, TransportState/*new*/> afterStateChange;
300 
305  time::steady_clock::time_point
306  getExpirationTime() const noexcept
307  {
308  return m_expirationTime;
309  }
310 
316  virtual ssize_t
318  {
319  return QUEUE_UNSUPPORTED;
320  }
321 
322 protected: // upper interface to be invoked by subclass
329  void
330  receive(const Block& packet, const EndpointId& endpoint = {});
331 
332 protected: // properties to be set by subclass
333  void
334  setLocalUri(const FaceUri& uri) noexcept
335  {
336  m_localUri = uri;
337  }
338 
339  void
340  setRemoteUri(const FaceUri& uri) noexcept
341  {
342  m_remoteUri = uri;
343  }
344 
345  void
346  setScope(ndn::nfd::FaceScope scope) noexcept
347  {
348  m_scope = scope;
349  }
350 
351  void
352  setLinkType(ndn::nfd::LinkType linkType) noexcept
353  {
354  m_linkType = linkType;
355  }
356 
357  void
358  setMtu(ssize_t mtu) noexcept;
359 
360  void
361  setSendQueueCapacity(ssize_t sendQueueCapacity) noexcept
362  {
363  m_sendQueueCapacity = sendQueueCapacity;
364  }
365 
373  void
374  setState(TransportState newState);
375 
376  void
377  setExpirationTime(const time::steady_clock::time_point& expirationTime) noexcept
378  {
379  m_expirationTime = expirationTime;
380  }
381 
382 protected: // to be overridden by subclass
389  virtual bool
390  canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const;
391 
398  virtual void
399  afterChangePersistency(ndn::nfd::FacePersistency oldPersistency);
400 
409  virtual void
410  doClose() = 0;
411 
412 private: // to be overridden by subclass
417  virtual void
418  doSend(const Block& packet) = 0;
419 
420 private:
421  Face* m_face = nullptr;
422  LinkService* m_service = nullptr;
423  FaceUri m_localUri;
424  FaceUri m_remoteUri;
425  ndn::nfd::FaceScope m_scope = ndn::nfd::FACE_SCOPE_NONE;
426  ndn::nfd::FacePersistency m_persistency = ndn::nfd::FACE_PERSISTENCY_NONE;
427  ndn::nfd::LinkType m_linkType = ndn::nfd::LINK_TYPE_NONE;
428  ssize_t m_mtu = MTU_INVALID;
429  ssize_t m_sendQueueCapacity = QUEUE_UNSUPPORTED;
431  time::steady_clock::time_point m_expirationTime = time::steady_clock::time_point::max();
432 };
433 
434 std::ostream&
435 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh);
436 
437 template<typename T>
438 std::enable_if_t<std::is_base_of_v<Transport, T> && !std::is_same_v<Transport, T>,
439  std::ostream&>
440 operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
441 {
442  return os << FaceLogHelper<Transport>(flh.obj);
443 }
444 
445 } // namespace nfd::face
446 
447 #endif // NFD_DAEMON_FACE_TRANSPORT_HPP
Represents a counter of number of bytes.
Definition: counter.hpp:88
Represents a counter of number of packets.
Definition: counter.hpp:70
Generalization of a network interface.
Definition: face.hpp:56
For internal use by FaceLogging macros.
Counters provided by a transport.
Definition: transport.hpp:54
ByteCounter nInBytes
Total incoming bytes.
Definition: transport.hpp:78
PacketCounter nInPackets
Count of incoming packets.
Definition: transport.hpp:62
PacketCounter nOutPackets
Count of outgoing packets.
Definition: transport.hpp:69
ByteCounter nOutBytes
Total outgoing bytes.
Definition: transport.hpp:86
The lower half of a Face.
Definition: transport.hpp:114
const LinkService * getLinkService() const noexcept
Returns the LinkService to which this transport is attached.
Definition: transport.hpp:155
void setScope(ndn::nfd::FaceScope scope) noexcept
Definition: transport.hpp:346
void receive(const Block &packet, const EndpointId &endpoint={})
Pass a received link-layer packet to the upper layer for further processing.
Definition: transport.cpp:101
void setPersistency(ndn::nfd::FacePersistency newPersistency)
Changes the persistency setting of the transport.
Definition: transport.cpp:152
ssize_t getMtu() const noexcept
Returns the maximum payload size.
Definition: transport.hpp:270
FaceUri getLocalUri() const noexcept
Returns a FaceUri representing the local endpoint.
Definition: transport.hpp:200
ndn::nfd::FacePersistency getPersistency() const noexcept
Returns the current persistency setting of the transport.
Definition: transport.hpp:227
virtual void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
Invoked after the persistency has been changed.
Definition: transport.cpp:170
ndn::nfd::FaceScope getScope() const noexcept
Returns whether the transport is local or non-local for scope control purposes.
Definition: transport.hpp:218
LinkService * getLinkService() noexcept
Returns the LinkService to which this transport is attached.
Definition: transport.hpp:164
void setMtu(ssize_t mtu) noexcept
Definition: transport.cpp:114
void send(const Block &packet)
Send a link-layer packet.
Definition: transport.cpp:80
void setFaceAndLinkService(Face &face, LinkService &service) noexcept
Set Face and LinkService for this transport.
Definition: transport.cpp:57
const Face * getFace() const noexcept
Returns the Face to which this transport is attached.
Definition: transport.hpp:146
TransportState getState() const noexcept
Returns the current transport state.
Definition: transport.hpp:291
virtual void doClose()=0
Performs Transport specific operations to close the transport.
bool canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
Check whether the persistency can be changed to newPersistency.
Definition: transport.cpp:130
virtual const Counters & getCounters() const
Definition: transport.hpp:170
void setExpirationTime(const time::steady_clock::time_point &expirationTime) noexcept
Definition: transport.hpp:377
TransportCounters Counters
Counters provided by a transport.
Definition: transport.hpp:119
FaceUri getRemoteUri() const noexcept
Returns a FaceUri representing the remote endpoint.
Definition: transport.hpp:209
Transport()
Default constructor.
virtual bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Invoked by canChangePersistencyTo to perform the check.
Definition: transport.cpp:146
void setSendQueueCapacity(ssize_t sendQueueCapacity) noexcept
Definition: transport.hpp:361
signal::Signal< Transport, TransportState, TransportState > afterStateChange
Signals when the transport state changes.
Definition: transport.hpp:299
void setState(TransportState newState)
Set transport state.
Definition: transport.cpp:175
void setLocalUri(const FaceUri &uri) noexcept
Definition: transport.hpp:334
time::steady_clock::time_point getExpirationTime() const noexcept
Returns the expiration time of the transport.
Definition: transport.hpp:306
ssize_t getSendQueueCapacity() const noexcept
Returns the capacity of the send queue (in bytes).
Definition: transport.hpp:281
void setLinkType(ndn::nfd::LinkType linkType) noexcept
Definition: transport.hpp:352
void setRemoteUri(const FaceUri &uri) noexcept
Definition: transport.hpp:340
ndn::nfd::LinkType getLinkType() const noexcept
Returns the link type of the transport.
Definition: transport.hpp:254
virtual ssize_t getSendQueueLength()
Returns the current send queue length of the transport (in octets).
Definition: transport.hpp:317
void close()
Request the transport to be closed.
Definition: transport.cpp:67
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition: face.cpp:45
constexpr ssize_t QUEUE_UNSUPPORTED
Indicates that the transport does not support reading the queue capacity/length.
Definition: transport.hpp:102
TransportState
Indicates the state of a transport.
Definition: transport.hpp:37
@ CLOSED
the transport is closed, and can be safely deallocated
@ CLOSING
the transport is being closed gracefully, either by the peer or by a call to close()
@ FAILED
the transport is being closed due to a failure
@ DOWN
the transport is temporarily down, and is being recovered
@ UP
the transport is up and can transmit packets
constexpr ssize_t MTU_INVALID
(for internal use) Indicates that the MTU field is unset.
Definition: transport.hpp:97
std::variant< std::monostate, ethernet::Address, udp::Endpoint > EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:77
constexpr ssize_t QUEUE_ERROR
Indicates that the transport was unable to retrieve the queue capacity/length.
Definition: transport.hpp:107
constexpr ssize_t MTU_UNLIMITED
Indicates that the transport has no limit on payload size.
Definition: transport.hpp:92