blob: a2630a44e2af2133647de135d972d7a8a3f3f764 [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
25NS_LOG_COMPONENT_DEFINE ("ndn.Limits.Window");
26
27namespace ns3 {
28namespace ndn {
29
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070030NS_OBJECT_ENSURE_REGISTERED (LimitsWindow);
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070031
32TypeId
33LimitsWindow::GetTypeId ()
34{
35 static TypeId tid = TypeId ("ns3::ndn::Limits::Window")
36 .SetGroupName ("Ndn")
37 .SetParent <Limits> ()
38 .AddConstructor <LimitsWindow> ()
39
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070040 .AddTraceSource ("CurMaxLimit",
41 "Current maximum limit",
42 MakeTraceSourceAccessor (&LimitsWindow::m_curMaxLimit))
43
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070044 .AddTraceSource ("Outstanding",
45 "Number of outstanding interests",
46 MakeTraceSourceAccessor (&LimitsWindow::m_outstanding))
47 ;
48 return tid;
49}
50
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070051void
52LimitsWindow::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 Afanasyevf5c07742012-10-31 13:13:05 -070059bool
60LimitsWindow::IsBelowLimit ()
61{
62 if (!IsEnabled ()) return true;
63
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070064 return (m_curMaxLimit - m_outstanding >= 1.0);
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070065}
66
67void
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070068LimitsWindow::BorrowLimit ()
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070069{
70 if (!IsEnabled ()) return;
71
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070072 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
76void
77LimitsWindow::ReturnLimit ()
78{
79 if (!IsEnabled ()) return;
80
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070081 NS_ASSERT_MSG (m_outstanding >= (uint32_t)1, "Should not be possible, unless we decreasing this number twice somewhere");
82 m_outstanding -= 1;
Alexander Afanasyev08ab5722012-11-06 17:22:25 -080083
84 FireAvailableSlotCallback ();
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070085}
86
87} // namespace ndn
88} // namespace ns3