blob: aff2e93ec433e0947de7cbb37919b6ea3a271503 [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
25using namespace ns3;
26
27NS_LOG_COMPONENT_DEFINE ("StatsTree");
28
Alexander Afanasyeve77db792012-08-09 11:10:58 -070029namespace ns3
30{
Alexander Afanasyev0845c092012-07-13 17:45:33 -070031namespace ndnSIM
32{
33
34StatsTree::StatsTree ()
35 : m_tree ("")
36{
37}
38
39void
40StatsTree::Step ()
41{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070042 NS_LOG_FUNCTION (this);
43
Alexander Afanasyev0845c092012-07-13 17:45:33 -070044 // walking the tree, aggregating and stepping on every node, starting the leaves
45 // for (trie_type::
46
47 WalkLeftRightRoot (&m_tree);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070048 m_tree.payload ().Step ();
49 NS_LOG_DEBUG ("[" << m_tree.key () << "] " << m_tree.payload ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070050}
51
52void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070053StatsTree::NewPitEntry (const ns3::NdnNameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070054{
55 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070056
57 item.first->payload ().NewPitEntry ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -070058}
59
60void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070061StatsTree::Incoming (const NdnNameComponents &key, Ptr<NdnFace> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070062{
63 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070064
65 item.first->payload ().AddIncoming (face);
66}
67
68void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070069StatsTree::Outgoing (const NdnNameComponents &key, Ptr<NdnFace> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070070{
71 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070072
73 item.first->payload ().AddOutgoing (face);
74}
75
76void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070077StatsTree::Satisfy (const NdnNameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070078{
79 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070080
81 item.first->payload ().Satisfy ();
82}
83
84void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070085StatsTree::Timeout (const NdnNameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070086{
87 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070088
89 item.first->payload ().Timeout ();
90}
91
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070092void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070093StatsTree::Rx (const ns3::NdnNameComponents &key, ns3::Ptr<ns3::NdnFace> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070094{
95 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
96
97 item.first->payload ().Rx (face, amount);
98}
99
100void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700101StatsTree::Tx (const ns3::NdnNameComponents &key, ns3::Ptr<ns3::NdnFace> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700102{
103 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
104
105 item.first->payload ().Tx (face, amount);
106}
107
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700108// const LoadStatsNode &
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700109// StatsTree::Get (const ns3::NdnNameComponents &key) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700110const LoadStatsNode &
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700111StatsTree::operator [] (const ns3::NdnNameComponents &key) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700112{
113 tree_type::iterator foundItem, lastItem;
114 bool reachLast;
115 boost::tie (foundItem, reachLast, lastItem) = const_cast<tree_type&> (m_tree).find (key);
116
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700117 return lastItem->payload ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700118}
119
120const LoadStatsNode&
121StatsTree::WalkLeftRightRoot (tree_type *node)
122{
123 tree_type::point_iterator item (*node), end;
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700124
125 while (item != end)
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700126 {
127 node->payload () += WalkLeftRightRoot (&*item);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700128 item->payload ().Step ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700129
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700130 NS_LOG_DEBUG ("[" << item->key () << "] " << item->payload ());
131 // item->prune (); // will do only if necessary
132
133 tree_type::point_iterator prune_iterator = item;
134 item++;
135
136 prune_iterator->prune ();
137 }
138
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700139 return node->payload ();
140}
141
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700142void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700143StatsTree::RemoveFace (ns3::Ptr<ns3::NdnFace> face)
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700144{
145 tree_type::recursive_iterator item (&m_tree), end;
146 for (; item != end; item ++)
147 {
148 item->payload ().RemoveFace (face);
149 }
150}
151
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700152std::ostream &
153operator << (std::ostream &os, const StatsTree &tree)
154{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700155 // os << "[" << tree.m_tree.key () << "]: " << tree.m_tree.payload ();
156 os << tree.m_tree;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700157 return os;
158}
159
160
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700161} // ndnSIM
162} // ns3
163