blob: 84fdd18b5a9bab6124b57336b17b8874ff08a6ed [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"
30#include "ns3/double.h"
31
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070032#include "ns3/ndn-app-face.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080033#include "ns3/ndn-interest.h"
34#include "ns3/ndn-content-object.h"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080035
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036NS_LOG_COMPONENT_DEFINE ("ndn.ConsumerCbr");
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080037
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038namespace ns3 {
39namespace ndn {
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080040
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041NS_OBJECT_ENSURE_REGISTERED (ConsumerCbr);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080042
43TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070044ConsumerCbr::GetTypeId (void)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080045{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046 static TypeId tid = TypeId ("ns3::ndn::ConsumerCbr")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070047 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048 .SetParent<Consumer> ()
49 .AddConstructor<ConsumerCbr> ()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080050
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080051 .AddAttribute ("Frequency", "Frequency of interest packets",
52 StringValue ("1.0"),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070053 MakeDoubleAccessor (&ConsumerCbr::m_frequency),
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080054 MakeDoubleChecker<double> ())
Alexander Afanasyevf8b0f712012-12-28 19:25:12 -080055
Alexander Afanasyev59e67712012-02-07 11:49:34 -080056 .AddAttribute ("Randomize", "Type of send time randomization: none (default), uniform, exponential",
57 StringValue ("none"),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058 MakeStringAccessor (&ConsumerCbr::SetRandomize, &ConsumerCbr::GetRandomize),
Alexander Afanasyev59e67712012-02-07 11:49:34 -080059 MakeStringChecker ())
Alexander Afanasyevf8b0f712012-12-28 19:25:12 -080060
61 .AddAttribute ("MaxSeq",
62 "Maximum sequence number to request",
63 IntegerValue (std::numeric_limits<uint32_t>::max ()),
64 MakeIntegerAccessor (&ConsumerCbr::m_seqMax),
65 MakeIntegerChecker<uint32_t> ())
66
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080067 ;
68
69 return tid;
70}
71
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072ConsumerCbr::ConsumerCbr ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080073 : m_frequency (1.0)
Alexander Afanasyev59e67712012-02-07 11:49:34 -080074 , m_firstTime (true)
75 , m_random (0)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080076{
77 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev18750bb2012-02-02 23:11:30 -080078 m_seqMax = std::numeric_limits<uint32_t>::max ();
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080079}
80
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070081ConsumerCbr::~ConsumerCbr ()
Alexander Afanasyev59e67712012-02-07 11:49:34 -080082{
83 if (m_random)
84 delete m_random;
85}
86
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080087void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070088ConsumerCbr::ScheduleNextPacket ()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080089{
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080090 // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
Alexander Afanasyev94cebd02012-01-16 12:22:34 -080091 // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n";
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080092
Alexander Afanasyev59e67712012-02-07 11:49:34 -080093 if (m_firstTime)
94 {
95 m_sendEvent = Simulator::Schedule (Seconds (0.0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 &Consumer::SendPacket, this);
Alexander Afanasyev59e67712012-02-07 11:49:34 -080097 m_firstTime = false;
98 }
99 else if (!m_sendEvent.IsRunning ())
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800100 m_sendEvent = Simulator::Schedule (
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800101 (m_random == 0) ?
102 Seconds(1.0 / m_frequency)
103 :
104 Seconds(m_random->GetValue ()),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700105 &Consumer::SendPacket, this);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800106}
107
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800108void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700109ConsumerCbr::SetRandomize (const std::string &value)
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800110{
111 if (m_random)
112 delete m_random;
113
114 else if (value == "uniform")
115 {
116 m_random = new UniformVariable (0.0, 2 * 1.0 / m_frequency);
117 }
118 else if (value == "exponential")
119 {
120 m_random = new ExponentialVariable (1.0 / m_frequency, 50 * 1.0 / m_frequency);
121 }
122 else
123 m_random = 0;
124
125 m_randomType = value;
126}
127
128std::string
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700129ConsumerCbr::GetRandomize () const
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800130{
131 return m_randomType;
132}
133
134
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800135///////////////////////////////////////////////////
136// Process incoming packets //
137///////////////////////////////////////////////////
138
139// void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700140// Consumer::OnContentObject (const Ptr<const ContentObjectHeader> &contentObject,
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800141// const Ptr<const Packet> &payload)
142// {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700143// Consumer::OnContentObject (contentObject, payload); // tracing inside
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800144// }
145
146// void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700147// Consumer::OnNack (const Ptr<const InterestHeader> &interest)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800148// {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149// Consumer::OnNack (interest); // tracing inside
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800150// }
151
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152} // namespace ndn
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800153} // namespace ns3