Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -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 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 21 | #include "ndn-consumer-batches.hpp" |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 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" |
Alexander Afanasyev | 669cafd | 2012-11-06 10:17:33 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 31 | #include "../utils/batches.hpp" |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 33 | NS_LOG_COMPONENT_DEFINE("ndn.ConsumerBatches"); |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 35 | namespace ns3 { |
| 36 | namespace ndn { |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 38 | NS_OBJECT_ENSURE_REGISTERED(ConsumerBatches); |
| 39 | |
| 40 | TypeId |
| 41 | ConsumerBatches::GetTypeId(void) |
| 42 | { |
| 43 | static TypeId tid = |
| 44 | TypeId("ns3::ndn::ConsumerBatches") |
| 45 | .SetGroupName("Ndn") |
| 46 | .SetParent<Consumer>() |
| 47 | .AddConstructor<ConsumerBatches>() |
| 48 | |
| 49 | .AddAttribute("Batches", |
| 50 | "Batches to schedule. Should be vector, containing pairs of time and amount", |
| 51 | // TypeId::ATTR_SET, |
| 52 | StringValue(""), MakeBatchesAccessor(&ConsumerBatches::m_batches), |
| 53 | MakeBatchesChecker()); |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 54 | |
| 55 | return tid; |
| 56 | } |
| 57 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 58 | ConsumerBatches::ConsumerBatches() |
| 59 | : m_initial(true) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
| 63 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 64 | ConsumerBatches::StartApplication() |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 65 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | Consumer::StartApplication(); |
| 67 | |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 68 | // std::cout << "Batches: " << batches << "\n"; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | for (Batches::const_iterator i = m_batches.begin(); i != m_batches.end(); i++) { |
| 70 | Simulator::ScheduleWithContext(GetNode()->GetId(), i->get<0>(), &ConsumerBatches::AddBatch, |
| 71 | this, i->get<1>()); |
| 72 | } |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | ConsumerBatches::AddBatch(uint32_t amount) |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 77 | { |
| 78 | // std::cout << Simulator::Now () << " adding batch of " << amount << "\n"; |
| 79 | m_seqMax += amount; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 80 | m_rtt->ClearSent(); // this is important, otherwise RTT estimation for the new batch will be |
| 81 | // affected by previous batch history |
Alexander Afanasyev | 3476edf | 2012-08-14 11:26:00 -0700 | [diff] [blame] | 82 | m_initial = true; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 83 | ScheduleNextPacket(); |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 87 | ConsumerBatches::ScheduleNextPacket() |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 88 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 89 | if (!m_sendEvent.IsRunning()) { |
| 90 | Time delay = Seconds(0); |
| 91 | if (!m_initial) |
| 92 | delay = m_rtt->RetransmitTimeout(); |
| 93 | |
| 94 | m_initial = false; |
| 95 | m_sendEvent = Simulator::Schedule(delay, &Consumer::SendPacket, this); |
| 96 | } |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /////////////////////////////////////////////////// |
| 100 | // Process incoming packets // |
| 101 | /////////////////////////////////////////////////// |
| 102 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 103 | } // namespace ndn |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 104 | } // namespace ns3 |