blob: 86a149d38f4604ff003a658e58d03a865b13dc5c [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 Afanasyev0560eec2012-07-16 15:44:31 -070029NS_LOG_COMPONENT_DEFINE ("LoadStatsNode");
30
Alexander Afanasyeve77db792012-08-09 11:10:58 -070031namespace ns3
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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065LoadStatsNode::AddIncoming (ns3::Ptr<ns3::NdnFace> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070066{
67 m_incoming [face].count ()++;
68}
69
70void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070071LoadStatsNode::AddOutgoing (ns3::Ptr<ns3::NdnFace> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070072{
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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700117LoadStatsNode::Rx (ns3::Ptr<ns3::NdnFace> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700118{
119 m_pit.rx () += amount;
120 m_incoming [face].rx () += amount;
121}
122
123void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700124LoadStatsNode::Tx (ns3::Ptr<ns3::NdnFace> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700125{
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;
Alexander Afanasyev1cb4aad2012-08-09 14:58:16 -0700161 for (stats_container::const_iterator item = m_incoming.begin ();
162 item != m_incoming.end ();
163 item ++)
164 {
165 zero &= item->second.IsZero ();
166 }
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700167
Alexander Afanasyev1cb4aad2012-08-09 14:58:16 -0700168 for (stats_container::const_iterator item = m_outgoing.begin ();
169 item != m_outgoing.end ();
170 item ++)
171 {
172 zero &= item->second.IsZero ();
173 }
174
175// std::for_each (m_incoming.begin (), m_incoming.end (),
176// zero &= ll::bind (&LoadStatsFace::IsZero,
177// ll::bind (&stats_container::value_type::second, ll::_1)));
178//
179// std::for_each (m_outgoing.begin (), m_outgoing.end (),
180// zero &= ll::bind (&LoadStatsFace::IsZero,
181// ll::bind (&stats_container::value_type::second, ll::_1)));
182
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700183 zero &= m_pit.IsZero ();
184
185 return zero;
186}
187
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700188
189void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700190LoadStatsNode::RemoveFace (ns3::Ptr<ns3::NdnFace> face)
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700191{
192 NS_LOG_FUNCTION (this);
193 m_incoming.erase (face);
194 m_outgoing.erase (face);
195}
196
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700197bool
198LoadStatsNode::operator == (const LoadStatsNode &other) const
199{
200 if (other.m_incoming.size () > 0 ||
201 other.m_outgoing.size () > 0 ||
202 !other.m_pit.IsZero ())
203 return false;
204
205 return IsZero ();
206}
207
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700208std::ostream&
209operator << (std::ostream &os, const LoadStatsNode &node)
210{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700211 os << "PIT: " << node.m_pit;// << std::endl;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700212 return os;
213}
214
215
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700216} // ndnSIM
217} // ns3