blob: c0e84d81643272076f8ccb15bd37df083de49571 [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"
26#include <math.h>
Shockb0f83152012-12-25 14:16:47 +080027
28
29NS_LOG_COMPONENT_DEFINE ("ndn.ConsumerZipfMandelbrot");
30
31namespace ns3 {
32namespace ndn {
33
34NS_OBJECT_ENSURE_REGISTERED (ConsumerZipfMandelbrot);
35
36TypeId
37ConsumerZipfMandelbrot::GetTypeId (void)
38{
39 static TypeId tid = TypeId ("ns3::ndn::ConsumerZipfMandelbrot")
40 .SetGroupName ("Ndn")
41 .SetParent<ConsumerCbr> ()
42 .AddConstructor<ConsumerZipfMandelbrot> ()
Alexander Afanasyev13800102012-12-25 00:30:31 -080043
44 .AddAttribute ("NumberOfContents", "Number of the Contents in total",
45 StringValue ("100"),
46 MakeUintegerAccessor (&ConsumerZipfMandelbrot::SetNumberOfContents, &ConsumerZipfMandelbrot::GetNumberOfContents),
47 MakeUintegerChecker<uint32_t> ())
48
49 // Alex: q and s are not yet really working
50 //
51 // .AddAttribute ("q", "parameter of improve rank",
52 // StringValue ("0.7"),
53 // MakeDoubleAccessor (&ConsumerZipfMandelbrot::m_q),
54 // MakeDoubleChecker<double>())
55 // .AddAttribute ("s", "parameter of power",
56 // StringValue ("0.7"),
57 // MakeDoubleAccessor (&ConsumerZipfMandelbrot::m_s),
58 // MakeDoubleChecker<double>())
Shockb0f83152012-12-25 14:16:47 +080059 ;
60
61 return tid;
62}
63
64
65ConsumerZipfMandelbrot::ConsumerZipfMandelbrot()
Alexander Afanasyev13800102012-12-25 00:30:31 -080066 : m_q (0.7)
67 , m_s (0.7)
68 , m_SeqRng (0.0, 1.0)
Shockb0f83152012-12-25 14:16:47 +080069{
Alexander Afanasyev13800102012-12-25 00:30:31 -080070 // SetNumberOfContents is called by NS-3 object system during the initialization
Shockb0f83152012-12-25 14:16:47 +080071}
72
Alexander Afanasyev13800102012-12-25 00:30:31 -080073ConsumerZipfMandelbrot::~ConsumerZipfMandelbrot()
74{
Shockb0f83152012-12-25 14:16:47 +080075}
76
77void
Alexander Afanasyev13800102012-12-25 00:30:31 -080078ConsumerZipfMandelbrot::SetNumberOfContents (uint32_t numOfContents)
79{
80 m_N = numOfContents;
81
82 m_Pcum = std::vector<double> (m_N + 1);
83
84 m_Pcum[0] = 0.0;
85 for (uint32_t i=1; i<=m_N; i++)
86 {
87 m_Pcum[i] = m_Pcum[i-1] + 1.0/pow(i+m_q, m_s);
88 }
89
90 for (uint32_t i=1; i<=m_N; i++)
91 {
92 m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N];
93 NS_LOG_LOGIC("cum Probability ["<<i<<"]="<<m_Pcum[i]);
94 }
95}
96
97uint32_t
98ConsumerZipfMandelbrot::GetNumberOfContents () const
99{
100 return m_N;
101}
102
103
104void
Shockb0f83152012-12-25 14:16:47 +0800105ConsumerZipfMandelbrot::SendPacket() {
Alexander Afanasyev13800102012-12-25 00:30:31 -0800106 if (!m_active) return;
Shockb0f83152012-12-25 14:16:47 +0800107
Alexander Afanasyev13800102012-12-25 00:30:31 -0800108 NS_LOG_FUNCTION_NOARGS ();
Shockb0f83152012-12-25 14:16:47 +0800109
Alexander Afanasyev13800102012-12-25 00:30:31 -0800110 uint32_t seq=std::numeric_limits<uint32_t>::max (); //invalid
Shockb0f83152012-12-25 14:16:47 +0800111
Alexander Afanasyev13800102012-12-25 00:30:31 -0800112 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n";
Shockb0f83152012-12-25 14:16:47 +0800113
Alexander Afanasyev13800102012-12-25 00:30:31 -0800114 while (m_retxSeqs.size ())
115 {
116 seq = *m_retxSeqs.begin ();
117 m_retxSeqs.erase (m_retxSeqs.begin ());
Shockb0f83152012-12-25 14:16:47 +0800118
Alexander Afanasyev13800102012-12-25 00:30:31 -0800119 // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ());
120 // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ())
121 // {
Shockb0f83152012-12-25 14:16:47 +0800122
Alexander Afanasyev13800102012-12-25 00:30:31 -0800123 // NS_LOG_DEBUG ("Expire " << seq);
124 // m_seqLifetimes.erase (seq); // lifetime expired. Trying to find another unexpired sequence number
125 // continue;
126 // }
127 NS_LOG_DEBUG("=interest seq "<<seq<<" from m_retxSeqs");
128 break;
129 }
Shockb0f83152012-12-25 14:16:47 +0800130
Alexander Afanasyev13800102012-12-25 00:30:31 -0800131 if (seq == std::numeric_limits<uint32_t>::max ()) //no retransmission
132 {
133 if (m_seqMax != std::numeric_limits<uint32_t>::max ())
134 {
135 if (m_seq >= m_seqMax)
136 {
137 return; // we are totally done
138 }
139 }
Shockb0f83152012-12-25 14:16:47 +0800140
Alexander Afanasyev13800102012-12-25 00:30:31 -0800141 seq = ConsumerZipfMandelbrot::GetNextSeq();
142 m_seq ++;
143 }
Shockb0f83152012-12-25 14:16:47 +0800144
Alexander Afanasyev13800102012-12-25 00:30:31 -0800145 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
Shockb0f83152012-12-25 14:16:47 +0800146
Alexander Afanasyev13800102012-12-25 00:30:31 -0800147 //
148 Ptr<NameComponents> nameWithSequence = Create<NameComponents> (m_interestName);
149 (*nameWithSequence) (seq);
150 //
Shockb0f83152012-12-25 14:16:47 +0800151
Alexander Afanasyev13800102012-12-25 00:30:31 -0800152 InterestHeader interestHeader;
153 interestHeader.SetNonce (m_rand.GetValue ());
154 interestHeader.SetName (nameWithSequence);
Shockb0f83152012-12-25 14:16:47 +0800155
Alexander Afanasyev13800102012-12-25 00:30:31 -0800156 // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
157 NS_LOG_INFO ("> Interest for " << seq<<", Total: "<<m_seq<<", face: "<<m_face->GetId());
Shockb0f83152012-12-25 14:16:47 +0800158
Alexander Afanasyev13800102012-12-25 00:30:31 -0800159 Ptr<Packet> packet = Create<Packet> ();
Shockb0f83152012-12-25 14:16:47 +0800160
Alexander Afanasyev13800102012-12-25 00:30:31 -0800161 //NS_LOG_DEBUG ("= Interest for " << seq<<", Total: "<<m_seq<<", face: "<<m_face->GetId());
162 packet->AddHeader (interestHeader);
163 //NS_LOG_DEBUG ("Interest packet size: " << packet->GetSize ());
Shockb0f83152012-12-25 14:16:47 +0800164
Alexander Afanasyev13800102012-12-25 00:30:31 -0800165 NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
Shockb0f83152012-12-25 14:16:47 +0800166
Alexander Afanasyev13800102012-12-25 00:30:31 -0800167 m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800168 m_seqFullDelay.insert (SeqTimeout (seq, Simulator::Now ()));
169
170 m_seqLastDelay.erase (seq);
171 m_seqLastDelay.insert (SeqTimeout (seq, Simulator::Now ()));
172
173 m_seqRetxCounts[seq] ++;
174
Alexander Afanasyev13800102012-12-25 00:30:31 -0800175 m_transmittedInterests (&interestHeader, this, m_face);
Shockb0f83152012-12-25 14:16:47 +0800176
Alexander Afanasyev13800102012-12-25 00:30:31 -0800177 m_rtt->SentSeq (SequenceNumber32 (seq), 1);
Shockb0f83152012-12-25 14:16:47 +0800178
Alexander Afanasyev13800102012-12-25 00:30:31 -0800179 m_protocolHandler (packet);
Shockb0f83152012-12-25 14:16:47 +0800180
Alexander Afanasyev13800102012-12-25 00:30:31 -0800181 ConsumerZipfMandelbrot::ScheduleNextPacket ();
Shockb0f83152012-12-25 14:16:47 +0800182}
183
184
185uint32_t
Alexander Afanasyev13800102012-12-25 00:30:31 -0800186ConsumerZipfMandelbrot::GetNextSeq()
187{
188 uint32_t content_index = 1; //[1, m_N]
189 double p_sum = 0;
Shockb0f83152012-12-25 14:16:47 +0800190
Alexander Afanasyev13800102012-12-25 00:30:31 -0800191 double p_random = m_SeqRng.GetValue();
192 while (p_random == 0)
193 {
194 p_random = m_SeqRng.GetValue();
195 }
196 //if (p_random == 0)
197 NS_LOG_LOGIC("p_random="<<p_random);
198 for (uint32_t i=1; i<=m_N; i++)
199 {
200 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]
201 if (p_random <= p_sum)
202 {
203 content_index = i;
204 break;
205 } //if
206 } //for
207 //content_index = 1;
208 NS_LOG_DEBUG("RandomNumber="<<content_index);
209 return content_index;
Shockb0f83152012-12-25 14:16:47 +0800210}
211
212void
213ConsumerZipfMandelbrot::ScheduleNextPacket() {
214
Alexander Afanasyev13800102012-12-25 00:30:31 -0800215 if (m_firstTime)
216 {
217 m_sendEvent = Simulator::Schedule (Seconds (0.0),
218 &ConsumerZipfMandelbrot::SendPacket, this);
219 m_firstTime = false;
220 }
221 else if (!m_sendEvent.IsRunning ())
222 m_sendEvent = Simulator::Schedule (
223 (m_random == 0) ?
224 Seconds(1.0 / m_frequency)
225 :
226 Seconds(m_random->GetValue ()),
227 &ConsumerZipfMandelbrot::SendPacket, this);
Shockb0f83152012-12-25 14:16:47 +0800228}
229
230} /* namespace ndn */
231} /* namespace ns3 */