A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
dsr-network-queue.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Yufei Cheng
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
18  *
19  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20  * ResiliNets Research Group https://resilinets.org/
21  * Information and Telecommunication Technology Center (ITTC)
22  * and Department of Electrical Engineering and Computer Science
23  * The University of Kansas Lawrence, KS USA.
24  *
25  * Work supported in part by NSF FIND (Future Internet Design) Program
26  * under grant CNS-0626918 (Postmodern Internet Architecture),
27  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28  * US Department of Defense (DoD), and ITTC at The University of Kansas.
29  */
30 
31 #include "dsr-network-queue.h"
32 
33 #include "ns3/ipv4-route.h"
34 #include "ns3/log.h"
35 #include "ns3/socket.h"
36 #include "ns3/test.h"
37 
38 #include <algorithm>
39 #include <functional>
40 #include <map>
41 
42 namespace ns3
43 {
44 
45 NS_LOG_COMPONENT_DEFINE("DsrNetworkQueue");
46 
47 namespace dsr
48 {
49 
50 NS_OBJECT_ENSURE_REGISTERED(DsrNetworkQueue);
51 
52 TypeId
54 {
55  static TypeId tid = TypeId("ns3::dsr::DsrNetworkQueue")
56  .SetParent<Object>()
57  .SetGroupName("Dsr")
58  .AddConstructor<DsrNetworkQueue>();
59  return tid;
60 }
61 
62 DsrNetworkQueue::DsrNetworkQueue(uint32_t maxLen, Time maxDelay)
63  : m_size(0),
64  m_maxSize(maxLen),
65  m_maxDelay(maxDelay)
66 {
67  NS_LOG_FUNCTION(this);
68 }
69 
71  : m_size(0)
72 {
73  NS_LOG_FUNCTION(this);
74 }
75 
77 {
78  NS_LOG_FUNCTION(this);
79  Flush();
80 }
81 
82 void
84 {
85  m_maxSize = maxSize;
86 }
87 
88 void
90 {
91  m_maxDelay = delay;
92 }
93 
94 uint32_t
96 {
97  return m_maxSize;
98 }
99 
100 Time
102 {
103  return m_maxDelay;
104 }
105 
106 bool
108 {
109  Cleanup();
110  for (auto i = m_dsrNetworkQueue.begin(); i != m_dsrNetworkQueue.end(); ++i)
111  {
112  if (i->GetNextHopAddress() == nextHop)
113  {
114  entry = *i;
115  i = m_dsrNetworkQueue.erase(i);
116  return true;
117  }
118  }
119  return false;
120 }
121 
122 bool
124 {
125  Cleanup();
126  for (auto i = m_dsrNetworkQueue.begin(); i != m_dsrNetworkQueue.end(); ++i)
127  {
128  if (i->GetNextHopAddress() == nextHop)
129  {
130  return true;
131  }
132  }
133  return false;
134 }
135 
136 bool
138 {
139  NS_LOG_FUNCTION(this << m_size << m_maxSize);
140  if (m_size >= m_maxSize)
141  {
142  return false;
143  }
144  Time now = Simulator::Now();
145  entry.SetInsertedTimeStamp(now);
146  m_dsrNetworkQueue.push_back(entry);
147  m_size++;
148  NS_LOG_LOGIC("The network queue size is " << m_size);
149  return true;
150 }
151 
152 bool
154 {
155  NS_LOG_FUNCTION(this);
156  Cleanup();
157  auto i = m_dsrNetworkQueue.begin();
158  if (i == m_dsrNetworkQueue.end())
159  {
160  // no elements in array
161  NS_LOG_LOGIC("No queued packet in the network queue");
162  return false;
163  }
164  entry = *i;
165  m_dsrNetworkQueue.erase(i);
166  m_size--;
167  return true;
168 }
169 
170 void
172 {
173  NS_LOG_FUNCTION(this);
174  if (m_dsrNetworkQueue.empty())
175  {
176  return;
177  }
178 
179  Time now = Simulator::Now();
180  uint32_t n = 0;
181  for (auto i = m_dsrNetworkQueue.begin(); i != m_dsrNetworkQueue.end();)
182  {
183  if (i->GetInsertedTimeStamp() + m_maxDelay > now)
184  {
185  i++;
186  }
187  else
188  {
189  NS_LOG_LOGIC("Outdated packet");
190  i = m_dsrNetworkQueue.erase(i);
191  n++;
192  }
193  }
194  m_size -= n;
195 }
196 
197 uint32_t
199 {
200  NS_LOG_FUNCTION(this);
201  return m_size;
202 }
203 
204 void
206 {
207  NS_LOG_FUNCTION(this);
209  m_size = 0;
210 }
211 
212 } // namespace dsr
213 } // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
A base class which provides memory management and object aggregation.
Definition: object.h:89
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
DSR Network Queue Entry.
void SetInsertedTimeStamp(Time time)
Set inserted time stamp function.
void Flush()
Clear the queue.
uint32_t m_maxSize
Maximum queue size.
uint32_t GetMaxNetworkSize() const
Return the maximum queue size.
bool FindPacketWithNexthop(Ipv4Address nextHop, DsrNetworkQueueEntry &entry)
Find the packet entry with a given next hop.
uint32_t GetSize()
Number of entries.
void SetMaxNetworkDelay(Time delay)
Set the maximum entry lifetime in the queue.
bool Enqueue(DsrNetworkQueueEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
static TypeId GetTypeId()
Get the type ID.
std::vector< DsrNetworkQueueEntry > m_dsrNetworkQueue
Queue (vector) of entries.
Time m_maxDelay
Maximum entry lifetime.
void Cleanup()
Clean the queue by removing entries that exceeded lifetime.
void SetMaxNetworkSize(uint32_t maxSize)
Set the maximum queue size.
uint32_t m_size
Current queue size.
bool Find(Ipv4Address nextHop)
Try to find an entry with a particular next hop, and return true if found.
Time GetMaxNetworkDelay() const
Return the maximum entry lifetime for this queue.
bool Dequeue(DsrNetworkQueueEntry &entry)
Return first found (the earliest) entry for given destination.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.