blob: 39e9d603ba6ad118ef6a81f2671ea6fa324f826f [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"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ns3/ndn-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
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070029NS_LOG_COMPONENT_DEFINE ("ndn.LoadStatsNode");
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070030
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070031namespace ns3 {
32namespace ndn {
33namespace ndnSIM {
Alexander Afanasyev0845c092012-07-13 17:45:33 -070034
35void
36LoadStatsNode::Step ()
37{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070038 NS_LOG_FUNCTION (this);
39
Alexander Afanasyev0845c092012-07-13 17:45:33 -070040 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
57void
58LoadStatsNode::NewPitEntry ()
59{
60 m_pit.count ()++;
61}
62
63void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064LoadStatsNode::AddIncoming (ns3::Ptr<Face> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070065{
66 m_incoming [face].count ()++;
67}
68
69void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070070LoadStatsNode::AddOutgoing (ns3::Ptr<Face> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070071{
72 m_outgoing [face].count ()++;
73}
74
75void
76LoadStatsNode::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
95void
96LoadStatsNode::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 Afanasyev1c0248b2012-07-24 15:59:50 -0700115void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700116LoadStatsNode::Rx (ns3::Ptr<Face> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700117{
118 m_pit.rx () += amount;
119 m_incoming [face].rx () += amount;
120}
121
122void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700123LoadStatsNode::Tx (ns3::Ptr<Face> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700124{
125 m_pit.tx () += amount;
126 m_outgoing [face].tx () += amount;
127}
128
129
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700130LoadStatsNode &
131LoadStatsNode::operator += (const LoadStatsNode &stats)
132{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700133 NS_LOG_FUNCTION (this << &stats);
134
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700135 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 Afanasyev0560eec2012-07-16 15:44:31 -0700156bool
157LoadStatsNode::IsZero () const
158{
159 bool zero = true;
Alexander Afanasyev1cb4aad2012-08-09 14:58:16 -0700160 for (stats_container::const_iterator item = m_incoming.begin ();
161 item != m_incoming.end ();
162 item ++)
163 {
164 zero &= item->second.IsZero ();
165 }
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700166
Alexander Afanasyev1cb4aad2012-08-09 14:58:16 -0700167 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 Afanasyev0560eec2012-07-16 15:44:31 -0700182 zero &= m_pit.IsZero ();
183
184 return zero;
185}
186
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700187
188void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700189LoadStatsNode::RemoveFace (ns3::Ptr<Face> face)
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700190{
191 NS_LOG_FUNCTION (this);
192 m_incoming.erase (face);
193 m_outgoing.erase (face);
194}
195
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700196bool
197LoadStatsNode::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 Afanasyev0845c092012-07-13 17:45:33 -0700207std::ostream&
208operator << (std::ostream &os, const LoadStatsNode &node)
209{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700210 os << "PIT: " << node.m_pit;// << std::endl;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700211 return os;
212}
213
214
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700215} // namespace ndnSIM
216} // namespace ndn
217} // namespace ns3