blob: 806b8e3da4fcf93512ac0a656e0539f261e631bc [file] [log] [blame]
Shockb0f83152012-12-25 14:16:47 +08001/* -*- 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
Alexander Afanasyev0c395372014-12-20 15:54:02 -080021#include "ndn-consumer-zipf-mandelbrot.hpp"
Shockb0f83152012-12-25 14:16:47 +080022
Alexander Afanasyev0c395372014-12-20 15:54:02 -080023#include "ns3/ndn-app-face.hpp"
24#include "ns3/ndn-interest.hpp"
25#include "ns3/ndn-data.hpp"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080026
Alexander Afanasyev0c395372014-12-20 15:54:02 -080027#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.hpp"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080028
Shockb0f83152012-12-25 14:16:47 +080029#include <math.h>
Shockb0f83152012-12-25 14:16:47 +080030
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080031NS_LOG_COMPONENT_DEFINE("ndn.ConsumerZipfMandelbrot");
Shockb0f83152012-12-25 14:16:47 +080032
33namespace ns3 {
34namespace ndn {
35
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036NS_OBJECT_ENSURE_REGISTERED(ConsumerZipfMandelbrot);
Shockb0f83152012-12-25 14:16:47 +080037
38TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039ConsumerZipfMandelbrot::GetTypeId(void)
Shockb0f83152012-12-25 14:16:47 +080040{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041 static TypeId tid =
42 TypeId("ns3::ndn::ConsumerZipfMandelbrot")
43 .SetGroupName("Ndn")
44 .SetParent<ConsumerCbr>()
45 .AddConstructor<ConsumerZipfMandelbrot>()
Alexander Afanasyev13800102012-12-25 00:30:31 -080046
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047 .AddAttribute("NumberOfContents", "Number of the Contents in total", StringValue("100"),
48 MakeUintegerAccessor(&ConsumerZipfMandelbrot::SetNumberOfContents,
49 &ConsumerZipfMandelbrot::GetNumberOfContents),
50 MakeUintegerChecker<uint32_t>())
Alexander Afanasyev13800102012-12-25 00:30:31 -080051
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052 .AddAttribute("q", "parameter of improve rank", StringValue("0.7"),
53 MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetQ,
54 &ConsumerZipfMandelbrot::GetQ),
55 MakeDoubleChecker<double>())
56
57 .AddAttribute("s", "parameter of power", StringValue("0.7"),
58 MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetS,
59 &ConsumerZipfMandelbrot::GetS),
60 MakeDoubleChecker<double>());
Shockb0f83152012-12-25 14:16:47 +080061
62 return tid;
63}
64
Shockb0f83152012-12-25 14:16:47 +080065ConsumerZipfMandelbrot::ConsumerZipfMandelbrot()
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 : m_N(100) // needed here to make sure when SetQ/SetS are called, there is a valid value of N
67 , m_q(0.7)
68 , m_s(0.7)
69 , m_SeqRng(0.0, 1.0)
Shockb0f83152012-12-25 14:16:47 +080070{
Alexander Afanasyev13800102012-12-25 00:30:31 -080071 // SetNumberOfContents is called by NS-3 object system during the initialization
Shockb0f83152012-12-25 14:16:47 +080072}
73
Alexander Afanasyev13800102012-12-25 00:30:31 -080074ConsumerZipfMandelbrot::~ConsumerZipfMandelbrot()
75{
Shockb0f83152012-12-25 14:16:47 +080076}
77
78void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079ConsumerZipfMandelbrot::SetNumberOfContents(uint32_t numOfContents)
Alexander Afanasyev13800102012-12-25 00:30:31 -080080{
81 m_N = numOfContents;
82
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083 NS_LOG_DEBUG(m_q << " and " << m_s << " and " << m_N);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080084
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 m_Pcum = std::vector<double>(m_N + 1);
Alexander Afanasyev13800102012-12-25 00:30:31 -080086
87 m_Pcum[0] = 0.0;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 for (uint32_t i = 1; i <= m_N; i++) {
89 m_Pcum[i] = m_Pcum[i - 1] + 1.0 / std::pow(i + m_q, m_s);
90 }
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080091
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 for (uint32_t i = 1; i <= m_N; i++) {
93 m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N];
94 NS_LOG_LOGIC("Cumulative probability [" << i << "]=" << m_Pcum[i]);
Alexander Afanasyev13800102012-12-25 00:30:31 -080095 }
96}
97
98uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099ConsumerZipfMandelbrot::GetNumberOfContents() const
Alexander Afanasyev13800102012-12-25 00:30:31 -0800100{
101 return m_N;
102}
103
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800104void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105ConsumerZipfMandelbrot::SetQ(double q)
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800106{
107 m_q = q;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 SetNumberOfContents(m_N);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800109}
110
111double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112ConsumerZipfMandelbrot::GetQ() const
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800113{
114 return m_q;
115}
116
117void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118ConsumerZipfMandelbrot::SetS(double s)
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800119{
120 m_s = s;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 SetNumberOfContents(m_N);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800122}
123
124double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125ConsumerZipfMandelbrot::GetS() const
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800126{
127 return m_s;
128}
Alexander Afanasyev13800102012-12-25 00:30:31 -0800129
130void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131ConsumerZipfMandelbrot::SendPacket()
132{
133 if (!m_active)
134 return;
Shockb0f83152012-12-25 14:16:47 +0800135
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 NS_LOG_FUNCTION_NOARGS();
Shockb0f83152012-12-25 14:16:47 +0800137
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800138 uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid
Shockb0f83152012-12-25 14:16:47 +0800139
Alexander Afanasyev13800102012-12-25 00:30:31 -0800140 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n";
Shockb0f83152012-12-25 14:16:47 +0800141
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 while (m_retxSeqs.size()) {
143 seq = *m_retxSeqs.begin();
144 m_retxSeqs.erase(m_retxSeqs.begin());
Shockb0f83152012-12-25 14:16:47 +0800145
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146 // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ());
147 // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ())
148 // {
Shockb0f83152012-12-25 14:16:47 +0800149
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800150 // NS_LOG_DEBUG ("Expire " << seq);
151 // m_seqLifetimes.erase (seq); // lifetime expired. Trying to find another unexpired
152 // sequence number
153 // continue;
154 // }
155 NS_LOG_DEBUG("=interest seq " << seq << " from m_retxSeqs");
156 break;
157 }
158
159 if (seq == std::numeric_limits<uint32_t>::max()) // no retransmission
160 {
161 if (m_seqMax != std::numeric_limits<uint32_t>::max()) {
162 if (m_seq >= m_seqMax) {
163 return; // we are totally done
164 }
Alexander Afanasyev13800102012-12-25 00:30:31 -0800165 }
Shockb0f83152012-12-25 14:16:47 +0800166
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800167 seq = ConsumerZipfMandelbrot::GetNextSeq();
168 m_seq++;
169 }
Shockb0f83152012-12-25 14:16:47 +0800170
Alexander Afanasyev13800102012-12-25 00:30:31 -0800171 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
Shockb0f83152012-12-25 14:16:47 +0800172
Alexander Afanasyev13800102012-12-25 00:30:31 -0800173 //
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800174 Ptr<Name> nameWithSequence = Create<Name>(m_interestName);
175 nameWithSequence->appendSeqNum(seq);
Alexander Afanasyev13800102012-12-25 00:30:31 -0800176 //
Shockb0f83152012-12-25 14:16:47 +0800177
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800178 Ptr<Interest> interest = Create<Interest>();
179 interest->SetNonce(m_rand.GetValue());
180 interest->SetName(nameWithSequence);
Shockb0f83152012-12-25 14:16:47 +0800181
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700182 // NS_LOG_INFO ("Requesting Interest: \n" << *interest);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800183 NS_LOG_INFO("> Interest for " << seq << ", Total: " << m_seq << ", face: " << m_face->GetId());
184 NS_LOG_DEBUG("Trying to add " << seq << " with " << Simulator::Now() << ". already "
185 << m_seqTimeouts.size() << " items");
Shockb0f83152012-12-25 14:16:47 +0800186
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800187 m_seqTimeouts.insert(SeqTimeout(seq, Simulator::Now()));
188 m_seqFullDelay.insert(SeqTimeout(seq, Simulator::Now()));
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800189
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800190 m_seqLastDelay.erase(seq);
191 m_seqLastDelay.insert(SeqTimeout(seq, Simulator::Now()));
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800192
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800193 m_seqRetxCounts[seq]++;
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800194
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195 m_rtt->SentSeq(SequenceNumber32(seq), 1);
Shockb0f83152012-12-25 14:16:47 +0800196
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800197 FwHopCountTag hopCountTag;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800198 interest->GetPayload()->AddPacketTag(hopCountTag);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800199
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800200 m_transmittedInterests(interest, this, m_face);
201 m_face->ReceiveInterest(interest);
Shockb0f83152012-12-25 14:16:47 +0800202
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203 ConsumerZipfMandelbrot::ScheduleNextPacket();
Shockb0f83152012-12-25 14:16:47 +0800204}
205
Shockb0f83152012-12-25 14:16:47 +0800206uint32_t
Alexander Afanasyev13800102012-12-25 00:30:31 -0800207ConsumerZipfMandelbrot::GetNextSeq()
208{
209 uint32_t content_index = 1; //[1, m_N]
210 double p_sum = 0;
Shockb0f83152012-12-25 14:16:47 +0800211
Alexander Afanasyev13800102012-12-25 00:30:31 -0800212 double p_random = m_SeqRng.GetValue();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213 while (p_random == 0) {
214 p_random = m_SeqRng.GetValue();
215 }
216 // if (p_random == 0)
217 NS_LOG_LOGIC("p_random=" << p_random);
218 for (uint32_t i = 1; i <= m_N; i++) {
219 p_sum = m_Pcum[i]; // m_Pcum[i] = m_Pcum[i-1] + p[i], p[0] = 0; e.g.: p_cum[1] = p[1],
220 // p_cum[2] = p[1] + p[2]
221 if (p_random <= p_sum) {
222 content_index = i;
223 break;
224 } // if
225 } // for
226 // content_index = 1;
227 NS_LOG_DEBUG("RandomNumber=" << content_index);
Alexander Afanasyev13800102012-12-25 00:30:31 -0800228 return content_index;
Shockb0f83152012-12-25 14:16:47 +0800229}
230
231void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800232ConsumerZipfMandelbrot::ScheduleNextPacket()
233{
Shockb0f83152012-12-25 14:16:47 +0800234
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800235 if (m_firstTime) {
236 m_sendEvent = Simulator::Schedule(Seconds(0.0), &ConsumerZipfMandelbrot::SendPacket, this);
237 m_firstTime = false;
238 }
239 else if (!m_sendEvent.IsRunning())
240 m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency)
241 : Seconds(m_random->GetValue()),
242 &ConsumerZipfMandelbrot::SendPacket, this);
Shockb0f83152012-12-25 14:16:47 +0800243}
244
245} /* namespace ndn */
246} /* namespace ns3 */