Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 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: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "stats-based-randomized-interest-accept.h" |
| 22 | |
| 23 | #include "ns3/ndn-interest-header.h" |
| 24 | #include "ns3/ndn-content-object-header.h" |
| 25 | #include "ns3/ndn-pit.h" |
| 26 | #include "ns3/ndn-pit-entry.h" |
| 27 | |
| 28 | #include "ns3/assert.h" |
| 29 | #include "ns3/log.h" |
| 30 | #include "ns3/simulator.h" |
| 31 | #include "ns3/random-variable.h" |
| 32 | #include "ns3/double.h" |
| 33 | |
| 34 | #include <boost/foreach.hpp> |
| 35 | #include <boost/lambda/lambda.hpp> |
| 36 | #include <boost/lambda/bind.hpp> |
| 37 | namespace ll = boost::lambda; |
| 38 | |
| 39 | NS_LOG_COMPONENT_DEFINE ("ndn.fw.StatsBasedRandomizedInterestAccept"); |
| 40 | |
| 41 | namespace ns3 { |
| 42 | namespace ndn { |
| 43 | namespace fw { |
| 44 | |
| 45 | NS_OBJECT_ENSURE_REGISTERED (StatsBasedRandomizedInterestAccept); |
| 46 | |
| 47 | TypeId |
| 48 | StatsBasedRandomizedInterestAccept::GetTypeId (void) |
| 49 | { |
| 50 | static TypeId tid = TypeId ("ns3::ndn::fw::StatsBasedRandomizedInterestAccept") |
| 51 | .SetGroupName ("Ndn") |
| 52 | .SetParent <super> () |
| 53 | .AddConstructor <StatsBasedRandomizedInterestAccept> () |
| 54 | |
| 55 | .AddAttribute ("Threshold", "Minimum number of incoming interests to enable dropping decision", |
| 56 | DoubleValue (0.25), |
| 57 | MakeDoubleAccessor (&StatsBasedRandomizedInterestAccept::m_threshold), |
| 58 | MakeDoubleChecker<double> ()) |
| 59 | |
| 60 | .AddAttribute ("GraceAcceptProbability", "Probability to accept Interest even though stats telling that satisfaction ratio is 0", |
| 61 | DoubleValue (0.01), |
| 62 | MakeDoubleAccessor (&StatsBasedRandomizedInterestAccept::m_graceAcceptProbability), |
| 63 | MakeDoubleChecker<double> ()) |
| 64 | ; |
| 65 | return tid; |
| 66 | } |
| 67 | |
| 68 | StatsBasedRandomizedInterestAccept::StatsBasedRandomizedInterestAccept () |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | StatsBasedRandomizedInterestAccept::DoDispose () |
| 74 | { |
| 75 | super::DoDispose (); |
| 76 | } |
| 77 | |
| 78 | bool |
| 79 | StatsBasedRandomizedInterestAccept::WillSendOutInterest (Ptr<Face> outFace, |
| 80 | Ptr<const InterestHeader> header, |
| 81 | Ptr<pit::Entry> pitEntry) |
| 82 | { |
| 83 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 84 | // override all (if any) parent processing |
| 85 | |
| 86 | pit::Entry::out_iterator outgoing = |
| 87 | pitEntry->GetOutgoing ().find (outFace); |
| 88 | |
| 89 | if (outgoing != pitEntry->GetOutgoing ().end ()) |
| 90 | { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // check stats |
| 95 | Ptr<Face> inFace = pitEntry->GetIncoming ().begin ()->m_face; |
| 96 | // const ndnSIM::LoadStatsFace &stats = GetStatsTree ()[header->GetName ()].incoming ().find (inFace)->second; |
| 97 | const ndnSIM::LoadStatsFace &stats = GetStatsTree ()["/"].incoming ().find (inFace)->second; |
| 98 | |
| 99 | if (stats.count ().GetStats ().get<0> () >= m_threshold * pitEntry->GetFibEntry ()->GetLimits ().GetMaxLimit ()) |
| 100 | { |
| 101 | double ratio = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ()); |
| 102 | // NS_ASSERT_MSG (ratio > 0, "If count is a reasonable value, ratio cannot be negative"); |
| 103 | UniformVariable randAccept (0, 1); |
| 104 | double dice = randAccept.GetValue (); |
| 105 | if (ratio < 0 || dice < ratio + m_graceAcceptProbability) |
| 106 | { |
| 107 | // ok, accepting the interests |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | // boo. bad luck |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (pitEntry->GetFibEntry ()->GetLimits ().IsBelowLimit ()) |
| 117 | { |
| 118 | if (outFace->GetLimits ().IsBelowLimit ()) |
| 119 | { |
| 120 | pitEntry->AddOutgoing (outFace); |
| 121 | return true; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | NS_LOG_DEBUG ("Face limit. Reverting back per-prefix allowance"); |
| 126 | pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding (); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | StatsBasedRandomizedInterestAccept::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry) |
| 135 | { |
| 136 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 137 | |
| 138 | for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin (); |
| 139 | face != pitEntry->GetOutgoing ().end (); |
| 140 | face ++) |
| 141 | { |
| 142 | face->m_face->GetLimits ().RemoveOutstanding (); |
| 143 | } |
| 144 | |
| 145 | pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding (); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void |
| 150 | StatsBasedRandomizedInterestAccept::WillSatisfyPendingInterest (Ptr<Face> inFace, |
| 151 | Ptr<pit::Entry> pitEntry) |
| 152 | { |
| 153 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 154 | |
| 155 | super::WillSatisfyPendingInterest (inFace, pitEntry); |
| 156 | |
| 157 | for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin (); |
| 158 | face != pitEntry->GetOutgoing ().end (); |
| 159 | face ++) |
| 160 | { |
| 161 | face->m_face->GetLimits ().RemoveOutstanding (); |
| 162 | } |
| 163 | |
| 164 | pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding (); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | } // namespace fw |
| 169 | } // namespace ndn |
| 170 | } // namespace ns3 |