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