blob: 2c7f71ffc5196a043eb3bf21550bee5af3058d69 [file] [log] [blame]
Shockb0f83152012-12-25 14:16:47 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08002/**
3 * Copyright (c) 2011-2015 Tsinghua University, P.R.China.
Shockb0f83152012-12-25 14:16:47 +08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Shockb0f83152012-12-25 14:16:47 +08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Shockb0f83152012-12-25 14:16:47 +080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Shockb0f83152012-12-25 14:16:47 +080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Xiaoke Jiang <shock.jiang@gmail.com>
20 **/
Shockb0f83152012-12-25 14:16:47 +080021
Alexander Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-consumer-zipf-mandelbrot.hpp"
Shockb0f83152012-12-25 14:16:47 +080023
Mickey Sweatt89046c12014-11-16 20:32:27 -080024#include "model/ndn-app-face.hpp"
25#include "utils/ndn-fw-hop-count-tag.hpp"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080026
Shockb0f83152012-12-25 14:16:47 +080027#include <math.h>
Shockb0f83152012-12-25 14:16:47 +080028
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080029NS_LOG_COMPONENT_DEFINE("ndn.ConsumerZipfMandelbrot");
Shockb0f83152012-12-25 14:16:47 +080030
31namespace ns3 {
32namespace ndn {
33
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034NS_OBJECT_ENSURE_REGISTERED(ConsumerZipfMandelbrot);
Shockb0f83152012-12-25 14:16:47 +080035
36TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037ConsumerZipfMandelbrot::GetTypeId(void)
Shockb0f83152012-12-25 14:16:47 +080038{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039 static TypeId tid =
40 TypeId("ns3::ndn::ConsumerZipfMandelbrot")
41 .SetGroupName("Ndn")
42 .SetParent<ConsumerCbr>()
43 .AddConstructor<ConsumerZipfMandelbrot>()
Alexander Afanasyev13800102012-12-25 00:30:31 -080044
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 .AddAttribute("NumberOfContents", "Number of the Contents in total", StringValue("100"),
46 MakeUintegerAccessor(&ConsumerZipfMandelbrot::SetNumberOfContents,
47 &ConsumerZipfMandelbrot::GetNumberOfContents),
48 MakeUintegerChecker<uint32_t>())
Alexander Afanasyev13800102012-12-25 00:30:31 -080049
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 .AddAttribute("q", "parameter of improve rank", StringValue("0.7"),
51 MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetQ,
52 &ConsumerZipfMandelbrot::GetQ),
53 MakeDoubleChecker<double>())
54
55 .AddAttribute("s", "parameter of power", StringValue("0.7"),
56 MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetS,
57 &ConsumerZipfMandelbrot::GetS),
58 MakeDoubleChecker<double>());
Shockb0f83152012-12-25 14:16:47 +080059
60 return tid;
61}
62
Shockb0f83152012-12-25 14:16:47 +080063ConsumerZipfMandelbrot::ConsumerZipfMandelbrot()
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064 : m_N(100) // needed here to make sure when SetQ/SetS are called, there is a valid value of N
65 , m_q(0.7)
66 , m_s(0.7)
67 , m_SeqRng(0.0, 1.0)
Shockb0f83152012-12-25 14:16:47 +080068{
Alexander Afanasyev13800102012-12-25 00:30:31 -080069 // SetNumberOfContents is called by NS-3 object system during the initialization
Shockb0f83152012-12-25 14:16:47 +080070}
71
Alexander Afanasyev13800102012-12-25 00:30:31 -080072ConsumerZipfMandelbrot::~ConsumerZipfMandelbrot()
73{
Shockb0f83152012-12-25 14:16:47 +080074}
75
76void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077ConsumerZipfMandelbrot::SetNumberOfContents(uint32_t numOfContents)
Alexander Afanasyev13800102012-12-25 00:30:31 -080078{
79 m_N = numOfContents;
80
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 NS_LOG_DEBUG(m_q << " and " << m_s << " and " << m_N);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080082
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083 m_Pcum = std::vector<double>(m_N + 1);
Alexander Afanasyev13800102012-12-25 00:30:31 -080084
85 m_Pcum[0] = 0.0;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 for (uint32_t i = 1; i <= m_N; i++) {
87 m_Pcum[i] = m_Pcum[i - 1] + 1.0 / std::pow(i + m_q, m_s);
88 }
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080089
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 for (uint32_t i = 1; i <= m_N; i++) {
91 m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N];
92 NS_LOG_LOGIC("Cumulative probability [" << i << "]=" << m_Pcum[i]);
Alexander Afanasyev13800102012-12-25 00:30:31 -080093 }
94}
95
96uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097ConsumerZipfMandelbrot::GetNumberOfContents() const
Alexander Afanasyev13800102012-12-25 00:30:31 -080098{
99 return m_N;
100}
101
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800102void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103ConsumerZipfMandelbrot::SetQ(double q)
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800104{
105 m_q = q;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 SetNumberOfContents(m_N);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800107}
108
109double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110ConsumerZipfMandelbrot::GetQ() const
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800111{
112 return m_q;
113}
114
115void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116ConsumerZipfMandelbrot::SetS(double s)
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800117{
118 m_s = s;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 SetNumberOfContents(m_N);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800120}
121
122double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800123ConsumerZipfMandelbrot::GetS() const
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800124{
125 return m_s;
126}
Alexander Afanasyev13800102012-12-25 00:30:31 -0800127
128void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129ConsumerZipfMandelbrot::SendPacket()
130{
131 if (!m_active)
132 return;
Shockb0f83152012-12-25 14:16:47 +0800133
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 NS_LOG_FUNCTION_NOARGS();
Shockb0f83152012-12-25 14:16:47 +0800135
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid
Shockb0f83152012-12-25 14:16:47 +0800137
Alexander Afanasyev13800102012-12-25 00:30:31 -0800138 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n";
Shockb0f83152012-12-25 14:16:47 +0800139
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800140 while (m_retxSeqs.size()) {
141 seq = *m_retxSeqs.begin();
142 m_retxSeqs.erase(m_retxSeqs.begin());
Shockb0f83152012-12-25 14:16:47 +0800143
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800144 // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ());
145 // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ())
146 // {
Shockb0f83152012-12-25 14:16:47 +0800147
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148 // NS_LOG_DEBUG ("Expire " << seq);
149 // m_seqLifetimes.erase (seq); // lifetime expired. Trying to find another unexpired
150 // sequence number
151 // continue;
152 // }
153 NS_LOG_DEBUG("=interest seq " << seq << " from m_retxSeqs");
154 break;
155 }
156
157 if (seq == std::numeric_limits<uint32_t>::max()) // no retransmission
158 {
159 if (m_seqMax != std::numeric_limits<uint32_t>::max()) {
160 if (m_seq >= m_seqMax) {
161 return; // we are totally done
162 }
Alexander Afanasyev13800102012-12-25 00:30:31 -0800163 }
Shockb0f83152012-12-25 14:16:47 +0800164
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800165 seq = ConsumerZipfMandelbrot::GetNextSeq();
166 m_seq++;
167 }
Shockb0f83152012-12-25 14:16:47 +0800168
Alexander Afanasyev13800102012-12-25 00:30:31 -0800169 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
Shockb0f83152012-12-25 14:16:47 +0800170
Alexander Afanasyev13800102012-12-25 00:30:31 -0800171 //
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700172 shared_ptr<Name> nameWithSequence = make_shared<Name>(m_interestName);
Mickey Sweatt89046c12014-11-16 20:32:27 -0800173 nameWithSequence->appendSequenceNumber(seq);
Alexander Afanasyev13800102012-12-25 00:30:31 -0800174 //
Shockb0f83152012-12-25 14:16:47 +0800175
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700176 shared_ptr<Interest> interest = make_shared<Interest>();
Mickey Sweatt89046c12014-11-16 20:32:27 -0800177 interest->setNonce(m_rand.GetValue());
178 interest->setName(*nameWithSequence);
Shockb0f83152012-12-25 14:16:47 +0800179
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700180 // NS_LOG_INFO ("Requesting Interest: \n" << *interest);
Mickey Sweatt89046c12014-11-16 20:32:27 -0800181 NS_LOG_INFO("> Interest for " << seq << ", Total: " << m_seq << ", face: " << m_face->getId());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800182 NS_LOG_DEBUG("Trying to add " << seq << " with " << Simulator::Now() << ". already "
183 << m_seqTimeouts.size() << " items");
Shockb0f83152012-12-25 14:16:47 +0800184
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800185 m_seqTimeouts.insert(SeqTimeout(seq, Simulator::Now()));
186 m_seqFullDelay.insert(SeqTimeout(seq, Simulator::Now()));
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800187
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800188 m_seqLastDelay.erase(seq);
189 m_seqLastDelay.insert(SeqTimeout(seq, Simulator::Now()));
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800190
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800191 m_seqRetxCounts[seq]++;
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800192
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800193 m_rtt->SentSeq(SequenceNumber32(seq), 1);
Shockb0f83152012-12-25 14:16:47 +0800194
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195 m_transmittedInterests(interest, this, m_face);
Mickey Sweatt89046c12014-11-16 20:32:27 -0800196 m_face->onReceiveInterest(*interest);
Shockb0f83152012-12-25 14:16:47 +0800197
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800198 ConsumerZipfMandelbrot::ScheduleNextPacket();
Shockb0f83152012-12-25 14:16:47 +0800199}
200
Shockb0f83152012-12-25 14:16:47 +0800201uint32_t
Alexander Afanasyev13800102012-12-25 00:30:31 -0800202ConsumerZipfMandelbrot::GetNextSeq()
203{
204 uint32_t content_index = 1; //[1, m_N]
205 double p_sum = 0;
Shockb0f83152012-12-25 14:16:47 +0800206
Alexander Afanasyev13800102012-12-25 00:30:31 -0800207 double p_random = m_SeqRng.GetValue();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800208 while (p_random == 0) {
209 p_random = m_SeqRng.GetValue();
210 }
211 // if (p_random == 0)
212 NS_LOG_LOGIC("p_random=" << p_random);
213 for (uint32_t i = 1; i <= m_N; i++) {
214 p_sum = m_Pcum[i]; // m_Pcum[i] = m_Pcum[i-1] + p[i], p[0] = 0; e.g.: p_cum[1] = p[1],
215 // p_cum[2] = p[1] + p[2]
216 if (p_random <= p_sum) {
217 content_index = i;
218 break;
219 } // if
220 } // for
221 // content_index = 1;
222 NS_LOG_DEBUG("RandomNumber=" << content_index);
Alexander Afanasyev13800102012-12-25 00:30:31 -0800223 return content_index;
Shockb0f83152012-12-25 14:16:47 +0800224}
225
226void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800227ConsumerZipfMandelbrot::ScheduleNextPacket()
228{
Shockb0f83152012-12-25 14:16:47 +0800229
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800230 if (m_firstTime) {
231 m_sendEvent = Simulator::Schedule(Seconds(0.0), &ConsumerZipfMandelbrot::SendPacket, this);
232 m_firstTime = false;
233 }
234 else if (!m_sendEvent.IsRunning())
235 m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency)
236 : Seconds(m_random->GetValue()),
237 &ConsumerZipfMandelbrot::SendPacket, this);
Shockb0f83152012-12-25 14:16:47 +0800238}
239
240} /* namespace ndn */
241} /* namespace ns3 */