blob: d244061acd7e1e11c539b9ab6a6313ccf53afba2 [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",
46 StringValue ("1000"),
47 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 Afanasyev359bfb72012-01-09 18:42:50 -080053 ;
54
55 return tid;
56}
57
58CcnxConsumerWindow::CcnxConsumerWindow ()
59 : m_inFlight (0)
60{
61}
62
63void
64CcnxConsumerWindow::SetWindow (uint32_t window)
65{
66 m_window = window;
67}
68
69uint32_t
70CcnxConsumerWindow::GetWindow () const
71{
72 return m_window;
73}
74
75void
76CcnxConsumerWindow::ScheduleNextPacket ()
77{
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -080078 if (m_window == static_cast<uint32_t> (0) || m_inFlight >= m_window)
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080079 {
80 if (!m_sendEvent.IsRunning ())
81 m_sendEvent = Simulator::Schedule (Seconds (m_rtt->RetransmitTimeout ().ToDouble (Time::S) * 0.1), &CcnxConsumer::SendPacket, this);
82 return;
83 }
84
85 // std::cout << "Window: " << m_window << ", InFlight: " << m_inFlight << "\n";
86 m_inFlight++;
87 if (!m_sendEvent.IsRunning ())
88 m_sendEvent = Simulator::ScheduleNow (&CcnxConsumer::SendPacket, this);
89}
90
91///////////////////////////////////////////////////
92// Process incoming packets //
93///////////////////////////////////////////////////
94
95void
96CcnxConsumerWindow::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
97 const Ptr<const Packet> &payload)
98{
99 CcnxConsumer::OnContentObject (contentObject, payload);
100
101 m_window = m_window + 1;
102
103 if (m_inFlight > 0) m_inFlight--;
104 ScheduleNextPacket ();
105}
106
107void
108CcnxConsumerWindow::OnNack (const Ptr<const CcnxInterestHeader> &interest)
109{
110 CcnxConsumer::OnNack (interest);
111 if (m_inFlight > 0) m_inFlight--;
112
Alexander Afanasyeve4c2ece2012-01-11 10:44:40 -0800113 if (m_window > static_cast<uint32_t> (0))
Alexander Afanasyev359bfb72012-01-09 18:42:50 -0800114 {
115 // m_window = 0.5 * m_window;//m_window - 1;
116 m_window = m_window - 1;
117 }
118}
119
120void
121CcnxConsumerWindow::OnTimeout (uint32_t sequenceNumber)
122{
123 if (m_inFlight > 0) m_inFlight--;
124 CcnxConsumer::OnTimeout (sequenceNumber);
125}
126
127} // namespace ns3