blob: 474c4fc5b0a5f3acf1ac23eca4a0ea711a2bec7e [file] [log] [blame]
Alexander Afanasyev359bfb72012-01-09 18:42:50 -08001/* -*- 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 Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-consumer-window.h"
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080022#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 Afanasyev2b4c9472012-08-09 15:00:38 -070031NS_LOG_COMPONENT_DEFINE ("ndn.ConsumerWindow");
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080032
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033namespace ns3 {
34namespace ndn {
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080035
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036NS_OBJECT_ENSURE_REGISTERED (ConsumerWindow);
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080037
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080038TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039ConsumerWindow::GetTypeId (void)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080040{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041 static TypeId tid = TypeId ("ns3::ndn::ConsumerWindow")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043 .SetParent<Consumer> ()
44 .AddConstructor<ConsumerWindow> ()
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080045
46 .AddAttribute ("Window", "Initial size of the window",
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080047 StringValue ("1"),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048 MakeUintegerAccessor (&ConsumerWindow::GetWindow, &ConsumerWindow::SetWindow),
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080049 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080050
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080051 .AddAttribute ("PayloadSize", "Average size of content object size (to calculate interest generation rate)",
52 UintegerValue (1040),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070053 MakeUintegerAccessor (&ConsumerWindow::GetPayloadSize, &ConsumerWindow::SetPayloadSize),
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080054 MakeUintegerChecker<uint32_t>())
Alexander Afanasyevee4ce7e2013-05-07 11:30:21 -070055
Alexander Afanasyevfdd45412013-05-16 22:24:33 -070056 .AddAttribute ("Size",
57 "Amount of data in megabytes to request, relying on PayloadSize parameter (alternative to MaxSeq attribute)",
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080058 DoubleValue (-1), // don't impose limit by default
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 MakeDoubleAccessor (&ConsumerWindow::GetMaxSize, &ConsumerWindow::SetMaxSize),
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080060 MakeDoubleChecker<double> ())
61
Alexander Afanasyevee4ce7e2013-05-07 11:30:21 -070062 .AddAttribute ("MaxSeq",
Alexander Afanasyevfdd45412013-05-16 22:24:33 -070063 "Maximum sequence number to request (alternative to Size attribute, would activate only if Size is -1). "
64 "The parameter is activated only if Size negative (not set)",
Alexander Afanasyevee4ce7e2013-05-07 11:30:21 -070065 IntegerValue (std::numeric_limits<uint32_t>::max ()),
Alexander Afanasyevfdd45412013-05-16 22:24:33 -070066 MakeUintegerAccessor (&ConsumerWindow::GetSeqMax, &ConsumerWindow::SetSeqMax),
67 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyevee4ce7e2013-05-07 11:30:21 -070068
Alexander Afanasyevd1f7c7b2013-01-31 13:34:37 -080069 .AddAttribute ("InitialWindowOnTimeout", "Set window to initial value when timeout occurs",
70 BooleanValue (true),
71 MakeBooleanAccessor (&ConsumerWindow::m_setInitialWindowOnTimeout),
72 MakeBooleanChecker ())
73
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080074 .AddTraceSource ("WindowTrace",
75 "Window that controls how many outstanding interests are allowed",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070076 MakeTraceSourceAccessor (&ConsumerWindow::m_window))
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080077 .AddTraceSource ("InFlight",
78 "Current number of outstanding interests",
Alexander Afanasyevce8684f2013-05-03 11:12:14 -070079 MakeTraceSourceAccessor (&ConsumerWindow::m_inFlight))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080080 ;
81
82 return tid;
83}
84
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085ConsumerWindow::ConsumerWindow ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080086 : m_payloadSize (1040)
87 , m_inFlight (0)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080088{
89}
90
91void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070092ConsumerWindow::SetWindow (uint32_t window)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080093{
Alexander Afanasyevd1f7c7b2013-01-31 13:34:37 -080094 m_initialWindow = window;
95 m_window = m_initialWindow;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080096}
97
98uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070099ConsumerWindow::GetWindow () const
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800100{
Alexander Afanasyevd1f7c7b2013-01-31 13:34:37 -0800101 return m_initialWindow;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800102}
103
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800104uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700105ConsumerWindow::GetPayloadSize () const
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800106{
107 return m_payloadSize;
108}
109
110void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700111ConsumerWindow::SetPayloadSize (uint32_t payload)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800112{
113 m_payloadSize = payload;
114}
115
116double
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700117ConsumerWindow::GetMaxSize () const
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800118{
119 if (m_seqMax == 0)
120 return -1.0;
121
122 return m_maxSize;
123}
124
125void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700126ConsumerWindow::SetMaxSize (double size)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800127{
128 m_maxSize = size;
129 if (m_maxSize < 0)
130 {
131 m_seqMax = 0;
132 return;
133 }
134
135 m_seqMax = floor(1.0 + m_maxSize * 1024.0 * 1024.0 / m_payloadSize);
136 NS_LOG_DEBUG ("MaxSeqNo: " << m_seqMax);
Alexander Afanasyev1180b042012-01-25 19:26:13 -0800137 // std::cout << "MaxSeqNo: " << m_seqMax << "\n";
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800138}
139
Alexander Afanasyevfdd45412013-05-16 22:24:33 -0700140uint32_t
141ConsumerWindow::GetSeqMax () const
142{
143 return m_seqMax;
144}
145
146void
147ConsumerWindow::SetSeqMax (uint32_t seqMax)
148{
149 if (m_maxSize < 0)
150 m_seqMax = seqMax;
151
152 // ignore otherwise
153}
154
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800155
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800156void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157ConsumerWindow::ScheduleNextPacket ()
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800158{
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800159 if (m_window == static_cast<uint32_t> (0))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800160 {
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800161 Simulator::Remove (m_sendEvent);
162
163 NS_LOG_DEBUG ("Next event in " << (std::min<double> (0.5, m_rtt->RetransmitTimeout ().ToDouble (Time::S))) << " sec");
164 m_sendEvent = Simulator::Schedule (Seconds (std::min<double> (0.5, m_rtt->RetransmitTimeout ().ToDouble (Time::S))),
165 &Consumer::SendPacket, this);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800166 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800167 else if (m_inFlight >= m_window)
168 {
169 // simply do nothing
170 }
Alexander Afanasyeve1b2a2d2012-11-16 11:53:42 -0800171 else
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800172 {
Alexander Afanasyeve1b2a2d2012-11-16 11:53:42 -0800173 if (m_sendEvent.IsRunning ())
174 {
175 Simulator::Remove (m_sendEvent);
176 }
177
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700178 m_sendEvent = Simulator::ScheduleNow (&Consumer::SendPacket, this);
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800179 }
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800180}
181
182///////////////////////////////////////////////////
183// Process incoming packets //
184///////////////////////////////////////////////////
185
186void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700187ConsumerWindow::OnContentObject (const Ptr<const ContentObject> &contentObject,
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800188 Ptr<Packet> payload)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800189{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700190 Consumer::OnContentObject (contentObject, payload);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800191
192 m_window = m_window + 1;
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800193
194 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyeve1b2a2d2012-11-16 11:53:42 -0800195 NS_LOG_DEBUG ("Window: " << m_window << ", InFlight: " << m_inFlight);
196
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800197 ScheduleNextPacket ();
198}
199
200void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700201ConsumerWindow::OnNack (const Ptr<const Interest> &interest, Ptr<Packet> payload)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800202{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700203 Consumer::OnNack (interest, payload);
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800204
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800205 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800206
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -0800207 if (m_window > static_cast<uint32_t> (0))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800208 {
209 // m_window = 0.5 * m_window;//m_window - 1;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800210 m_window = std::max<uint32_t> (0, m_window - 1);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800211 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800212
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800213 NS_LOG_DEBUG ("Window: " << m_window << ", InFlight: " << m_inFlight);
214
215 ScheduleNextPacket ();
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800216}
217
218void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700219ConsumerWindow::OnTimeout (uint32_t sequenceNumber)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800220{
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800221 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyevd1f7c7b2013-01-31 13:34:37 -0800222
223 if (m_setInitialWindowOnTimeout)
224 {
225 // m_window = std::max<uint32_t> (0, m_window - 1);
226 m_window = m_initialWindow;
227 }
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800228
229 NS_LOG_DEBUG ("Window: " << m_window << ", InFlight: " << m_inFlight);
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230 Consumer::OnTimeout (sequenceNumber);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800231}
232
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -0700233void
234ConsumerWindow::WillSendOutInterest (uint32_t sequenceNumber)
235{
236 m_inFlight ++;
237 Consumer::WillSendOutInterest (sequenceNumber);
238}
239
240
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700241} // namespace ndn
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800242} // namespace ns3