OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2011 Hemanth Narra |
| 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: Hemanth Narra <hemanthnarra222@gmail.com> |
| 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 Experimenta
tion on GENI), |
| 29 * US Department of Defense (DoD), and ITTC at The University of Kansas. |
| 30 */ |
| 31 #include "ns3/simulator.h" |
| 32 #include "ns3/uinteger.h" |
| 33 #include "ns3/log.h" |
| 34 #include "tdma-mac-queue.h" |
| 35 |
| 36 using namespace std; |
| 37 NS_LOG_COMPONENT_DEFINE ("TdmaMacQueue"); |
| 38 namespace ns3 { |
| 39 |
| 40 NS_OBJECT_ENSURE_REGISTERED (TdmaMacQueue); |
| 41 |
| 42 TdmaMacQueue::Item::Item (Ptr<const Packet> packet, |
| 43 const WifiMacHeader &hdr, |
| 44 Time tstamp) |
| 45 : packet (packet), |
| 46 hdr (hdr), |
| 47 tstamp (tstamp) |
| 48 { |
| 49 } |
| 50 |
| 51 TypeId |
| 52 TdmaMacQueue::GetTypeId (void) |
| 53 { |
| 54 static TypeId tid = TypeId ("ns3::TdmaMacQueue") |
| 55 .SetParent<Object> () |
| 56 .AddConstructor<TdmaMacQueue> () |
| 57 .AddAttribute ("MaxPacketNumber", "If a packet arrives when there are alread
y this number of packets, it is dropped.", |
| 58 UintegerValue (400), |
| 59 MakeUintegerAccessor (&TdmaMacQueue::m_maxSize), |
| 60 MakeUintegerChecker<uint32_t> ()) |
| 61 .AddAttribute ("MaxDelay", "If a packet stays longer than this delay in the
queue, it is dropped.", |
| 62 TimeValue (Seconds (10.0)), |
| 63 MakeTimeAccessor (&TdmaMacQueue::m_maxDelay), |
| 64 MakeTimeChecker ()) |
| 65 ; |
| 66 return tid; |
| 67 } |
| 68 |
| 69 TdmaMacQueue::TdmaMacQueue () |
| 70 : m_size (0), |
| 71 m_count (0) |
| 72 { |
| 73 NS_LOG_FUNCTION_NOARGS (); |
| 74 } |
| 75 |
| 76 TdmaMacQueue::~TdmaMacQueue () |
| 77 { |
| 78 Flush (); |
| 79 } |
| 80 |
| 81 void |
| 82 TdmaMacQueue::SetMaxSize (uint32_t maxSize) |
| 83 { |
| 84 m_maxSize = maxSize; |
| 85 } |
| 86 |
| 87 void |
| 88 TdmaMacQueue::SetMacPtr (Ptr<TdmaMac> macPtr) |
| 89 { |
| 90 m_macPtr = macPtr; |
| 91 } |
| 92 |
| 93 void |
| 94 TdmaMacQueue::SetMaxDelay (Time delay) |
| 95 { |
| 96 m_maxDelay = delay; |
| 97 } |
| 98 |
| 99 void |
| 100 TdmaMacQueue::SetTdmaMacTxDropCallback (Callback<void,Ptr<const Packet> > callba
ck) |
| 101 { |
| 102 m_txDropCallback = callback; |
| 103 } |
| 104 |
| 105 uint32_t |
| 106 TdmaMacQueue::GetMaxSize (void) const |
| 107 { |
| 108 return m_maxSize; |
| 109 } |
| 110 |
| 111 Time |
| 112 TdmaMacQueue::GetMaxDelay (void) const |
| 113 { |
| 114 return m_maxDelay; |
| 115 } |
| 116 |
| 117 bool |
| 118 TdmaMacQueue::Enqueue (Ptr<const Packet> packet, const WifiMacHeader &hdr) |
| 119 { |
| 120 NS_LOG_DEBUG ("Queue Size: " << GetSize () << " Max Size: " << GetMaxSize ()); |
| 121 Cleanup (); |
| 122 if (m_size == m_maxSize) |
| 123 { |
| 124 return false; |
| 125 } |
| 126 Time now = Simulator::Now (); |
| 127 m_queue.push_back (Item (packet, hdr, now)); |
| 128 m_size++; |
| 129 NS_LOG_DEBUG ("Inserted packet of size: " << packet->GetSize () |
| 130 << " uid: " << packet->GetUid ()); |
| 131 return true; |
| 132 } |
| 133 |
| 134 void |
| 135 TdmaMacQueue::Cleanup (void) |
| 136 { |
| 137 NS_LOG_FUNCTION_NOARGS (); |
| 138 if (m_queue.empty ()) |
| 139 { |
| 140 return; |
| 141 } |
| 142 Time now = Simulator::Now (); |
| 143 uint32_t n = 0; |
| 144 for (PacketQueueI i = m_queue.begin (); i != m_queue.end (); ) |
| 145 { |
| 146 if (i->tstamp + m_maxDelay > now) |
| 147 { |
| 148 i++; |
| 149 } |
| 150 else |
| 151 { |
| 152 m_count++; |
| 153 NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << "s Dropping this pack
et as its exceeded queue time, pid: " << i->packet->GetUid () |
| 154 << " macPtr: " << m_macP
tr |
| 155 << " queueSize: " << m_q
ueue.size () |
| 156 << " count:" << m_count)
; |
| 157 m_txDropCallback (i->packet); |
| 158 i = m_queue.erase (i); |
| 159 n++; |
| 160 } |
| 161 } |
| 162 m_size -= n; |
| 163 } |
| 164 |
| 165 Ptr<const Packet> |
| 166 TdmaMacQueue::Dequeue (WifiMacHeader *hdr) |
| 167 { |
| 168 NS_LOG_FUNCTION_NOARGS (); |
| 169 Cleanup (); |
| 170 if (!m_queue.empty ()) |
| 171 { |
| 172 Item i = m_queue.front (); |
| 173 m_queue.pop_front (); |
| 174 m_size--; |
| 175 *hdr = i.hdr; |
| 176 NS_LOG_DEBUG ("Dequeued packet of size: " << i.packet->GetSize ()); |
| 177 return i.packet; |
| 178 } |
| 179 return 0; |
| 180 } |
| 181 |
| 182 Ptr<const Packet> |
| 183 TdmaMacQueue::Peek (WifiMacHeader *hdr) |
| 184 { |
| 185 NS_LOG_FUNCTION_NOARGS (); |
| 186 Cleanup (); |
| 187 if (!m_queue.empty ()) |
| 188 { |
| 189 Item i = m_queue.front (); |
| 190 *hdr = i.hdr; |
| 191 return i.packet; |
| 192 } |
| 193 return 0; |
| 194 } |
| 195 |
| 196 bool |
| 197 TdmaMacQueue::IsEmpty (void) |
| 198 { |
| 199 Cleanup (); |
| 200 return m_queue.empty (); |
| 201 } |
| 202 |
| 203 uint32_t |
| 204 TdmaMacQueue::GetSize (void) |
| 205 { |
| 206 return m_size; |
| 207 } |
| 208 |
| 209 void |
| 210 TdmaMacQueue::Flush (void) |
| 211 { |
| 212 m_queue.erase (m_queue.begin (), m_queue.end ()); |
| 213 m_size = 0; |
| 214 } |
| 215 |
| 216 Mac48Address |
| 217 TdmaMacQueue::GetAddressForPacket (enum WifiMacHeader::AddressType type, PacketQ
ueueI it) |
| 218 { |
| 219 if (type == WifiMacHeader::ADDR1) |
| 220 { |
| 221 return it->hdr.GetAddr1 (); |
| 222 } |
| 223 if (type == WifiMacHeader::ADDR2) |
| 224 { |
| 225 return it->hdr.GetAddr2 (); |
| 226 } |
| 227 if (type == WifiMacHeader::ADDR3) |
| 228 { |
| 229 return it->hdr.GetAddr3 (); |
| 230 } |
| 231 return 0; |
| 232 } |
| 233 |
| 234 bool |
| 235 TdmaMacQueue::Remove (Ptr<const Packet> packet) |
| 236 { |
| 237 PacketQueueI it = m_queue.begin (); |
| 238 for (; it != m_queue.end (); it++) |
| 239 { |
| 240 if (it->packet == packet) |
| 241 { |
| 242 m_queue.erase (it); |
| 243 m_size--; |
| 244 return true; |
| 245 } |
| 246 } |
| 247 return false; |
| 248 } |
| 249 } // namespace ns3 |
OLD | NEW |