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 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 33 | #include "ns3/ndn-app-face.hpp" |
| 34 | #include "ns3/ndn-interest.hpp" |
| 35 | #include "ns3/ndn-data.hpp" |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 37 | NS_LOG_COMPONENT_DEFINE("ndn.ConsumerCbr"); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 39 | namespace ns3 { |
| 40 | namespace ndn { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 41 | |
| 42 | NS_OBJECT_ENSURE_REGISTERED(ConsumerCbr); |
| 43 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 44 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 45 | ConsumerCbr::GetTypeId(void) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 46 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 47 | static TypeId tid = |
| 48 | TypeId("ns3::ndn::ConsumerCbr") |
| 49 | .SetGroupName("Ndn") |
| 50 | .SetParent<Consumer>() |
| 51 | .AddConstructor<ConsumerCbr>() |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 53 | .AddAttribute("Frequency", "Frequency of interest packets", StringValue("1.0"), |
| 54 | MakeDoubleAccessor(&ConsumerCbr::m_frequency), MakeDoubleChecker<double>()) |
Alexander Afanasyev | f8b0f71 | 2012-12-28 19:25:12 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 56 | .AddAttribute("Randomize", |
| 57 | "Type of send time randomization: none (default), uniform, exponential", |
| 58 | StringValue("none"), |
| 59 | MakeStringAccessor(&ConsumerCbr::SetRandomize, &ConsumerCbr::GetRandomize), |
| 60 | MakeStringChecker()) |
| 61 | |
| 62 | .AddAttribute("MaxSeq", "Maximum sequence number to request", |
| 63 | IntegerValue(std::numeric_limits<uint32_t>::max()), |
| 64 | MakeIntegerAccessor(&ConsumerCbr::m_seqMax), MakeIntegerChecker<uint32_t>()) |
Alexander Afanasyev | f8b0f71 | 2012-12-28 19:25:12 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 66 | ; |
| 67 | |
| 68 | return tid; |
| 69 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 70 | |
| 71 | ConsumerCbr::ConsumerCbr() |
| 72 | : m_frequency(1.0) |
| 73 | , m_firstTime(true) |
| 74 | , m_random(0) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 75 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 76 | NS_LOG_FUNCTION_NOARGS(); |
| 77 | m_seqMax = std::numeric_limits<uint32_t>::max(); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 80 | ConsumerCbr::~ConsumerCbr() |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 81 | { |
| 82 | if (m_random) |
| 83 | delete m_random; |
| 84 | } |
| 85 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 86 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 87 | ConsumerCbr::ScheduleNextPacket() |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 88 | { |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 89 | // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate (); |
Alexander Afanasyev | 94cebd0 | 2012-01-16 12:22:34 -0800 | [diff] [blame] | 90 | // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n"; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 92 | if (m_firstTime) { |
| 93 | m_sendEvent = Simulator::Schedule(Seconds(0.0), &Consumer::SendPacket, this); |
| 94 | m_firstTime = false; |
| 95 | } |
| 96 | else if (!m_sendEvent.IsRunning()) |
| 97 | m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency) |
| 98 | : Seconds(m_random->GetValue()), |
| 99 | &Consumer::SendPacket, this); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 102 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 103 | ConsumerCbr::SetRandomize(const std::string& value) |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 104 | { |
| 105 | if (m_random) |
| 106 | delete m_random; |
| 107 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 108 | if (value == "uniform") { |
| 109 | m_random = new UniformVariable(0.0, 2 * 1.0 / m_frequency); |
| 110 | } |
| 111 | else if (value == "exponential") { |
| 112 | m_random = new ExponentialVariable(1.0 / m_frequency, 50 * 1.0 / m_frequency); |
| 113 | } |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 114 | else |
| 115 | m_random = 0; |
| 116 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 117 | m_randomType = value; |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | std::string |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame^] | 121 | ConsumerCbr::GetRandomize() const |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 122 | { |
| 123 | return m_randomType; |
| 124 | } |
| 125 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 126 | /////////////////////////////////////////////////// |
| 127 | // Process incoming packets // |
| 128 | /////////////////////////////////////////////////// |
| 129 | |
| 130 | // void |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 131 | // Consumer::OnData (const Ptr<const Data> &contentObject, |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 132 | // const Ptr<const Packet> &payload) |
| 133 | // { |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 134 | // Consumer::OnData (contentObject, payload); // tracing inside |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 135 | // } |
| 136 | |
| 137 | // void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 138 | // Consumer::OnNack (const Ptr<const Interest> &interest) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 139 | // { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 140 | // Consumer::OnNack (interest); // tracing inside |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 141 | // } |
| 142 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 143 | } // namespace ndn |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 144 | } // namespace ns3 |