blob: 000cc858af53bab57c077474bb0c1457316df443 [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
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 Afanasyev1a0fff62013-01-19 14:29:51 -080026
27#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
28
Shockb0f83152012-12-25 14:16:47 +080029#include <math.h>
Shockb0f83152012-12-25 14:16:47 +080030
31
32NS_LOG_COMPONENT_DEFINE ("ndn.ConsumerZipfMandelbrot");
33
34namespace ns3 {
35namespace ndn {
36
37NS_OBJECT_ENSURE_REGISTERED (ConsumerZipfMandelbrot);
38
39TypeId
40ConsumerZipfMandelbrot::GetTypeId (void)
41{
42 static TypeId tid = TypeId ("ns3::ndn::ConsumerZipfMandelbrot")
43 .SetGroupName ("Ndn")
44 .SetParent<ConsumerCbr> ()
45 .AddConstructor<ConsumerZipfMandelbrot> ()
Alexander Afanasyev13800102012-12-25 00:30:31 -080046
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080047 .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 Afanasyev13800102012-12-25 00:30:31 -080056 .AddAttribute ("NumberOfContents", "Number of the Contents in total",
57 StringValue ("100"),
58 MakeUintegerAccessor (&ConsumerZipfMandelbrot::SetNumberOfContents, &ConsumerZipfMandelbrot::GetNumberOfContents),
59 MakeUintegerChecker<uint32_t> ())
60
Shockb0f83152012-12-25 14:16:47 +080061 ;
62
63 return tid;
64}
65
66
67ConsumerZipfMandelbrot::ConsumerZipfMandelbrot()
Alexander Afanasyev13800102012-12-25 00:30:31 -080068 : m_q (0.7)
69 , m_s (0.7)
70 , m_SeqRng (0.0, 1.0)
Shockb0f83152012-12-25 14:16:47 +080071{
Alexander Afanasyev13800102012-12-25 00:30:31 -080072 // SetNumberOfContents is called by NS-3 object system during the initialization
Shockb0f83152012-12-25 14:16:47 +080073}
74
Alexander Afanasyev13800102012-12-25 00:30:31 -080075ConsumerZipfMandelbrot::~ConsumerZipfMandelbrot()
76{
Shockb0f83152012-12-25 14:16:47 +080077}
78
79void
Alexander Afanasyev13800102012-12-25 00:30:31 -080080ConsumerZipfMandelbrot::SetNumberOfContents (uint32_t numOfContents)
81{
82 m_N = numOfContents;
83
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080084 // NS_LOG_DEBUG (m_q << " and " << m_s << " and " << m_N);
85
Alexander Afanasyev13800102012-12-25 00:30:31 -080086 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 Tarnoi5cd9a152013-02-15 15:59:15 -080091 m_Pcum[i] = m_Pcum[i-1] + 1.0 / std::pow(i+m_q, m_s);
Alexander Afanasyev13800102012-12-25 00:30:31 -080092 }
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080093
Alexander Afanasyev13800102012-12-25 00:30:31 -080094 for (uint32_t i=1; i<=m_N; i++)
95 {
96 m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N];
Saran Tarnoi5cd9a152013-02-15 15:59:15 -080097 NS_LOG_LOGIC ("Cumulative probability [" << i << "]=" << m_Pcum[i]);
Alexander Afanasyev13800102012-12-25 00:30:31 -080098 }
99}
100
101uint32_t
102ConsumerZipfMandelbrot::GetNumberOfContents () const
103{
104 return m_N;
105}
106
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800107void
108ConsumerZipfMandelbrot::SetQ (double q)
109{
110 m_q = q;
111 SetNumberOfContents (m_N);
112}
113
114double
115ConsumerZipfMandelbrot::GetQ () const
116{
117 return m_q;
118}
119
120void
121ConsumerZipfMandelbrot::SetS (double s)
122{
123 m_s = s;
124 SetNumberOfContents (m_N);
125}
126
127double
128ConsumerZipfMandelbrot::GetS () const
129{
130 return m_s;
131}
Alexander Afanasyev13800102012-12-25 00:30:31 -0800132
133void
Shockb0f83152012-12-25 14:16:47 +0800134ConsumerZipfMandelbrot::SendPacket() {
Alexander Afanasyev13800102012-12-25 00:30:31 -0800135 if (!m_active) return;
Shockb0f83152012-12-25 14:16:47 +0800136
Alexander Afanasyev13800102012-12-25 00:30:31 -0800137 NS_LOG_FUNCTION_NOARGS ();
Shockb0f83152012-12-25 14:16:47 +0800138
Alexander Afanasyev13800102012-12-25 00:30:31 -0800139 uint32_t seq=std::numeric_limits<uint32_t>::max (); //invalid
Shockb0f83152012-12-25 14:16:47 +0800140
Alexander Afanasyev13800102012-12-25 00:30:31 -0800141 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n";
Shockb0f83152012-12-25 14:16:47 +0800142
Alexander Afanasyev13800102012-12-25 00:30:31 -0800143 while (m_retxSeqs.size ())
144 {
145 seq = *m_retxSeqs.begin ();
146 m_retxSeqs.erase (m_retxSeqs.begin ());
Shockb0f83152012-12-25 14:16:47 +0800147
Alexander Afanasyev13800102012-12-25 00:30:31 -0800148 // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ());
149 // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ())
150 // {
Shockb0f83152012-12-25 14:16:47 +0800151
Alexander Afanasyev13800102012-12-25 00:30:31 -0800152 // 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 }
Shockb0f83152012-12-25 14:16:47 +0800159
Alexander Afanasyev13800102012-12-25 00:30:31 -0800160 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 }
Shockb0f83152012-12-25 14:16:47 +0800169
Alexander Afanasyev13800102012-12-25 00:30:31 -0800170 seq = ConsumerZipfMandelbrot::GetNextSeq();
171 m_seq ++;
172 }
Shockb0f83152012-12-25 14:16:47 +0800173
Alexander Afanasyev13800102012-12-25 00:30:31 -0800174 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
Shockb0f83152012-12-25 14:16:47 +0800175
Alexander Afanasyev13800102012-12-25 00:30:31 -0800176 //
177 Ptr<NameComponents> nameWithSequence = Create<NameComponents> (m_interestName);
178 (*nameWithSequence) (seq);
179 //
Shockb0f83152012-12-25 14:16:47 +0800180
Alexander Afanasyev13800102012-12-25 00:30:31 -0800181 InterestHeader interestHeader;
182 interestHeader.SetNonce (m_rand.GetValue ());
183 interestHeader.SetName (nameWithSequence);
Shockb0f83152012-12-25 14:16:47 +0800184
Alexander Afanasyev13800102012-12-25 00:30:31 -0800185 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
186 NS_LOG_INFO ("> Interest for " << seq<<", Total: "<<m_seq<<", face: "<<m_face->GetId());
Shockb0f83152012-12-25 14:16:47 +0800187
Alexander Afanasyev13800102012-12-25 00:30:31 -0800188 Ptr<Packet> packet = Create<Packet> ();
Shockb0f83152012-12-25 14:16:47 +0800189
Alexander Afanasyev13800102012-12-25 00:30:31 -0800190 //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 ());
Shockb0f83152012-12-25 14:16:47 +0800193
Alexander Afanasyev13800102012-12-25 00:30:31 -0800194 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
Shockb0f83152012-12-25 14:16:47 +0800195
Alexander Afanasyev13800102012-12-25 00:30:31 -0800196 m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800197 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 Tarnoi5cd9a152013-02-15 15:59:15 -0800203
Alexander Afanasyev13800102012-12-25 00:30:31 -0800204 m_transmittedInterests (&interestHeader, this, m_face);
Shockb0f83152012-12-25 14:16:47 +0800205
Alexander Afanasyev13800102012-12-25 00:30:31 -0800206 m_rtt->SentSeq (SequenceNumber32 (seq), 1);
Shockb0f83152012-12-25 14:16:47 +0800207
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800208 FwHopCountTag hopCountTag;
209 packet->AddPacketTag (hopCountTag);
Saran Tarnoi5cd9a152013-02-15 15:59:15 -0800210
Alexander Afanasyev13800102012-12-25 00:30:31 -0800211 m_protocolHandler (packet);
Shockb0f83152012-12-25 14:16:47 +0800212
Alexander Afanasyev13800102012-12-25 00:30:31 -0800213 ConsumerZipfMandelbrot::ScheduleNextPacket ();
Shockb0f83152012-12-25 14:16:47 +0800214}
215
216
217uint32_t
Alexander Afanasyev13800102012-12-25 00:30:31 -0800218ConsumerZipfMandelbrot::GetNextSeq()
219{
220 uint32_t content_index = 1; //[1, m_N]
221 double p_sum = 0;
Shockb0f83152012-12-25 14:16:47 +0800222
Alexander Afanasyev13800102012-12-25 00:30:31 -0800223 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;
Shockb0f83152012-12-25 14:16:47 +0800242}
243
244void
245ConsumerZipfMandelbrot::ScheduleNextPacket() {
246
Alexander Afanasyev13800102012-12-25 00:30:31 -0800247 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);
Shockb0f83152012-12-25 14:16:47 +0800260}
261
262} /* namespace ndn */
263} /* namespace ns3 */