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-2019, 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 {
33 namespace face {
34 
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 
91 const ssize_t MTU_UNLIMITED = -1;
92 
95 const ssize_t MTU_INVALID = -2;
96 
99 const ssize_t QUEUE_UNSUPPORTED = -1;
100 
103 const ssize_t QUEUE_ERROR = -2;
104 
108 class Transport : protected virtual TransportCounters, noncopyable
109 {
110 public:
115 
124  Transport();
125 
126  virtual
127  ~Transport();
128 
129 public:
133  void
134  setFaceAndLinkService(Face& face, LinkService& service);
135 
138  const Face*
139  getFace() const;
140 
143  const LinkService*
144  getLinkService() const;
145 
148  LinkService*
149  getLinkService();
150 
151  virtual const Counters&
152  getCounters() const;
153 
154 public: // upper interface
163  void
164  close();
165 
172  void
173  send(const Block& packet, const EndpointId& endpoint = 0);
174 
175 public: // static properties
178  FaceUri
179  getLocalUri() const;
180 
183  FaceUri
184  getRemoteUri() const;
185 
188  ndn::nfd::FaceScope
189  getScope() const;
190 
193  ndn::nfd::FacePersistency
194  getPersistency() const;
195 
204  bool
205  canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const;
206 
209  void
210  setPersistency(ndn::nfd::FacePersistency newPersistency);
211 
214  ndn::nfd::LinkType
215  getLinkType() const;
216 
226  ssize_t
227  getMtu() const;
228 
233  ssize_t
234  getSendQueueCapacity() const;
235 
236 public: // dynamic properties
240  getState() const;
241 
244  signal::Signal<Transport, TransportState/*old*/, TransportState/*new*/> afterStateChange;
245 
249  time::steady_clock::TimePoint
250  getExpirationTime() const;
251 
256  virtual ssize_t
258  {
259  return QUEUE_UNSUPPORTED;
260  }
261 
262 protected: // upper interface to be invoked by subclass
268  void
269  receive(const Block& packet, const EndpointId& endpoint = 0);
270 
271 protected: // properties to be set by subclass
272  void
273  setLocalUri(const FaceUri& uri);
274 
275  void
276  setRemoteUri(const FaceUri& uri);
277 
278  void
279  setScope(ndn::nfd::FaceScope scope);
280 
281  void
282  setLinkType(ndn::nfd::LinkType linkType);
283 
284  void
285  setMtu(ssize_t mtu);
286 
287  void
288  setSendQueueCapacity(ssize_t sendQueueCapacity);
289 
297  void
298  setState(TransportState newState);
299 
300  void
301  setExpirationTime(const time::steady_clock::TimePoint& expirationTime);
302 
303 protected: // to be overridden by subclass
310  virtual bool
311  canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const;
312 
319  virtual void
320  afterChangePersistency(ndn::nfd::FacePersistency oldPersistency);
321 
330  virtual void
331  doClose() = 0;
332 
333 private: // to be overridden by subclass
339  virtual void
340  doSend(const Block& packet, const EndpointId& endpoint) = 0;
341 
342 public:
347  static constexpr ssize_t MIN_MTU = 64;
348 
349 private:
350  Face* m_face;
351  LinkService* m_service;
352  FaceUri m_localUri;
353  FaceUri m_remoteUri;
354  ndn::nfd::FaceScope m_scope;
355  ndn::nfd::FacePersistency m_persistency;
356  ndn::nfd::LinkType m_linkType;
357  ssize_t m_mtu;
358  ssize_t m_sendQueueCapacity;
359  TransportState m_state;
360  time::steady_clock::TimePoint m_expirationTime;
361 };
362 
363 inline const Face*
365 {
366  return m_face;
367 }
368 
369 inline const LinkService*
371 {
372  return m_service;
373 }
374 
375 inline LinkService*
377 {
378  return m_service;
379 }
380 
381 inline const Transport::Counters&
383 {
384  return *this;
385 }
386 
387 inline FaceUri
389 {
390  return m_localUri;
391 }
392 
393 inline void
394 Transport::setLocalUri(const FaceUri& uri)
395 {
396  m_localUri = uri;
397 }
398 
399 inline FaceUri
401 {
402  return m_remoteUri;
403 }
404 
405 inline void
406 Transport::setRemoteUri(const FaceUri& uri)
407 {
408  m_remoteUri = uri;
409 }
410 
411 inline ndn::nfd::FaceScope
413 {
414  return m_scope;
415 }
416 
417 inline void
418 Transport::setScope(ndn::nfd::FaceScope scope)
419 {
420  m_scope = scope;
421 }
422 
423 inline ndn::nfd::FacePersistency
425 {
426  return m_persistency;
427 }
428 
429 inline ndn::nfd::LinkType
431 {
432  return m_linkType;
433 }
434 
435 inline void
436 Transport::setLinkType(ndn::nfd::LinkType linkType)
437 {
438  m_linkType = linkType;
439 }
440 
441 inline ssize_t
443 {
444  return m_mtu;
445 }
446 
447 inline void
448 Transport::setMtu(ssize_t mtu)
449 {
450  BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu > 0);
451  m_mtu = mtu;
452 }
453 
454 inline ssize_t
456 {
457  return m_sendQueueCapacity;
458 }
459 
460 inline void
461 Transport::setSendQueueCapacity(ssize_t sendQueueCapacity)
462 {
463  m_sendQueueCapacity = sendQueueCapacity;
464 }
465 
466 inline TransportState
468 {
469  return m_state;
470 }
471 
472 inline time::steady_clock::TimePoint
474 {
475  return m_expirationTime;
476 }
477 
478 inline void
479 Transport::setExpirationTime(const time::steady_clock::TimePoint& expirationTime)
480 {
481  m_expirationTime = expirationTime;
482 }
483 
484 std::ostream&
485 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh);
486 
487 template<typename T>
488 typename std::enable_if<std::is_base_of<Transport, T>::value &&
489  !std::is_same<Transport, T>::value, std::ostream&>::type
490 operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
491 {
492  return os << FaceLogHelper<Transport>(flh.obj);
493 }
494 
495 } // namespace face
496 } // namespace nfd
497 
498 #endif // NFD_DAEMON_FACE_TRANSPORT_HPP
void setSendQueueCapacity(ssize_t sendQueueCapacity)
Definition: transport.hpp:461
const ssize_t QUEUE_UNSUPPORTED
indicates that the transport does not support reading the queue capacity/length
Definition: transport.hpp:99
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:394
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:418
represents a counter of number of bytes
Definition: counter.hpp:84
virtual ssize_t getSendQueueLength()
Definition: transport.hpp:257
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:91
TransportState
Indicates the state of a transport.
Definition: transport.hpp:37
void setMtu(ssize_t mtu)
Definition: transport.hpp:448
const ssize_t MTU_INVALID
(for internal use) indicates MTU field is unset
Definition: transport.hpp:95
ssize_t getMtu() const
Definition: transport.hpp:442
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition: face.cpp:47
const ssize_t QUEUE_ERROR
indicates that the transport was unable to retrieve the queue capacity/length
Definition: transport.hpp:103
ndn::nfd::LinkType getLinkType() const
Definition: transport.hpp:430
signal::Signal< Transport, TransportState, TransportState > afterStateChange
signals when transport state changes
Definition: transport.hpp:244
The lower half of a Face.
Definition: transport.hpp:108
uint64_t EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:65
FaceUri getRemoteUri() const
Definition: transport.hpp:400
ByteCounter nInBytes
total incoming bytes
Definition: transport.hpp:78
represents a counter of number of packets
Definition: counter.hpp:66
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:62
const Face * getFace() const
Definition: transport.hpp:364
generalization of a network interface
Definition: face.hpp:52
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:436
ndn::nfd::FaceScope getScope() const
Definition: transport.hpp:412
the transport is being closed gracefully, either by the peer or by a call to close() ...
PacketCounter nOutPackets
count of outgoing packets
Definition: transport.hpp:69
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:424
Counters provided by a transport.
Definition: transport.hpp:53
const LinkService * getLinkService() const
Definition: transport.hpp:370
TransportState getState() const
Definition: transport.hpp:467
FaceUri getLocalUri() const
Definition: transport.hpp:388
ssize_t getSendQueueCapacity() const
Definition: transport.hpp:455
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:406
the transport is up and can transmit packets
time::steady_clock::TimePoint getExpirationTime() const
Definition: transport.hpp:473
ByteCounter nOutBytes
total outgoing bytes
Definition: transport.hpp:86
void setExpirationTime(const time::steady_clock::TimePoint &expirationTime)
Definition: transport.hpp:479
virtual const Counters & getCounters() const
Definition: transport.hpp:382
the transport is temporarily down, and is being recovered