NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
ndn-consumer-zipf-mandelbrot.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
23 
24 #include "model/ndn-app-face.hpp"
26 
27 #include <math.h>
28 
29 NS_LOG_COMPONENT_DEFINE("ndn.ConsumerZipfMandelbrot");
30 
31 namespace ns3 {
32 namespace ndn {
33 
35 
36 TypeId
38 {
39  static TypeId tid =
40  TypeId("ns3::ndn::ConsumerZipfMandelbrot")
41  .SetGroupName("Ndn")
42  .SetParent<ConsumerCbr>()
43  .AddConstructor<ConsumerZipfMandelbrot>()
44 
45  .AddAttribute("NumberOfContents", "Number of the Contents in total", StringValue("100"),
46  MakeUintegerAccessor(&ConsumerZipfMandelbrot::SetNumberOfContents,
47  &ConsumerZipfMandelbrot::GetNumberOfContents),
48  MakeUintegerChecker<uint32_t>())
49 
50  .AddAttribute("q", "parameter of improve rank", StringValue("0.7"),
51  MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetQ,
52  &ConsumerZipfMandelbrot::GetQ),
53  MakeDoubleChecker<double>())
54 
55  .AddAttribute("s", "parameter of power", StringValue("0.7"),
56  MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetS,
57  &ConsumerZipfMandelbrot::GetS),
58  MakeDoubleChecker<double>());
59 
60  return tid;
61 }
62 
64  : m_N(100) // needed here to make sure when SetQ/SetS are called, there is a valid value of N
65  , m_q(0.7)
66  , m_s(0.7)
67  , m_seqRng(CreateObject<UniformRandomVariable>())
68 {
69  // SetNumberOfContents is called by NS-3 object system during the initialization
70 }
71 
73 {
74 }
75 
76 void
77 ConsumerZipfMandelbrot::SetNumberOfContents(uint32_t numOfContents)
78 {
79  m_N = numOfContents;
80 
81  NS_LOG_DEBUG(m_q << " and " << m_s << " and " << m_N);
82 
83  m_Pcum = std::vector<double>(m_N + 1);
84 
85  m_Pcum[0] = 0.0;
86  for (uint32_t i = 1; i <= m_N; i++) {
87  m_Pcum[i] = m_Pcum[i - 1] + 1.0 / std::pow(i + m_q, m_s);
88  }
89 
90  for (uint32_t i = 1; i <= m_N; i++) {
91  m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N];
92  NS_LOG_LOGIC("Cumulative probability [" << i << "]=" << m_Pcum[i]);
93  }
94 }
95 
96 uint32_t
97 ConsumerZipfMandelbrot::GetNumberOfContents() const
98 {
99  return m_N;
100 }
101 
102 void
103 ConsumerZipfMandelbrot::SetQ(double q)
104 {
105  m_q = q;
106  SetNumberOfContents(m_N);
107 }
108 
109 double
110 ConsumerZipfMandelbrot::GetQ() const
111 {
112  return m_q;
113 }
114 
115 void
116 ConsumerZipfMandelbrot::SetS(double s)
117 {
118  m_s = s;
119  SetNumberOfContents(m_N);
120 }
121 
122 double
123 ConsumerZipfMandelbrot::GetS() const
124 {
125  return m_s;
126 }
127 
128 void
130 {
131  if (!m_active)
132  return;
133 
134  NS_LOG_FUNCTION_NOARGS();
135 
136  uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid
137 
138  // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n";
139 
140  while (m_retxSeqs.size()) {
141  seq = *m_retxSeqs.begin();
142  m_retxSeqs.erase(m_retxSeqs.begin());
143 
144  // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ());
145  // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ())
146  // {
147 
148  // NS_LOG_DEBUG ("Expire " << seq);
149  // m_seqLifetimes.erase (seq); // lifetime expired. Trying to find another unexpired
150  // sequence number
151  // continue;
152  // }
153  NS_LOG_DEBUG("=interest seq " << seq << " from m_retxSeqs");
154  break;
155  }
156 
157  if (seq == std::numeric_limits<uint32_t>::max()) // no retransmission
158  {
159  if (m_seqMax != std::numeric_limits<uint32_t>::max()) {
160  if (m_seq >= m_seqMax) {
161  return; // we are totally done
162  }
163  }
164 
166  m_seq++;
167  }
168 
169  // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
170 
171  //
172  shared_ptr<Name> nameWithSequence = make_shared<Name>(m_interestName);
173  nameWithSequence->appendSequenceNumber(seq);
174  //
175 
176  shared_ptr<Interest> interest = make_shared<Interest>();
177  interest->setNonce(m_rand->GetValue(0, std::numeric_limits<uint32_t>::max()));
178  interest->setName(*nameWithSequence);
179 
180  // NS_LOG_INFO ("Requesting Interest: \n" << *interest);
181  NS_LOG_INFO("> Interest for " << seq << ", Total: " << m_seq << ", face: " << m_face->getId());
182  NS_LOG_DEBUG("Trying to add " << seq << " with " << Simulator::Now() << ". already "
183  << m_seqTimeouts.size() << " items");
184 
185  m_seqTimeouts.insert(SeqTimeout(seq, Simulator::Now()));
186  m_seqFullDelay.insert(SeqTimeout(seq, Simulator::Now()));
187 
188  m_seqLastDelay.erase(seq);
189  m_seqLastDelay.insert(SeqTimeout(seq, Simulator::Now()));
190 
191  m_seqRetxCounts[seq]++;
192 
193  m_rtt->SentSeq(SequenceNumber32(seq), 1);
194 
195  m_transmittedInterests(interest, this, m_face);
196  m_face->onReceiveInterest(*interest);
197 
199 }
200 
201 uint32_t
203 {
204  uint32_t content_index = 1; //[1, m_N]
205  double p_sum = 0;
206 
207  double p_random = m_seqRng->GetValue();
208  while (p_random == 0) {
209  p_random = m_seqRng->GetValue();
210  }
211  // if (p_random == 0)
212  NS_LOG_LOGIC("p_random=" << p_random);
213  for (uint32_t i = 1; i <= m_N; i++) {
214  p_sum = m_Pcum[i]; // m_Pcum[i] = m_Pcum[i-1] + p[i], p[0] = 0; e.g.: p_cum[1] = p[1],
215  // p_cum[2] = p[1] + p[2]
216  if (p_random <= p_sum) {
217  content_index = i;
218  break;
219  } // if
220  } // for
221  // content_index = 1;
222  NS_LOG_DEBUG("RandomNumber=" << content_index);
223  return content_index;
224 }
225 
226 void
228 {
229 
230  if (m_firstTime) {
231  m_sendEvent = Simulator::Schedule(Seconds(0.0), &ConsumerZipfMandelbrot::SendPacket, this);
232  m_firstTime = false;
233  }
234  else if (!m_sendEvent.IsRunning())
235  m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency)
236  : Seconds(m_random->GetValue()),
238 }
239 
240 } /* namespace ndn */
241 } /* namespace ns3 */
uint32_t m_seqMax
maximum number of sequence number
Copyright (c) 2011-2015 Regents of the University of California.
Ptr< RandomVariableStream > m_random
NS_OBJECT_ENSURE_REGISTERED(ContentStore)
uint32_t m_seq
currently requested sequence number
shared_ptr< AppFace > m_face
automatically created application face through which application communicates
Definition: ndn-app.hpp:103
Ptr< RttEstimator > m_rtt
RTT estimator.
Name m_interestName
NDN Name of the Interest (use Name)
Ndn application for sending out Interest packets at a "constant" rate (Poisson process) ...
virtual void ScheduleNextPacket()
Constructs the Interest packet and sends it using a callback to the underlying NDN protocol...
ndn ConsumerZipfMandelbrot
Copyright (c) 2011-2015 Tsinghua University, P.R.China.
Copyright (c) 2011-2015 Regents of the University of California.
TracedCallback< shared_ptr< const Interest >, Ptr< App >, shared_ptr< Face > > m_transmittedInterests
App-level trace of transmitted Interests.
Definition: ndn-app.hpp:113
Ptr< UniformRandomVariable > m_rand
nonce generator
ConsumerZipfMandelbrot()
Default constructor Sets up randomized Number Generator (RNG) Note: m_seq of its parent class Consume...
EventId m_sendEvent
EventId of pending "send packet" event.
bool m_active
Flag to indicate that application is active (set by StartApplication and StopApplication) ...
Definition: ndn-app.hpp:102