OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2013 Mohammed J.F. Alenazi |
| 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: Mohammed J.F. Alenazi <malenazi@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 by King Saud University and |
| 27 * the ITTC at The University of Kansas. |
| 28 */ |
| 29 |
| 30 |
| 31 #include "epidemic-packet-queue.h" |
| 32 #include <algorithm> |
| 33 #include <functional> |
| 34 #include "ns3/ipv4-route.h" |
| 35 #include "ns3/socket.h" |
| 36 #include "ns3/log.h" |
| 37 #include "epidemic-packet.h" |
| 38 |
| 39 using namespace std; |
| 40 |
| 41 |
| 42 |
| 43 /** |
| 44 * \file |
| 45 * \ingroup epidemic |
| 46 * ns3::Epidemic::QueueEntry and ns3::Epidemic::PacketQueue declarations. |
| 47 */ |
| 48 |
| 49 |
| 50 namespace ns3 { |
| 51 NS_LOG_COMPONENT_DEFINE ("EpidemicPacketQueue"); |
| 52 |
| 53 namespace Epidemic { |
| 54 |
| 55 |
| 56 QueueEntry::QueueEntry (void) |
| 57 : m_packet (0), |
| 58 m_header (Ipv4Header ()), |
| 59 m_ucb (UnicastForwardCallback ()), |
| 60 m_ecb (ErrorCallback ()), |
| 61 m_expire (Simulator::Now ()), |
| 62 m_packetID (0) |
| 63 { |
| 64 } |
| 65 |
| 66 QueueEntry::QueueEntry (Ptr<const Packet> pa, |
| 67 Ipv4Header const & h, |
| 68 UnicastForwardCallback ucb, |
| 69 ErrorCallback ecb, |
| 70 Time exp /* = Simulator::Now () */, |
| 71 uint32_t packetID /* = 0 */) |
| 72 : m_packet (pa), |
| 73 m_header (h), |
| 74 m_ucb (ucb), |
| 75 m_ecb (ecb), |
| 76 m_expire (exp), |
| 77 m_packetID (packetID) |
| 78 { |
| 79 } |
| 80 |
| 81 |
| 82 |
| 83 |
| 84 bool |
| 85 QueueEntry::operator== (QueueEntry const & o) const |
| 86 { |
| 87 return (m_packetID == o.m_packetID); |
| 88 } |
| 89 |
| 90 QueueEntry::UnicastForwardCallback |
| 91 QueueEntry::GetUnicastForwardCallback () const |
| 92 { |
| 93 return m_ucb; |
| 94 } |
| 95 |
| 96 void |
| 97 QueueEntry::SetUnicastForwardCallback (QueueEntry::UnicastForwardCallback ucb) |
| 98 { |
| 99 m_ucb = ucb; |
| 100 } |
| 101 |
| 102 QueueEntry::ErrorCallback |
| 103 QueueEntry::GetErrorCallback () const |
| 104 { |
| 105 return m_ecb; |
| 106 } |
| 107 |
| 108 void |
| 109 QueueEntry::SetErrorCallback (QueueEntry::ErrorCallback ecb) |
| 110 { |
| 111 m_ecb = ecb; |
| 112 } |
| 113 |
| 114 Ptr<const Packet> |
| 115 QueueEntry::GetPacket () const |
| 116 { |
| 117 return m_packet; |
| 118 } |
| 119 |
| 120 void |
| 121 QueueEntry::SetPacket (Ptr<const Packet> p) |
| 122 { |
| 123 NS_LOG_FUNCTION (this << p); |
| 124 m_packet = p; |
| 125 } |
| 126 |
| 127 Ipv4Header |
| 128 QueueEntry::GetIpv4Header () const |
| 129 { |
| 130 return m_header; |
| 131 } |
| 132 |
| 133 void |
| 134 QueueEntry::SetIpv4Header (Ipv4Header h) |
| 135 { |
| 136 NS_LOG_FUNCTION (this << h); |
| 137 m_header = h; |
| 138 } |
| 139 |
| 140 void |
| 141 QueueEntry::SetExpireTime (Time exp) |
| 142 { |
| 143 NS_LOG_FUNCTION (this << exp); |
| 144 m_expire = exp; |
| 145 } |
| 146 |
| 147 Time |
| 148 QueueEntry::GetExpireTime () const |
| 149 { |
| 150 return m_expire; |
| 151 } |
| 152 |
| 153 uint32_t |
| 154 QueueEntry::GetPacketID () const |
| 155 { |
| 156 return m_packetID; |
| 157 } |
| 158 |
| 159 void |
| 160 QueueEntry::SetPacketID (uint32_t id) |
| 161 { |
| 162 NS_LOG_FUNCTION (this << id); |
| 163 m_packetID = id; |
| 164 } |
| 165 |
| 166 |
| 167 |
| 168 PacketQueue::PacketQueue (uint32_t maxLen) |
| 169 { |
| 170 NS_LOG_FUNCTION (this << maxLen); |
| 171 m_maxLen = maxLen; |
| 172 } |
| 173 |
| 174 uint32_t |
| 175 PacketQueue::GetSize () |
| 176 { |
| 177 return m_map.size (); |
| 178 } |
| 179 |
| 180 bool |
| 181 PacketQueue::Enqueue (QueueEntry & entry) |
| 182 { |
| 183 NS_LOG_FUNCTION (this << entry.GetPacketID ()); |
| 184 // Add or update the entry |
| 185 m_map[entry.GetPacketID ()] = entry; |
| 186 Purge (true); |
| 187 return true; |
| 188 } |
| 189 |
| 190 bool |
| 191 PacketQueue::Dequeue (QueueEntry& entry) |
| 192 { |
| 193 NS_LOG_FUNCTION (this << entry.GetPacketID ()); |
| 194 if (m_map.size () > 0) |
| 195 { |
| 196 PacketIdMap::iterator entry_map = m_map.begin (); |
| 197 entry = entry_map->second; |
| 198 m_map.erase (entry_map); |
| 199 return true; |
| 200 } |
| 201 return false; |
| 202 } |
| 203 |
| 204 uint32_t |
| 205 PacketQueue::GetMaxQueueLen () const |
| 206 { |
| 207 NS_LOG_FUNCTION (this ); |
| 208 return m_maxLen; |
| 209 } |
| 210 |
| 211 void |
| 212 PacketQueue::SetMaxQueueLen (uint32_t len) |
| 213 { |
| 214 NS_LOG_FUNCTION (this << len); |
| 215 m_maxLen = len; |
| 216 } |
| 217 |
| 218 QueueEntry |
| 219 PacketQueue::Find (uint32_t packetID) |
| 220 { |
| 221 NS_LOG_FUNCTION (this << packetID); |
| 222 PacketIdMap::iterator entry = m_map.find (packetID); |
| 223 if (entry != m_map.end ()) |
| 224 { |
| 225 return entry->second; |
| 226 } |
| 227 return QueueEntry (); |
| 228 } |
| 229 |
| 230 |
| 231 // static |
| 232 bool |
| 233 PacketQueue::IsEarlier (const PacketIdMapPair & e1, |
| 234 const PacketIdMapPair & e2) |
| 235 { |
| 236 return (e1.second.GetExpireTime () < e2.second.GetExpireTime ()); |
| 237 } |
| 238 |
| 239 |
| 240 void |
| 241 PacketQueue::Purge (bool outdated /* = false */) |
| 242 { |
| 243 NS_LOG_FUNCTION (this << outdated); |
| 244 if (outdated && m_map.size () > m_maxLen) |
| 245 { |
| 246 Drop (std::min_element (m_map.begin (), m_map.end (), IsEarlier), |
| 247 "Drop the oldest packet"); |
| 248 } |
| 249 else |
| 250 { |
| 251 for (PacketIdMap::iterator i = m_map.begin (); |
| 252 i != m_map.end (); ++i) |
| 253 { |
| 254 if (i->second.GetExpireTime () < Now ()) |
| 255 { |
| 256 Drop (i, "Drop outdated packet "); |
| 257 } |
| 258 } |
| 259 } |
| 260 } |
| 261 |
| 262 void |
| 263 PacketQueue::Drop (PacketIdMap::iterator en, std::string reason) |
| 264 { |
| 265 NS_LOG_FUNCTION (this << en->second.GetPacketID () << reason); |
| 266 m_map.erase (en->second.GetPacketID ()); |
| 267 } |
| 268 |
| 269 |
| 270 |
| 271 SummaryVectorHeader |
| 272 PacketQueue::GetSummaryVector () |
| 273 { |
| 274 NS_LOG_FUNCTION (this ); |
| 275 Purge (true); |
| 276 SummaryVectorHeader sm (m_map.size ()); |
| 277 for (PacketIdMap::iterator i = m_map.begin (); i != m_map.end (); ++i) |
| 278 { |
| 279 sm.Add (i->first); |
| 280 } |
| 281 return sm; |
| 282 } |
| 283 |
| 284 |
| 285 SummaryVectorHeader |
| 286 PacketQueue::FindDisjointPackets (SummaryVectorHeader list) |
| 287 { |
| 288 NS_LOG_FUNCTION (this << list); |
| 289 SummaryVectorHeader sm (std::min (m_map.size (), list.Size ())); |
| 290 for (PacketIdMap::iterator i = m_map.begin (); i != m_map.end (); ++i) |
| 291 { |
| 292 if (!list.Contains (i->first)) |
| 293 { |
| 294 sm.Add (i->first); |
| 295 } |
| 296 } |
| 297 return sm; |
| 298 } |
| 299 |
| 300 |
| 301 void |
| 302 PacketQueue::DropExpiredPackets () |
| 303 { |
| 304 NS_LOG_FUNCTION (this ); |
| 305 Purge (true); |
| 306 } |
| 307 |
| 308 } //end namespace epidemic |
| 309 } //end namespace ns3 |
OLD | NEW |