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  .AddConstructor<DsrNetworkQueue> ()
55  ;
56  return tid;
57 }
58 
59 DsrNetworkQueue::DsrNetworkQueue (uint32_t maxLen, Time maxDelay)
60  : m_size (0),
61  m_maxSize (maxLen),
62  m_maxDelay (maxDelay)
63 {
64  NS_LOG_FUNCTION (this);
65 }
66 
68 {
69  NS_LOG_FUNCTION (this);
70 }
71 
73 {
74  NS_LOG_FUNCTION (this);
75  Flush ();
76 }
77 
78 void
80 {
81  m_maxSize = maxSize;
82 }
83 
84 void
86 {
87  m_maxDelay = delay;
88 }
89 
90 uint32_t
92 {
93  return m_maxSize;
94 }
95 
96 Time
98 {
99  return m_maxDelay;
100 }
101 
102 bool
104 {
105  Cleanup ();
106  for (std::vector<DsrNetworkQueueEntry>::iterator i = m_dsrNetworkQueue.begin (); i != m_dsrNetworkQueue.end (); ++i)
107  {
108  if (i->GetNextHopAddress () == nextHop)
109  {
110  entry = *i;
111  i = m_dsrNetworkQueue.erase (i);
112  return true;
113  }
114  }
115  return false;
116 }
117 
118 bool
120 {
121  Cleanup ();
122  for (std::vector<DsrNetworkQueueEntry>::iterator i = m_dsrNetworkQueue.begin (); i != m_dsrNetworkQueue.end (); ++i)
123  {
124  if (i->GetNextHopAddress () == nextHop)
125  {
126  return true;
127  }
128  }
129  return false;
130 }
131 
132 bool
134 {
135  NS_LOG_FUNCTION (this << m_size << m_maxSize);
136  if (m_size >= m_maxSize)
137  {
138  return false;
139  }
140  Time now = Simulator::Now ();
141  entry.SetInsertedTimeStamp (now);
142  m_dsrNetworkQueue.push_back (entry);
143  m_size++;
144  NS_LOG_LOGIC ("The network queue size is " << m_size);
145  return true;
146 }
147 
148 bool
150 {
151  NS_LOG_FUNCTION (this);
152  Cleanup ();
153  std::vector<DsrNetworkQueueEntry>::iterator i = m_dsrNetworkQueue.begin ();
154  if (i == m_dsrNetworkQueue.end ())
155  {
156  // no elements in array
157  NS_LOG_LOGIC ("No queued packet in the network queue");
158  return false;
159  }
160  entry = *i;
161  m_dsrNetworkQueue.erase (i);
162  m_size--;
163  return true;
164 }
165 
166 void
168 {
169  NS_LOG_FUNCTION (this);
170  if (m_dsrNetworkQueue.empty ())
171  {
172  return;
173  }
174 
175  Time now = Simulator::Now ();
176  uint32_t n = 0;
177  for (std::vector<DsrNetworkQueueEntry>::iterator i = m_dsrNetworkQueue.begin (); i != m_dsrNetworkQueue.end (); )
178  {
179  if (i->GetInsertedTimeStamp () + m_maxDelay > now)
180  {
181  i++;
182  }
183  else
184  {
185  NS_LOG_LOGIC ("Outdated packet");
186  i = m_dsrNetworkQueue.erase (i);
187  n++;
188  }
189  }
190  m_size -= n;
191 }
192 
193 uint32_t
195 {
196  NS_LOG_FUNCTION (this);
197  return m_size;
198 }
199 
200 void
202 {
203  NS_LOG_FUNCTION (this);
204  m_dsrNetworkQueue.erase (m_dsrNetworkQueue.begin (), m_dsrNetworkQueue.end ());
205  m_size = 0;
206 }
207 
208 } // namespace dsr
209 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t GetSize()
Number of entries.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
bool Enqueue(DsrNetworkQueueEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue...
uint32_t m_maxSize
Maximum queue size.
bool Dequeue(DsrNetworkQueueEntry &entry)
Return first found (the earliest) entry for given destination.
DSR Network Queue Entry.
uint32_t GetMaxNetworkSize(void) const
Return the maximum queue size.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
void SetMaxNetworkDelay(Time delay)
Set the maximum entry lifetime in the queue.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Time GetMaxNetworkDelay(void) const
Return the maximum entry lifetime for this queue.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
bool FindPacketWithNexthop(Ipv4Address nextHop, DsrNetworkQueueEntry &entry)
void Cleanup(void)
Clean the queue by removing entries that exceeded lifetime.
uint32_t m_size
Current queue size.
void SetInsertedTimeStamp(Time time)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void Flush(void)
Clear the queue.
std::vector< DsrNetworkQueueEntry > m_dsrNetworkQueue
Queue (vector) of entries.
Time m_maxDelay
Maximum entry lifetime.
static TypeId GetTypeId(void)
void SetMaxNetworkSize(uint32_t maxSize)
Set the maximum queue size.
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
bool Find(Ipv4Address nextHop)