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