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