Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 20 | #include "ndn-consumer-window.hpp" |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 21 | #include "ns3/ptr.h" |
| 22 | #include "ns3/log.h" |
| 23 | #include "ns3/simulator.h" |
| 24 | #include "ns3/packet.h" |
| 25 | #include "ns3/callback.h" |
| 26 | #include "ns3/string.h" |
| 27 | #include "ns3/uinteger.h" |
| 28 | #include "ns3/double.h" |
Xinyu Ma | 60562d4 | 2019-03-14 14:05:20 -0700 | [diff] [blame] | 29 | #include <limits> |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 31 | NS_LOG_COMPONENT_DEFINE("ndn.ConsumerWindow"); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 33 | namespace ns3 { |
| 34 | namespace ndn { |
Alexander Afanasyev | 3a3ce1a | 2013-01-31 11:26:11 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 36 | NS_OBJECT_ENSURE_REGISTERED(ConsumerWindow); |
Alexander Afanasyev | 3a3ce1a | 2013-01-31 11:26:11 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 38 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 39 | ConsumerWindow::GetTypeId(void) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 40 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 41 | static TypeId tid = |
| 42 | TypeId("ns3::ndn::ConsumerWindow") |
| 43 | .SetGroupName("Ndn") |
| 44 | .SetParent<Consumer>() |
| 45 | .AddConstructor<ConsumerWindow>() |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 47 | .AddAttribute("Window", "Initial size of the window", StringValue("1"), |
| 48 | MakeUintegerAccessor(&ConsumerWindow::GetWindow, &ConsumerWindow::SetWindow), |
| 49 | MakeUintegerChecker<uint32_t>()) |
Alexander Afanasyev | e4c2ece | 2012-01-11 10:44:40 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 51 | .AddAttribute("PayloadSize", |
| 52 | "Average size of content object size (to calculate interest generation rate)", |
| 53 | UintegerValue(1040), MakeUintegerAccessor(&ConsumerWindow::GetPayloadSize, |
| 54 | &ConsumerWindow::SetPayloadSize), |
| 55 | MakeUintegerChecker<uint32_t>()) |
Alexander Afanasyev | ee4ce7e | 2013-05-07 11:30:21 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 57 | .AddAttribute("Size", "Amount of data in megabytes to request, relying on PayloadSize " |
| 58 | "parameter (alternative to MaxSeq attribute)", |
| 59 | DoubleValue(-1), // don't impose limit by default |
| 60 | MakeDoubleAccessor(&ConsumerWindow::GetMaxSize, &ConsumerWindow::SetMaxSize), |
| 61 | MakeDoubleChecker<double>()) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 63 | .AddAttribute("MaxSeq", "Maximum sequence number to request (alternative to Size attribute, " |
| 64 | "would activate only if Size is -1). " |
| 65 | "The parameter is activated only if Size negative (not set)", |
| 66 | IntegerValue(std::numeric_limits<uint32_t>::max()), |
| 67 | MakeUintegerAccessor(&ConsumerWindow::GetSeqMax, &ConsumerWindow::SetSeqMax), |
| 68 | MakeUintegerChecker<uint32_t>()) |
Alexander Afanasyev | ee4ce7e | 2013-05-07 11:30:21 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 70 | .AddAttribute("InitialWindowOnTimeout", "Set window to initial value when timeout occurs", |
| 71 | BooleanValue(true), |
| 72 | MakeBooleanAccessor(&ConsumerWindow::m_setInitialWindowOnTimeout), |
| 73 | MakeBooleanChecker()) |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 75 | .AddTraceSource("WindowTrace", |
| 76 | "Window that controls how many outstanding interests are allowed", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 77 | MakeTraceSourceAccessor(&ConsumerWindow::m_window), |
| 78 | "ns3::ndn::ConsumerWindow::WindowTraceCallback") |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | .AddTraceSource("InFlight", "Current number of outstanding interests", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 80 | MakeTraceSourceAccessor(&ConsumerWindow::m_inFlight), |
| 81 | "ns3::ndn::ConsumerWindow::WindowTraceCallback"); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 82 | |
| 83 | return tid; |
| 84 | } |
| 85 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 86 | ConsumerWindow::ConsumerWindow() |
| 87 | : m_payloadSize(1040) |
| 88 | , m_inFlight(0) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 89 | { |
| 90 | } |
| 91 | |
| 92 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 93 | ConsumerWindow::SetWindow(uint32_t window) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 94 | { |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 95 | m_initialWindow = window; |
| 96 | m_window = m_initialWindow; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 100 | ConsumerWindow::GetWindow() const |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 101 | { |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 102 | return m_initialWindow; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 105 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 106 | ConsumerWindow::GetPayloadSize() const |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 107 | { |
| 108 | return m_payloadSize; |
| 109 | } |
| 110 | |
| 111 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 112 | ConsumerWindow::SetPayloadSize(uint32_t payload) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 113 | { |
| 114 | m_payloadSize = payload; |
| 115 | } |
| 116 | |
| 117 | double |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 118 | ConsumerWindow::GetMaxSize() const |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 119 | { |
| 120 | if (m_seqMax == 0) |
| 121 | return -1.0; |
| 122 | |
| 123 | return m_maxSize; |
| 124 | } |
| 125 | |
| 126 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 127 | ConsumerWindow::SetMaxSize(double size) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 128 | { |
| 129 | m_maxSize = size; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 130 | if (m_maxSize < 0) { |
Xinyu Ma | 60562d4 | 2019-03-14 14:05:20 -0700 | [diff] [blame] | 131 | m_seqMax = std::numeric_limits<uint32_t>::max(); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 132 | return; |
| 133 | } |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 134 | |
| 135 | m_seqMax = floor(1.0 + m_maxSize * 1024.0 * 1024.0 / m_payloadSize); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 136 | NS_LOG_DEBUG("MaxSeqNo: " << m_seqMax); |
Alexander Afanasyev | 1180b04 | 2012-01-25 19:26:13 -0800 | [diff] [blame] | 137 | // std::cout << "MaxSeqNo: " << m_seqMax << "\n"; |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Alexander Afanasyev | fdd4541 | 2013-05-16 22:24:33 -0700 | [diff] [blame] | 140 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 141 | ConsumerWindow::GetSeqMax() const |
Alexander Afanasyev | fdd4541 | 2013-05-16 22:24:33 -0700 | [diff] [blame] | 142 | { |
| 143 | return m_seqMax; |
| 144 | } |
| 145 | |
| 146 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 147 | ConsumerWindow::SetSeqMax(uint32_t seqMax) |
Alexander Afanasyev | fdd4541 | 2013-05-16 22:24:33 -0700 | [diff] [blame] | 148 | { |
| 149 | if (m_maxSize < 0) |
| 150 | m_seqMax = seqMax; |
| 151 | |
| 152 | // ignore otherwise |
| 153 | } |
| 154 | |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 155 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 156 | ConsumerWindow::ScheduleNextPacket() |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 157 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 158 | if (m_window == static_cast<uint32_t>(0)) { |
| 159 | Simulator::Remove(m_sendEvent); |
Alexander Afanasyev | 91d7c7c | 2013-01-31 15:01:08 -0800 | [diff] [blame] | 160 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 161 | NS_LOG_DEBUG( |
| 162 | "Next event in " << (std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S))) |
| 163 | << " sec"); |
| 164 | m_sendEvent = |
| 165 | Simulator::Schedule(Seconds( |
| 166 | std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S))), |
| 167 | &Consumer::SendPacket, this); |
| 168 | } |
| 169 | else if (m_inFlight >= m_window) { |
| 170 | // simply do nothing |
| 171 | } |
| 172 | else { |
| 173 | if (m_sendEvent.IsRunning()) { |
| 174 | Simulator::Remove(m_sendEvent); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 175 | } |
Alexander Afanasyev | e1b2a2d | 2012-11-16 11:53:42 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 177 | m_sendEvent = Simulator::ScheduleNow(&Consumer::SendPacket, this); |
| 178 | } |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /////////////////////////////////////////////////// |
| 182 | // Process incoming packets // |
| 183 | /////////////////////////////////////////////////// |
| 184 | |
| 185 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 186 | ConsumerWindow::OnData(shared_ptr<const Data> contentObject) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 187 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 188 | Consumer::OnData(contentObject); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 189 | |
| 190 | m_window = m_window + 1; |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame] | 191 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 192 | if (m_inFlight > static_cast<uint32_t>(0)) |
| 193 | m_inFlight--; |
| 194 | NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight); |
Alexander Afanasyev | e1b2a2d | 2012-11-16 11:53:42 -0800 | [diff] [blame] | 195 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 196 | ScheduleNextPacket(); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 200 | ConsumerWindow::OnTimeout(uint32_t sequenceNumber) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 201 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 202 | if (m_inFlight > static_cast<uint32_t>(0)) |
| 203 | m_inFlight--; |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 204 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 205 | if (m_setInitialWindowOnTimeout) { |
| 206 | // m_window = std::max<uint32_t> (0, m_window - 1); |
| 207 | m_window = m_initialWindow; |
| 208 | } |
Alexander Afanasyev | 91d7c7c | 2013-01-31 15:01:08 -0800 | [diff] [blame] | 209 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 210 | NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight); |
| 211 | Consumer::OnTimeout(sequenceNumber); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Alexander Afanasyev | 79b2fb3 | 2013-04-12 11:24:55 -0700 | [diff] [blame] | 214 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 215 | ConsumerWindow::WillSendOutInterest(uint32_t sequenceNumber) |
Alexander Afanasyev | 79b2fb3 | 2013-04-12 11:24:55 -0700 | [diff] [blame] | 216 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 217 | m_inFlight++; |
| 218 | Consumer::WillSendOutInterest(sequenceNumber); |
Alexander Afanasyev | 79b2fb3 | 2013-04-12 11:24:55 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 221 | } // namespace ndn |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 222 | } // namespace ns3 |