Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Original copyright notice from NFD: |
| 24 | * |
| 25 | * Copyright (c) 2014, Regents of the University of California, |
| 26 | * Arizona Board of Regents, |
| 27 | * Colorado State University, |
| 28 | * University Pierre & Marie Curie, Sorbonne University, |
| 29 | * Washington University in St. Louis, |
| 30 | * Beijing Institute of Technology, |
| 31 | * The University of Memphis |
| 32 | * |
| 33 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 34 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 35 | * |
| 36 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 37 | * of the GNU General Public License as published by the Free Software Foundation, |
| 38 | * either version 3 of the License, or (at your option) any later version. |
| 39 | * |
| 40 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 41 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 42 | * PURPOSE. See the GNU General Public License for more details. |
| 43 | * |
| 44 | * You should have received a copy of the GNU General Public License along with |
| 45 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 46 | */ |
| 47 | |
| 48 | #ifndef NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP |
| 49 | #define NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP |
| 50 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 51 | #include "../face.hpp" |
Junxiao Shi | 728873f | 2015-01-20 16:01:26 -0700 | [diff] [blame] | 52 | #include "signal.hpp" |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 53 | #include "concepts.hpp" |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 54 | #include "time.hpp" |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 55 | #include "scheduler.hpp" |
| 56 | #include "scheduler-scoped-event-id.hpp" |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 57 | #include <boost/concept_check.hpp> |
| 58 | |
| 59 | namespace ndn { |
| 60 | namespace util { |
| 61 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 62 | class NotificationSubscriberBase : noncopyable |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 63 | { |
| 64 | public: |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 65 | virtual |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 66 | ~NotificationSubscriberBase(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 67 | |
| 68 | /** \return InterestLifetime of Interests to retrieve notifications |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 69 | * |
| 70 | * This must be greater than FreshnessPeriod of Notification Data packets, |
| 71 | * to ensure correct operation of this subscriber implementation. |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 72 | */ |
| 73 | time::milliseconds |
| 74 | getInterestLifetime() const |
| 75 | { |
| 76 | return m_interestLifetime; |
| 77 | } |
| 78 | |
| 79 | bool |
| 80 | isRunning() const |
| 81 | { |
| 82 | return m_isRunning; |
| 83 | } |
| 84 | |
| 85 | /** \brief start or resume receiving notifications |
| 86 | * \note onNotification must have at least one listener, |
| 87 | * otherwise this operation has no effect. |
| 88 | */ |
| 89 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 90 | start(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 91 | |
| 92 | /** \brief stop receiving notifications |
| 93 | */ |
| 94 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 95 | stop(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 97 | protected: |
| 98 | /** \brief construct a NotificationSubscriber |
| 99 | * \note The subscriber is not started after construction. |
| 100 | * User should add one or more handlers to onNotification, and invoke .start(). |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 101 | */ |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 102 | NotificationSubscriberBase(Face& face, const Name& prefix, |
| 103 | time::milliseconds interestLifetime); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 104 | |
| 105 | private: |
| 106 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 107 | sendInitialInterest(); |
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 | sendNextInterest(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 112 | virtual bool |
| 113 | hasSubscriber() const = 0; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 114 | |
| 115 | /** \brief Check if the subscriber is or should be stopped. |
| 116 | * \return true if the subscriber is stopped. |
| 117 | */ |
| 118 | bool |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 119 | shouldStop(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 120 | |
| 121 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 122 | afterReceiveData(const Data& data); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 123 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 124 | /** \brief decode the Data as a notification, and deliver it to subscribers |
| 125 | * \return whether decode was successful |
| 126 | */ |
| 127 | virtual bool |
| 128 | decodeAndDeliver(const Data& data) = 0; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 129 | |
| 130 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 131 | afterReceiveNack(const lp::Nack& nack); |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 132 | |
| 133 | void |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 134 | afterTimeout(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 135 | |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 136 | time::milliseconds |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 137 | exponentialBackoff(lp::Nack nack); |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 138 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 139 | public: |
| 140 | /** \brief fires when a NACK is received |
| 141 | */ |
| 142 | signal::Signal<NotificationSubscriberBase, lp::Nack> onNack; |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 143 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 144 | /** \brief fires when no Notification is received within .getInterestLifetime period |
| 145 | */ |
| 146 | signal::Signal<NotificationSubscriberBase> onTimeout; |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 147 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 148 | /** \brief fires when a Data packet in the Notification Stream cannot be decoded as Notification |
| 149 | */ |
| 150 | signal::Signal<NotificationSubscriberBase, Data> onDecodeError; |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 151 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 152 | private: |
| 153 | Face& m_face; |
| 154 | Name m_prefix; |
| 155 | bool m_isRunning; |
| 156 | uint64_t m_lastSequenceNo; |
Teng Liang | f8bf1b0 | 2016-07-17 11:54:19 -0700 | [diff] [blame] | 157 | uint64_t m_lastNackSequenceNo; |
| 158 | uint64_t m_attempts; |
| 159 | util::scheduler::Scheduler m_scheduler; |
| 160 | util::scheduler::ScopedEventId m_nackEvent; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 161 | const PendingInterestId* m_lastInterestId; |
| 162 | time::milliseconds m_interestLifetime; |
| 163 | }; |
| 164 | |
Junxiao Shi | 18244e8 | 2016-12-03 15:42:01 +0000 | [diff] [blame^] | 165 | /** \brief provides a subscriber of Notification Stream |
| 166 | * \sa https://redmine.named-data.net/projects/nfd/wiki/Notification |
| 167 | * \tparam Notification type of Notification item, appears in payload of Data packets |
| 168 | */ |
| 169 | template<typename Notification> |
| 170 | class NotificationSubscriber : public NotificationSubscriberBase |
| 171 | { |
| 172 | public: |
| 173 | BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Notification>)); |
| 174 | BOOST_CONCEPT_ASSERT((WireDecodable<Notification>)); |
| 175 | |
| 176 | /** \brief construct a NotificationSubscriber |
| 177 | * \note The subscriber is not started after construction. |
| 178 | * User should add one or more handlers to onNotification, and invoke .start(). |
| 179 | */ |
| 180 | NotificationSubscriber(Face& face, const Name& prefix, |
| 181 | time::milliseconds interestLifetime = time::seconds(60)) |
| 182 | : NotificationSubscriberBase(face, prefix, interestLifetime) |
| 183 | { |
| 184 | } |
| 185 | |
| 186 | public: |
| 187 | /** \brief fires when a Notification is received |
| 188 | * \note Removing all handlers will cause the subscriber to stop. |
| 189 | */ |
| 190 | signal::Signal<NotificationSubscriber, Notification> onNotification; |
| 191 | |
| 192 | private: |
| 193 | virtual bool |
| 194 | hasSubscriber() const override |
| 195 | { |
| 196 | return !onNotification.isEmpty(); |
| 197 | } |
| 198 | |
| 199 | virtual bool |
| 200 | decodeAndDeliver(const Data& data) override |
| 201 | { |
| 202 | Notification notification; |
| 203 | try { |
| 204 | notification.wireDecode(data.getContent().blockFromValue()); |
| 205 | } |
| 206 | catch (const tlv::Error&) { |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | onNotification(notification); |
| 211 | return true; |
| 212 | } |
| 213 | }; |
| 214 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 215 | } // namespace util |
| 216 | } // namespace ndn |
| 217 | |
| 218 | #endif // NDN_UTIL_NOTIFICATION_SUBSCRIBER_HPP |