Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 31 | NS_LOG_COMPONENT_DEFINE ("CcnxConsumerWindow"); |
| 32 | |
| 33 | namespace ns3 |
| 34 | { |
| 35 | |
| 36 | NS_OBJECT_ENSURE_REGISTERED (CcnxConsumerWindow); |
| 37 | |
| 38 | TypeId |
| 39 | CcnxConsumerWindow::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 Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame^] | 46 | StringValue ("1"), |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 47 | MakeUintegerAccessor (&CcnxConsumerWindow::GetWindow, &CcnxConsumerWindow::SetWindow), |
| 48 | MakeUintegerChecker<uint32_t> ()) |
Alexander Afanasyev | e4c2ece | 2012-01-11 10:44:40 -0800 | [diff] [blame] | 49 | |
| 50 | .AddTraceSource ("WindowTrace", |
| 51 | "Window that controls how many outstanding interests are allowed", |
| 52 | MakeTraceSourceAccessor (&CcnxConsumerWindow::m_window)) |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame^] | 53 | .AddTraceSource ("InFlight", |
| 54 | "Current number of outstanding interests", |
| 55 | MakeTraceSourceAccessor (&CcnxConsumerWindow::m_window)) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 56 | ; |
| 57 | |
| 58 | return tid; |
| 59 | } |
| 60 | |
| 61 | CcnxConsumerWindow::CcnxConsumerWindow () |
| 62 | : m_inFlight (0) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | CcnxConsumerWindow::SetWindow (uint32_t window) |
| 68 | { |
| 69 | m_window = window; |
| 70 | } |
| 71 | |
| 72 | uint32_t |
| 73 | CcnxConsumerWindow::GetWindow () const |
| 74 | { |
| 75 | return m_window; |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | CcnxConsumerWindow::ScheduleNextPacket () |
| 80 | { |
Alexander Afanasyev | e4c2ece | 2012-01-11 10:44:40 -0800 | [diff] [blame] | 81 | if (m_window == static_cast<uint32_t> (0) || m_inFlight >= m_window) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 82 | { |
| 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 Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 89 | if (!m_sendEvent.IsRunning ()) |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame^] | 90 | { |
| 91 | m_inFlight++; |
| 92 | m_sendEvent = Simulator::ScheduleNow (&CcnxConsumer::SendPacket, this); |
| 93 | } |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /////////////////////////////////////////////////// |
| 97 | // Process incoming packets // |
| 98 | /////////////////////////////////////////////////// |
| 99 | |
| 100 | void |
| 101 | CcnxConsumerWindow::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 Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame^] | 107 | |
| 108 | if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 109 | ScheduleNextPacket (); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | CcnxConsumerWindow::OnNack (const Ptr<const CcnxInterestHeader> &interest) |
| 114 | { |
| 115 | CcnxConsumer::OnNack (interest); |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame^] | 116 | if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | e4c2ece | 2012-01-11 10:44:40 -0800 | [diff] [blame] | 118 | if (m_window > static_cast<uint32_t> (0)) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 119 | { |
| 120 | // m_window = 0.5 * m_window;//m_window - 1; |
| 121 | m_window = m_window - 1; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | CcnxConsumerWindow::OnTimeout (uint32_t sequenceNumber) |
| 127 | { |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame^] | 128 | if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 129 | CcnxConsumer::OnTimeout (sequenceNumber); |
| 130 | } |
| 131 | |
| 132 | } // namespace ns3 |