blob: 86457c8f99c889919149f0e2559d3a89a4f6ab60 [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 Afanasyev359bfb72012-01-09 18:42:50 -080035
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036NS_OBJECT_ENSURE_REGISTERED (ConsumerWindow);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080037
38TypeId
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>())
55 .AddAttribute ("Size", "Amount of data in megabytes to request (relies on PayloadSize parameter)",
56 DoubleValue (-1), // don't impose limit by default
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057 MakeDoubleAccessor (&ConsumerWindow::GetMaxSize, &ConsumerWindow::SetMaxSize),
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080058 MakeDoubleChecker<double> ())
59
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080060 .AddTraceSource ("WindowTrace",
61 "Window that controls how many outstanding interests are allowed",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070062 MakeTraceSourceAccessor (&ConsumerWindow::m_window))
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080063 .AddTraceSource ("InFlight",
64 "Current number of outstanding interests",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070065 MakeTraceSourceAccessor (&ConsumerWindow::m_window))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080066 ;
67
68 return tid;
69}
70
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070071ConsumerWindow::ConsumerWindow ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080072 : m_payloadSize (1040)
73 , m_inFlight (0)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080074{
75}
76
77void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070078ConsumerWindow::SetWindow (uint32_t window)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080079{
80 m_window = window;
81}
82
83uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070084ConsumerWindow::GetWindow () const
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080085{
86 return m_window;
87}
88
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080089uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090ConsumerWindow::GetPayloadSize () const
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080091{
92 return m_payloadSize;
93}
94
95void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096ConsumerWindow::SetPayloadSize (uint32_t payload)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080097{
98 m_payloadSize = payload;
99}
100
101double
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102ConsumerWindow::GetMaxSize () const
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800103{
104 if (m_seqMax == 0)
105 return -1.0;
106
107 return m_maxSize;
108}
109
110void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700111ConsumerWindow::SetMaxSize (double size)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800112{
113 m_maxSize = size;
114 if (m_maxSize < 0)
115 {
116 m_seqMax = 0;
117 return;
118 }
119
120 m_seqMax = floor(1.0 + m_maxSize * 1024.0 * 1024.0 / m_payloadSize);
121 NS_LOG_DEBUG ("MaxSeqNo: " << m_seqMax);
Alexander Afanasyev1180b042012-01-25 19:26:13 -0800122 // std::cout << "MaxSeqNo: " << m_seqMax << "\n";
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800123}
124
125
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800126void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127ConsumerWindow::ScheduleNextPacket ()
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800128{
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -0800129 if (m_window == static_cast<uint32_t> (0) || m_inFlight >= m_window)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800130 {
131 if (!m_sendEvent.IsRunning ())
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700132 m_sendEvent = Simulator::Schedule (Seconds (m_rtt->RetransmitTimeout ().ToDouble (Time::S) * 0.1), &Consumer::SendPacket, this);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800133 return;
134 }
135
136 // std::cout << "Window: " << m_window << ", InFlight: " << m_inFlight << "\n";
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800137 if (!m_sendEvent.IsRunning ())
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800138 {
139 m_inFlight++;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700140 m_sendEvent = Simulator::ScheduleNow (&Consumer::SendPacket, this);
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800141 }
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800142}
143
144///////////////////////////////////////////////////
145// Process incoming packets //
146///////////////////////////////////////////////////
147
148void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149ConsumerWindow::OnContentObject (const Ptr<const ContentObjectHeader> &contentObject,
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800150 Ptr<Packet> payload)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800151{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152 Consumer::OnContentObject (contentObject, payload);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800153
154 m_window = m_window + 1;
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800155
156 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800157 ScheduleNextPacket ();
158}
159
160void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700161ConsumerWindow::OnNack (const Ptr<const InterestHeader> &interest, Ptr<Packet> payload)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800162{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700163 Consumer::OnNack (interest, payload);
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800164 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800165
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -0800166 if (m_window > static_cast<uint32_t> (0))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800167 {
168 // m_window = 0.5 * m_window;//m_window - 1;
169 m_window = m_window - 1;
170 }
171}
172
173void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700174ConsumerWindow::OnTimeout (uint32_t sequenceNumber)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800175{
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800176 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700177 Consumer::OnTimeout (sequenceNumber);
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800178}
179
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180} // namespace ndn
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800181} // namespace ns3