A Discrete-Event Network Simulator
API
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 (std::vector<DsrNetworkQueueEntry>::iterator i = m_dsrNetworkQueue.begin();
111  i != m_dsrNetworkQueue.end();
112  ++i)
113  {
114  if (i->GetNextHopAddress() == nextHop)
115  {
116  entry = *i;
117  i = m_dsrNetworkQueue.erase(i);
118  return true;
119  }
120  }
121  return false;
122 }
123 
124 bool
126 {
127  Cleanup();
128  for (std::vector<DsrNetworkQueueEntry>::iterator i = m_dsrNetworkQueue.begin();
129  i != m_dsrNetworkQueue.end();
130  ++i)
131  {
132  if (i->GetNextHopAddress() == nextHop)
133  {
134  return true;
135  }
136  }
137  return false;
138 }
139 
140 bool
142 {
143  NS_LOG_FUNCTION(this << m_size << m_maxSize);
144  if (m_size >= m_maxSize)
145  {
146  return false;
147  }
148  Time now = Simulator::Now();
149  entry.SetInsertedTimeStamp(now);
150  m_dsrNetworkQueue.push_back(entry);
151  m_size++;
152  NS_LOG_LOGIC("The network queue size is " << m_size);
153  return true;
154 }
155 
156 bool
158 {
159  NS_LOG_FUNCTION(this);
160  Cleanup();
161  std::vector<DsrNetworkQueueEntry>::iterator i = m_dsrNetworkQueue.begin();
162  if (i == m_dsrNetworkQueue.end())
163  {
164  // no elements in array
165  NS_LOG_LOGIC("No queued packet in the network queue");
166  return false;
167  }
168  entry = *i;
169  m_dsrNetworkQueue.erase(i);
170  m_size--;
171  return true;
172 }
173 
174 void
176 {
177  NS_LOG_FUNCTION(this);
178  if (m_dsrNetworkQueue.empty())
179  {
180  return;
181  }
182 
183  Time now = Simulator::Now();
184  uint32_t n = 0;
185  for (std::vector<DsrNetworkQueueEntry>::iterator i = m_dsrNetworkQueue.begin();
186  i != m_dsrNetworkQueue.end();)
187  {
188  if (i->GetInsertedTimeStamp() + m_maxDelay > now)
189  {
190  i++;
191  }
192  else
193  {
194  NS_LOG_LOGIC("Outdated packet");
195  i = m_dsrNetworkQueue.erase(i);
196  n++;
197  }
198  }
199  m_size -= n;
200 }
201 
202 uint32_t
204 {
205  NS_LOG_FUNCTION(this);
206  return m_size;
207 }
208 
209 void
211 {
212  NS_LOG_FUNCTION(this);
214  m_size = 0;
215 }
216 
217 } // namespace dsr
218 } // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
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:199
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
DSR Network Queue Entry.
void SetInsertedTimeStamp(Time time)
Set inserted time stamp function.
Introspection did not find any typical Config paths.
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.