blob: f7bbec261f0c9cbc7694ddedffcd034554b87138 [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
56 .AddAttribute ("Size", "Amount of data in megabytes to request, relying on PayloadSize parameter (alternative to MaxSeq attribute)",
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080057 DoubleValue (-1), // don't impose limit by default
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058 MakeDoubleAccessor (&ConsumerWindow::GetMaxSize, &ConsumerWindow::SetMaxSize),
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080059 MakeDoubleChecker<double> ())
60
Alexander Afanasyevee4ce7e2013-05-07 11:30:21 -070061 .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 Afanasyevd1f7c7b2013-01-31 13:34:37 -080067 .AddAttribute ("InitialWindowOnTimeout", "Set window to initial value when timeout occurs",
68 BooleanValue (true),
69 MakeBooleanAccessor (&ConsumerWindow::m_setInitialWindowOnTimeout),
70 MakeBooleanChecker ())
71
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080072 .AddTraceSource ("WindowTrace",
73 "Window that controls how many outstanding interests are allowed",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074 MakeTraceSourceAccessor (&ConsumerWindow::m_window))
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080075 .AddTraceSource ("InFlight",
76 "Current number of outstanding interests",
Alexander Afanasyevce8684f2013-05-03 11:12:14 -070077 MakeTraceSourceAccessor (&ConsumerWindow::m_inFlight))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080078 ;
79
80 return tid;
81}
82
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070083ConsumerWindow::ConsumerWindow ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080084 : m_payloadSize (1040)
85 , m_inFlight (0)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080086{
87}
88
89void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090ConsumerWindow::SetWindow (uint32_t window)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080091{
Alexander Afanasyevd1f7c7b2013-01-31 13:34:37 -080092 m_initialWindow = window;
93 m_window = m_initialWindow;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080094}
95
96uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070097ConsumerWindow::GetWindow () const
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080098{
Alexander Afanasyevd1f7c7b2013-01-31 13:34:37 -080099 return m_initialWindow;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800100}
101
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800102uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103ConsumerWindow::GetPayloadSize () const
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800104{
105 return m_payloadSize;
106}
107
108void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700109ConsumerWindow::SetPayloadSize (uint32_t payload)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800110{
111 m_payloadSize = payload;
112}
113
114double
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700115ConsumerWindow::GetMaxSize () const
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800116{
117 if (m_seqMax == 0)
118 return -1.0;
119
120 return m_maxSize;
121}
122
123void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700124ConsumerWindow::SetMaxSize (double size)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800125{
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 Afanasyev1180b042012-01-25 19:26:13 -0800135 // std::cout << "MaxSeqNo: " << m_seqMax << "\n";
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800136}
137
138
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800139void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700140ConsumerWindow::ScheduleNextPacket ()
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800141{
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800142 if (m_window == static_cast<uint32_t> (0))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800143 {
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800144 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 Afanasyev359bfb72012-01-09 18:42:50 -0800149 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800150 else if (m_inFlight >= m_window)
151 {
152 // simply do nothing
153 }
Alexander Afanasyeve1b2a2d2012-11-16 11:53:42 -0800154 else
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800155 {
Alexander Afanasyeve1b2a2d2012-11-16 11:53:42 -0800156 if (m_sendEvent.IsRunning ())
157 {
158 Simulator::Remove (m_sendEvent);
159 }
160
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700161 m_sendEvent = Simulator::ScheduleNow (&Consumer::SendPacket, this);
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800162 }
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800163}
164
165///////////////////////////////////////////////////
166// Process incoming packets //
167///////////////////////////////////////////////////
168
169void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700170ConsumerWindow::OnContentObject (const Ptr<const ContentObject> &contentObject,
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800171 Ptr<Packet> payload)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800172{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700173 Consumer::OnContentObject (contentObject, payload);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800174
175 m_window = m_window + 1;
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800176
177 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyeve1b2a2d2012-11-16 11:53:42 -0800178 NS_LOG_DEBUG ("Window: " << m_window << ", InFlight: " << m_inFlight);
179
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800180 ScheduleNextPacket ();
181}
182
183void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700184ConsumerWindow::OnNack (const Ptr<const Interest> &interest, Ptr<Packet> payload)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800185{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700186 Consumer::OnNack (interest, payload);
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800187
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800188 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800189
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -0800190 if (m_window > static_cast<uint32_t> (0))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800191 {
192 // m_window = 0.5 * m_window;//m_window - 1;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800193 m_window = std::max<uint32_t> (0, m_window - 1);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800194 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800195
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800196 NS_LOG_DEBUG ("Window: " << m_window << ", InFlight: " << m_inFlight);
197
198 ScheduleNextPacket ();
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800199}
200
201void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700202ConsumerWindow::OnTimeout (uint32_t sequenceNumber)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800203{
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800204 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyevd1f7c7b2013-01-31 13:34:37 -0800205
206 if (m_setInitialWindowOnTimeout)
207 {
208 // m_window = std::max<uint32_t> (0, m_window - 1);
209 m_window = m_initialWindow;
210 }
Alexander Afanasyev91d7c7c2013-01-31 15:01:08 -0800211
212 NS_LOG_DEBUG ("Window: " << m_window << ", InFlight: " << m_inFlight);
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213 Consumer::OnTimeout (sequenceNumber);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800214}
215
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -0700216void
217ConsumerWindow::WillSendOutInterest (uint32_t sequenceNumber)
218{
219 m_inFlight ++;
220 Consumer::WillSendOutInterest (sequenceNumber);
221}
222
223
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700224} // namespace ndn
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800225} // namespace ns3