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