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