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 () |
| 47 | : m_bucketMax (0) |
| 48 | , m_bucketLeak (0) |
| 49 | , m_bucket (0) |
| 50 | { } |
| 51 | |
| 52 | virtual |
| 53 | ~LimitsRate () { } |
| 54 | |
| 55 | virtual void |
| 56 | SetLimits (double rate, double delay) |
| 57 | { |
| 58 | super::SetLimits (rate, delay); |
| 59 | |
| 60 | // maximum allowed burst |
| 61 | m_bucketMax = GetMaxRate () * GetMaxDelay (); |
| 62 | |
| 63 | // amount of packets allowed every second (leak rate) |
| 64 | m_bucketLeak = GetMaxRate (); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @brief Check if Interest limit is reached (token bucket is not empty) |
| 69 | */ |
| 70 | virtual bool |
| 71 | IsBelowLimit (); |
| 72 | |
| 73 | /** |
| 74 | * @brief Get token from the bucket |
| 75 | */ |
| 76 | virtual void |
| 77 | BorrowLimit (); |
| 78 | |
| 79 | /** |
| 80 | * @brief Does nothing (token bucket leakage is time-dependent only) |
| 81 | */ |
| 82 | virtual void |
| 83 | ReturnLimit (); |
| 84 | |
| 85 | /** |
| 86 | * @brief Update current leak ratio and maximum burst |
| 87 | */ |
| 88 | virtual void |
| 89 | UpdateCurrentLimit (double limit); |
| 90 | |
| 91 | /** |
| 92 | */ |
| 93 | virtual double |
| 94 | GetCurrentLimit () const |
| 95 | { |
| 96 | return m_bucketLeak; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | /** |
| 101 | * @brief Leak bucket, assuming `interval' seconds between leakages |
| 102 | * |
| 103 | * @param interval Time interval for leakage. Used to calculate size of the leak |
| 104 | */ |
| 105 | void |
| 106 | LeakBucket (double interval); |
| 107 | |
| 108 | private: |
| 109 | double m_bucketMax; ///< \brief Maximum Interest allowance for this face (maximum tokens that can be issued at the same time) |
| 110 | double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second (token bucket leak rate) |
| 111 | double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face (current size of token bucket) |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | } // namespace ndn |
| 116 | } // namespace ns3 |
| 117 | |
| 118 | #endif // _NDN_LIMITS_RATE_H_ |