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