blob: e64bb9d2aa287c99a52098d24a49d71d67548617 [file] [log] [blame]
Alexander Afanasyev0845c092012-07-13 17:45:33 -07001/* -*- 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 Afanasyev0560eec2012-07-16 15:44:31 -070023#include "ns3/log.h"
24#include <boost/lambda/lambda.hpp>
25#include <boost/lambda/bind.hpp>
26
27namespace ll = boost::lambda;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070028
29using namespace ns3;
30
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070031NS_LOG_COMPONENT_DEFINE ("LoadStatsNode");
32
Alexander Afanasyev0845c092012-07-13 17:45:33 -070033namespace ndnSIM
34{
35
36void
37LoadStatsNode::Step ()
38{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070039 NS_LOG_FUNCTION (this);
40
Alexander Afanasyev0845c092012-07-13 17:45:33 -070041 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
58void
59LoadStatsNode::NewPitEntry ()
60{
61 m_pit.count ()++;
62}
63
64void
65LoadStatsNode::AddIncoming (ns3::Ptr<ns3::CcnxFace> face)
66{
67 m_incoming [face].count ()++;
68}
69
70void
71LoadStatsNode::AddOutgoing (ns3::Ptr<ns3::CcnxFace> face)
72{
73 m_outgoing [face].count ()++;
74}
75
76void
77LoadStatsNode::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
96void
97LoadStatsNode::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 Afanasyev1c0248b2012-07-24 15:59:50 -0700116void
117LoadStatsNode::Rx (ns3::Ptr<ns3::CcnxFace> face, uint32_t amount)
118{
119 m_pit.rx () += amount;
120 m_incoming [face].rx () += amount;
121}
122
123void
124LoadStatsNode::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 Afanasyev0845c092012-07-13 17:45:33 -0700131LoadStatsNode &
132LoadStatsNode::operator += (const LoadStatsNode &stats)
133{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700134 NS_LOG_FUNCTION (this << &stats);
135
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700136 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 Afanasyev0560eec2012-07-16 15:44:31 -0700157bool
158LoadStatsNode::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
173bool
174LoadStatsNode::operator == (const LoadStatsNode &other) const
175{
176 if (other.m_incoming.size () > 0 ||
177 other.m_outgoing.size () > 0 ||
178 !other.m_pit.IsZero ())
179 return false;
180
181 return IsZero ();
182}
183
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700184std::ostream&
185operator << (std::ostream &os, const LoadStatsNode &node)
186{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700187 os << "PIT: " << node.m_pit;// << std::endl;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700188 return os;
189}
190
191
192}