blob: 59184c84ce733712ae113ad4b57d18fcd0995cff [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
116LoadStatsNode &
117LoadStatsNode::operator += (const LoadStatsNode &stats)
118{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700119 NS_LOG_FUNCTION (this << &stats);
120
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700121 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 Afanasyev0560eec2012-07-16 15:44:31 -0700142bool
143LoadStatsNode::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
158bool
159LoadStatsNode::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 Afanasyev0845c092012-07-13 17:45:33 -0700169std::ostream&
170operator << (std::ostream &os, const LoadStatsNode &node)
171{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700172 os << "PIT: " << node.m_pit;// << std::endl;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700173 return os;
174}
175
176
177}