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