blob: c5c5098e22eff04dcb946949d44f6e637b02a1e2 [file] [log] [blame]
Alexander Afanasyevf5c07742012-10-31 13:13:05 -07001/* -*- 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 Afanasyev0c395372014-12-20 15:54:02 -080021#include "ndn-limits-window.hpp"
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070022
23#include "ns3/log.h"
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070024
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080025NS_LOG_COMPONENT_DEFINE("ndn.Limits.Window");
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070026
27namespace ns3 {
28namespace ndn {
29
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080030NS_OBJECT_ENSURE_REGISTERED(LimitsWindow);
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070031
32TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033LimitsWindow::GetTypeId()
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070034{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080035 static TypeId tid = TypeId("ns3::ndn::Limits::Window")
36 .SetGroupName("Ndn")
37 .SetParent<Limits>()
38 .AddConstructor<LimitsWindow>()
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070039
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 .AddTraceSource("CurMaxLimit", "Current maximum limit",
41 MakeTraceSourceAccessor(&LimitsWindow::m_curMaxLimit))
42
43 .AddTraceSource("Outstanding", "Number of outstanding interests",
44 MakeTraceSourceAccessor(&LimitsWindow::m_outstanding));
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070045 return tid;
46}
47
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070048void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049LimitsWindow::UpdateCurrentLimit(double limit)
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070050{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 NS_ASSERT_MSG(limit >= 0.0, "Limit should be greater or equal to zero");
52
53 m_curMaxLimit = std::min(limit, GetMaxRate() * GetMaxDelay());
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070054}
55
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070056bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057LimitsWindow::IsBelowLimit()
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070058{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 if (!IsEnabled())
60 return true;
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070061
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070062 return (m_curMaxLimit - m_outstanding >= 1.0);
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070063}
64
65void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066LimitsWindow::BorrowLimit()
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070067{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 if (!IsEnabled())
69 return;
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070070
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 NS_ASSERT_MSG(m_curMaxLimit - m_outstanding >= 1.0,
72 "Should not be possible, unless we IsBelowLimit was not checked correctly");
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070073 m_outstanding += 1;
74}
75
76void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077LimitsWindow::ReturnLimit()
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070078{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 if (!IsEnabled())
80 return;
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070081
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 NS_ASSERT_MSG(m_outstanding >= (uint32_t)1,
83 "Should not be possible, unless we decreasing this number twice somewhere");
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070084 m_outstanding -= 1;
Alexander Afanasyev08ab5722012-11-06 17:22:25 -080085
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 FireAvailableSlotCallback();
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070087}
88
89} // namespace ndn
90} // namespace ns3