blob: 09f3d059bcb0a5880989c3a40aa2916c832955d8 [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-face.h"
22
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070023#include "ns3/log.h"
24
25#include <boost/mpl/for_each.hpp>
26#include <boost/mpl/range_c.hpp>
27
28using namespace boost::mpl;
29using namespace boost::tuples;
30
31NS_LOG_COMPONENT_DEFINE ("LoadStatsFace");
32
Alexander Afanasyev0845c092012-07-13 17:45:33 -070033namespace ndnSIM
34{
35
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070036std::ostream &
37operator << (std::ostream &os, const LoadStats::stats_tuple &tuple);
38
Alexander Afanasyev0845c092012-07-13 17:45:33 -070039void
40LoadStatsFace::Step ()
41{
42 m_count.Step ();
43 m_satisfied.Step ();
44 m_unsatisfied.Step ();
45}
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070046
47struct update_retval
48{
49 update_retval (const LoadStats &count, const LoadStats &other, LoadStats::stats_tuple &tuple)
50 : count_ (count)
51 , other_ (other)
52 , tuple_ (tuple) {}
53 const LoadStats &count_;
54 const LoadStats &other_;
55 LoadStats::stats_tuple &tuple_;
56
57 template< typename U >
58 void operator () (U)
59 {
60 if (count_.GetStats ().get<U::value> () < LoadStats::PRECISION)
61 tuple_.get<U::value> () = -1;
62 else
63 tuple_.get<U::value> () = other_.GetStats ().get<U::value> () / count_.GetStats ().get<U::value> ();
64 }
65};
66
Alexander Afanasyev0845c092012-07-13 17:45:33 -070067LoadStats::stats_tuple
68LoadStatsFace::GetSatisfiedRatio () const
69{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070070 LoadStats::stats_tuple retval;
71 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
72 (update_retval (m_count, m_satisfied, retval));
73
74 NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ());
75 return retval;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070076}
77
78LoadStats::stats_tuple
79LoadStatsFace::GetUnsatisfiedRatio () const
80{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070081 LoadStats::stats_tuple retval;
82 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
83 (update_retval (m_count, m_unsatisfied, retval));
84
85 NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ());
86 return retval;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070087}
88
89LoadStatsFace &
90LoadStatsFace::operator += (const LoadStatsFace &load)
91{
92 m_count += load.m_count;
93 m_satisfied += load.m_satisfied;
94 m_unsatisfied += load.m_unsatisfied;
95
96 return *this;
97}
98
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070099bool
100LoadStatsFace::IsZero () const
101{
102 return m_count.IsZero () && m_satisfied.IsZero () && m_unsatisfied.IsZero ();
103}
104
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700105struct print_tuple
106{
107 print_tuple (const LoadStats::stats_tuple &tuple, std::ostream &os)
108 : tuple_ (tuple)
109 , os_ (os)
110 {
111 }
112
113 const LoadStats::stats_tuple &tuple_;
114 std::ostream &os_;
115
116 template< typename U >
117 void operator () (U)
118 {
119 os_ << tuple_.get< U::value > () << " ";
120 }
121};
122
123
124std::ostream &
125operator << (std::ostream &os, const LoadStats::stats_tuple &tuple)
126{
127 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
128 (print_tuple (tuple, os));
129
130 return os;
131}
132
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700133std::ostream &
134operator << (std::ostream &os, const LoadStatsFace &stats)
135{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700136 LoadStats::stats_tuple
137 satisfied = stats.GetSatisfiedRatio (),
138 unsatisfied = stats.GetUnsatisfiedRatio ();
139
140 os << "ration satisfied: " << satisfied << "/ unsatisfied: " << unsatisfied;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700141 return os;
142}
143
144}