NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-consumer-zipf-mandelbrot.cc
1 /* -*- 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-data.h"
26 
27 #include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
28 
29 #include <math.h>
30 
31 
32 NS_LOG_COMPONENT_DEFINE ("ndn.ConsumerZipfMandelbrot");
33 
34 namespace ns3 {
35 namespace ndn {
36 
37 NS_OBJECT_ENSURE_REGISTERED (ConsumerZipfMandelbrot);
38 
39 TypeId
40 ConsumerZipfMandelbrot::GetTypeId (void)
41 {
42  static TypeId tid = TypeId ("ns3::ndn::ConsumerZipfMandelbrot")
43  .SetGroupName ("Ndn")
44  .SetParent<ConsumerCbr> ()
45  .AddConstructor<ConsumerZipfMandelbrot> ()
46 
47  .AddAttribute ("NumberOfContents", "Number of the Contents in total",
48  StringValue ("100"),
49  MakeUintegerAccessor (&ConsumerZipfMandelbrot::SetNumberOfContents, &ConsumerZipfMandelbrot::GetNumberOfContents),
50  MakeUintegerChecker<uint32_t> ())
51 
52  .AddAttribute ("q", "parameter of improve rank",
53  StringValue ("0.7"),
54  MakeDoubleAccessor (&ConsumerZipfMandelbrot::SetQ, &ConsumerZipfMandelbrot::GetQ),
55  MakeDoubleChecker<double> ())
56 
57  .AddAttribute ("s", "parameter of power",
58  StringValue ("0.7"),
59  MakeDoubleAccessor (&ConsumerZipfMandelbrot::SetS, &ConsumerZipfMandelbrot::GetS),
60  MakeDoubleChecker<double> ())
61  ;
62 
63  return tid;
64 }
65 
66 
68  : m_N (100) // needed here to make sure when SetQ/SetS are called, there is a valid value of N
69  , m_q (0.7)
70  , m_s (0.7)
71  , m_SeqRng (0.0, 1.0)
72 {
73  // SetNumberOfContents is called by NS-3 object system during the initialization
74 }
75 
76 ConsumerZipfMandelbrot::~ConsumerZipfMandelbrot()
77 {
78 }
79 
80 void
81 ConsumerZipfMandelbrot::SetNumberOfContents (uint32_t numOfContents)
82 {
83  m_N = numOfContents;
84 
85  NS_LOG_DEBUG (m_q << " and " << m_s << " and " << m_N);
86 
87  m_Pcum = std::vector<double> (m_N + 1);
88 
89  m_Pcum[0] = 0.0;
90  for (uint32_t i=1; i<=m_N; i++)
91  {
92  m_Pcum[i] = m_Pcum[i-1] + 1.0 / std::pow(i+m_q, m_s);
93  }
94 
95  for (uint32_t i=1; i<=m_N; i++)
96  {
97  m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N];
98  NS_LOG_LOGIC ("Cumulative probability [" << i << "]=" << m_Pcum[i]);
99  }
100 }
101 
102 uint32_t
103 ConsumerZipfMandelbrot::GetNumberOfContents () const
104 {
105  return m_N;
106 }
107 
108 void
109 ConsumerZipfMandelbrot::SetQ (double q)
110 {
111  m_q = q;
112  SetNumberOfContents (m_N);
113 }
114 
115 double
116 ConsumerZipfMandelbrot::GetQ () const
117 {
118  return m_q;
119 }
120 
121 void
122 ConsumerZipfMandelbrot::SetS (double s)
123 {
124  m_s = s;
125  SetNumberOfContents (m_N);
126 }
127 
128 double
129 ConsumerZipfMandelbrot::GetS () const
130 {
131  return m_s;
132 }
133 
134 void
135 ConsumerZipfMandelbrot::SendPacket() {
136  if (!m_active) return;
137 
138  NS_LOG_FUNCTION_NOARGS ();
139 
140  uint32_t seq=std::numeric_limits<uint32_t>::max (); //invalid
141 
142  // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n";
143 
144  while (m_retxSeqs.size ())
145  {
146  seq = *m_retxSeqs.begin ();
147  m_retxSeqs.erase (m_retxSeqs.begin ());
148 
149  // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ());
150  // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ())
151  // {
152 
153  // NS_LOG_DEBUG ("Expire " << seq);
154  // m_seqLifetimes.erase (seq); // lifetime expired. Trying to find another unexpired sequence number
155  // continue;
156  // }
157  NS_LOG_DEBUG("=interest seq "<<seq<<" from m_retxSeqs");
158  break;
159  }
160 
161  if (seq == std::numeric_limits<uint32_t>::max ()) //no retransmission
162  {
163  if (m_seqMax != std::numeric_limits<uint32_t>::max ())
164  {
165  if (m_seq >= m_seqMax)
166  {
167  return; // we are totally done
168  }
169  }
170 
171  seq = ConsumerZipfMandelbrot::GetNextSeq();
172  m_seq ++;
173  }
174 
175  // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
176 
177  //
178  Ptr<Name> nameWithSequence = Create<Name> (m_interestName);
179  nameWithSequence->appendSeqNum (seq);
180  //
181 
182  Ptr<Interest> interest = Create<Interest> ();
183  interest->SetNonce (m_rand.GetValue ());
184  interest->SetName (nameWithSequence);
185 
186  // NS_LOG_INFO ("Requesting Interest: \n" << *interest);
187  NS_LOG_INFO ("> Interest for " << seq<<", Total: "<<m_seq<<", face: "<<m_face->GetId());
188  NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");
189 
190  m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
191  m_seqFullDelay.insert (SeqTimeout (seq, Simulator::Now ()));
192 
193  m_seqLastDelay.erase (seq);
194  m_seqLastDelay.insert (SeqTimeout (seq, Simulator::Now ()));
195 
196  m_seqRetxCounts[seq] ++;
197 
198  m_rtt->SentSeq (SequenceNumber32 (seq), 1);
199 
200  FwHopCountTag hopCountTag;
201  interest->GetPayload ()->AddPacketTag (hopCountTag);
202 
203  m_transmittedInterests (interest, this, m_face);
204  m_face->ReceiveInterest (interest);
205 
207 }
208 
209 
210 uint32_t
211 ConsumerZipfMandelbrot::GetNextSeq()
212 {
213  uint32_t content_index = 1; //[1, m_N]
214  double p_sum = 0;
215 
216  double p_random = m_SeqRng.GetValue();
217  while (p_random == 0)
218  {
219  p_random = m_SeqRng.GetValue();
220  }
221  //if (p_random == 0)
222  NS_LOG_LOGIC("p_random="<<p_random);
223  for (uint32_t i=1; i<=m_N; i++)
224  {
225  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]
226  if (p_random <= p_sum)
227  {
228  content_index = i;
229  break;
230  } //if
231  } //for
232  //content_index = 1;
233  NS_LOG_DEBUG("RandomNumber="<<content_index);
234  return content_index;
235 }
236 
237 void
239 
240  if (m_firstTime)
241  {
242  m_sendEvent = Simulator::Schedule (Seconds (0.0),
243  &ConsumerZipfMandelbrot::SendPacket, this);
244  m_firstTime = false;
245  }
246  else if (!m_sendEvent.IsRunning ())
247  m_sendEvent = Simulator::Schedule (
248  (m_random == 0) ?
249  Seconds(1.0 / m_frequency)
250  :
251  Seconds(m_random->GetValue ()),
252  &ConsumerZipfMandelbrot::SendPacket, this);
253 }
254 
255 } /* namespace ndn */
256 } /* namespace ns3 */
bool m_active
Flag to indicate that application is active (set by StartApplication and StopApplication) ...
Definition: ndn-app.h:106
ConsumerZipfMandelbrot()
Default constructor Sets up randomized Number Generator (RNG) Note: m_seq of its parent class Consume...
ConsumerCbr()
Default constructor Sets up randomizer function and packet sequence number.
Name m_interestName
NDN Name of the Interest (use Name)
Definition: ndn-consumer.h:138
TracedCallback< Ptr< const Interest >, Ptr< App >, Ptr< Face > > m_transmittedInterests
App-level trace of transmitted Interests.
Definition: ndn-app.h:120
UniformVariable m_rand
nonce generator
Definition: ndn-consumer.h:127
EventId m_sendEvent
EventId of pending "send packet" event.
Definition: ndn-consumer.h:131
Ptr< RttEstimator > m_rtt
RTT estimator.
Definition: ndn-consumer.h:135
uint32_t m_seqMax
maximum number of sequence number
Definition: ndn-consumer.h:130
virtual void ScheduleNextPacket()
Constructs the Interest packet and sends it using a callback to the underlying NDN protocol...
uint32_t m_seq
currently requested sequence number
Definition: ndn-consumer.h:129
Ptr< Face > m_face
automatically created application face through which application communicates
Definition: ndn-app.h:107