Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
Junxiao Shi | 07115cc | 2019-01-23 19:00:59 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019 Regents of the University of California, |
Davide Pesavento | b5f8bcc | 2017-02-05 17:58:05 -0500 | [diff] [blame] | 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. |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 28 | #ifndef NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP |
| 29 | #define NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP |
| 30 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 31 | #include "ndn-cxx/face.hpp" |
| 32 | #include "ndn-cxx/util/concepts.hpp" |
| 33 | #include "ndn-cxx/util/scheduler.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 34 | #include "ndn-cxx/util/signal.hpp" |
| 35 | #include "ndn-cxx/util/time.hpp" |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace util { |
| 39 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 40 | class NotificationSubscriberBase : noncopyable |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 43 | virtual |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 44 | ~NotificationSubscriberBase(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 45 | |
| 46 | /** \return InterestLifetime of Interests to retrieve notifications |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 47 | * |
| 48 | * This must be greater than FreshnessPeriod of Notification Data packets, |
| 49 | * to ensure correct operation of this subscriber implementation. |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 50 | */ |
| 51 | time::milliseconds |
| 52 | getInterestLifetime() const |
| 53 | { |
| 54 | return m_interestLifetime; |
| 55 | } |
| 56 | |
| 57 | bool |
| 58 | isRunning() const |
| 59 | { |
| 60 | return m_isRunning; |
| 61 | } |
| 62 | |
| 63 | /** \brief start or resume receiving notifications |
| 64 | * \note onNotification must have at least one listener, |
| 65 | * otherwise this operation has no effect. |
| 66 | */ |
| 67 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 68 | start(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 69 | |
| 70 | /** \brief stop receiving notifications |
| 71 | */ |
| 72 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 73 | stop(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 74 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 75 | protected: |
| 76 | /** \brief construct a NotificationSubscriber |
| 77 | * \note The subscriber is not started after construction. |
| 78 | * User should add one or more handlers to onNotification, and invoke .start(). |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 79 | */ |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 80 | NotificationSubscriberBase(Face& face, const Name& prefix, |
| 81 | time::milliseconds interestLifetime); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 85 | sendInitialInterest(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 86 | |
| 87 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 88 | sendNextInterest(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | 946a51c | 2018-11-24 00:25:57 +0000 | [diff] [blame] | 90 | void |
| 91 | sendInterest(const Interest& interest); |
| 92 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 93 | virtual bool |
| 94 | hasSubscriber() const = 0; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 95 | |
| 96 | /** \brief Check if the subscriber is or should be stopped. |
| 97 | * \return true if the subscriber is stopped. |
| 98 | */ |
| 99 | bool |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 100 | shouldStop(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 101 | |
| 102 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 103 | afterReceiveData(const Data& data); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 104 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 105 | /** \brief decode the Data as a notification, and deliver it to subscribers |
| 106 | * \return whether decode was successful |
| 107 | */ |
| 108 | virtual bool |
| 109 | decodeAndDeliver(const Data& data) = 0; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 110 | |
| 111 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 112 | afterReceiveNack(const lp::Nack& nack); |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 113 | |
| 114 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 115 | afterTimeout(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 116 | |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 117 | time::milliseconds |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 118 | exponentialBackoff(lp::Nack nack); |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 119 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 120 | public: |
| 121 | /** \brief fires when a NACK is received |
| 122 | */ |
| 123 | signal::Signal<NotificationSubscriberBase, lp::Nack> onNack; |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 124 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 125 | /** \brief fires when no Notification is received within .getInterestLifetime period |
| 126 | */ |
| 127 | signal::Signal<NotificationSubscriberBase> onTimeout; |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 128 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 129 | /** \brief fires when a Data packet in the Notification Stream cannot be decoded as Notification |
| 130 | */ |
| 131 | signal::Signal<NotificationSubscriberBase, Data> onDecodeError; |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 133 | private: |
| 134 | Face& m_face; |
| 135 | Name m_prefix; |
| 136 | bool m_isRunning; |
Junxiao Shi | 946a51c | 2018-11-24 00:25:57 +0000 | [diff] [blame] | 137 | uint64_t m_lastSequenceNum; |
| 138 | uint64_t m_lastNackSequenceNum; |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 139 | uint64_t m_attempts; |
Davide Pesavento | fd61231 | 2019-03-15 18:45:46 -0400 | [diff] [blame^] | 140 | Scheduler m_scheduler; |
| 141 | scheduler::ScopedEventId m_nackEvent; |
Junxiao Shi | 4fdcb27 | 2019-02-11 15:05:46 -0700 | [diff] [blame] | 142 | ScopedPendingInterestHandle m_lastInterest; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 143 | time::milliseconds m_interestLifetime; |
| 144 | }; |
| 145 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 146 | /** \brief provides a subscriber of Notification Stream |
| 147 | * \sa https://redmine.named-data.net/projects/nfd/wiki/Notification |
| 148 | * \tparam Notification type of Notification item, appears in payload of Data packets |
| 149 | */ |
| 150 | template<typename Notification> |
| 151 | class NotificationSubscriber : public NotificationSubscriberBase |
| 152 | { |
| 153 | public: |
| 154 | BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Notification>)); |
| 155 | BOOST_CONCEPT_ASSERT((WireDecodable<Notification>)); |
| 156 | |
| 157 | /** \brief construct a NotificationSubscriber |
| 158 | * \note The subscriber is not started after construction. |
| 159 | * User should add one or more handlers to onNotification, and invoke .start(). |
| 160 | */ |
| 161 | NotificationSubscriber(Face& face, const Name& prefix, |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 162 | time::milliseconds interestLifetime = 1_min) |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 163 | : NotificationSubscriberBase(face, prefix, interestLifetime) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | public: |
| 168 | /** \brief fires when a Notification is received |
| 169 | * \note Removing all handlers will cause the subscriber to stop. |
| 170 | */ |
| 171 | signal::Signal<NotificationSubscriber, Notification> onNotification; |
| 172 | |
| 173 | private: |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 174 | bool |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 175 | hasSubscriber() const override |
| 176 | { |
| 177 | return !onNotification.isEmpty(); |
| 178 | } |
| 179 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 180 | bool |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame] | 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 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 196 | } // namespace util |
| 197 | } // namespace ndn |
| 198 | |
| 199 | #endif // NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP |