Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #ifndef _NDN_LIMITS_RATE_H_ |
| 22 | #define _NDN_LIMITS_RATE_H_ |
| 23 | |
| 24 | #include "ndn-limits.h" |
| 25 | |
| 26 | namespace ns3 { |
| 27 | namespace ndn { |
| 28 | |
| 29 | /** |
| 30 | * \ingroup ndn |
| 31 | * \brief Structure to manage limits for outstanding interests |
| 32 | */ |
| 33 | class LimitsRate : |
| 34 | public Limits |
| 35 | { |
| 36 | public: |
| 37 | typedef Limits super; |
| 38 | |
| 39 | static TypeId |
| 40 | GetTypeId (); |
| 41 | |
| 42 | /** |
| 43 | * \brief Constructor |
| 44 | * \param prefix smart pointer to the prefix for the FIB entry |
| 45 | */ |
| 46 | LimitsRate () |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 47 | : m_isLeakScheduled (false) |
| 48 | , m_bucketMax (0) |
| 49 | , m_bucketLeak (1) |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 50 | , m_bucket (0) |
| 51 | { } |
| 52 | |
| 53 | virtual |
| 54 | ~LimitsRate () { } |
| 55 | |
| 56 | virtual void |
Alexander Afanasyev | ccd5bb4 | 2012-10-31 17:27:49 -0700 | [diff] [blame] | 57 | SetLimits (double rate, double delay); |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | a8f5d88 | 2012-11-09 14:22:48 -0800 | [diff] [blame] | 59 | virtual |
| 60 | double |
| 61 | GetMaxLimit () const |
| 62 | { |
| 63 | return GetMaxRate (); |
| 64 | } |
| 65 | |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 66 | /** |
| 67 | * @brief Check if Interest limit is reached (token bucket is not empty) |
| 68 | */ |
| 69 | virtual bool |
| 70 | IsBelowLimit (); |
| 71 | |
| 72 | /** |
| 73 | * @brief Get token from the bucket |
| 74 | */ |
| 75 | virtual void |
| 76 | BorrowLimit (); |
| 77 | |
| 78 | /** |
| 79 | * @brief Does nothing (token bucket leakage is time-dependent only) |
| 80 | */ |
| 81 | virtual void |
| 82 | ReturnLimit (); |
| 83 | |
| 84 | /** |
Alexander Afanasyev | ccd5bb4 | 2012-10-31 17:27:49 -0700 | [diff] [blame] | 85 | * @brief Update normalized amount that should be leaked every second (token bucket leak rate) and leak rate |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 86 | */ |
| 87 | virtual void |
| 88 | UpdateCurrentLimit (double limit); |
| 89 | |
| 90 | /** |
Alexander Afanasyev | ccd5bb4 | 2012-10-31 17:27:49 -0700 | [diff] [blame] | 91 | * @brief Get normalized amount that should be leaked every second (token bucket leak rate) |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 92 | */ |
| 93 | virtual double |
| 94 | GetCurrentLimit () const |
| 95 | { |
| 96 | return m_bucketLeak; |
| 97 | } |
| 98 | |
Alexander Afanasyev | a8f5d88 | 2012-11-09 14:22:48 -0800 | [diff] [blame] | 99 | virtual double |
| 100 | GetCurrentLimitRate () const |
| 101 | { |
| 102 | return m_bucketLeak; |
| 103 | } |
| 104 | |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 105 | protected: |
| 106 | // from Node |
| 107 | void |
| 108 | NotifyNewAggregate (); |
| 109 | |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 110 | private: |
| 111 | /** |
| 112 | * @brief Leak bucket, assuming `interval' seconds between leakages |
| 113 | * |
| 114 | * @param interval Time interval for leakage. Used to calculate size of the leak |
| 115 | */ |
| 116 | void |
| 117 | LeakBucket (double interval); |
| 118 | |
| 119 | private: |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 120 | bool m_isLeakScheduled; |
| 121 | |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 122 | double m_bucketMax; ///< \brief Maximum Interest allowance for this face (maximum tokens that can be issued at the same time) |
| 123 | double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second (token bucket leak rate) |
| 124 | double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face (current size of token bucket) |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | } // namespace ndn |
| 129 | } // namespace ns3 |
| 130 | |
| 131 | #endif // _NDN_LIMITS_RATE_H_ |