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