blob: 77f258cb74dbd495c2d6c6bf9f60799ff57420ec [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
21#include "ccnx-consumer-window.h"
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
31NS_LOG_COMPONENT_DEFINE ("CcnxConsumerWindow");
32
33namespace ns3
34{
35
36NS_OBJECT_ENSURE_REGISTERED (CcnxConsumerWindow);
37
38TypeId
39CcnxConsumerWindow::GetTypeId (void)
40{
41 static TypeId tid = TypeId ("ns3::CcnxConsumerWindow")
42 .SetParent<CcnxConsumer> ()
43 .AddConstructor<CcnxConsumerWindow> ()
44
45 .AddAttribute ("Window", "Initial size of the window",
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080046 StringValue ("1"),
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080047 MakeUintegerAccessor (&CcnxConsumerWindow::GetWindow, &CcnxConsumerWindow::SetWindow),
48 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080049
50 .AddTraceSource ("WindowTrace",
51 "Window that controls how many outstanding interests are allowed",
52 MakeTraceSourceAccessor (&CcnxConsumerWindow::m_window))
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080053 .AddTraceSource ("InFlight",
54 "Current number of outstanding interests",
55 MakeTraceSourceAccessor (&CcnxConsumerWindow::m_window))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080056 ;
57
58 return tid;
59}
60
61CcnxConsumerWindow::CcnxConsumerWindow ()
62 : m_inFlight (0)
63{
64}
65
66void
67CcnxConsumerWindow::SetWindow (uint32_t window)
68{
69 m_window = window;
70}
71
72uint32_t
73CcnxConsumerWindow::GetWindow () const
74{
75 return m_window;
76}
77
78void
79CcnxConsumerWindow::ScheduleNextPacket ()
80{
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080081 if (m_window == static_cast<uint32_t> (0) || m_inFlight >= m_window)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080082 {
83 if (!m_sendEvent.IsRunning ())
84 m_sendEvent = Simulator::Schedule (Seconds (m_rtt->RetransmitTimeout ().ToDouble (Time::S) * 0.1), &CcnxConsumer::SendPacket, this);
85 return;
86 }
87
88 // std::cout << "Window: " << m_window << ", InFlight: " << m_inFlight << "\n";
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080089 if (!m_sendEvent.IsRunning ())
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -080090 {
91 m_inFlight++;
92 m_sendEvent = Simulator::ScheduleNow (&CcnxConsumer::SendPacket, this);
93 }
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080094}
95
96///////////////////////////////////////////////////
97// Process incoming packets //
98///////////////////////////////////////////////////
99
100void
101CcnxConsumerWindow::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
102 const Ptr<const Packet> &payload)
103{
104 CcnxConsumer::OnContentObject (contentObject, payload);
105
106 m_window = m_window + 1;
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800107
108 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800109 ScheduleNextPacket ();
110}
111
112void
113CcnxConsumerWindow::OnNack (const Ptr<const CcnxInterestHeader> &interest)
114{
115 CcnxConsumer::OnNack (interest);
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800116 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800117
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -0800118 if (m_window > static_cast<uint32_t> (0))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800119 {
120 // m_window = 0.5 * m_window;//m_window - 1;
121 m_window = m_window - 1;
122 }
123}
124
125void
126CcnxConsumerWindow::OnTimeout (uint32_t sequenceNumber)
127{
Alexander Afanasyev06b42ec2012-01-11 19:05:36 -0800128 if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--;
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800129 CcnxConsumer::OnTimeout (sequenceNumber);
130}
131
132} // namespace ns3