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 | |
| 116 | LoadStatsNode & |
| 117 | LoadStatsNode::operator += (const LoadStatsNode &stats) |
| 118 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 119 | NS_LOG_FUNCTION (this << &stats); |
| 120 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 121 | m_pit += stats.m_pit; |
| 122 | |
| 123 | // aggregate incoming |
| 124 | for (stats_container::const_iterator item = stats.m_incoming.begin (); |
| 125 | item != stats.m_incoming.end (); |
| 126 | item ++) |
| 127 | { |
| 128 | m_incoming [item->first] += item->second; |
| 129 | } |
| 130 | |
| 131 | // aggregate outgoing |
| 132 | for (stats_container::const_iterator item = stats.m_outgoing.begin (); |
| 133 | item != stats.m_outgoing.end (); |
| 134 | item ++) |
| 135 | { |
| 136 | m_outgoing [item->first] += item->second; |
| 137 | } |
| 138 | |
| 139 | return *this; |
| 140 | } |
| 141 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 142 | bool |
| 143 | LoadStatsNode::IsZero () const |
| 144 | { |
| 145 | bool zero = true; |
| 146 | std::for_each (m_incoming.begin (), m_incoming.end (), |
| 147 | zero &= ll::bind (&LoadStatsFace::IsZero, |
| 148 | ll::bind (&stats_container::value_type::second, ll::_1))); |
| 149 | |
| 150 | std::for_each (m_outgoing.begin (), m_outgoing.end (), |
| 151 | zero &= ll::bind (&LoadStatsFace::IsZero, |
| 152 | ll::bind (&stats_container::value_type::second, ll::_1))); |
| 153 | zero &= m_pit.IsZero (); |
| 154 | |
| 155 | return zero; |
| 156 | } |
| 157 | |
| 158 | bool |
| 159 | LoadStatsNode::operator == (const LoadStatsNode &other) const |
| 160 | { |
| 161 | if (other.m_incoming.size () > 0 || |
| 162 | other.m_outgoing.size () > 0 || |
| 163 | !other.m_pit.IsZero ()) |
| 164 | return false; |
| 165 | |
| 166 | return IsZero (); |
| 167 | } |
| 168 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 169 | std::ostream& |
| 170 | operator << (std::ostream &os, const LoadStatsNode &node) |
| 171 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 172 | os << "PIT: " << node.m_pit;// << std::endl; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 173 | return os; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | } |