Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -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 "load-stats-node.h" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 22 | #include "ns3/ndn-face.h" |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 23 | #include "ns3/log.h" |
| 24 | #include <boost/lambda/lambda.hpp> |
| 25 | #include <boost/lambda/bind.hpp> |
| 26 | |
| 27 | namespace ll = boost::lambda; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 29 | NS_LOG_COMPONENT_DEFINE ("ndn.LoadStatsNode"); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 31 | namespace ns3 { |
| 32 | namespace ndn { |
| 33 | namespace ndnSIM { |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 34 | |
| 35 | void |
| 36 | LoadStatsNode::Step () |
| 37 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 38 | NS_LOG_FUNCTION (this); |
| 39 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 40 | m_pit.Step (); |
| 41 | |
| 42 | for (stats_container::iterator item = m_incoming.begin (); |
| 43 | item != m_incoming.end (); |
| 44 | item ++) |
| 45 | { |
| 46 | item->second.Step (); |
| 47 | } |
| 48 | |
| 49 | for (stats_container::iterator item = m_outgoing.begin (); |
| 50 | item != m_outgoing.end (); |
| 51 | item ++) |
| 52 | { |
| 53 | item->second.Step (); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | LoadStatsNode::NewPitEntry () |
| 59 | { |
| 60 | m_pit.count ()++; |
| 61 | } |
| 62 | |
| 63 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 64 | LoadStatsNode::AddIncoming (ns3::Ptr<Face> face) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 65 | { |
| 66 | m_incoming [face].count ()++; |
| 67 | } |
| 68 | |
| 69 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 70 | LoadStatsNode::AddOutgoing (ns3::Ptr<Face> face) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 71 | { |
| 72 | m_outgoing [face].count ()++; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | LoadStatsNode::Satisfy () |
| 77 | { |
| 78 | m_pit.satisfied ()++; |
| 79 | |
| 80 | for (stats_container::iterator item = m_incoming.begin (); |
| 81 | item != m_incoming.end (); |
| 82 | item ++) |
| 83 | { |
| 84 | item->second.satisfied ()++; |
| 85 | } |
| 86 | |
| 87 | for (stats_container::iterator item = m_outgoing.begin (); |
| 88 | item != m_outgoing.end (); |
| 89 | item ++) |
| 90 | { |
| 91 | item->second.satisfied ()++; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame] | 96 | LoadStatsNode::RemoveFromStats () |
| 97 | { |
| 98 | m_pit.count ()--; |
| 99 | |
| 100 | for (stats_container::iterator item = m_incoming.begin (); |
| 101 | item != m_incoming.end (); |
| 102 | item ++) |
| 103 | { |
| 104 | item->second.count ()--; |
| 105 | } |
| 106 | |
| 107 | for (stats_container::iterator item = m_outgoing.begin (); |
| 108 | item != m_outgoing.end (); |
| 109 | item ++) |
| 110 | { |
| 111 | item->second.count ()--; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 116 | LoadStatsNode::Timeout () |
| 117 | { |
| 118 | m_pit.unsatisfied ()++; |
| 119 | |
| 120 | for (stats_container::iterator item = m_incoming.begin (); |
| 121 | item != m_incoming.end (); |
| 122 | item ++) |
| 123 | { |
| 124 | item->second.unsatisfied ()++; |
| 125 | } |
| 126 | |
| 127 | for (stats_container::iterator item = m_outgoing.begin (); |
| 128 | item != m_outgoing.end (); |
| 129 | item ++) |
| 130 | { |
| 131 | item->second.unsatisfied ()++; |
| 132 | } |
| 133 | } |
| 134 | |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 135 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 136 | LoadStatsNode::Rx (ns3::Ptr<Face> face, uint32_t amount) |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 137 | { |
| 138 | m_pit.rx () += amount; |
| 139 | m_incoming [face].rx () += amount; |
| 140 | } |
| 141 | |
| 142 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 143 | LoadStatsNode::Tx (ns3::Ptr<Face> face, uint32_t amount) |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 144 | { |
| 145 | m_pit.tx () += amount; |
| 146 | m_outgoing [face].tx () += amount; |
| 147 | } |
| 148 | |
| 149 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 150 | LoadStatsNode & |
| 151 | LoadStatsNode::operator += (const LoadStatsNode &stats) |
| 152 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 153 | NS_LOG_FUNCTION (this << &stats); |
| 154 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 155 | m_pit += stats.m_pit; |
| 156 | |
| 157 | // aggregate incoming |
| 158 | for (stats_container::const_iterator item = stats.m_incoming.begin (); |
| 159 | item != stats.m_incoming.end (); |
| 160 | item ++) |
| 161 | { |
| 162 | m_incoming [item->first] += item->second; |
| 163 | } |
| 164 | |
| 165 | // aggregate outgoing |
| 166 | for (stats_container::const_iterator item = stats.m_outgoing.begin (); |
| 167 | item != stats.m_outgoing.end (); |
| 168 | item ++) |
| 169 | { |
| 170 | m_outgoing [item->first] += item->second; |
| 171 | } |
| 172 | |
| 173 | return *this; |
| 174 | } |
| 175 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 176 | bool |
| 177 | LoadStatsNode::IsZero () const |
| 178 | { |
| 179 | bool zero = true; |
Alexander Afanasyev | 1cb4aad | 2012-08-09 14:58:16 -0700 | [diff] [blame] | 180 | for (stats_container::const_iterator item = m_incoming.begin (); |
| 181 | item != m_incoming.end (); |
| 182 | item ++) |
| 183 | { |
| 184 | zero &= item->second.IsZero (); |
| 185 | } |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 186 | |
Alexander Afanasyev | 1cb4aad | 2012-08-09 14:58:16 -0700 | [diff] [blame] | 187 | for (stats_container::const_iterator item = m_outgoing.begin (); |
| 188 | item != m_outgoing.end (); |
| 189 | item ++) |
| 190 | { |
| 191 | zero &= item->second.IsZero (); |
| 192 | } |
| 193 | |
| 194 | // std::for_each (m_incoming.begin (), m_incoming.end (), |
| 195 | // zero &= ll::bind (&LoadStatsFace::IsZero, |
| 196 | // ll::bind (&stats_container::value_type::second, ll::_1))); |
| 197 | // |
| 198 | // std::for_each (m_outgoing.begin (), m_outgoing.end (), |
| 199 | // zero &= ll::bind (&LoadStatsFace::IsZero, |
| 200 | // ll::bind (&stats_container::value_type::second, ll::_1))); |
| 201 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 202 | zero &= m_pit.IsZero (); |
| 203 | |
| 204 | return zero; |
| 205 | } |
| 206 | |
Alexander Afanasyev | 33364b6 | 2012-07-26 17:53:56 -0700 | [diff] [blame] | 207 | |
| 208 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 209 | LoadStatsNode::RemoveFace (ns3::Ptr<Face> face) |
Alexander Afanasyev | 33364b6 | 2012-07-26 17:53:56 -0700 | [diff] [blame] | 210 | { |
| 211 | NS_LOG_FUNCTION (this); |
| 212 | m_incoming.erase (face); |
| 213 | m_outgoing.erase (face); |
| 214 | } |
| 215 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 216 | bool |
| 217 | LoadStatsNode::operator == (const LoadStatsNode &other) const |
| 218 | { |
| 219 | if (other.m_incoming.size () > 0 || |
| 220 | other.m_outgoing.size () > 0 || |
| 221 | !other.m_pit.IsZero ()) |
| 222 | return false; |
| 223 | |
| 224 | return IsZero (); |
| 225 | } |
| 226 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 227 | std::ostream& |
| 228 | operator << (std::ostream &os, const LoadStatsNode &node) |
| 229 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 230 | os << "PIT: " << node.m_pit;// << std::endl; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 231 | return os; |
| 232 | } |
| 233 | |
| 234 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 235 | } // namespace ndnSIM |
| 236 | } // namespace ndn |
| 237 | } // namespace ns3 |