notification-subscriber.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
48 #ifndef NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
49 #define NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
50 
51 #include "../face.hpp"
52 #include "signal.hpp"
53 #include "concepts.hpp"
54 #include "time.hpp"
55 #include "scheduler.hpp"
57 #include <boost/concept_check.hpp>
58 
59 namespace ndn {
60 namespace util {
61 
62 class NotificationSubscriberBase : noncopyable
63 {
64 public:
65  virtual
67 
73  time::milliseconds
75  {
76  return m_interestLifetime;
77  }
78 
79  bool
80  isRunning() const
81  {
82  return m_isRunning;
83  }
84 
89  void
90  start();
91 
94  void
95  stop();
96 
97 protected:
102  NotificationSubscriberBase(Face& face, const Name& prefix,
103  time::milliseconds interestLifetime);
104 
105 private:
106  void
107  sendInitialInterest();
108 
109  void
110  sendNextInterest();
111 
112  virtual bool
113  hasSubscriber() const = 0;
114 
118  bool
119  shouldStop();
120 
121  void
122  afterReceiveData(const Data& data);
123 
127  virtual bool
128  decodeAndDeliver(const Data& data) = 0;
129 
130  void
131  afterReceiveNack(const lp::Nack& nack);
132 
133  void
134  afterTimeout();
135 
136  time::milliseconds
137  exponentialBackoff(lp::Nack nack);
138 
139 public:
143 
147 
151 
152 private:
153  Face& m_face;
154  Name m_prefix;
155  bool m_isRunning;
156  uint64_t m_lastSequenceNo;
157  uint64_t m_lastNackSequenceNo;
158  uint64_t m_attempts;
159  util::scheduler::Scheduler m_scheduler;
160  util::scheduler::ScopedEventId m_nackEvent;
161  const PendingInterestId* m_lastInterestId;
162  time::milliseconds m_interestLifetime;
163 };
164 
169 template<typename Notification>
171 {
172 public:
173  BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Notification>));
174  BOOST_CONCEPT_ASSERT((WireDecodable<Notification>));
175 
180  NotificationSubscriber(Face& face, const Name& prefix,
181  time::milliseconds interestLifetime = time::seconds(60))
182  : NotificationSubscriberBase(face, prefix, interestLifetime)
183  {
184  }
185 
186 public:
191 
192 private:
193  bool
194  hasSubscriber() const override
195  {
196  return !onNotification.isEmpty();
197  }
198 
199  bool
200  decodeAndDeliver(const Data& data) override
201  {
202  Notification notification;
203  try {
204  notification.wireDecode(data.getContent().blockFromValue());
205  }
206  catch (const tlv::Error&) {
207  return false;
208  }
209 
210  onNotification(notification);
211  return true;
212  }
213 };
214 
215 } // namespace util
216 } // namespace ndn
217 
218 #endif // NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
void start()
start or resume receiving notifications
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
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:437
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:121
provides a subscriber of Notification Stream
NotificationSubscriber(Face &face, const Name &prefix, time::milliseconds interestLifetime=time::seconds(60))
construct a NotificationSubscriber
Name abstraction to represent an absolute name.
Definition: name.hpp:46
Event that is automatically cancelled upon destruction.
const Block & getContent() const
Get content Block.
Definition: data.cpp:230
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:37
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:76
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