blob: f27281ad9aeded2f0297a544fa98d722736a4dee [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) 2012 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 "stats-tree.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ns3/ndn-face.h"
Alexander Afanasyev0845c092012-07-13 17:45:33 -070023#include "ns3/log.h"
24
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070025NS_LOG_COMPONENT_DEFINE ("ndn.StatsTree");
Alexander Afanasyev0845c092012-07-13 17:45:33 -070026
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070027namespace ns3 {
28namespace ndn {
29namespace ndnSIM {
Alexander Afanasyev0845c092012-07-13 17:45:33 -070030
31StatsTree::StatsTree ()
32 : m_tree ("")
33{
34}
35
36void
37StatsTree::Step ()
38{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070039 NS_LOG_FUNCTION (this);
40
Alexander Afanasyev0845c092012-07-13 17:45:33 -070041 // walking the tree, aggregating and stepping on every node, starting the leaves
42 // for (trie_type::
43
44 WalkLeftRightRoot (&m_tree);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070045 m_tree.payload ().Step ();
46 NS_LOG_DEBUG ("[" << m_tree.key () << "] " << m_tree.payload ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070047}
48
49void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070050StatsTree::NewPitEntry (const NameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070051{
52 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070053
54 item.first->payload ().NewPitEntry ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -070055}
56
57void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058StatsTree::Incoming (const NameComponents &key, Ptr<Face> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070059{
60 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070061
62 item.first->payload ().AddIncoming (face);
63}
64
65void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070066StatsTree::Outgoing (const NameComponents &key, Ptr<Face> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070067{
68 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070069
70 item.first->payload ().AddOutgoing (face);
71}
72
73void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074StatsTree::Satisfy (const NameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070075{
76 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070077
78 item.first->payload ().Satisfy ();
79}
80
81void
Alexander Afanasyevc7719612012-08-30 17:42:20 -070082StatsTree::RemoveFromStats (const NameComponents &key)
83{
84 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
85
86 item.first->payload ().RemoveFromStats ();
87}
88
89void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090StatsTree::Timeout (const NameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070091{
92 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070093
94 item.first->payload ().Timeout ();
95}
96
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070097void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098StatsTree::Rx (const NameComponents &key, Ptr<Face> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070099{
100 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
101
102 item.first->payload ().Rx (face, amount);
103}
104
105void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106StatsTree::Tx (const NameComponents &key, Ptr<Face> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700107{
108 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
109
110 item.first->payload ().Tx (face, amount);
111}
112
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700113// const LoadStatsNode &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700114// StatsTree::Get (const NameComponents &key) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700115const LoadStatsNode &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700116StatsTree::operator [] (const NameComponents &key) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700117{
118 tree_type::iterator foundItem, lastItem;
119 bool reachLast;
120 boost::tie (foundItem, reachLast, lastItem) = const_cast<tree_type&> (m_tree).find (key);
121
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700122 return lastItem->payload ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700123}
124
125const LoadStatsNode&
126StatsTree::WalkLeftRightRoot (tree_type *node)
127{
128 tree_type::point_iterator item (*node), end;
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700129
130 while (item != end)
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700131 {
132 node->payload () += WalkLeftRightRoot (&*item);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700133 item->payload ().Step ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700134
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700135 NS_LOG_DEBUG ("[" << item->key () << "] " << item->payload ());
136 // item->prune (); // will do only if necessary
137
138 tree_type::point_iterator prune_iterator = item;
139 item++;
140
Alexander Afanasyev70426a02012-08-15 15:39:18 -0700141 prune_iterator->prune_node ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700142 }
143
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700144 return node->payload ();
145}
146
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700147void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700148StatsTree::RemoveFace (Ptr<Face> face)
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700149{
150 tree_type::recursive_iterator item (&m_tree), end;
151 for (; item != end; item ++)
152 {
153 item->payload ().RemoveFace (face);
154 }
155}
156
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700157std::ostream &
158operator << (std::ostream &os, const StatsTree &tree)
159{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700160 // os << "[" << tree.m_tree.key () << "]: " << tree.m_tree.payload ();
161 os << tree.m_tree;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700162 return os;
163}
164
165
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700166} // namespace ndnSIM
167} // namespace ndn
168} // namespace ns3
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700169