Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (c) 2011-2015 Tsinghua University, P.R.China. |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * 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. |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * 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. |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * 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 | * |
| 19 | * @author Xiaoke Jiang <shock.jiang@gmail.com> |
| 20 | **/ |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 22 | #include "ndn-consumer-zipf-mandelbrot.hpp" |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 23 | |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 24 | #include "utils/ndn-fw-hop-count-tag.hpp" |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 25 | |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 26 | #include <math.h> |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 28 | NS_LOG_COMPONENT_DEFINE("ndn.ConsumerZipfMandelbrot"); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 29 | |
| 30 | namespace ns3 { |
| 31 | namespace ndn { |
| 32 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 33 | NS_OBJECT_ENSURE_REGISTERED(ConsumerZipfMandelbrot); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 34 | |
| 35 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 36 | ConsumerZipfMandelbrot::GetTypeId(void) |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 37 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 38 | static TypeId tid = |
| 39 | TypeId("ns3::ndn::ConsumerZipfMandelbrot") |
| 40 | .SetGroupName("Ndn") |
| 41 | .SetParent<ConsumerCbr>() |
| 42 | .AddConstructor<ConsumerZipfMandelbrot>() |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 44 | .AddAttribute("NumberOfContents", "Number of the Contents in total", StringValue("100"), |
| 45 | MakeUintegerAccessor(&ConsumerZipfMandelbrot::SetNumberOfContents, |
| 46 | &ConsumerZipfMandelbrot::GetNumberOfContents), |
| 47 | MakeUintegerChecker<uint32_t>()) |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 49 | .AddAttribute("q", "parameter of improve rank", StringValue("0.7"), |
| 50 | MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetQ, |
| 51 | &ConsumerZipfMandelbrot::GetQ), |
| 52 | MakeDoubleChecker<double>()) |
| 53 | |
| 54 | .AddAttribute("s", "parameter of power", StringValue("0.7"), |
| 55 | MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetS, |
| 56 | &ConsumerZipfMandelbrot::GetS), |
| 57 | MakeDoubleChecker<double>()); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 58 | |
| 59 | return tid; |
| 60 | } |
| 61 | |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 62 | ConsumerZipfMandelbrot::ConsumerZipfMandelbrot() |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 63 | : m_N(100) // needed here to make sure when SetQ/SetS are called, there is a valid value of N |
| 64 | , m_q(0.7) |
| 65 | , m_s(0.7) |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 66 | , m_seqRng(CreateObject<UniformRandomVariable>()) |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 67 | { |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 68 | // SetNumberOfContents is called by NS-3 object system during the initialization |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 69 | } |
| 70 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 71 | ConsumerZipfMandelbrot::~ConsumerZipfMandelbrot() |
| 72 | { |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | ConsumerZipfMandelbrot::SetNumberOfContents(uint32_t numOfContents) |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 77 | { |
| 78 | m_N = numOfContents; |
| 79 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 80 | NS_LOG_DEBUG(m_q << " and " << m_s << " and " << m_N); |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 81 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 82 | m_Pcum = std::vector<double>(m_N + 1); |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 83 | |
| 84 | m_Pcum[0] = 0.0; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 85 | for (uint32_t i = 1; i <= m_N; i++) { |
| 86 | m_Pcum[i] = m_Pcum[i - 1] + 1.0 / std::pow(i + m_q, m_s); |
| 87 | } |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 89 | for (uint32_t i = 1; i <= m_N; i++) { |
| 90 | m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N]; |
| 91 | NS_LOG_LOGIC("Cumulative probability [" << i << "]=" << m_Pcum[i]); |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 96 | ConsumerZipfMandelbrot::GetNumberOfContents() const |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 97 | { |
| 98 | return m_N; |
| 99 | } |
| 100 | |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 101 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 102 | ConsumerZipfMandelbrot::SetQ(double q) |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 103 | { |
| 104 | m_q = q; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 105 | SetNumberOfContents(m_N); |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | double |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | ConsumerZipfMandelbrot::GetQ() const |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 110 | { |
| 111 | return m_q; |
| 112 | } |
| 113 | |
| 114 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 115 | ConsumerZipfMandelbrot::SetS(double s) |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 116 | { |
| 117 | m_s = s; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 118 | SetNumberOfContents(m_N); |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | double |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 122 | ConsumerZipfMandelbrot::GetS() const |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 123 | { |
| 124 | return m_s; |
| 125 | } |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 126 | |
| 127 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 128 | ConsumerZipfMandelbrot::SendPacket() |
| 129 | { |
| 130 | if (!m_active) |
| 131 | return; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 133 | NS_LOG_FUNCTION_NOARGS(); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 135 | uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 136 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 137 | // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n"; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 139 | while (m_retxSeqs.size()) { |
| 140 | seq = *m_retxSeqs.begin(); |
| 141 | m_retxSeqs.erase(m_retxSeqs.begin()); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 143 | // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ()); |
| 144 | // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ()) |
| 145 | // { |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 146 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 147 | // NS_LOG_DEBUG ("Expire " << seq); |
| 148 | // m_seqLifetimes.erase (seq); // lifetime expired. Trying to find another unexpired |
| 149 | // sequence number |
| 150 | // continue; |
| 151 | // } |
| 152 | NS_LOG_DEBUG("=interest seq " << seq << " from m_retxSeqs"); |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | if (seq == std::numeric_limits<uint32_t>::max()) // no retransmission |
| 157 | { |
| 158 | if (m_seqMax != std::numeric_limits<uint32_t>::max()) { |
| 159 | if (m_seq >= m_seqMax) { |
| 160 | return; // we are totally done |
| 161 | } |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 162 | } |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 163 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 164 | seq = ConsumerZipfMandelbrot::GetNextSeq(); |
| 165 | m_seq++; |
| 166 | } |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 167 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 168 | // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n"; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 170 | // |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 171 | shared_ptr<Name> nameWithSequence = make_shared<Name>(m_interestName); |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 172 | nameWithSequence->appendSequenceNumber(seq); |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 173 | // |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 174 | |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 175 | shared_ptr<Interest> interest = make_shared<Interest>(); |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 176 | interest->setNonce(m_rand->GetValue(0, std::numeric_limits<uint32_t>::max())); |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 177 | interest->setName(*nameWithSequence); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 179 | // NS_LOG_INFO ("Requesting Interest: \n" << *interest); |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 180 | NS_LOG_INFO("> Interest for " << seq << ", Total: " << m_seq << ", face: " << m_face->getId()); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 181 | NS_LOG_DEBUG("Trying to add " << seq << " with " << Simulator::Now() << ". already " |
| 182 | << m_seqTimeouts.size() << " items"); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 183 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 184 | m_seqTimeouts.insert(SeqTimeout(seq, Simulator::Now())); |
| 185 | m_seqFullDelay.insert(SeqTimeout(seq, Simulator::Now())); |
Alexander Afanasyev | 400aae1 | 2013-01-19 13:27:52 -0800 | [diff] [blame] | 186 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 187 | m_seqLastDelay.erase(seq); |
| 188 | m_seqLastDelay.insert(SeqTimeout(seq, Simulator::Now())); |
Alexander Afanasyev | 400aae1 | 2013-01-19 13:27:52 -0800 | [diff] [blame] | 189 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 190 | m_seqRetxCounts[seq]++; |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 191 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 192 | m_rtt->SentSeq(SequenceNumber32(seq), 1); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 193 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 194 | m_transmittedInterests(interest, this, m_face); |
Alexander Afanasyev | 50ea1a3 | 2016-09-08 15:44:08 -0700 | [diff] [blame] | 195 | m_appLink->onReceiveInterest(*interest); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 196 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 197 | ConsumerZipfMandelbrot::ScheduleNextPacket(); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 200 | uint32_t |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 201 | ConsumerZipfMandelbrot::GetNextSeq() |
| 202 | { |
| 203 | uint32_t content_index = 1; //[1, m_N] |
| 204 | double p_sum = 0; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 205 | |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 206 | double p_random = m_seqRng->GetValue(); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 207 | while (p_random == 0) { |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 208 | p_random = m_seqRng->GetValue(); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 209 | } |
| 210 | // if (p_random == 0) |
| 211 | NS_LOG_LOGIC("p_random=" << p_random); |
| 212 | for (uint32_t i = 1; i <= m_N; i++) { |
| 213 | p_sum = m_Pcum[i]; // m_Pcum[i] = m_Pcum[i-1] + p[i], p[0] = 0; e.g.: p_cum[1] = p[1], |
| 214 | // p_cum[2] = p[1] + p[2] |
| 215 | if (p_random <= p_sum) { |
| 216 | content_index = i; |
| 217 | break; |
| 218 | } // if |
| 219 | } // for |
| 220 | // content_index = 1; |
| 221 | NS_LOG_DEBUG("RandomNumber=" << content_index); |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 222 | return content_index; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 226 | ConsumerZipfMandelbrot::ScheduleNextPacket() |
| 227 | { |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 228 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 229 | if (m_firstTime) { |
| 230 | m_sendEvent = Simulator::Schedule(Seconds(0.0), &ConsumerZipfMandelbrot::SendPacket, this); |
| 231 | m_firstTime = false; |
| 232 | } |
| 233 | else if (!m_sendEvent.IsRunning()) |
| 234 | m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency) |
| 235 | : Seconds(m_random->GetValue()), |
| 236 | &ConsumerZipfMandelbrot::SendPacket, this); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | } /* namespace ndn */ |
| 240 | } /* namespace ns3 */ |