blob: fc5be6d69ab3e6a01425ca883acecbfc549dc297 [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 Afanasyev029d38d2012-01-09 13:50:50 -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 Afanasyev029d38d2012-01-09 13:50:50 -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 Afanasyev029d38d2012-01-09 13:50:50 -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 Afanasyev029d38d2012-01-09 13:50:50 -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 Afanasyev029d38d2012-01-09 13:50:50 -080019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-consumer-cbr.hpp"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -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/boolean.h"
28#include "ns3/uinteger.h"
Alexander Afanasyeva89bc102013-07-16 10:17:31 -070029#include "ns3/integer.h"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080030#include "ns3/double.h"
31
Mickey Sweatt89046c12014-11-16 20:32:27 -080032#include "model/ndn-app-face.hpp"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080033
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034NS_LOG_COMPONENT_DEFINE("ndn.ConsumerCbr");
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080035
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036namespace ns3 {
37namespace ndn {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038
39NS_OBJECT_ENSURE_REGISTERED(ConsumerCbr);
40
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080041TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042ConsumerCbr::GetTypeId(void)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080043{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 static TypeId tid =
45 TypeId("ns3::ndn::ConsumerCbr")
46 .SetGroupName("Ndn")
47 .SetParent<Consumer>()
48 .AddConstructor<ConsumerCbr>()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080049
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 .AddAttribute("Frequency", "Frequency of interest packets", StringValue("1.0"),
51 MakeDoubleAccessor(&ConsumerCbr::m_frequency), MakeDoubleChecker<double>())
Alexander Afanasyevf8b0f712012-12-28 19:25:12 -080052
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 .AddAttribute("Randomize",
54 "Type of send time randomization: none (default), uniform, exponential",
55 StringValue("none"),
56 MakeStringAccessor(&ConsumerCbr::SetRandomize, &ConsumerCbr::GetRandomize),
57 MakeStringChecker())
58
59 .AddAttribute("MaxSeq", "Maximum sequence number to request",
60 IntegerValue(std::numeric_limits<uint32_t>::max()),
61 MakeIntegerAccessor(&ConsumerCbr::m_seqMax), MakeIntegerChecker<uint32_t>())
Alexander Afanasyevf8b0f712012-12-28 19:25:12 -080062
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080063 ;
64
65 return tid;
66}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067
68ConsumerCbr::ConsumerCbr()
69 : m_frequency(1.0)
70 , m_firstTime(true)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080071{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 NS_LOG_FUNCTION_NOARGS();
73 m_seqMax = std::numeric_limits<uint32_t>::max();
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080074}
75
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076ConsumerCbr::~ConsumerCbr()
Alexander Afanasyev59e67712012-02-07 11:49:34 -080077{
Alexander Afanasyev59e67712012-02-07 11:49:34 -080078}
79
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080080void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081ConsumerCbr::ScheduleNextPacket()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080082{
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080083 // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
Alexander Afanasyev94cebd02012-01-16 12:22:34 -080084 // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n";
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080085
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 if (m_firstTime) {
87 m_sendEvent = Simulator::Schedule(Seconds(0.0), &Consumer::SendPacket, this);
88 m_firstTime = false;
89 }
90 else if (!m_sendEvent.IsRunning())
91 m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency)
92 : Seconds(m_random->GetValue()),
93 &Consumer::SendPacket, this);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080094}
95
Alexander Afanasyev59e67712012-02-07 11:49:34 -080096void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097ConsumerCbr::SetRandomize(const std::string& value)
Alexander Afanasyev59e67712012-02-07 11:49:34 -080098{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 if (value == "uniform") {
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700100 m_random = CreateObject<UniformRandomVariable>();
101 m_random->SetAttribute("Min", DoubleValue(0.0));
102 m_random->SetAttribute("Max", DoubleValue(2 * 1.0 / m_frequency));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 }
104 else if (value == "exponential") {
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700105 m_random = CreateObject<ExponentialRandomVariable>();
106 m_random->SetAttribute("Mean", DoubleValue(1.0 / m_frequency));
107 m_random->SetAttribute("Bound", DoubleValue(50 * 1.0 / m_frequency));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 }
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800109 else
110 m_random = 0;
111
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 m_randomType = value;
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800113}
114
115std::string
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116ConsumerCbr::GetRandomize() const
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800117{
118 return m_randomType;
119}
120
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700121} // namespace ndn
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800122} // namespace ns3