NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-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 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 "../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>));
153  BOOST_CONCEPT_ASSERT((WireDecodable<Notification>));
154 
160  time::milliseconds interestLifetime = 1_min)
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) 2011-2015 Regents of the University of California.
const Block & getContent() const
Get Content.
Definition: data.cpp:234
provides a lightweight signal / event system
Definition: signal.hpp:50
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
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:42
Event that is automatically cancelled upon destruction.
NotificationSubscriber(Face &face, const Name &prefix, time::milliseconds interestLifetime=1_min)
construct a NotificationSubscriber
Block blockFromValue() const
Definition: block.cpp:325
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
Definition: tlv.hpp:50
NotificationSubscriberBase(Face &face, const Name &prefix, time::milliseconds interestLifetime)
construct a NotificationSubscriber
void stop()
stop receiving notifications