blob: 584c04f2a8af059f2b121f0b3b0bc5c0783aa7e6 [file] [log] [blame]
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-consumer-batches.h"
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080022#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 Afanasyev669cafd2012-11-06 10:17:33 -080030
31#include "../utils/batches.h"
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080032
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033NS_LOG_COMPONENT_DEFINE ("ndn.ConsumerBatches");
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080034
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070035namespace ns3 {
36namespace ndn {
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080037
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038NS_OBJECT_ENSURE_REGISTERED (ConsumerBatches);
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080039
40TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041ConsumerBatches::GetTypeId (void)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080042{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043 static TypeId tid = TypeId ("ns3::ndn::ConsumerBatches")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070044 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045 .SetParent<Consumer> ()
46 .AddConstructor<ConsumerBatches> ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080047
48 .AddAttribute ("Batches", "Batches to schedule. Should be vector, containing pairs of time and amount",
49 // TypeId::ATTR_SET,
50 StringValue (""),
Alexander Afanasyevc3893b22012-12-11 12:22:00 -080051 MakeBatchesAccessor (&ConsumerBatches::m_batches),
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080052 MakeBatchesChecker ())
53 ;
54
55 return tid;
56}
57
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058ConsumerBatches::ConsumerBatches ()
Alexander Afanasyev3476edf2012-08-14 11:26:00 -070059 : m_initial (true)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080060{
61}
62
63void
Alexander Afanasyevc3893b22012-12-11 12:22:00 -080064ConsumerBatches::StartApplication ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080065{
Alexander Afanasyevc3893b22012-12-11 12:22:00 -080066 Consumer::StartApplication ();
67
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080068 // std::cout << "Batches: " << batches << "\n";
Alexander Afanasyevc3893b22012-12-11 12:22:00 -080069 for (Batches::const_iterator i = m_batches.begin (); i != m_batches.end (); i++)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080070 {
Alexander Afanasyevc3893b22012-12-11 12:22:00 -080071 Simulator::ScheduleWithContext (GetNode ()->GetId (), i->get<0> (), &ConsumerBatches::AddBatch, this, i->get<1> ());
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080072 }
73}
74
75void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070076ConsumerBatches::AddBatch (uint32_t amount)
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080077{
78 // std::cout << Simulator::Now () << " adding batch of " << amount << "\n";
79 m_seqMax += amount;
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -080080 m_rtt->ClearSent (); // this is important, otherwise RTT estimation for the new batch will be affected by previous batch history
Alexander Afanasyev3476edf2012-08-14 11:26:00 -070081 m_initial = true;
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080082 ScheduleNextPacket ();
83}
84
85void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070086ConsumerBatches::ScheduleNextPacket ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080087{
88 if (!m_sendEvent.IsRunning ())
Alexander Afanasyev3476edf2012-08-14 11:26:00 -070089 {
90 Time delay = Seconds (0);
91 if (!m_initial) 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
98///////////////////////////////////////////////////
99// Process incoming packets //
100///////////////////////////////////////////////////
101
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102} // namespace ndn
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -0800103} // namespace ns3