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 | |
| 52 | // Alex: q and s are not yet really working |
| 53 | // |
| 54 | // .AddAttribute ("q", "parameter of improve rank", |
| 55 | // StringValue ("0.7"), |
| 56 | // MakeDoubleAccessor (&ConsumerZipfMandelbrot::m_q), |
| 57 | // MakeDoubleChecker<double>()) |
| 58 | // .AddAttribute ("s", "parameter of power", |
| 59 | // StringValue ("0.7"), |
| 60 | // MakeDoubleAccessor (&ConsumerZipfMandelbrot::m_s), |
| 61 | // MakeDoubleChecker<double>()) |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 62 | ; |
| 63 | |
| 64 | return tid; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | ConsumerZipfMandelbrot::ConsumerZipfMandelbrot() |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 69 | : m_q (0.7) |
| 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 | |
| 85 | m_Pcum = std::vector<double> (m_N + 1); |
| 86 | |
| 87 | m_Pcum[0] = 0.0; |
| 88 | for (uint32_t i=1; i<=m_N; i++) |
| 89 | { |
| 90 | m_Pcum[i] = m_Pcum[i-1] + 1.0/pow(i+m_q, m_s); |
| 91 | } |
| 92 | |
| 93 | for (uint32_t i=1; i<=m_N; i++) |
| 94 | { |
| 95 | m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N]; |
| 96 | NS_LOG_LOGIC("cum Probability ["<<i<<"]="<<m_Pcum[i]); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | uint32_t |
| 101 | ConsumerZipfMandelbrot::GetNumberOfContents () const |
| 102 | { |
| 103 | return m_N; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 108 | ConsumerZipfMandelbrot::SendPacket() { |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 109 | if (!m_active) return; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 111 | NS_LOG_FUNCTION_NOARGS (); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 113 | uint32_t seq=std::numeric_limits<uint32_t>::max (); //invalid |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 115 | // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n"; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 117 | while (m_retxSeqs.size ()) |
| 118 | { |
| 119 | seq = *m_retxSeqs.begin (); |
| 120 | m_retxSeqs.erase (m_retxSeqs.begin ()); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 122 | // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ()); |
| 123 | // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ()) |
| 124 | // { |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 125 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 126 | // NS_LOG_DEBUG ("Expire " << seq); |
| 127 | // m_seqLifetimes.erase (seq); // lifetime expired. Trying to find another unexpired sequence number |
| 128 | // continue; |
| 129 | // } |
| 130 | NS_LOG_DEBUG("=interest seq "<<seq<<" from m_retxSeqs"); |
| 131 | break; |
| 132 | } |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 133 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 134 | if (seq == std::numeric_limits<uint32_t>::max ()) //no retransmission |
| 135 | { |
| 136 | if (m_seqMax != std::numeric_limits<uint32_t>::max ()) |
| 137 | { |
| 138 | if (m_seq >= m_seqMax) |
| 139 | { |
| 140 | return; // we are totally done |
| 141 | } |
| 142 | } |
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 | seq = ConsumerZipfMandelbrot::GetNextSeq(); |
| 145 | m_seq ++; |
| 146 | } |
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 | // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n"; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 150 | // |
| 151 | Ptr<NameComponents> nameWithSequence = Create<NameComponents> (m_interestName); |
| 152 | (*nameWithSequence) (seq); |
| 153 | // |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 154 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 155 | InterestHeader interestHeader; |
| 156 | interestHeader.SetNonce (m_rand.GetValue ()); |
| 157 | interestHeader.SetName (nameWithSequence); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 159 | // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader); |
| 160 | NS_LOG_INFO ("> Interest for " << seq<<", Total: "<<m_seq<<", face: "<<m_face->GetId()); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 162 | Ptr<Packet> packet = Create<Packet> (); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 163 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 164 | //NS_LOG_DEBUG ("= Interest for " << seq<<", Total: "<<m_seq<<", face: "<<m_face->GetId()); |
| 165 | packet->AddHeader (interestHeader); |
| 166 | //NS_LOG_DEBUG ("Interest packet size: " << packet->GetSize ()); |
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 | 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] | 169 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 170 | m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ())); |
Alexander Afanasyev | 400aae1 | 2013-01-19 13:27:52 -0800 | [diff] [blame] | 171 | m_seqFullDelay.insert (SeqTimeout (seq, Simulator::Now ())); |
| 172 | |
| 173 | m_seqLastDelay.erase (seq); |
| 174 | m_seqLastDelay.insert (SeqTimeout (seq, Simulator::Now ())); |
| 175 | |
| 176 | m_seqRetxCounts[seq] ++; |
| 177 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 178 | m_transmittedInterests (&interestHeader, this, m_face); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 179 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 180 | m_rtt->SentSeq (SequenceNumber32 (seq), 1); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 181 | |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 182 | FwHopCountTag hopCountTag; |
| 183 | packet->AddPacketTag (hopCountTag); |
| 184 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 185 | m_protocolHandler (packet); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 186 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 187 | ConsumerZipfMandelbrot::ScheduleNextPacket (); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
| 191 | uint32_t |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 192 | ConsumerZipfMandelbrot::GetNextSeq() |
| 193 | { |
| 194 | uint32_t content_index = 1; //[1, m_N] |
| 195 | double p_sum = 0; |
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 | double p_random = m_SeqRng.GetValue(); |
| 198 | while (p_random == 0) |
| 199 | { |
| 200 | p_random = m_SeqRng.GetValue(); |
| 201 | } |
| 202 | //if (p_random == 0) |
| 203 | NS_LOG_LOGIC("p_random="<<p_random); |
| 204 | for (uint32_t i=1; i<=m_N; i++) |
| 205 | { |
| 206 | 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] |
| 207 | if (p_random <= p_sum) |
| 208 | { |
| 209 | content_index = i; |
| 210 | break; |
| 211 | } //if |
| 212 | } //for |
| 213 | //content_index = 1; |
| 214 | NS_LOG_DEBUG("RandomNumber="<<content_index); |
| 215 | return content_index; |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void |
| 219 | ConsumerZipfMandelbrot::ScheduleNextPacket() { |
| 220 | |
Alexander Afanasyev | 1380010 | 2012-12-25 00:30:31 -0800 | [diff] [blame] | 221 | if (m_firstTime) |
| 222 | { |
| 223 | m_sendEvent = Simulator::Schedule (Seconds (0.0), |
| 224 | &ConsumerZipfMandelbrot::SendPacket, this); |
| 225 | m_firstTime = false; |
| 226 | } |
| 227 | else if (!m_sendEvent.IsRunning ()) |
| 228 | m_sendEvent = Simulator::Schedule ( |
| 229 | (m_random == 0) ? |
| 230 | Seconds(1.0 / m_frequency) |
| 231 | : |
| 232 | Seconds(m_random->GetValue ()), |
| 233 | &ConsumerZipfMandelbrot::SendPacket, this); |
Shock | b0f8315 | 2012-12-25 14:16:47 +0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | } /* namespace ndn */ |
| 237 | } /* namespace ns3 */ |