blob: 8fd1591516519fab0c18cd440afee3b9dc7ab2b7 [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
Shockb0f83152012-12-25 14:16:47 +080024#include <math.h>
Shockb0f83152012-12-25 14:16:47 +080025
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080026NS_LOG_COMPONENT_DEFINE("ndn.ConsumerZipfMandelbrot");
Shockb0f83152012-12-25 14:16:47 +080027
28namespace ns3 {
29namespace ndn {
30
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080031NS_OBJECT_ENSURE_REGISTERED(ConsumerZipfMandelbrot);
Shockb0f83152012-12-25 14:16:47 +080032
33TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034ConsumerZipfMandelbrot::GetTypeId(void)
Shockb0f83152012-12-25 14:16:47 +080035{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036 static TypeId tid =
37 TypeId("ns3::ndn::ConsumerZipfMandelbrot")
38 .SetGroupName("Ndn")
39 .SetParent<ConsumerCbr>()
40 .AddConstructor<ConsumerZipfMandelbrot>()
Alexander Afanasyev13800102012-12-25 00:30:31 -080041
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042 .AddAttribute("NumberOfContents", "Number of the Contents in total", StringValue("100"),
43 MakeUintegerAccessor(&ConsumerZipfMandelbrot::SetNumberOfContents,
44 &ConsumerZipfMandelbrot::GetNumberOfContents),
45 MakeUintegerChecker<uint32_t>())
Alexander Afanasyev13800102012-12-25 00:30:31 -080046
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047 .AddAttribute("q", "parameter of improve rank", StringValue("0.7"),
48 MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetQ,
49 &ConsumerZipfMandelbrot::GetQ),
50 MakeDoubleChecker<double>())
51
52 .AddAttribute("s", "parameter of power", StringValue("0.7"),
53 MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetS,
54 &ConsumerZipfMandelbrot::GetS),
55 MakeDoubleChecker<double>());
Shockb0f83152012-12-25 14:16:47 +080056
57 return tid;
58}
59
Shockb0f83152012-12-25 14:16:47 +080060ConsumerZipfMandelbrot::ConsumerZipfMandelbrot()
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 : m_N(100) // needed here to make sure when SetQ/SetS are called, there is a valid value of N
62 , m_q(0.7)
63 , m_s(0.7)
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070064 , m_seqRng(CreateObject<UniformRandomVariable>())
Shockb0f83152012-12-25 14:16:47 +080065{
Alexander Afanasyev13800102012-12-25 00:30:31 -080066 // SetNumberOfContents is called by NS-3 object system during the initialization
Shockb0f83152012-12-25 14:16:47 +080067}
68
Alexander Afanasyev13800102012-12-25 00:30:31 -080069ConsumerZipfMandelbrot::~ConsumerZipfMandelbrot()
70{
Shockb0f83152012-12-25 14:16:47 +080071}
72
73void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074ConsumerZipfMandelbrot::SetNumberOfContents(uint32_t numOfContents)
Alexander Afanasyev13800102012-12-25 00:30:31 -080075{
76 m_N = numOfContents;
77
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 NS_LOG_DEBUG(m_q << " and " << m_s << " and " << m_N);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080079
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 m_Pcum = std::vector<double>(m_N + 1);
Alexander Afanasyev13800102012-12-25 00:30:31 -080081
82 m_Pcum[0] = 0.0;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083 for (uint32_t i = 1; i <= m_N; i++) {
84 m_Pcum[i] = m_Pcum[i - 1] + 1.0 / std::pow(i + m_q, m_s);
85 }
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080086
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 for (uint32_t i = 1; i <= m_N; i++) {
88 m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N];
89 NS_LOG_LOGIC("Cumulative probability [" << i << "]=" << m_Pcum[i]);
Alexander Afanasyev13800102012-12-25 00:30:31 -080090 }
91}
92
93uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094ConsumerZipfMandelbrot::GetNumberOfContents() const
Alexander Afanasyev13800102012-12-25 00:30:31 -080095{
96 return m_N;
97}
98
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080099void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100ConsumerZipfMandelbrot::SetQ(double q)
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800101{
102 m_q = q;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 SetNumberOfContents(m_N);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800104}
105
106double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107ConsumerZipfMandelbrot::GetQ() const
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800108{
109 return m_q;
110}
111
112void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113ConsumerZipfMandelbrot::SetS(double s)
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800114{
115 m_s = s;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 SetNumberOfContents(m_N);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800117}
118
119double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120ConsumerZipfMandelbrot::GetS() const
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800121{
122 return m_s;
123}
Alexander Afanasyev13800102012-12-25 00:30:31 -0800124
125void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126ConsumerZipfMandelbrot::SendPacket()
127{
128 if (!m_active)
129 return;
Shockb0f83152012-12-25 14:16:47 +0800130
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 NS_LOG_FUNCTION_NOARGS();
Shockb0f83152012-12-25 14:16:47 +0800132
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133 uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid
Shockb0f83152012-12-25 14:16:47 +0800134
Alexander Afanasyev13800102012-12-25 00:30:31 -0800135 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n";
Shockb0f83152012-12-25 14:16:47 +0800136
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 while (m_retxSeqs.size()) {
138 seq = *m_retxSeqs.begin();
139 m_retxSeqs.erase(m_retxSeqs.begin());
Shockb0f83152012-12-25 14:16:47 +0800140
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800141 // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ());
142 // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ())
143 // {
Shockb0f83152012-12-25 14:16:47 +0800144
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 // NS_LOG_DEBUG ("Expire " << seq);
146 // m_seqLifetimes.erase (seq); // lifetime expired. Trying to find another unexpired
147 // sequence number
148 // continue;
149 // }
150 NS_LOG_DEBUG("=interest seq " << seq << " from m_retxSeqs");
151 break;
152 }
153
154 if (seq == std::numeric_limits<uint32_t>::max()) // no retransmission
155 {
156 if (m_seqMax != std::numeric_limits<uint32_t>::max()) {
157 if (m_seq >= m_seqMax) {
158 return; // we are totally done
159 }
Alexander Afanasyev13800102012-12-25 00:30:31 -0800160 }
Shockb0f83152012-12-25 14:16:47 +0800161
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800162 seq = ConsumerZipfMandelbrot::GetNextSeq();
163 m_seq++;
164 }
Shockb0f83152012-12-25 14:16:47 +0800165
Alexander Afanasyev13800102012-12-25 00:30:31 -0800166 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
Shockb0f83152012-12-25 14:16:47 +0800167
Alexander Afanasyev13800102012-12-25 00:30:31 -0800168 //
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700169 shared_ptr<Name> nameWithSequence = make_shared<Name>(m_interestName);
Mickey Sweatt89046c12014-11-16 20:32:27 -0800170 nameWithSequence->appendSequenceNumber(seq);
Alexander Afanasyev13800102012-12-25 00:30:31 -0800171 //
Shockb0f83152012-12-25 14:16:47 +0800172
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700173 shared_ptr<Interest> interest = make_shared<Interest>();
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700174 interest->setNonce(m_rand->GetValue(0, std::numeric_limits<uint32_t>::max()));
Mickey Sweatt89046c12014-11-16 20:32:27 -0800175 interest->setName(*nameWithSequence);
Shockb0f83152012-12-25 14:16:47 +0800176
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700177 // NS_LOG_INFO ("Requesting Interest: \n" << *interest);
Mickey Sweatt89046c12014-11-16 20:32:27 -0800178 NS_LOG_INFO("> Interest for " << seq << ", Total: " << m_seq << ", face: " << m_face->getId());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800179 NS_LOG_DEBUG("Trying to add " << seq << " with " << Simulator::Now() << ". already "
180 << m_seqTimeouts.size() << " items");
Shockb0f83152012-12-25 14:16:47 +0800181
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800182 m_seqTimeouts.insert(SeqTimeout(seq, Simulator::Now()));
183 m_seqFullDelay.insert(SeqTimeout(seq, Simulator::Now()));
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800184
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800185 m_seqLastDelay.erase(seq);
186 m_seqLastDelay.insert(SeqTimeout(seq, Simulator::Now()));
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800187
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800188 m_seqRetxCounts[seq]++;
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800189
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800190 m_rtt->SentSeq(SequenceNumber32(seq), 1);
Shockb0f83152012-12-25 14:16:47 +0800191
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800192 m_transmittedInterests(interest, this, m_face);
Alexander Afanasyev50ea1a32016-09-08 15:44:08 -0700193 m_appLink->onReceiveInterest(*interest);
Shockb0f83152012-12-25 14:16:47 +0800194
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195 ConsumerZipfMandelbrot::ScheduleNextPacket();
Shockb0f83152012-12-25 14:16:47 +0800196}
197
Shockb0f83152012-12-25 14:16:47 +0800198uint32_t
Alexander Afanasyev13800102012-12-25 00:30:31 -0800199ConsumerZipfMandelbrot::GetNextSeq()
200{
201 uint32_t content_index = 1; //[1, m_N]
202 double p_sum = 0;
Shockb0f83152012-12-25 14:16:47 +0800203
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700204 double p_random = m_seqRng->GetValue();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800205 while (p_random == 0) {
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700206 p_random = m_seqRng->GetValue();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800207 }
208 // if (p_random == 0)
209 NS_LOG_LOGIC("p_random=" << p_random);
210 for (uint32_t i = 1; i <= m_N; i++) {
211 p_sum = m_Pcum[i]; // m_Pcum[i] = m_Pcum[i-1] + p[i], p[0] = 0; e.g.: p_cum[1] = p[1],
212 // p_cum[2] = p[1] + p[2]
213 if (p_random <= p_sum) {
214 content_index = i;
215 break;
216 } // if
217 } // for
218 // content_index = 1;
219 NS_LOG_DEBUG("RandomNumber=" << content_index);
Alexander Afanasyev13800102012-12-25 00:30:31 -0800220 return content_index;
Shockb0f83152012-12-25 14:16:47 +0800221}
222
223void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800224ConsumerZipfMandelbrot::ScheduleNextPacket()
225{
Shockb0f83152012-12-25 14:16:47 +0800226
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800227 if (m_firstTime) {
228 m_sendEvent = Simulator::Schedule(Seconds(0.0), &ConsumerZipfMandelbrot::SendPacket, this);
229 m_firstTime = false;
230 }
231 else if (!m_sendEvent.IsRunning())
232 m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency)
233 : Seconds(m_random->GetValue()),
234 &ConsumerZipfMandelbrot::SendPacket, this);
Shockb0f83152012-12-25 14:16:47 +0800235}
236
237} /* namespace ndn */
238} /* namespace ns3 */