notification-subscriber.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 ndn-cxx library (NDN C++ library with eXperimental eXtensions).
12  *
13  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
14  * terms of the GNU Lesser General Public License as published by the Free Software
15  * Foundation, either version 3 of the License, or (at your option) any later version.
16  *
17  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20  *
21  * You should have received copies of the GNU General Public License and GNU Lesser
22  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
23  * <http://www.gnu.org/licenses/>.
24  *
25  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
26  */
27 
28 #ifndef NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
29 #define NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
30 
31 #include "ndn-cxx/face.hpp"
34 #include "ndn-cxx/util/signal.hpp"
35 #include "ndn-cxx/util/time.hpp"
36 
37 namespace ndn {
38 namespace util {
39 
40 class NotificationSubscriberBase : noncopyable
41 {
42 public:
43  virtual
45 
51  time::milliseconds
53  {
54  return m_interestLifetime;
55  }
56 
57  bool
58  isRunning() const
59  {
60  return m_isRunning;
61  }
62 
67  void
68  start();
69 
72  void
73  stop();
74 
75 protected:
80  NotificationSubscriberBase(Face& face, const Name& prefix,
81  time::milliseconds interestLifetime);
82 
83 private:
84  void
85  sendInitialInterest();
86 
87  void
88  sendNextInterest();
89 
90  void
91  sendInterest(const Interest& interest);
92 
93  virtual bool
94  hasSubscriber() const = 0;
95 
99  bool
100  shouldStop();
101 
102  void
103  afterReceiveData(const Data& data);
104 
108  virtual bool
109  decodeAndDeliver(const Data& data) = 0;
110 
111  void
112  afterReceiveNack(const lp::Nack& nack);
113 
114  void
115  afterTimeout();
116 
117  time::milliseconds
118  exponentialBackoff(lp::Nack nack);
119 
120 public:
124 
128 
132 
133 private:
134  Face& m_face;
135  Name m_prefix;
136  bool m_isRunning;
137  uint64_t m_lastSequenceNum;
138  uint64_t m_lastNackSequenceNum;
139  uint64_t m_attempts;
140  Scheduler m_scheduler;
141  scheduler::ScopedEventId m_nackEvent;
142  ScopedPendingInterestHandle m_lastInterest;
143  time::milliseconds m_interestLifetime;
144 };
145 
150 template<typename Notification>
152 {
153 public:
154  BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Notification>));
155  BOOST_CONCEPT_ASSERT((WireDecodable<Notification>));
156 
161  NotificationSubscriber(Face& face, const Name& prefix,
162  time::milliseconds interestLifetime = 1_min)
163  : NotificationSubscriberBase(face, prefix, interestLifetime)
164  {
165  }
166 
167 public:
172 
173 private:
174  bool
175  hasSubscriber() const override
176  {
177  return !onNotification.isEmpty();
178  }
179 
180  bool
181  decodeAndDeliver(const Data& data) override
182  {
183  Notification notification;
184  try {
185  notification.wireDecode(data.getContent().blockFromValue());
186  }
187  catch (const tlv::Error&) {
188  return false;
189  }
190 
191  onNotification(notification);
192  return true;
193  }
194 };
195 
196 } // namespace util
197 } // namespace ndn
198 
199 #endif // NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP
void start()
start or resume receiving notifications
Definition: data.cpp:26
Represents an Interest packet.
Definition: interest.hpp:50
provides a lightweight signal / event system
Definition: signal.hpp:52
signal::Signal< NotificationSubscriber, Notification > onNotification
fires when a Notification is received
represents a Network Nack
Definition: nack.hpp:38
signal::Signal< NotificationSubscriberBase, lp::Nack > onNack
fires when a NACK is received
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:90
provides a subscriber of Notification Stream
Represents an absolute name.
Definition: name.hpp:44
Generic time-based scheduler.
Definition: scheduler.hpp:134
NotificationSubscriber(Face &face, const Name &prefix, time::milliseconds interestLifetime=1_min)
construct a NotificationSubscriber
Block blockFromValue() const
Definition: block.cpp:314
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:172
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:39
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
Definition: tlv.hpp:51
NotificationSubscriberBase(Face &face, const Name &prefix, time::milliseconds interestLifetime)
construct a NotificationSubscriber
void stop()
stop receiving notifications