notification-subscriber.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
28 #ifndef NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
29 #define NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
30 
31 #include "../face.hpp"
32 #include "signal.hpp"
33 #include "concepts.hpp"
34 #include "time.hpp"
35 #include "scheduler.hpp"
37 
38 namespace ndn {
39 namespace util {
40 
41 class NotificationSubscriberBase : noncopyable
42 {
43 public:
44  virtual
46 
52  time::milliseconds
54  {
55  return m_interestLifetime;
56  }
57 
58  bool
59  isRunning() const
60  {
61  return m_isRunning;
62  }
63 
68  void
69  start();
70 
73  void
74  stop();
75 
76 protected:
81  NotificationSubscriberBase(Face& face, const Name& prefix,
82  time::milliseconds interestLifetime);
83 
84 private:
85  void
86  sendInitialInterest();
87 
88  void
89  sendNextInterest();
90 
91  virtual bool
92  hasSubscriber() const = 0;
93 
97  bool
98  shouldStop();
99 
100  void
101  afterReceiveData(const Data& data);
102 
106  virtual bool
107  decodeAndDeliver(const Data& data) = 0;
108 
109  void
110  afterReceiveNack(const lp::Nack& nack);
111 
112  void
113  afterTimeout();
114 
115  time::milliseconds
116  exponentialBackoff(lp::Nack nack);
117 
118 public:
122 
126 
130 
131 private:
132  Face& m_face;
133  Name m_prefix;
134  bool m_isRunning;
135  uint64_t m_lastSequenceNo;
136  uint64_t m_lastNackSequenceNo;
137  uint64_t m_attempts;
138  util::scheduler::Scheduler m_scheduler;
139  util::scheduler::ScopedEventId m_nackEvent;
140  const PendingInterestId* m_lastInterestId;
141  time::milliseconds m_interestLifetime;
142 };
143 
148 template<typename Notification>
150 {
151 public:
152  BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Notification>));
154 
159  NotificationSubscriber(Face& face, const Name& prefix,
160  time::milliseconds interestLifetime = time::seconds(60))
161  : NotificationSubscriberBase(face, prefix, interestLifetime)
162  {
163  }
164 
165 public:
170 
171 private:
172  bool
173  hasSubscriber() const override
174  {
175  return !onNotification.isEmpty();
176  }
177 
178  bool
179  decodeAndDeliver(const Data& data) override
180  {
181  Notification notification;
182  try {
183  notification.wireDecode(data.getContent().blockFromValue());
184  }
185  catch (const tlv::Error&) {
186  return false;
187  }
188 
189  onNotification(notification);
190  return true;
191  }
192 };
193 
194 } // namespace util
195 } // namespace ndn
196 
197 #endif // NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
void start()
start or resume receiving notifications
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
BOOST_CONCEPT_ASSERT((boost::EqualityComparable< Data >))
time::milliseconds getInterestLifetime() const
provides a lightweight signal / event system
signal::Signal< NotificationSubscriber, Notification > onNotification
fires when a Notification is received
represents a Network Nack
Definition: nack.hpp:40
signal::Signal< NotificationSubscriberBase, lp::Nack > onNack
fires when a NACK is received
Block blockFromValue() const
Definition: block.cpp:323
signal::Signal< NotificationSubscriberBase > onTimeout
fires when no Notification is received within .getInterestLifetime period
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:95
provides a subscriber of Notification Stream
NotificationSubscriber(Face &face, const Name &prefix, time::milliseconds interestLifetime=time::seconds(60))
construct a NotificationSubscriber
Represents an absolute name.
Definition: name.hpp:42
Event that is automatically cancelled upon destruction.
const Block & getContent() const
Get Content.
Definition: data.cpp:185
signal::Signal< NotificationSubscriberBase, Data > onDecodeError
fires when a Data packet in the Notification Stream cannot be decoded as Notification ...
Represents a Data packet.
Definition: data.hpp:35
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
represents an error in TLV encoding or decoding
NotificationSubscriberBase(Face &face, const Name &prefix, time::milliseconds interestLifetime)
construct a NotificationSubscriber
void stop()
stop receiving notifications