blob: 2e4a9ec447c3fd04275fe08eec571ee26bc45f87 [file] [log] [blame]
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -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.h"
22
23#include "ns3/log.h"
24#include "ns3/simulator.h"
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070025#include "ns3/random-variable.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070026
27NS_LOG_COMPONENT_DEFINE ("ndn.Limits");
28
29namespace ns3 {
30namespace ndn {
31
32NS_OBJECT_ENSURE_REGISTERED (Limits);
33
34TypeId
35Limits::GetTypeId ()
36{
37 static TypeId tid = TypeId ("ns3::ndn::Limits")
38 .SetGroupName ("Ndn")
39 .SetParent <Object> ()
40 .AddConstructor <Limits> ()
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070041
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070042 .AddTraceSource ("CurMaxLimit",
43 "Current maximum limit",
44 MakeTraceSourceAccessor (&Limits::m_curMaxLimit))
45
46 .AddTraceSource ("Outstanding",
47 "Number of outstanding interests",
48 MakeTraceSourceAccessor (&Limits::m_outstanding))
49 ;
50 return tid;
51}
52
53void
Alexander Afanasyeva28ec562012-10-25 14:07:32 -070054Limits::SetMaxLimit (double max)
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070055{
56 m_maxLimit = max;
57 m_curMaxLimit = max;
58}
59
Alexander Afanasyeva28ec562012-10-25 14:07:32 -070060double
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070061Limits::GetMaxLimit () const
62{
63 return m_maxLimit;
64}
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070065
66void
Alexander Afanasyeva28ec562012-10-25 14:07:32 -070067Limits::UpdateCurrentLimit (double limit)
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070068{
Alexander Afanasyeva28ec562012-10-25 14:07:32 -070069 NS_ASSERT_MSG (limit >= 0.0, "Limit should be greater or equal to zero");
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070070
Alexander Afanasyeva28ec562012-10-25 14:07:32 -070071 m_curMaxLimit = std::min (limit, m_maxLimit);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070072}
73
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070074bool
75Limits::IsBelowLimit ()
76{
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070077 if (!IsEnabled ()) return true;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070078
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070079 if (m_curMaxLimit - m_outstanding >= 1.0)
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070080 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070081 // static UniformVariable acceptanceProbability (0, m_curMaxLimit);
82 // double value = acceptanceProbability.GetValue ();
Alexander Afanasyeva28ec562012-10-25 14:07:32 -070083 double value = m_outstanding + 1;
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070084
85 if (m_outstanding < value)
86 {
87 m_outstanding += 1.0;
88 return true;
89 }
90 else
91 return false;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070092 }
93 else
94 return false;
95}
96
97void
98Limits::RemoveOutstanding ()
99{
Alexander Afanasyeva28ec562012-10-25 14:07:32 -0700100 if (!IsEnabled ()) return;
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700101
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700102 NS_LOG_DEBUG (m_outstanding);
Alexander Afanasyevcdd223c2012-09-03 19:56:19 -0700103 NS_ASSERT_MSG (m_outstanding >= (uint32_t)1, "Should not be possible, unless we decreasing this number twice somewhere");
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700104 m_outstanding -= 1;
105}
106
107} // namespace ndn
108} // namespace ns3