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-2018, 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 "core/counter.hpp"
30 #include "face-log.hpp"
31 
32 #include <ndn-cxx/encoding/nfd-constants.hpp>
33 
34 namespace nfd {
35 namespace face {
36 
37 class Face;
38 class LinkService;
39 
42 enum class TransportState {
43  NONE,
44  UP,
45  DOWN,
46  CLOSING,
47  FAILED,
48  CLOSED
49 };
50 
51 std::ostream&
52 operator<<(std::ostream& os, TransportState state);
53 
59 {
60 public:
68 
75 
84 
92 };
93 
96 const ssize_t MTU_UNLIMITED = -1;
97 
100 const ssize_t MTU_INVALID = -2;
101 
104 const ssize_t QUEUE_UNSUPPORTED = -1;
105 
108 const ssize_t QUEUE_ERROR = -2;
109 
113 class Transport : protected virtual TransportCounters, noncopyable
114 {
115 public:
118  typedef uint64_t EndpointId;
119 
122  class Packet
123  {
124  public:
125  Packet() = default;
126 
127  explicit
128  Packet(Block&& packet);
129 
130  public:
133  Block packet;
134 
141  EndpointId remoteEndpoint;
142  };
143 
147 
156  Transport();
157 
158  virtual
159  ~Transport();
160 
161 public:
165  void
166  setFaceAndLinkService(Face& face, LinkService& service);
167 
170  const Face*
171  getFace() const;
172 
175  const LinkService*
176  getLinkService() const;
177 
180  LinkService*
181  getLinkService();
182 
183  virtual const Counters&
184  getCounters() const;
185 
186 public: // upper interface
195  void
196  close();
197 
202  void
203  send(Packet&& packet);
204 
205 public: // static properties
208  FaceUri
209  getLocalUri() const;
210 
213  FaceUri
214  getRemoteUri() const;
215 
218  ndn::nfd::FaceScope
219  getScope() const;
220 
223  ndn::nfd::FacePersistency
224  getPersistency() const;
225 
234  bool
235  canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const;
236 
239  void
240  setPersistency(ndn::nfd::FacePersistency newPersistency);
241 
244  ndn::nfd::LinkType
245  getLinkType() const;
246 
256  ssize_t
257  getMtu() const;
258 
263  ssize_t
264  getSendQueueCapacity() const;
265 
266 public: // dynamic properties
270  getState() const;
271 
274  signal::Signal<Transport, TransportState/*old*/, TransportState/*new*/> afterStateChange;
275 
279  time::steady_clock::TimePoint
280  getExpirationTime() const;
281 
286  virtual ssize_t
288  {
289  return QUEUE_UNSUPPORTED;
290  }
291 
292 protected: // upper interface to be invoked by subclass
296  void
297  receive(Packet&& packet);
298 
299 protected: // properties to be set by subclass
300  void
301  setLocalUri(const FaceUri& uri);
302 
303  void
304  setRemoteUri(const FaceUri& uri);
305 
306  void
307  setScope(ndn::nfd::FaceScope scope);
308 
309  void
310  setLinkType(ndn::nfd::LinkType linkType);
311 
312  void
313  setMtu(ssize_t mtu);
314 
315  void
316  setSendQueueCapacity(ssize_t sendQueueCapacity);
317 
325  void
326  setState(TransportState newState);
327 
328  void
329  setExpirationTime(const time::steady_clock::TimePoint& expirationTime);
330 
331 protected: // to be overridden by subclass
338  virtual bool
339  canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const;
340 
347  virtual void
348  afterChangePersistency(ndn::nfd::FacePersistency oldPersistency);
349 
358  virtual void
359  doClose() = 0;
360 
361 private: // to be overridden by subclass
366  virtual void
367  doSend(Packet&& packet) = 0;
368 
369 public:
374  static constexpr ssize_t MIN_MTU = 64;
375 
376 private:
377  Face* m_face;
378  LinkService* m_service;
379  FaceUri m_localUri;
380  FaceUri m_remoteUri;
381  ndn::nfd::FaceScope m_scope;
382  ndn::nfd::FacePersistency m_persistency;
383  ndn::nfd::LinkType m_linkType;
384  ssize_t m_mtu;
385  ssize_t m_sendQueueCapacity;
386  TransportState m_state;
387  time::steady_clock::TimePoint m_expirationTime;
388 };
389 
390 inline const Face*
392 {
393  return m_face;
394 }
395 
396 inline const LinkService*
398 {
399  return m_service;
400 }
401 
402 inline LinkService*
404 {
405  return m_service;
406 }
407 
408 inline const Transport::Counters&
410 {
411  return *this;
412 }
413 
414 inline FaceUri
416 {
417  return m_localUri;
418 }
419 
420 inline void
421 Transport::setLocalUri(const FaceUri& uri)
422 {
423  m_localUri = uri;
424 }
425 
426 inline FaceUri
428 {
429  return m_remoteUri;
430 }
431 
432 inline void
433 Transport::setRemoteUri(const FaceUri& uri)
434 {
435  m_remoteUri = uri;
436 }
437 
438 inline ndn::nfd::FaceScope
440 {
441  return m_scope;
442 }
443 
444 inline void
445 Transport::setScope(ndn::nfd::FaceScope scope)
446 {
447  m_scope = scope;
448 }
449 
450 inline ndn::nfd::FacePersistency
452 {
453  return m_persistency;
454 }
455 
456 inline ndn::nfd::LinkType
458 {
459  return m_linkType;
460 }
461 
462 inline void
463 Transport::setLinkType(ndn::nfd::LinkType linkType)
464 {
465  m_linkType = linkType;
466 }
467 
468 inline ssize_t
470 {
471  return m_mtu;
472 }
473 
474 inline void
475 Transport::setMtu(ssize_t mtu)
476 {
477  BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu > 0);
478  m_mtu = mtu;
479 }
480 
481 inline ssize_t
483 {
484  return m_sendQueueCapacity;
485 }
486 
487 inline void
488 Transport::setSendQueueCapacity(ssize_t sendQueueCapacity)
489 {
490  m_sendQueueCapacity = sendQueueCapacity;
491 }
492 
493 inline TransportState
495 {
496  return m_state;
497 }
498 
499 inline time::steady_clock::TimePoint
501 {
502  return m_expirationTime;
503 }
504 
505 inline void
506 Transport::setExpirationTime(const time::steady_clock::TimePoint& expirationTime)
507 {
508  m_expirationTime = expirationTime;
509 }
510 
511 std::ostream&
512 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh);
513 
514 template<typename T>
515 typename std::enable_if<std::is_base_of<Transport, T>::value &&
516  !std::is_same<Transport, T>::value, std::ostream&>::type
517 operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
518 {
519  return os << FaceLogHelper<Transport>(flh.obj);
520 }
521 
522 } // namespace face
523 } // namespace nfd
524 
525 #endif // NFD_DAEMON_FACE_TRANSPORT_HPP
void setSendQueueCapacity(ssize_t sendQueueCapacity)
Definition: transport.hpp:488
const ssize_t QUEUE_UNSUPPORTED
indicates that the transport does not support reading the queue capacity/length
Definition: transport.hpp:104
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:421
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:445
represents a counter of number of bytes
Definition: counter.hpp:95
virtual ssize_t getSendQueueLength()
Definition: transport.hpp:287
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:96
TransportState
indicates the state of a transport
Definition: transport.hpp:42
void setMtu(ssize_t mtu)
Definition: transport.hpp:475
const ssize_t MTU_INVALID
(for internal use) indicates MTU field is unset
Definition: transport.hpp:100
ssize_t getMtu() const
Definition: transport.hpp:469
Block packet
the packet as a TLV block
Definition: transport.hpp:133
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:108
ndn::nfd::LinkType getLinkType() const
Definition: transport.hpp:457
signal::Signal< Transport, TransportState, TransportState > afterStateChange
signals when transport state changes
Definition: transport.hpp:274
the lower part of a Face
Definition: transport.hpp:113
FaceUri getRemoteUri() const
Definition: transport.hpp:427
stores a packet along with the remote endpoint
Definition: transport.hpp:122
ByteCounter nInBytes
total incoming bytes
Definition: transport.hpp:83
represents a counter of number of packets
Definition: counter.hpp:77
TransportCounters Counters
counters provided by Transport
Definition: transport.hpp:146
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:67
uint64_t EndpointId
identifies an endpoint on the link
Definition: transport.hpp:118
const Face * getFace() const
Definition: transport.hpp:391
generalization of a network interface
Definition: face.hpp:67
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:463
ndn::nfd::FaceScope getScope() const
Definition: transport.hpp:439
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:74
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:451
counters provided by Transport
Definition: transport.hpp:58
const LinkService * getLinkService() const
Definition: transport.hpp:397
TransportState getState() const
Definition: transport.hpp:494
EndpointId remoteEndpoint
identifies the remote endpoint
Definition: transport.hpp:141
FaceUri getLocalUri() const
Definition: transport.hpp:415
ssize_t getSendQueueCapacity() const
Definition: transport.hpp:482
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:433
the transport is up and can transmit packets
time::steady_clock::TimePoint getExpirationTime() const
Definition: transport.hpp:500
ByteCounter nOutBytes
total outgoing bytes
Definition: transport.hpp:91
void setExpirationTime(const time::steady_clock::TimePoint &expirationTime)
Definition: transport.hpp:506
virtual const Counters & getCounters() const
Definition: transport.hpp:409
the transport is temporarily down, and is being recovered