Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -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 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 21 | #include "ndn-limits-window.hpp" |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 22 | |
| 23 | #include "ns3/log.h" |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 25 | NS_LOG_COMPONENT_DEFINE("ndn.Limits.Window"); |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ns3 { |
| 28 | namespace ndn { |
| 29 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 30 | NS_OBJECT_ENSURE_REGISTERED(LimitsWindow); |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 31 | |
| 32 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 33 | LimitsWindow::GetTypeId() |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 34 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 35 | static TypeId tid = TypeId("ns3::ndn::Limits::Window") |
| 36 | .SetGroupName("Ndn") |
| 37 | .SetParent<Limits>() |
| 38 | .AddConstructor<LimitsWindow>() |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 40 | .AddTraceSource("CurMaxLimit", "Current maximum limit", |
| 41 | MakeTraceSourceAccessor(&LimitsWindow::m_curMaxLimit)) |
| 42 | |
| 43 | .AddTraceSource("Outstanding", "Number of outstanding interests", |
| 44 | MakeTraceSourceAccessor(&LimitsWindow::m_outstanding)); |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 45 | return tid; |
| 46 | } |
| 47 | |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 48 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 49 | LimitsWindow::UpdateCurrentLimit(double limit) |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 50 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 51 | NS_ASSERT_MSG(limit >= 0.0, "Limit should be greater or equal to zero"); |
| 52 | |
| 53 | m_curMaxLimit = std::min(limit, GetMaxRate() * GetMaxDelay()); |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 56 | bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 57 | LimitsWindow::IsBelowLimit() |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 58 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 59 | if (!IsEnabled()) |
| 60 | return true; |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 62 | return (m_curMaxLimit - m_outstanding >= 1.0); |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | LimitsWindow::BorrowLimit() |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 67 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 68 | if (!IsEnabled()) |
| 69 | return; |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | NS_ASSERT_MSG(m_curMaxLimit - m_outstanding >= 1.0, |
| 72 | "Should not be possible, unless we IsBelowLimit was not checked correctly"); |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 73 | m_outstanding += 1; |
| 74 | } |
| 75 | |
| 76 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 77 | LimitsWindow::ReturnLimit() |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 78 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | if (!IsEnabled()) |
| 80 | return; |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 82 | NS_ASSERT_MSG(m_outstanding >= (uint32_t)1, |
| 83 | "Should not be possible, unless we decreasing this number twice somewhere"); |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 84 | m_outstanding -= 1; |
Alexander Afanasyev | 08ab572 | 2012-11-06 17:22:25 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 86 | FireAvailableSlotCallback(); |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace ndn |
| 90 | } // namespace ns3 |