Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [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 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 21 | #include "ndn-consumer-window.hpp" |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 22 | #include "ns3/ptr.h" |
| 23 | #include "ns3/log.h" |
| 24 | #include "ns3/simulator.h" |
| 25 | #include "ns3/packet.h" |
| 26 | #include "ns3/callback.h" |
| 27 | #include "ns3/string.h" |
| 28 | #include "ns3/uinteger.h" |
| 29 | #include "ns3/double.h" |
| 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", |
| 77 | MakeTraceSourceAccessor(&ConsumerWindow::m_window)) |
| 78 | .AddTraceSource("InFlight", "Current number of outstanding interests", |
| 79 | MakeTraceSourceAccessor(&ConsumerWindow::m_inFlight)); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 80 | |
| 81 | return tid; |
| 82 | } |
| 83 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 84 | ConsumerWindow::ConsumerWindow() |
| 85 | : m_payloadSize(1040) |
| 86 | , m_inFlight(0) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 87 | { |
| 88 | } |
| 89 | |
| 90 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 91 | ConsumerWindow::SetWindow(uint32_t window) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 92 | { |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 93 | m_initialWindow = window; |
| 94 | m_window = m_initialWindow; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 98 | ConsumerWindow::GetWindow() const |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 99 | { |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 100 | return m_initialWindow; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 103 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 104 | ConsumerWindow::GetPayloadSize() const |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 105 | { |
| 106 | return m_payloadSize; |
| 107 | } |
| 108 | |
| 109 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 110 | ConsumerWindow::SetPayloadSize(uint32_t payload) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 111 | { |
| 112 | m_payloadSize = payload; |
| 113 | } |
| 114 | |
| 115 | double |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 116 | ConsumerWindow::GetMaxSize() const |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 117 | { |
| 118 | if (m_seqMax == 0) |
| 119 | return -1.0; |
| 120 | |
| 121 | return m_maxSize; |
| 122 | } |
| 123 | |
| 124 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 125 | ConsumerWindow::SetMaxSize(double size) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 126 | { |
| 127 | m_maxSize = size; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 128 | if (m_maxSize < 0) { |
| 129 | m_seqMax = 0; |
| 130 | return; |
| 131 | } |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 132 | |
| 133 | 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] | 134 | NS_LOG_DEBUG("MaxSeqNo: " << m_seqMax); |
Alexander Afanasyev | 1180b04 | 2012-01-25 19:26:13 -0800 | [diff] [blame] | 135 | // std::cout << "MaxSeqNo: " << m_seqMax << "\n"; |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Alexander Afanasyev | fdd4541 | 2013-05-16 22:24:33 -0700 | [diff] [blame] | 138 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 139 | ConsumerWindow::GetSeqMax() const |
Alexander Afanasyev | fdd4541 | 2013-05-16 22:24:33 -0700 | [diff] [blame] | 140 | { |
| 141 | return m_seqMax; |
| 142 | } |
| 143 | |
| 144 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 145 | ConsumerWindow::SetSeqMax(uint32_t seqMax) |
Alexander Afanasyev | fdd4541 | 2013-05-16 22:24:33 -0700 | [diff] [blame] | 146 | { |
| 147 | if (m_maxSize < 0) |
| 148 | m_seqMax = seqMax; |
| 149 | |
| 150 | // ignore otherwise |
| 151 | } |
| 152 | |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 153 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 154 | ConsumerWindow::ScheduleNextPacket() |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 155 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 156 | if (m_window == static_cast<uint32_t>(0)) { |
| 157 | Simulator::Remove(m_sendEvent); |
Alexander Afanasyev | 91d7c7c | 2013-01-31 15:01:08 -0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 159 | NS_LOG_DEBUG( |
| 160 | "Next event in " << (std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S))) |
| 161 | << " sec"); |
| 162 | m_sendEvent = |
| 163 | Simulator::Schedule(Seconds( |
| 164 | std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S))), |
| 165 | &Consumer::SendPacket, this); |
| 166 | } |
| 167 | else if (m_inFlight >= m_window) { |
| 168 | // simply do nothing |
| 169 | } |
| 170 | else { |
| 171 | if (m_sendEvent.IsRunning()) { |
| 172 | Simulator::Remove(m_sendEvent); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 173 | } |
Alexander Afanasyev | e1b2a2d | 2012-11-16 11:53:42 -0800 | [diff] [blame] | 174 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 175 | m_sendEvent = Simulator::ScheduleNow(&Consumer::SendPacket, this); |
| 176 | } |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /////////////////////////////////////////////////// |
| 180 | // Process incoming packets // |
| 181 | /////////////////////////////////////////////////// |
| 182 | |
| 183 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 184 | ConsumerWindow::OnData(shared_ptr<const Data> contentObject) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 185 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 186 | Consumer::OnData(contentObject); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 187 | |
| 188 | m_window = m_window + 1; |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame] | 189 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 190 | if (m_inFlight > static_cast<uint32_t>(0)) |
| 191 | m_inFlight--; |
| 192 | NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight); |
Alexander Afanasyev | e1b2a2d | 2012-11-16 11:53:42 -0800 | [diff] [blame] | 193 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 194 | ScheduleNextPacket(); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 198 | ConsumerWindow::OnNack(shared_ptr<const Interest> interest) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 199 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 200 | Consumer::OnNack(interest); |
Alexander Afanasyev | 3a3ce1a | 2013-01-31 11:26:11 -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 | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 204 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 205 | if (m_window > static_cast<uint32_t>(0)) { |
| 206 | // m_window = 0.5 * m_window;//m_window - 1; |
| 207 | m_window = std::max<uint32_t>(0, m_window - 1); |
| 208 | } |
Alexander Afanasyev | 3a3ce1a | 2013-01-31 11:26:11 -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); |
Alexander Afanasyev | 91d7c7c | 2013-01-31 15:01:08 -0800 | [diff] [blame] | 211 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 212 | ScheduleNextPacket(); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 216 | ConsumerWindow::OnTimeout(uint32_t sequenceNumber) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 217 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 218 | if (m_inFlight > static_cast<uint32_t>(0)) |
| 219 | m_inFlight--; |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 220 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 221 | if (m_setInitialWindowOnTimeout) { |
| 222 | // m_window = std::max<uint32_t> (0, m_window - 1); |
| 223 | m_window = m_initialWindow; |
| 224 | } |
Alexander Afanasyev | 91d7c7c | 2013-01-31 15:01:08 -0800 | [diff] [blame] | 225 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 226 | NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight); |
| 227 | Consumer::OnTimeout(sequenceNumber); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Alexander Afanasyev | 79b2fb3 | 2013-04-12 11:24:55 -0700 | [diff] [blame] | 230 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 231 | ConsumerWindow::WillSendOutInterest(uint32_t sequenceNumber) |
Alexander Afanasyev | 79b2fb3 | 2013-04-12 11:24:55 -0700 | [diff] [blame] | 232 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 233 | m_inFlight++; |
| 234 | Consumer::WillSendOutInterest(sequenceNumber); |
Alexander Afanasyev | 79b2fb3 | 2013-04-12 11:24:55 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 237 | } // namespace ndn |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 238 | } // namespace ns3 |