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