Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -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.h" |
| 22 | |
| 23 | #include "ns3/log.h" |
| 24 | #include "ns3/simulator.h" |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 25 | #include "ns3/random-variable.h" |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 26 | |
| 27 | NS_LOG_COMPONENT_DEFINE ("ndn.Limits"); |
| 28 | |
| 29 | namespace ns3 { |
| 30 | namespace ndn { |
| 31 | |
| 32 | NS_OBJECT_ENSURE_REGISTERED (Limits); |
| 33 | |
| 34 | TypeId |
| 35 | Limits::GetTypeId () |
| 36 | { |
| 37 | static TypeId tid = TypeId ("ns3::ndn::Limits") |
| 38 | .SetGroupName ("Ndn") |
| 39 | .SetParent <Object> () |
| 40 | .AddConstructor <Limits> () |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 42 | .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 | |
| 53 | void |
Alexander Afanasyev | a28ec56 | 2012-10-25 14:07:32 -0700 | [diff] [blame] | 54 | Limits::SetMaxLimit (double max) |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 55 | { |
| 56 | m_maxLimit = max; |
| 57 | m_curMaxLimit = max; |
| 58 | } |
| 59 | |
Alexander Afanasyev | a28ec56 | 2012-10-25 14:07:32 -0700 | [diff] [blame] | 60 | double |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 61 | Limits::GetMaxLimit () const |
| 62 | { |
| 63 | return m_maxLimit; |
| 64 | } |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 65 | |
| 66 | void |
Alexander Afanasyev | a28ec56 | 2012-10-25 14:07:32 -0700 | [diff] [blame] | 67 | Limits::UpdateCurrentLimit (double limit) |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 68 | { |
Alexander Afanasyev | a28ec56 | 2012-10-25 14:07:32 -0700 | [diff] [blame] | 69 | NS_ASSERT_MSG (limit >= 0.0, "Limit should be greater or equal to zero"); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | a28ec56 | 2012-10-25 14:07:32 -0700 | [diff] [blame] | 71 | m_curMaxLimit = std::min (limit, m_maxLimit); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 74 | bool |
| 75 | Limits::IsBelowLimit () |
| 76 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 77 | if (!IsEnabled ()) return true; |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 79 | if (m_curMaxLimit - m_outstanding >= 1.0) |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 80 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 81 | // static UniformVariable acceptanceProbability (0, m_curMaxLimit); |
| 82 | // double value = acceptanceProbability.GetValue (); |
Alexander Afanasyev | a28ec56 | 2012-10-25 14:07:32 -0700 | [diff] [blame] | 83 | double value = m_outstanding + 1; |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 84 | |
| 85 | if (m_outstanding < value) |
| 86 | { |
| 87 | m_outstanding += 1.0; |
| 88 | return true; |
| 89 | } |
| 90 | else |
| 91 | return false; |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 92 | } |
| 93 | else |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | Limits::RemoveOutstanding () |
| 99 | { |
Alexander Afanasyev | a28ec56 | 2012-10-25 14:07:32 -0700 | [diff] [blame] | 100 | if (!IsEnabled ()) return; |
Alexander Afanasyev | 187cba2 | 2012-08-28 11:16:52 -0700 | [diff] [blame] | 101 | |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 102 | NS_LOG_DEBUG (m_outstanding); |
Alexander Afanasyev | cdd223c | 2012-09-03 19:56:19 -0700 | [diff] [blame] | 103 | NS_ASSERT_MSG (m_outstanding >= (uint32_t)1, "Should not be possible, unless we decreasing this number twice somewhere"); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 104 | m_outstanding -= 1; |
| 105 | } |
| 106 | |
| 107 | } // namespace ndn |
| 108 | } // namespace ns3 |