Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 21 | #include "ndn-consumer-cbr.hpp" |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 22 | #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 Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 30 | #include "ns3/integer.h" |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 31 | #include "ns3/double.h" |
| 32 | |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 33 | #include "model/ndn-app-face.hpp" |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 35 | NS_LOG_COMPONENT_DEFINE("ndn.ConsumerCbr"); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 37 | namespace ns3 { |
| 38 | namespace ndn { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 39 | |
| 40 | NS_OBJECT_ENSURE_REGISTERED(ConsumerCbr); |
| 41 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 42 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 43 | ConsumerCbr::GetTypeId(void) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 44 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | static TypeId tid = |
| 46 | TypeId("ns3::ndn::ConsumerCbr") |
| 47 | .SetGroupName("Ndn") |
| 48 | .SetParent<Consumer>() |
| 49 | .AddConstructor<ConsumerCbr>() |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 51 | .AddAttribute("Frequency", "Frequency of interest packets", StringValue("1.0"), |
| 52 | MakeDoubleAccessor(&ConsumerCbr::m_frequency), MakeDoubleChecker<double>()) |
Alexander Afanasyev | f8b0f71 | 2012-12-28 19:25:12 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 54 | .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 Afanasyev | f8b0f71 | 2012-12-28 19:25:12 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 64 | ; |
| 65 | |
| 66 | return tid; |
| 67 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 68 | |
| 69 | ConsumerCbr::ConsumerCbr() |
| 70 | : m_frequency(1.0) |
| 71 | , m_firstTime(true) |
| 72 | , m_random(0) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 73 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | NS_LOG_FUNCTION_NOARGS(); |
| 75 | m_seqMax = std::numeric_limits<uint32_t>::max(); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 78 | ConsumerCbr::~ConsumerCbr() |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 79 | { |
| 80 | if (m_random) |
| 81 | delete m_random; |
| 82 | } |
| 83 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 84 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 85 | ConsumerCbr::ScheduleNextPacket() |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 86 | { |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 87 | // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate (); |
Alexander Afanasyev | 94cebd0 | 2012-01-16 12:22:34 -0800 | [diff] [blame] | 88 | // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n"; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 90 | 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 Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 100 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 101 | ConsumerCbr::SetRandomize(const std::string& value) |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 102 | { |
| 103 | if (m_random) |
| 104 | delete m_random; |
| 105 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 106 | 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 Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 112 | else |
| 113 | m_random = 0; |
| 114 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 115 | m_randomType = value; |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | std::string |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 119 | ConsumerCbr::GetRandomize() const |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 120 | { |
| 121 | return m_randomType; |
| 122 | } |
| 123 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 124 | } // namespace ndn |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 125 | } // namespace ns3 |