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