A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-network-queue.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Yufei Cheng
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
7 *
8 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
9 * ResiliNets Research Group https://resilinets.org/
10 * Information and Telecommunication Technology Center (ITTC)
11 * and Department of Electrical Engineering and Computer Science
12 * The University of Kansas Lawrence, KS USA.
13 *
14 * Work supported in part by NSF FIND (Future Internet Design) Program
15 * under grant CNS-0626918 (Postmodern Internet Architecture),
16 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
17 * US Department of Defense (DoD), and ITTC at The University of Kansas.
18 */
19
20#include "dsr-network-queue.h"
21
22#include "ns3/ipv4-route.h"
23#include "ns3/log.h"
24#include "ns3/socket.h"
25#include "ns3/test.h"
26
27#include <algorithm>
28#include <functional>
29#include <map>
30
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("DsrNetworkQueue");
35
36namespace dsr
37{
38
40
41TypeId
43{
44 static TypeId tid = TypeId("ns3::dsr::DsrNetworkQueue")
46 .SetGroupName("Dsr")
47 .AddConstructor<DsrNetworkQueue>();
48 return tid;
49}
50
52 : m_size(0),
53 m_maxSize(maxLen),
54 m_maxDelay(maxDelay)
55{
56 NS_LOG_FUNCTION(this);
57}
58
64
70
71void
76
77void
82
88
89Time
94
95bool
97{
98 Cleanup();
99 for (auto i = m_dsrNetworkQueue.begin(); i != m_dsrNetworkQueue.end(); ++i)
100 {
101 if (i->GetNextHopAddress() == nextHop)
102 {
103 entry = *i;
104 i = m_dsrNetworkQueue.erase(i);
105 return true;
106 }
107 }
108 return false;
109}
110
111bool
113{
114 Cleanup();
115 for (auto i = m_dsrNetworkQueue.begin(); i != m_dsrNetworkQueue.end(); ++i)
116 {
117 if (i->GetNextHopAddress() == nextHop)
118 {
119 return true;
120 }
121 }
122 return false;
123}
124
125bool
127{
128 NS_LOG_FUNCTION(this << m_size << m_maxSize);
129 if (m_size >= m_maxSize)
130 {
131 return false;
132 }
133 Time now = Simulator::Now();
134 entry.SetInsertedTimeStamp(now);
135 m_dsrNetworkQueue.push_back(entry);
136 m_size++;
137 NS_LOG_LOGIC("The network queue size is " << m_size);
138 return true;
139}
140
141bool
143{
144 NS_LOG_FUNCTION(this);
145 Cleanup();
146 auto i = m_dsrNetworkQueue.begin();
147 if (i == m_dsrNetworkQueue.end())
148 {
149 // no elements in array
150 NS_LOG_LOGIC("No queued packet in the network queue");
151 return false;
152 }
153 entry = *i;
154 m_dsrNetworkQueue.erase(i);
155 m_size--;
156 return true;
157}
158
159void
161{
162 NS_LOG_FUNCTION(this);
163 if (m_dsrNetworkQueue.empty())
164 {
165 return;
166 }
167
168 Time now = Simulator::Now();
169 uint32_t n = 0;
170 for (auto i = m_dsrNetworkQueue.begin(); i != m_dsrNetworkQueue.end();)
171 {
172 if (i->GetInsertedTimeStamp() + m_maxDelay > now)
173 {
174 i++;
175 }
176 else
177 {
178 NS_LOG_LOGIC("Outdated packet");
179 i = m_dsrNetworkQueue.erase(i);
180 n++;
181 }
182 }
183 m_size -= n;
184}
185
188{
189 NS_LOG_FUNCTION(this);
190 return m_size;
191}
192
193void
200
201} // namespace dsr
202} // namespace ns3
Ipv4 addresses are stored in host order in this class.
Object()
Constructor.
Definition object.cc:96
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
DSR Network Queue Entry.
void SetInsertedTimeStamp(Time time)
Set inserted time stamp function.
Introspection did not find any typical Config paths No Attributes are defined for this type No Tr...
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:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.