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 | #include "ndn-limits-rate.h" |
| 22 | |
| 23 | #include "ns3/log.h" |
| 24 | #include "ns3/simulator.h" |
| 25 | #include "ns3/random-variable.h" |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 26 | #include "ns3/ndn-face.h" |
| 27 | #include "ns3/node.h" |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 28 | |
| 29 | NS_LOG_COMPONENT_DEFINE ("ndn.Limits.Rate"); |
| 30 | |
| 31 | namespace ns3 { |
| 32 | namespace ndn { |
| 33 | |
| 34 | NS_OBJECT_ENSURE_REGISTERED (LimitsRate); |
| 35 | |
| 36 | TypeId |
| 37 | LimitsRate::GetTypeId () |
| 38 | { |
| 39 | static TypeId tid = TypeId ("ns3::ndn::Limits::Rate") |
| 40 | .SetGroupName ("Ndn") |
| 41 | .SetParent <Limits> () |
| 42 | .AddConstructor <LimitsRate> () |
| 43 | |
| 44 | ; |
| 45 | return tid; |
| 46 | } |
| 47 | |
| 48 | void |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 49 | LimitsRate::NotifyNewAggregate () |
| 50 | { |
| 51 | super::NotifyNewAggregate (); |
| 52 | |
| 53 | if (!m_isLeakScheduled) |
| 54 | { |
| 55 | if (GetObject<Face> () != 0) |
| 56 | { |
| 57 | NS_ASSERT_MSG (GetObject<Face> ()->GetNode () != 0, "Node object should exist on the face"); |
| 58 | |
| 59 | m_isLeakScheduled = true; |
Alexander Afanasyev | 08ab572 | 2012-11-06 17:22:25 -0800 | [diff] [blame] | 60 | UniformVariable r (0.0, 1.0); |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 61 | Simulator::ScheduleWithContext (GetObject<Face> ()->GetNode ()->GetId (), |
| 62 | Seconds (r.GetValue ()), &LimitsRate::LeakBucket, this, 0.0); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Alexander Afanasyev | ccd5bb4 | 2012-10-31 17:27:49 -0700 | [diff] [blame] | 67 | void |
| 68 | LimitsRate::SetLimits (double rate, double delay) |
| 69 | { |
| 70 | super::SetLimits (rate, delay); |
| 71 | |
| 72 | // maximum allowed burst |
| 73 | m_bucketMax = GetMaxRate () * GetMaxDelay (); |
| 74 | |
| 75 | // amount of packets allowed every second (leak rate) |
| 76 | m_bucketLeak = GetMaxRate (); |
| 77 | } |
| 78 | |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 79 | |
| 80 | void |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 81 | LimitsRate::UpdateCurrentLimit (double limit) |
| 82 | { |
| 83 | NS_ASSERT_MSG (limit >= 0.0, "Limit should be greater or equal to zero"); |
| 84 | |
| 85 | m_bucketLeak = std::min (limit, GetMaxRate ()); |
| 86 | m_bucketMax = m_bucketLeak * GetMaxDelay (); |
| 87 | } |
| 88 | |
| 89 | bool |
| 90 | LimitsRate::IsBelowLimit () |
| 91 | { |
| 92 | if (!IsEnabled ()) return true; |
| 93 | |
| 94 | return (m_bucketMax - m_bucket >= 1.0); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | LimitsRate::BorrowLimit () |
| 99 | { |
| 100 | if (!IsEnabled ()) return; |
| 101 | |
| 102 | NS_ASSERT_MSG (m_bucketMax - m_bucket >= 1.0, "Should not be possible, unless we IsBelowLimit was not checked correctly"); |
| 103 | m_bucket += 1; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | LimitsRate::ReturnLimit () |
| 108 | { |
| 109 | // do nothing |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | LimitsRate::LeakBucket (double interval) |
| 114 | { |
| 115 | const double leak = m_bucketLeak * interval; |
| 116 | |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 117 | #ifdef NS3_LOG_ENABLE |
| 118 | if (m_bucket>1) |
| 119 | { |
| 120 | NS_LOG_DEBUG ("Leak from " << m_bucket << " to " << std::max (0.0, m_bucket - leak)); |
| 121 | } |
| 122 | #endif |
Alexander Afanasyev | 08ab572 | 2012-11-06 17:22:25 -0800 | [diff] [blame] | 123 | |
| 124 | double bucketOld = m_bucket; |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 126 | m_bucket = std::max (0.0, m_bucket - leak); |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 127 | |
| 128 | // calculate interval so next time we will leak by 1.001, unless such interval would be more than 1 second |
| 129 | double newInterval = 1.0; |
| 130 | if (m_bucketLeak > 1.0) |
| 131 | { |
| 132 | newInterval = 1.001 / m_bucketLeak; |
| 133 | } |
Alexander Afanasyev | 08ab572 | 2012-11-06 17:22:25 -0800 | [diff] [blame] | 134 | |
| 135 | if (m_bucketMax - bucketOld < 1.0 && |
| 136 | m_bucketMax - m_bucket >= 1.0) // limit number of times this stuff is called |
| 137 | { |
| 138 | this->FireAvailableSlotCallback (); |
| 139 | } |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 140 | |
| 141 | Simulator::Schedule (Seconds (newInterval), &LimitsRate::LeakBucket, this, newInterval); |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } // namespace ndn |
| 145 | } // namespace ns3 |