NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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 <math.h>
25 
26 NS_LOG_COMPONENT_DEFINE("ndn.ConsumerZipfMandelbrot");
27 
28 namespace ns3 {
29 namespace ndn {
30 
32 
33 TypeId
35 {
36  static TypeId tid =
37  TypeId("ns3::ndn::ConsumerZipfMandelbrot")
38  .SetGroupName("Ndn")
39  .SetParent<ConsumerCbr>()
40  .AddConstructor<ConsumerZipfMandelbrot>()
41 
42  .AddAttribute("NumberOfContents", "Number of the Contents in total", StringValue("100"),
43  MakeUintegerAccessor(&ConsumerZipfMandelbrot::SetNumberOfContents,
44  &ConsumerZipfMandelbrot::GetNumberOfContents),
45  MakeUintegerChecker<uint32_t>())
46 
47  .AddAttribute("q", "parameter of improve rank", StringValue("0.7"),
48  MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetQ,
49  &ConsumerZipfMandelbrot::GetQ),
50  MakeDoubleChecker<double>())
51 
52  .AddAttribute("s", "parameter of power", StringValue("0.7"),
53  MakeDoubleAccessor(&ConsumerZipfMandelbrot::SetS,
54  &ConsumerZipfMandelbrot::GetS),
55  MakeDoubleChecker<double>());
56 
57  return tid;
58 }
59 
61  : m_N(100) // needed here to make sure when SetQ/SetS are called, there is a valid value of N
62  , m_q(0.7)
63  , m_s(0.7)
64  , m_seqRng(CreateObject<UniformRandomVariable>())
65 {
66  // SetNumberOfContents is called by NS-3 object system during the initialization
67 }
68 
70 {
71 }
72 
73 void
74 ConsumerZipfMandelbrot::SetNumberOfContents(uint32_t numOfContents)
75 {
76  m_N = numOfContents;
77 
78  NS_LOG_DEBUG(m_q << " and " << m_s << " and " << m_N);
79 
80  m_Pcum = std::vector<double>(m_N + 1);
81 
82  m_Pcum[0] = 0.0;
83  for (uint32_t i = 1; i <= m_N; i++) {
84  m_Pcum[i] = m_Pcum[i - 1] + 1.0 / std::pow(i + m_q, m_s);
85  }
86 
87  for (uint32_t i = 1; i <= m_N; i++) {
88  m_Pcum[i] = m_Pcum[i] / m_Pcum[m_N];
89  NS_LOG_LOGIC("Cumulative probability [" << i << "]=" << m_Pcum[i]);
90  }
91 }
92 
93 uint32_t
94 ConsumerZipfMandelbrot::GetNumberOfContents() const
95 {
96  return m_N;
97 }
98 
99 void
100 ConsumerZipfMandelbrot::SetQ(double q)
101 {
102  m_q = q;
103  SetNumberOfContents(m_N);
104 }
105 
106 double
107 ConsumerZipfMandelbrot::GetQ() const
108 {
109  return m_q;
110 }
111 
112 void
113 ConsumerZipfMandelbrot::SetS(double s)
114 {
115  m_s = s;
116  SetNumberOfContents(m_N);
117 }
118 
119 double
120 ConsumerZipfMandelbrot::GetS() const
121 {
122  return m_s;
123 }
124 
125 void
127 {
128  if (!m_active)
129  return;
130 
131  NS_LOG_FUNCTION_NOARGS();
132 
133  uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid
134 
135  // std::cout << Simulator::Now ().ToDouble (Time::S) << "s max -> " << m_seqMax << "\n";
136 
137  while (m_retxSeqs.size()) {
138  seq = *m_retxSeqs.begin();
139  m_retxSeqs.erase(m_retxSeqs.begin());
140 
141  // NS_ASSERT (m_seqLifetimes.find (seq) != m_seqLifetimes.end ());
142  // if (m_seqLifetimes.find (seq)->time <= Simulator::Now ())
143  // {
144 
145  // NS_LOG_DEBUG ("Expire " << seq);
146  // m_seqLifetimes.erase (seq); // lifetime expired. Trying to find another unexpired
147  // sequence number
148  // continue;
149  // }
150  NS_LOG_DEBUG("=interest seq " << seq << " from m_retxSeqs");
151  break;
152  }
153 
154  if (seq == std::numeric_limits<uint32_t>::max()) // no retransmission
155  {
156  if (m_seqMax != std::numeric_limits<uint32_t>::max()) {
157  if (m_seq >= m_seqMax) {
158  return; // we are totally done
159  }
160  }
161 
163  m_seq++;
164  }
165 
166  // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
167 
168  //
169  shared_ptr<Name> nameWithSequence = make_shared<Name>(m_interestName);
170  nameWithSequence->appendSequenceNumber(seq);
171  //
172 
173  shared_ptr<Interest> interest = make_shared<Interest>();
174  interest->setNonce(m_rand->GetValue(0, std::numeric_limits<uint32_t>::max()));
175  interest->setName(*nameWithSequence);
176 
177  // NS_LOG_INFO ("Requesting Interest: \n" << *interest);
178  NS_LOG_INFO("> Interest for " << seq << ", Total: " << m_seq << ", face: " << m_face->getId());
179  NS_LOG_DEBUG("Trying to add " << seq << " with " << Simulator::Now() << ". already "
180  << m_seqTimeouts.size() << " items");
181 
182  m_seqTimeouts.insert(SeqTimeout(seq, Simulator::Now()));
183  m_seqFullDelay.insert(SeqTimeout(seq, Simulator::Now()));
184 
185  m_seqLastDelay.erase(seq);
186  m_seqLastDelay.insert(SeqTimeout(seq, Simulator::Now()));
187 
188  m_seqRetxCounts[seq]++;
189 
190  m_rtt->SentSeq(SequenceNumber32(seq), 1);
191 
192  m_transmittedInterests(interest, this, m_face);
193  m_appLink->onReceiveInterest(*interest);
194 
196 }
197 
198 uint32_t
200 {
201  uint32_t content_index = 1; //[1, m_N]
202  double p_sum = 0;
203 
204  double p_random = m_seqRng->GetValue();
205  while (p_random == 0) {
206  p_random = m_seqRng->GetValue();
207  }
208  // if (p_random == 0)
209  NS_LOG_LOGIC("p_random=" << p_random);
210  for (uint32_t i = 1; i <= m_N; i++) {
211  p_sum = m_Pcum[i]; // m_Pcum[i] = m_Pcum[i-1] + p[i], p[0] = 0; e.g.: p_cum[1] = p[1],
212  // p_cum[2] = p[1] + p[2]
213  if (p_random <= p_sum) {
214  content_index = i;
215  break;
216  } // if
217  } // for
218  // content_index = 1;
219  NS_LOG_DEBUG("RandomNumber=" << content_index);
220  return content_index;
221 }
222 
223 void
225 {
226 
227  if (m_firstTime) {
228  m_sendEvent = Simulator::Schedule(Seconds(0.0), &ConsumerZipfMandelbrot::SendPacket, this);
229  m_firstTime = false;
230  }
231  else if (!m_sendEvent.IsRunning())
232  m_sendEvent = Simulator::Schedule((m_random == 0) ? Seconds(1.0 / m_frequency)
233  : Seconds(m_random->GetValue()),
235 }
236 
237 } /* namespace ndn */
238 } /* 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
void onReceiveInterest(const Interest &interest)
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:119
shared_ptr< Face > m_face
Definition: ndn-app.hpp:104
AppLinkService * m_appLink
Definition: ndn-app.hpp:105
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:103