blob: bde2be947a668926593b854a9b8ff38e44c8fedf [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-consumer-batches.hpp"
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080021#include "ns3/ptr.h"
22#include "ns3/log.h"
23#include "ns3/simulator.h"
24#include "ns3/packet.h"
25#include "ns3/callback.h"
26#include "ns3/string.h"
27#include "ns3/uinteger.h"
28#include "ns3/double.h"
Alexander Afanasyev669cafd2012-11-06 10:17:33 -080029
Mickey Sweatt89046c12014-11-16 20:32:27 -080030#include "utils/batches.hpp"
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080031
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080032NS_LOG_COMPONENT_DEFINE("ndn.ConsumerBatches");
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080033
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034namespace ns3 {
35namespace ndn {
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080036
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037NS_OBJECT_ENSURE_REGISTERED(ConsumerBatches);
38
39TypeId
40ConsumerBatches::GetTypeId(void)
41{
42 static TypeId tid =
43 TypeId("ns3::ndn::ConsumerBatches")
44 .SetGroupName("Ndn")
45 .SetParent<Consumer>()
46 .AddConstructor<ConsumerBatches>()
47
48 .AddAttribute("Batches",
49 "Batches to schedule. Should be vector, containing pairs of time and amount",
50 // TypeId::ATTR_SET,
51 StringValue(""), MakeBatchesAccessor(&ConsumerBatches::m_batches),
52 MakeBatchesChecker());
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080053
54 return tid;
55}
56
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057ConsumerBatches::ConsumerBatches()
58 : m_initial(true)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080059{
60}
61
62void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063ConsumerBatches::StartApplication()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080064{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 Consumer::StartApplication();
66
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080067 // std::cout << "Batches: " << batches << "\n";
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 for (Batches::const_iterator i = m_batches.begin(); i != m_batches.end(); i++) {
Mickey Sweatt89046c12014-11-16 20:32:27 -080069 Simulator::ScheduleWithContext(GetNode()->GetId(), std::get<0>(*i), &ConsumerBatches::AddBatch,
70 this, std::get<1>(*i));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 }
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080072}
73
74void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075ConsumerBatches::AddBatch(uint32_t amount)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080076{
77 // std::cout << Simulator::Now () << " adding batch of " << amount << "\n";
78 m_seqMax += amount;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 m_rtt->ClearSent(); // this is important, otherwise RTT estimation for the new batch will be
80 // affected by previous batch history
Alexander Afanasyev3476edf2012-08-14 11:26:00 -070081 m_initial = true;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 ScheduleNextPacket();
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080083}
84
85void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086ConsumerBatches::ScheduleNextPacket()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080087{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 if (!m_sendEvent.IsRunning()) {
89 Time delay = Seconds(0);
90 if (!m_initial)
91 delay = m_rtt->RetransmitTimeout();
92
93 m_initial = false;
94 m_sendEvent = Simulator::Schedule(delay, &Consumer::SendPacket, this);
95 }
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080096}
97
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098} // namespace ndn
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080099} // namespace ns3