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" |
| 29 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 30 | NS_LOG_COMPONENT_DEFINE("ndn.ConsumerWindow"); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 32 | namespace ns3 { |
| 33 | namespace ndn { |
Alexander Afanasyev | 3a3ce1a | 2013-01-31 11:26:11 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 35 | NS_OBJECT_ENSURE_REGISTERED(ConsumerWindow); |
Alexander Afanasyev | 3a3ce1a | 2013-01-31 11:26:11 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 37 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 38 | ConsumerWindow::GetTypeId(void) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 39 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 40 | static TypeId tid = |
| 41 | TypeId("ns3::ndn::ConsumerWindow") |
| 42 | .SetGroupName("Ndn") |
| 43 | .SetParent<Consumer>() |
| 44 | .AddConstructor<ConsumerWindow>() |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 46 | .AddAttribute("Window", "Initial size of the window", StringValue("1"), |
| 47 | MakeUintegerAccessor(&ConsumerWindow::GetWindow, &ConsumerWindow::SetWindow), |
| 48 | MakeUintegerChecker<uint32_t>()) |
Alexander Afanasyev | e4c2ece | 2012-01-11 10:44:40 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 50 | .AddAttribute("PayloadSize", |
| 51 | "Average size of content object size (to calculate interest generation rate)", |
| 52 | UintegerValue(1040), MakeUintegerAccessor(&ConsumerWindow::GetPayloadSize, |
| 53 | &ConsumerWindow::SetPayloadSize), |
| 54 | MakeUintegerChecker<uint32_t>()) |
Alexander Afanasyev | ee4ce7e | 2013-05-07 11:30:21 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 56 | .AddAttribute("Size", "Amount of data in megabytes to request, relying on PayloadSize " |
| 57 | "parameter (alternative to MaxSeq attribute)", |
| 58 | DoubleValue(-1), // don't impose limit by default |
| 59 | MakeDoubleAccessor(&ConsumerWindow::GetMaxSize, &ConsumerWindow::SetMaxSize), |
| 60 | MakeDoubleChecker<double>()) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 62 | .AddAttribute("MaxSeq", "Maximum sequence number to request (alternative to Size attribute, " |
| 63 | "would activate only if Size is -1). " |
| 64 | "The parameter is activated only if Size negative (not set)", |
| 65 | IntegerValue(std::numeric_limits<uint32_t>::max()), |
| 66 | MakeUintegerAccessor(&ConsumerWindow::GetSeqMax, &ConsumerWindow::SetSeqMax), |
| 67 | MakeUintegerChecker<uint32_t>()) |
Alexander Afanasyev | ee4ce7e | 2013-05-07 11:30:21 -0700 | [diff] [blame] | 68 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | .AddAttribute("InitialWindowOnTimeout", "Set window to initial value when timeout occurs", |
| 70 | BooleanValue(true), |
| 71 | MakeBooleanAccessor(&ConsumerWindow::m_setInitialWindowOnTimeout), |
| 72 | MakeBooleanChecker()) |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | .AddTraceSource("WindowTrace", |
| 75 | "Window that controls how many outstanding interests are allowed", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame^] | 76 | MakeTraceSourceAccessor(&ConsumerWindow::m_window), |
| 77 | "ns3::ndn::ConsumerWindow::WindowTraceCallback") |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 78 | .AddTraceSource("InFlight", "Current number of outstanding interests", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame^] | 79 | MakeTraceSourceAccessor(&ConsumerWindow::m_inFlight), |
| 80 | "ns3::ndn::ConsumerWindow::WindowTraceCallback"); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 81 | |
| 82 | return tid; |
| 83 | } |
| 84 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 85 | ConsumerWindow::ConsumerWindow() |
| 86 | : m_payloadSize(1040) |
| 87 | , m_inFlight(0) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 88 | { |
| 89 | } |
| 90 | |
| 91 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 92 | ConsumerWindow::SetWindow(uint32_t window) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 93 | { |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 94 | m_initialWindow = window; |
| 95 | m_window = m_initialWindow; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 99 | ConsumerWindow::GetWindow() const |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 100 | { |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 101 | return m_initialWindow; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 104 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 105 | ConsumerWindow::GetPayloadSize() const |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 106 | { |
| 107 | return m_payloadSize; |
| 108 | } |
| 109 | |
| 110 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 111 | ConsumerWindow::SetPayloadSize(uint32_t payload) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 112 | { |
| 113 | m_payloadSize = payload; |
| 114 | } |
| 115 | |
| 116 | double |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 117 | ConsumerWindow::GetMaxSize() const |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 118 | { |
| 119 | if (m_seqMax == 0) |
| 120 | return -1.0; |
| 121 | |
| 122 | return m_maxSize; |
| 123 | } |
| 124 | |
| 125 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 126 | ConsumerWindow::SetMaxSize(double size) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 127 | { |
| 128 | m_maxSize = size; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 129 | if (m_maxSize < 0) { |
| 130 | m_seqMax = 0; |
| 131 | return; |
| 132 | } |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 133 | |
| 134 | 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] | 135 | NS_LOG_DEBUG("MaxSeqNo: " << m_seqMax); |
Alexander Afanasyev | 1180b04 | 2012-01-25 19:26:13 -0800 | [diff] [blame] | 136 | // std::cout << "MaxSeqNo: " << m_seqMax << "\n"; |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Alexander Afanasyev | fdd4541 | 2013-05-16 22:24:33 -0700 | [diff] [blame] | 139 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | ConsumerWindow::GetSeqMax() const |
Alexander Afanasyev | fdd4541 | 2013-05-16 22:24:33 -0700 | [diff] [blame] | 141 | { |
| 142 | return m_seqMax; |
| 143 | } |
| 144 | |
| 145 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 146 | ConsumerWindow::SetSeqMax(uint32_t seqMax) |
Alexander Afanasyev | fdd4541 | 2013-05-16 22:24:33 -0700 | [diff] [blame] | 147 | { |
| 148 | if (m_maxSize < 0) |
| 149 | m_seqMax = seqMax; |
| 150 | |
| 151 | // ignore otherwise |
| 152 | } |
| 153 | |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 154 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 155 | ConsumerWindow::ScheduleNextPacket() |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 156 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 157 | if (m_window == static_cast<uint32_t>(0)) { |
| 158 | Simulator::Remove(m_sendEvent); |
Alexander Afanasyev | 91d7c7c | 2013-01-31 15:01:08 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 160 | NS_LOG_DEBUG( |
| 161 | "Next event in " << (std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S))) |
| 162 | << " sec"); |
| 163 | m_sendEvent = |
| 164 | Simulator::Schedule(Seconds( |
| 165 | std::min<double>(0.5, m_rtt->RetransmitTimeout().ToDouble(Time::S))), |
| 166 | &Consumer::SendPacket, this); |
| 167 | } |
| 168 | else if (m_inFlight >= m_window) { |
| 169 | // simply do nothing |
| 170 | } |
| 171 | else { |
| 172 | if (m_sendEvent.IsRunning()) { |
| 173 | Simulator::Remove(m_sendEvent); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 174 | } |
Alexander Afanasyev | e1b2a2d | 2012-11-16 11:53:42 -0800 | [diff] [blame] | 175 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 176 | m_sendEvent = Simulator::ScheduleNow(&Consumer::SendPacket, this); |
| 177 | } |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /////////////////////////////////////////////////// |
| 181 | // Process incoming packets // |
| 182 | /////////////////////////////////////////////////// |
| 183 | |
| 184 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 185 | ConsumerWindow::OnData(shared_ptr<const Data> contentObject) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 186 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 187 | Consumer::OnData(contentObject); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 188 | |
| 189 | m_window = m_window + 1; |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame] | 190 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 191 | if (m_inFlight > static_cast<uint32_t>(0)) |
| 192 | m_inFlight--; |
| 193 | NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight); |
Alexander Afanasyev | e1b2a2d | 2012-11-16 11:53:42 -0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 195 | ScheduleNextPacket(); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 199 | ConsumerWindow::OnTimeout(uint32_t sequenceNumber) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 200 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 201 | if (m_inFlight > static_cast<uint32_t>(0)) |
| 202 | m_inFlight--; |
Alexander Afanasyev | d1f7c7b | 2013-01-31 13:34:37 -0800 | [diff] [blame] | 203 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 204 | if (m_setInitialWindowOnTimeout) { |
| 205 | // m_window = std::max<uint32_t> (0, m_window - 1); |
| 206 | m_window = m_initialWindow; |
| 207 | } |
Alexander Afanasyev | 91d7c7c | 2013-01-31 15:01:08 -0800 | [diff] [blame] | 208 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 209 | NS_LOG_DEBUG("Window: " << m_window << ", InFlight: " << m_inFlight); |
| 210 | Consumer::OnTimeout(sequenceNumber); |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Alexander Afanasyev | 79b2fb3 | 2013-04-12 11:24:55 -0700 | [diff] [blame] | 213 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 214 | ConsumerWindow::WillSendOutInterest(uint32_t sequenceNumber) |
Alexander Afanasyev | 79b2fb3 | 2013-04-12 11:24:55 -0700 | [diff] [blame] | 215 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 216 | m_inFlight++; |
| 217 | Consumer::WillSendOutInterest(sequenceNumber); |
Alexander Afanasyev | 79b2fb3 | 2013-04-12 11:24:55 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 220 | } // namespace ndn |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 221 | } // namespace ns3 |