blob: 77fe130aebfc9ab4c4f84e48e13d1f1ebd2f3c30 [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 Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-consumer-cbr.h"
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 Afanasyev4aac5572012-08-09 10:49:55 -070033#include "ns3/ndn-app-face.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080034#include "ns3/ndn-interest.h"
35#include "ns3/ndn-content-object.h"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080036
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037NS_LOG_COMPONENT_DEFINE ("ndn.ConsumerCbr");
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080038
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039namespace ns3 {
40namespace ndn {
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080041
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042NS_OBJECT_ENSURE_REGISTERED (ConsumerCbr);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080043
44TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045ConsumerCbr::GetTypeId (void)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080046{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047 static TypeId tid = TypeId ("ns3::ndn::ConsumerCbr")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070048 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049 .SetParent<Consumer> ()
50 .AddConstructor<ConsumerCbr> ()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080051
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080052 .AddAttribute ("Frequency", "Frequency of interest packets",
53 StringValue ("1.0"),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070054 MakeDoubleAccessor (&ConsumerCbr::m_frequency),
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080055 MakeDoubleChecker<double> ())
Alexander Afanasyevf8b0f712012-12-28 19:25:12 -080056
Alexander Afanasyev59e67712012-02-07 11:49:34 -080057 .AddAttribute ("Randomize", "Type of send time randomization: none (default), uniform, exponential",
58 StringValue ("none"),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 MakeStringAccessor (&ConsumerCbr::SetRandomize, &ConsumerCbr::GetRandomize),
Alexander Afanasyev59e67712012-02-07 11:49:34 -080060 MakeStringChecker ())
Alexander Afanasyevf8b0f712012-12-28 19:25:12 -080061
62 .AddAttribute ("MaxSeq",
63 "Maximum sequence number to request",
64 IntegerValue (std::numeric_limits<uint32_t>::max ()),
65 MakeIntegerAccessor (&ConsumerCbr::m_seqMax),
66 MakeIntegerChecker<uint32_t> ())
67
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080068 ;
69
70 return tid;
71}
72
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073ConsumerCbr::ConsumerCbr ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080074 : m_frequency (1.0)
Alexander Afanasyev59e67712012-02-07 11:49:34 -080075 , m_firstTime (true)
76 , m_random (0)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080077{
78 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev18750bb2012-02-02 23:11:30 -080079 m_seqMax = std::numeric_limits<uint32_t>::max ();
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080080}
81
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070082ConsumerCbr::~ConsumerCbr ()
Alexander Afanasyev59e67712012-02-07 11:49:34 -080083{
84 if (m_random)
85 delete m_random;
86}
87
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080088void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089ConsumerCbr::ScheduleNextPacket ()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080090{
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080091 // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
Alexander Afanasyev94cebd02012-01-16 12:22:34 -080092 // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n";
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080093
Alexander Afanasyev59e67712012-02-07 11:49:34 -080094 if (m_firstTime)
95 {
96 m_sendEvent = Simulator::Schedule (Seconds (0.0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070097 &Consumer::SendPacket, this);
Alexander Afanasyev59e67712012-02-07 11:49:34 -080098 m_firstTime = false;
99 }
100 else if (!m_sendEvent.IsRunning ())
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800101 m_sendEvent = Simulator::Schedule (
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800102 (m_random == 0) ?
103 Seconds(1.0 / m_frequency)
104 :
105 Seconds(m_random->GetValue ()),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106 &Consumer::SendPacket, this);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800107}
108
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800109void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110ConsumerCbr::SetRandomize (const std::string &value)
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800111{
112 if (m_random)
113 delete m_random;
114
Alexander Afanasyev89e688a2013-05-04 15:17:49 -0700115 if (value == "uniform")
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800116 {
117 m_random = new UniformVariable (0.0, 2 * 1.0 / m_frequency);
118 }
119 else if (value == "exponential")
120 {
121 m_random = new ExponentialVariable (1.0 / m_frequency, 50 * 1.0 / m_frequency);
122 }
123 else
124 m_random = 0;
125
126 m_randomType = value;
127}
128
129std::string
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700130ConsumerCbr::GetRandomize () const
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800131{
132 return m_randomType;
133}
134
135
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800136///////////////////////////////////////////////////
137// Process incoming packets //
138///////////////////////////////////////////////////
139
140// void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700141// Consumer::OnData (const Ptr<const Data> &contentObject,
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800142// const Ptr<const Packet> &payload)
143// {
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700144// Consumer::OnData (contentObject, payload); // tracing inside
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800145// }
146
147// void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700148// Consumer::OnNack (const Ptr<const Interest> &interest)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800149// {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700150// Consumer::OnNack (interest); // tracing inside
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800151// }
152
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700153} // namespace ndn
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800154} // namespace ns3