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