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 | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 179 | (*nameWithSequence) (seq); |
| 180 | // |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 181 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 182 | Interest interestHeader; |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 183 | interestHeader.SetNonce (m_rand.GetValue ()); |
| 184 | interestHeader.SetName (nameWithSequence); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 185 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 186 | // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader); |
| 187 | NS_LOG_INFO ("> Interest for " << seq<<", Total: "<<m_seq<<", face: "<<m_face->GetId()); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 189 | Ptr<Packet> packet = Create<Packet> (); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 190 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 191 | //NS_LOG_DEBUG ("= Interest for " << seq<<", Total: "<<m_seq<<", face: "<<m_face->GetId()); |
| 192 | packet->AddHeader (interestHeader); |
| 193 | //NS_LOG_DEBUG ("Interest packet size: " << packet->GetSize ()); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 195 | 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] | 196 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 197 | m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ())); |
Alexander Afanasyev | 400aae1 | 2013-01-19 13:27:52 -0800 | [diff] [blame] | 198 | m_seqFullDelay.insert (SeqTimeout (seq, Simulator::Now ())); |
| 199 | |
| 200 | m_seqLastDelay.erase (seq); |
| 201 | m_seqLastDelay.insert (SeqTimeout (seq, Simulator::Now ())); |
| 202 | |
| 203 | m_seqRetxCounts[seq] ++; |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 204 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 205 | m_transmittedInterests (&interestHeader, this, m_face); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 206 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 207 | m_rtt->SentSeq (SequenceNumber32 (seq), 1); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 208 | |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 209 | FwHopCountTag hopCountTag; |
| 210 | packet->AddPacketTag (hopCountTag); |
Saran Tarnoi | 5cd9a15 | 2013-02-15 15:59:15 -0800 | [diff] [blame] | 211 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 212 | m_protocolHandler (packet); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 214 | ConsumerZipfMandelbrot::ScheduleNextPacket (); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | uint32_t |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 219 | ConsumerZipfMandelbrot::GetNextSeq() |
| 220 | { |
| 221 | uint32_t content_index = 1; //[1, m_N] |
| 222 | double p_sum = 0; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 223 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 224 | double p_random = m_SeqRng.GetValue(); |
| 225 | while (p_random == 0) |
| 226 | { |
| 227 | p_random = m_SeqRng.GetValue(); |
| 228 | } |
| 229 | //if (p_random == 0) |
| 230 | NS_LOG_LOGIC("p_random="<<p_random); |
| 231 | for (uint32_t i=1; i<=m_N; i++) |
| 232 | { |
| 233 | 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] |
| 234 | if (p_random <= p_sum) |
| 235 | { |
| 236 | content_index = i; |
| 237 | break; |
| 238 | } //if |
| 239 | } //for |
| 240 | //content_index = 1; |
| 241 | NS_LOG_DEBUG("RandomNumber="<<content_index); |
| 242 | return content_index; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void |
| 246 | ConsumerZipfMandelbrot::ScheduleNextPacket() { |
| 247 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 248 | if (m_firstTime) |
| 249 | { |
| 250 | m_sendEvent = Simulator::Schedule (Seconds (0.0), |
| 251 | &ConsumerZipfMandelbrot::SendPacket, this); |
| 252 | m_firstTime = false; |
| 253 | } |
| 254 | else if (!m_sendEvent.IsRunning ()) |
| 255 | m_sendEvent = Simulator::Schedule ( |
| 256 | (m_random == 0) ? |
| 257 | Seconds(1.0 / m_frequency) |
| 258 | : |
| 259 | Seconds(m_random->GetValue ()), |
| 260 | &ConsumerZipfMandelbrot::SendPacket, this); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | } /* namespace ndn */ |
| 264 | } /* namespace ns3 */ |