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