blob: f64ae22eb0cc2e4f32b782241558fa71393d7a46 [file] [log] [blame]
Alexander Afanasyev029d38d2012-01-09 13:50:50 -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: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
Alexander Afanasyev0c395372014-12-20 15:54:02 -080021#include "ndn-consumer-cbr.hpp"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -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/boolean.h"
29#include "ns3/uinteger.h"
Alexander Afanasyeva89bc102013-07-16 10:17:31 -070030#include "ns3/integer.h"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080031#include "ns3/double.h"
32
Alexander Afanasyev0c395372014-12-20 15:54:02 -080033#include "ns3/ndn-app-face.hpp"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080034
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080035NS_LOG_COMPONENT_DEFINE("ndn.ConsumerCbr");
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080036
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037namespace ns3 {
38namespace ndn {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039
40NS_OBJECT_ENSURE_REGISTERED(ConsumerCbr);
41
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080042TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043ConsumerCbr::GetTypeId(void)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080044{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 static TypeId tid =
46 TypeId("ns3::ndn::ConsumerCbr")
47 .SetGroupName("Ndn")
48 .SetParent<Consumer>()
49 .AddConstructor<ConsumerCbr>()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080050
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 .AddAttribute("Frequency", "Frequency of interest packets", StringValue("1.0"),
52 MakeDoubleAccessor(&ConsumerCbr::m_frequency), MakeDoubleChecker<double>())
Alexander Afanasyevf8b0f712012-12-28 19:25:12 -080053
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054 .AddAttribute("Randomize",
55 "Type of send time randomization: none (default), uniform, exponential",
56 StringValue("none"),
57 MakeStringAccessor(&ConsumerCbr::SetRandomize, &ConsumerCbr::GetRandomize),
58 MakeStringChecker())
59
60 .AddAttribute("MaxSeq", "Maximum sequence number to request",
61 IntegerValue(std::numeric_limits<uint32_t>::max()),
62 MakeIntegerAccessor(&ConsumerCbr::m_seqMax), MakeIntegerChecker<uint32_t>())
Alexander Afanasyevf8b0f712012-12-28 19:25:12 -080063
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080064 ;
65
66 return tid;
67}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068
69ConsumerCbr::ConsumerCbr()
70 : m_frequency(1.0)
71 , m_firstTime(true)
72 , m_random(0)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080073{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 NS_LOG_FUNCTION_NOARGS();
75 m_seqMax = std::numeric_limits<uint32_t>::max();
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080076}
77
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078ConsumerCbr::~ConsumerCbr()
Alexander Afanasyev59e67712012-02-07 11:49:34 -080079{
80 if (m_random)
81 delete m_random;
82}
83
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080084void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085ConsumerCbr::ScheduleNextPacket()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080086{
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080087 // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
Alexander Afanasyev94cebd02012-01-16 12:22:34 -080088 // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n";
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080089
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 if (m_firstTime) {
91 m_sendEvent = Simulator::Schedule(Seconds(0.0), &Consumer::SendPacket, this);
92 m_firstTime = false;
93 }
94 else if (!m_sendEvent.IsRunning())
95 m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency)
96 : Seconds(m_random->GetValue()),
97 &Consumer::SendPacket, this);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080098}
99
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800100void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101ConsumerCbr::SetRandomize(const std::string& value)
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800102{
103 if (m_random)
104 delete m_random;
105
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 if (value == "uniform") {
107 m_random = new UniformVariable(0.0, 2 * 1.0 / m_frequency);
108 }
109 else if (value == "exponential") {
110 m_random = new ExponentialVariable(1.0 / m_frequency, 50 * 1.0 / m_frequency);
111 }
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800112 else
113 m_random = 0;
114
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 m_randomType = value;
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800116}
117
118std::string
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119ConsumerCbr::GetRandomize() const
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800120{
121 return m_randomType;
122}
123
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800124///////////////////////////////////////////////////
125// Process incoming packets //
126///////////////////////////////////////////////////
127
128// void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700129// Consumer::OnData (const shared_ptr<const Data> &contentObject,
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800130// const Ptr<const Packet> &payload)
131// {
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700132// Consumer::OnData (contentObject, payload); // tracing inside
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800133// }
134
135// void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700136// Consumer::OnNack (const shared_ptr<const Interest> &interest)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800137// {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700138// Consumer::OnNack (interest); // tracing inside
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800139// }
140
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700141} // namespace ndn
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800142} // namespace ns3