transport.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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 #include <ndn-cxx/encoding/nfd-constants.hpp>
32 
33 namespace nfd {
34 namespace face {
35 
36 class Face;
37 class LinkService;
38 
41 enum class TransportState {
42  NONE,
43  UP,
44  DOWN,
45  CLOSING,
46  FAILED,
47  CLOSED
48 };
49 
50 std::ostream&
51 operator<<(std::ostream& os, TransportState state);
52 
58 {
59 public:
67 
74 
83 
91 };
92 
95 const ssize_t MTU_UNLIMITED = -1;
96 
99 const ssize_t MTU_INVALID = -2;
100 
104 class Transport : protected virtual TransportCounters, noncopyable
105 {
106 public:
109  typedef uint64_t EndpointId;
110 
113  class Packet
114  {
115  public:
116  Packet() = default;
117 
118  explicit
119  Packet(Block&& packet);
120 
121  public:
124  Block packet;
125 
132  EndpointId remoteEndpoint;
133  };
134 
138 
147  Transport();
148 
149  virtual
150  ~Transport();
151 
152 public:
156  void
157  setFaceAndLinkService(Face& face, LinkService& service);
158 
161  const Face*
162  getFace() const;
163 
166  const LinkService*
167  getLinkService() const;
168 
171  LinkService*
172  getLinkService();
173 
174  virtual const Counters&
175  getCounters() const;
176 
177 public: // upper interface
186  void
187  close();
188 
193  void
194  send(Packet&& packet);
195 
196 protected: // upper interface to be invoked by subclass
200  void
201  receive(Packet&& packet);
202 
203 public: // static properties
206  FaceUri
207  getLocalUri() const;
208 
211  FaceUri
212  getRemoteUri() const;
213 
216  ndn::nfd::FaceScope
217  getScope() const;
218 
221  ndn::nfd::FacePersistency
222  getPersistency() const;
223 
226  void
227  setPersistency(ndn::nfd::FacePersistency persistency);
228 
231  ndn::nfd::LinkType
232  getLinkType() const;
233 
243  ssize_t
244  getMtu() const;
245 
246 public: // dynamic properties
250  getState() const;
251 
254  signal::Signal<Transport, TransportState/*old*/, TransportState/*new*/> afterStateChange;
255 
259  time::steady_clock::TimePoint
260  getExpirationTime() const;
261 
262 protected: // properties to be set by subclass
263  void
264  setLocalUri(const FaceUri& uri);
265 
266  void
267  setRemoteUri(const FaceUri& uri);
268 
269  void
270  setScope(ndn::nfd::FaceScope scope);
271 
272  void
273  setLinkType(ndn::nfd::LinkType linkType);
274 
275  void
276  setMtu(ssize_t mtu);
277 
285  void
286  setState(TransportState newState);
287 
288  void
289  setExpirationTime(const time::steady_clock::TimePoint& expirationTime);
290 
291 protected: // to be overridden by subclass
296  virtual void
297  beforeChangePersistency(ndn::nfd::FacePersistency newPersistency) = 0;
298 
307  virtual void
308  doClose() = 0;
309 
310 private: // to be overridden by subclass
315  virtual void
316  doSend(Packet&& packet) = 0;
317 
318 private:
319  Face* m_face;
320  LinkService* m_service;
321  FaceUri m_localUri;
322  FaceUri m_remoteUri;
323  ndn::nfd::FaceScope m_scope;
324  ndn::nfd::FacePersistency m_persistency;
325  ndn::nfd::LinkType m_linkType;
326  ssize_t m_mtu;
327  TransportState m_state;
328  time::steady_clock::TimePoint m_expirationTime;
329 };
330 
331 inline const Face*
333 {
334  return m_face;
335 }
336 
337 inline const LinkService*
339 {
340  return m_service;
341 }
342 
343 inline LinkService*
345 {
346  return m_service;
347 }
348 
349 inline const Transport::Counters&
351 {
352  return *this;
353 }
354 
355 inline FaceUri
357 {
358  return m_localUri;
359 }
360 
361 inline void
362 Transport::setLocalUri(const FaceUri& uri)
363 {
364  m_localUri = uri;
365 }
366 
367 inline FaceUri
369 {
370  return m_remoteUri;
371 }
372 
373 inline void
374 Transport::setRemoteUri(const FaceUri& uri)
375 {
376  m_remoteUri = uri;
377 }
378 
379 inline ndn::nfd::FaceScope
381 {
382  return m_scope;
383 }
384 
385 inline void
386 Transport::setScope(ndn::nfd::FaceScope scope)
387 {
388  m_scope = scope;
389 }
390 
391 inline ndn::nfd::FacePersistency
393 {
394  return m_persistency;
395 }
396 
397 inline ndn::nfd::LinkType
399 {
400  return m_linkType;
401 }
402 
403 inline void
404 Transport::setLinkType(ndn::nfd::LinkType linkType)
405 {
406  m_linkType = linkType;
407 }
408 
409 inline ssize_t
411 {
412  return m_mtu;
413 }
414 
415 inline void
416 Transport::setMtu(ssize_t mtu)
417 {
418  BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu > 0);
419  m_mtu = mtu;
420 }
421 
422 inline TransportState
424 {
425  return m_state;
426 }
427 
428 inline time::steady_clock::TimePoint
430 {
431  return m_expirationTime;
432 }
433 
434 inline void
435 Transport::setExpirationTime(const time::steady_clock::TimePoint& expirationTime)
436 {
437  m_expirationTime = expirationTime;
438 }
439 
440 std::ostream&
441 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh);
442 
443 template<typename T>
444 typename std::enable_if<std::is_base_of<Transport, T>::value &&
445  !std::is_same<Transport, T>::value, std::ostream&>::type
446 operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
447 {
448  return os << FaceLogHelper<Transport>(flh.obj);
449 }
450 
451 } // namespace face
452 } // namespace nfd
453 
454 #endif // NFD_DAEMON_FACE_TRANSPORT_HPP
virtual void doClose()=0
performs Transport specific operations to close the transport
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:362
virtual ~Transport()
Definition: transport.cpp:71
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:386
represents a counter of number of bytes
Definition: counter.hpp:95
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
void setMtu(ssize_t mtu)
Definition: transport.hpp:416
const ssize_t MTU_INVALID
(for internal use) indicates MTU field is unset
Definition: transport.hpp:99
ssize_t getMtu() const
Definition: transport.hpp:410
Block packet
the packet as a TLV block
Definition: transport.hpp:124
virtual void beforeChangePersistency(ndn::nfd::FacePersistency newPersistency)=0
invoked before persistency is changed
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
ndn::nfd::LinkType getLinkType() const
Definition: transport.hpp:398
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
represents a counter of number of packets
Definition: counter.hpp:77
TransportCounters Counters
counters provided by Transport
Definition: transport.hpp:137
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
uint64_t EndpointId
identifies an endpoint on the link
Definition: transport.hpp:109
const Face * getFace() const
Definition: transport.hpp:332
generalization of a network interface
Definition: face.hpp:67
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:404
ndn::nfd::FaceScope getScope() const
Definition: transport.hpp:380
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
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:392
counters provided by Transport
Definition: transport.hpp:57
const LinkService * getLinkService() const
Definition: transport.hpp:338
TransportState getState() const
Definition: transport.hpp:423
EndpointId remoteEndpoint
identifies the remote endpoint
Definition: transport.hpp:132
FaceUri getLocalUri() const
Definition: transport.hpp:356
void setState(TransportState newState)
set transport state
Definition: transport.cpp:150
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:374
the transport is up
time::steady_clock::TimePoint getExpirationTime() const
Definition: transport.hpp:429
ByteCounter nOutBytes
total outgoing bytes
Definition: transport.hpp:90
void setExpirationTime(const time::steady_clock::TimePoint &expirationTime)
Definition: transport.hpp:435
void setFaceAndLinkService(Face &face, LinkService &service)
set Face and LinkService for Transport
Definition: transport.cpp:76
virtual const Counters & getCounters() const
Definition: transport.hpp:350
Transport()
constructor
Definition: transport.cpp:59
the transport is down temporarily, and is being recovered