blob: 215250efe837f377cf0f979662b8b37471cb0f71 [file] [log] [blame]
Alexander Afanasyev6f95e702012-10-31 16:27:31 -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
21#include "ndn-limits-rate.h"
22
23#include "ns3/log.h"
24#include "ns3/simulator.h"
25#include "ns3/random-variable.h"
26
27NS_LOG_COMPONENT_DEFINE ("ndn.Limits.Rate");
28
29namespace ns3 {
30namespace ndn {
31
32NS_OBJECT_ENSURE_REGISTERED (LimitsRate);
33
34TypeId
35LimitsRate::GetTypeId ()
36{
37 static TypeId tid = TypeId ("ns3::ndn::Limits::Rate")
38 .SetGroupName ("Ndn")
39 .SetParent <Limits> ()
40 .AddConstructor <LimitsRate> ()
41
42 ;
43 return tid;
44}
45
46void
47LimitsRate::UpdateCurrentLimit (double limit)
48{
49 NS_ASSERT_MSG (limit >= 0.0, "Limit should be greater or equal to zero");
50
51 m_bucketLeak = std::min (limit, GetMaxRate ());
52 m_bucketMax = m_bucketLeak * GetMaxDelay ();
53}
54
55bool
56LimitsRate::IsBelowLimit ()
57{
58 if (!IsEnabled ()) return true;
59
60 return (m_bucketMax - m_bucket >= 1.0);
61}
62
63void
64LimitsRate::BorrowLimit ()
65{
66 if (!IsEnabled ()) return;
67
68 NS_ASSERT_MSG (m_bucketMax - m_bucket >= 1.0, "Should not be possible, unless we IsBelowLimit was not checked correctly");
69 m_bucket += 1;
70}
71
72void
73LimitsRate::ReturnLimit ()
74{
75 // do nothing
76}
77
78void
79LimitsRate::LeakBucket (double interval)
80{
81 const double leak = m_bucketLeak * interval;
82
83 m_bucket = std::max (0.0, m_bucket - leak);
84}
85
86} // namespace ndn
87} // namespace ns3