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 | |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 50 | .AddAttribute ("PayloadSize", "Average size of content object size (to calculate interest generation rate)", |
| 51 | UintegerValue (1040), |
| 52 | MakeUintegerAccessor (&CcnxConsumerWindow::GetPayloadSize, &CcnxConsumerWindow::SetPayloadSize), |
| 53 | MakeUintegerChecker<uint32_t>()) |
| 54 | .AddAttribute ("Size", "Amount of data in megabytes to request (relies on PayloadSize parameter)", |
| 55 | DoubleValue (-1), // don't impose limit by default |
| 56 | MakeDoubleAccessor (&CcnxConsumerWindow::GetMaxSize, &CcnxConsumerWindow::SetMaxSize), |
| 57 | MakeDoubleChecker<double> ()) |
| 58 | |
Alexander Afanasyev | e4c2ece | 2012-01-11 10:44:40 -0800 | [diff] [blame] | 59 | .AddTraceSource ("WindowTrace", |
| 60 | "Window that controls how many outstanding interests are allowed", |
| 61 | MakeTraceSourceAccessor (&CcnxConsumerWindow::m_window)) |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame] | 62 | .AddTraceSource ("InFlight", |
| 63 | "Current number of outstanding interests", |
| 64 | MakeTraceSourceAccessor (&CcnxConsumerWindow::m_window)) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 65 | ; |
| 66 | |
| 67 | return tid; |
| 68 | } |
| 69 | |
| 70 | CcnxConsumerWindow::CcnxConsumerWindow () |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 71 | : m_payloadSize (1040) |
| 72 | , m_inFlight (0) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 73 | { |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | CcnxConsumerWindow::SetWindow (uint32_t window) |
| 78 | { |
| 79 | m_window = window; |
| 80 | } |
| 81 | |
| 82 | uint32_t |
| 83 | CcnxConsumerWindow::GetWindow () const |
| 84 | { |
| 85 | return m_window; |
| 86 | } |
| 87 | |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 88 | uint32_t |
| 89 | CcnxConsumerWindow::GetPayloadSize () const |
| 90 | { |
| 91 | return m_payloadSize; |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | CcnxConsumerWindow::SetPayloadSize (uint32_t payload) |
| 96 | { |
| 97 | m_payloadSize = payload; |
| 98 | } |
| 99 | |
| 100 | double |
| 101 | CcnxConsumerWindow::GetMaxSize () const |
| 102 | { |
| 103 | if (m_seqMax == 0) |
| 104 | return -1.0; |
| 105 | |
| 106 | return m_maxSize; |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | CcnxConsumerWindow::SetMaxSize (double size) |
| 111 | { |
| 112 | m_maxSize = size; |
| 113 | if (m_maxSize < 0) |
| 114 | { |
| 115 | m_seqMax = 0; |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | m_seqMax = floor(1.0 + m_maxSize * 1024.0 * 1024.0 / m_payloadSize); |
| 120 | NS_LOG_DEBUG ("MaxSeqNo: " << m_seqMax); |
Alexander Afanasyev | 1180b04 | 2012-01-25 19:26:13 -0800 | [diff] [blame^] | 121 | // std::cout << "MaxSeqNo: " << m_seqMax << "\n"; |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 125 | void |
| 126 | CcnxConsumerWindow::ScheduleNextPacket () |
| 127 | { |
Alexander Afanasyev | e4c2ece | 2012-01-11 10:44:40 -0800 | [diff] [blame] | 128 | if (m_window == static_cast<uint32_t> (0) || m_inFlight >= m_window) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 129 | { |
| 130 | if (!m_sendEvent.IsRunning ()) |
| 131 | m_sendEvent = Simulator::Schedule (Seconds (m_rtt->RetransmitTimeout ().ToDouble (Time::S) * 0.1), &CcnxConsumer::SendPacket, this); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | // std::cout << "Window: " << m_window << ", InFlight: " << m_inFlight << "\n"; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 136 | if (!m_sendEvent.IsRunning ()) |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame] | 137 | { |
| 138 | m_inFlight++; |
| 139 | m_sendEvent = Simulator::ScheduleNow (&CcnxConsumer::SendPacket, this); |
| 140 | } |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /////////////////////////////////////////////////// |
| 144 | // Process incoming packets // |
| 145 | /////////////////////////////////////////////////// |
| 146 | |
| 147 | void |
| 148 | CcnxConsumerWindow::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject, |
Alexander Afanasyev | e9c9d72 | 2012-01-19 16:59:30 -0800 | [diff] [blame] | 149 | Ptr<Packet> payload) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 150 | { |
| 151 | CcnxConsumer::OnContentObject (contentObject, payload); |
| 152 | |
| 153 | m_window = m_window + 1; |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame] | 154 | |
| 155 | if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 156 | ScheduleNextPacket (); |
| 157 | } |
| 158 | |
| 159 | void |
Alexander Afanasyev | e9c9d72 | 2012-01-19 16:59:30 -0800 | [diff] [blame] | 160 | CcnxConsumerWindow::OnNack (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> payload) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 161 | { |
Alexander Afanasyev | e9c9d72 | 2012-01-19 16:59:30 -0800 | [diff] [blame] | 162 | CcnxConsumer::OnNack (interest, payload); |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame] | 163 | if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | e4c2ece | 2012-01-11 10:44:40 -0800 | [diff] [blame] | 165 | if (m_window > static_cast<uint32_t> (0)) |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 166 | { |
| 167 | // m_window = 0.5 * m_window;//m_window - 1; |
| 168 | m_window = m_window - 1; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | CcnxConsumerWindow::OnTimeout (uint32_t sequenceNumber) |
| 174 | { |
Alexander Afanasyev | 06b42ec | 2012-01-11 19:05:36 -0800 | [diff] [blame] | 175 | if (m_inFlight > static_cast<uint32_t> (0)) m_inFlight--; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 176 | CcnxConsumer::OnTimeout (sequenceNumber); |
| 177 | } |
| 178 | |
| 179 | } // namespace ns3 |