blob: 743f764c75cb67cdd41adbb742d1d62d6b2ce49e [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
Alexander Afanasyevc7719612012-08-30 17:42:20 -070096LoadStatsNode::RemoveFromStats ()
97{
98 m_pit.count ()--;
99
100 for (stats_container::iterator item = m_incoming.begin ();
101 item != m_incoming.end ();
102 item ++)
103 {
104 item->second.count ()--;
105 }
106
107 for (stats_container::iterator item = m_outgoing.begin ();
108 item != m_outgoing.end ();
109 item ++)
110 {
111 item->second.count ()--;
112 }
113}
114
115void
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700116LoadStatsNode::Timeout ()
117{
118 m_pit.unsatisfied ()++;
119
120 for (stats_container::iterator item = m_incoming.begin ();
121 item != m_incoming.end ();
122 item ++)
123 {
124 item->second.unsatisfied ()++;
125 }
126
127 for (stats_container::iterator item = m_outgoing.begin ();
128 item != m_outgoing.end ();
129 item ++)
130 {
131 item->second.unsatisfied ()++;
132 }
133}
134
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700135void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700136LoadStatsNode::Rx (ns3::Ptr<Face> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700137{
138 m_pit.rx () += amount;
139 m_incoming [face].rx () += amount;
140}
141
142void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700143LoadStatsNode::Tx (ns3::Ptr<Face> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700144{
145 m_pit.tx () += amount;
146 m_outgoing [face].tx () += amount;
147}
148
149
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700150LoadStatsNode &
151LoadStatsNode::operator += (const LoadStatsNode &stats)
152{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700153 NS_LOG_FUNCTION (this << &stats);
154
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700155 m_pit += stats.m_pit;
156
157 // aggregate incoming
158 for (stats_container::const_iterator item = stats.m_incoming.begin ();
159 item != stats.m_incoming.end ();
160 item ++)
161 {
162 m_incoming [item->first] += item->second;
163 }
164
165 // aggregate outgoing
166 for (stats_container::const_iterator item = stats.m_outgoing.begin ();
167 item != stats.m_outgoing.end ();
168 item ++)
169 {
170 m_outgoing [item->first] += item->second;
171 }
172
173 return *this;
174}
175
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700176bool
177LoadStatsNode::IsZero () const
178{
179 bool zero = true;
Alexander Afanasyev1cb4aad2012-08-09 14:58:16 -0700180 for (stats_container::const_iterator item = m_incoming.begin ();
181 item != m_incoming.end ();
182 item ++)
183 {
184 zero &= item->second.IsZero ();
185 }
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700186
Alexander Afanasyev1cb4aad2012-08-09 14:58:16 -0700187 for (stats_container::const_iterator item = m_outgoing.begin ();
188 item != m_outgoing.end ();
189 item ++)
190 {
191 zero &= item->second.IsZero ();
192 }
193
194// std::for_each (m_incoming.begin (), m_incoming.end (),
195// zero &= ll::bind (&LoadStatsFace::IsZero,
196// ll::bind (&stats_container::value_type::second, ll::_1)));
197//
198// std::for_each (m_outgoing.begin (), m_outgoing.end (),
199// zero &= ll::bind (&LoadStatsFace::IsZero,
200// ll::bind (&stats_container::value_type::second, ll::_1)));
201
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700202 zero &= m_pit.IsZero ();
203
204 return zero;
205}
206
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700207
208void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700209LoadStatsNode::RemoveFace (ns3::Ptr<Face> face)
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700210{
211 NS_LOG_FUNCTION (this);
212 m_incoming.erase (face);
213 m_outgoing.erase (face);
214}
215
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700216bool
217LoadStatsNode::operator == (const LoadStatsNode &other) const
218{
219 if (other.m_incoming.size () > 0 ||
220 other.m_outgoing.size () > 0 ||
221 !other.m_pit.IsZero ())
222 return false;
223
224 return IsZero ();
225}
226
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700227std::ostream&
228operator << (std::ostream &os, const LoadStatsNode &node)
229{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700230 os << "PIT: " << node.m_pit;// << std::endl;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700231 return os;
232}
233
234
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700235} // namespace ndnSIM
236} // namespace ndn
237} // namespace ns3