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 |
| 96 | LoadStatsNode::Timeout () |
| 97 | { |
| 98 | m_pit.unsatisfied ()++; |
| 99 | |
| 100 | for (stats_container::iterator item = m_incoming.begin (); |
| 101 | item != m_incoming.end (); |
| 102 | item ++) |
| 103 | { |
| 104 | item->second.unsatisfied ()++; |
| 105 | } |
| 106 | |
| 107 | for (stats_container::iterator item = m_outgoing.begin (); |
| 108 | item != m_outgoing.end (); |
| 109 | item ++) |
| 110 | { |
| 111 | item->second.unsatisfied ()++; |
| 112 | } |
| 113 | } |
| 114 | |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 115 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 116 | LoadStatsNode::Rx (ns3::Ptr<Face> face, uint32_t amount) |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 117 | { |
| 118 | m_pit.rx () += amount; |
| 119 | m_incoming [face].rx () += amount; |
| 120 | } |
| 121 | |
| 122 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 123 | LoadStatsNode::Tx (ns3::Ptr<Face> face, uint32_t amount) |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 124 | { |
| 125 | m_pit.tx () += amount; |
| 126 | m_outgoing [face].tx () += amount; |
| 127 | } |
| 128 | |
| 129 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 130 | LoadStatsNode & |
| 131 | LoadStatsNode::operator += (const LoadStatsNode &stats) |
| 132 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 133 | NS_LOG_FUNCTION (this << &stats); |
| 134 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 135 | m_pit += stats.m_pit; |
| 136 | |
| 137 | // aggregate incoming |
| 138 | for (stats_container::const_iterator item = stats.m_incoming.begin (); |
| 139 | item != stats.m_incoming.end (); |
| 140 | item ++) |
| 141 | { |
| 142 | m_incoming [item->first] += item->second; |
| 143 | } |
| 144 | |
| 145 | // aggregate outgoing |
| 146 | for (stats_container::const_iterator item = stats.m_outgoing.begin (); |
| 147 | item != stats.m_outgoing.end (); |
| 148 | item ++) |
| 149 | { |
| 150 | m_outgoing [item->first] += item->second; |
| 151 | } |
| 152 | |
| 153 | return *this; |
| 154 | } |
| 155 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 156 | bool |
| 157 | LoadStatsNode::IsZero () const |
| 158 | { |
| 159 | bool zero = true; |
Alexander Afanasyev | 1cb4aad | 2012-08-09 14:58:16 -0700 | [diff] [blame] | 160 | for (stats_container::const_iterator item = m_incoming.begin (); |
| 161 | item != m_incoming.end (); |
| 162 | item ++) |
| 163 | { |
| 164 | zero &= item->second.IsZero (); |
| 165 | } |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 166 | |
Alexander Afanasyev | 1cb4aad | 2012-08-09 14:58:16 -0700 | [diff] [blame] | 167 | for (stats_container::const_iterator item = m_outgoing.begin (); |
| 168 | item != m_outgoing.end (); |
| 169 | item ++) |
| 170 | { |
| 171 | zero &= item->second.IsZero (); |
| 172 | } |
| 173 | |
| 174 | // std::for_each (m_incoming.begin (), m_incoming.end (), |
| 175 | // zero &= ll::bind (&LoadStatsFace::IsZero, |
| 176 | // ll::bind (&stats_container::value_type::second, ll::_1))); |
| 177 | // |
| 178 | // std::for_each (m_outgoing.begin (), m_outgoing.end (), |
| 179 | // zero &= ll::bind (&LoadStatsFace::IsZero, |
| 180 | // ll::bind (&stats_container::value_type::second, ll::_1))); |
| 181 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 182 | zero &= m_pit.IsZero (); |
| 183 | |
| 184 | return zero; |
| 185 | } |
| 186 | |
Alexander Afanasyev | 33364b6 | 2012-07-26 17:53:56 -0700 | [diff] [blame] | 187 | |
| 188 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 189 | LoadStatsNode::RemoveFace (ns3::Ptr<Face> face) |
Alexander Afanasyev | 33364b6 | 2012-07-26 17:53:56 -0700 | [diff] [blame] | 190 | { |
| 191 | NS_LOG_FUNCTION (this); |
| 192 | m_incoming.erase (face); |
| 193 | m_outgoing.erase (face); |
| 194 | } |
| 195 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 196 | bool |
| 197 | LoadStatsNode::operator == (const LoadStatsNode &other) const |
| 198 | { |
| 199 | if (other.m_incoming.size () > 0 || |
| 200 | other.m_outgoing.size () > 0 || |
| 201 | !other.m_pit.IsZero ()) |
| 202 | return false; |
| 203 | |
| 204 | return IsZero (); |
| 205 | } |
| 206 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 207 | std::ostream& |
| 208 | operator << (std::ostream &os, const LoadStatsNode &node) |
| 209 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 210 | os << "PIT: " << node.m_pit;// << std::endl; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 211 | return os; |
| 212 | } |
| 213 | |
| 214 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 215 | } // namespace ndnSIM |
| 216 | } // namespace ndn |
| 217 | } // namespace ns3 |