blob: 7a7116ca4ee980c75080ad665aa5fd6cbb270606 [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
29namespace ndnSIM
30{
31
32StatsTree::StatsTree ()
33 : m_tree ("")
34{
35}
36
37void
38StatsTree::Step ()
39{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070040 NS_LOG_FUNCTION (this);
41
Alexander Afanasyev0845c092012-07-13 17:45:33 -070042 // walking the tree, aggregating and stepping on every node, starting the leaves
43 // for (trie_type::
44
45 WalkLeftRightRoot (&m_tree);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070046 m_tree.payload ().Step ();
47 NS_LOG_DEBUG ("[" << m_tree.key () << "] " << m_tree.payload ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070048}
49
50void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070051StatsTree::NewPitEntry (const ns3::NdnNameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070052{
53 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070054
55 item.first->payload ().NewPitEntry ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -070056}
57
58void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070059StatsTree::Incoming (const NdnNameComponents &key, Ptr<NdnFace> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070060{
61 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070062
63 item.first->payload ().AddIncoming (face);
64}
65
66void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070067StatsTree::Outgoing (const NdnNameComponents &key, Ptr<NdnFace> face)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070068{
69 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070070
71 item.first->payload ().AddOutgoing (face);
72}
73
74void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070075StatsTree::Satisfy (const NdnNameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070076{
77 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070078
79 item.first->payload ().Satisfy ();
80}
81
82void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070083StatsTree::Timeout (const NdnNameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070084{
85 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070086
87 item.first->payload ().Timeout ();
88}
89
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070090void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070091StatsTree::Rx (const ns3::NdnNameComponents &key, ns3::Ptr<ns3::NdnFace> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070092{
93 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
94
95 item.first->payload ().Rx (face, amount);
96}
97
98void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070099StatsTree::Tx (const ns3::NdnNameComponents &key, ns3::Ptr<ns3::NdnFace> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700100{
101 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
102
103 item.first->payload ().Tx (face, amount);
104}
105
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700106// const LoadStatsNode &
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700107// StatsTree::Get (const ns3::NdnNameComponents &key) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700108const LoadStatsNode &
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700109StatsTree::operator [] (const ns3::NdnNameComponents &key) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700110{
111 tree_type::iterator foundItem, lastItem;
112 bool reachLast;
113 boost::tie (foundItem, reachLast, lastItem) = const_cast<tree_type&> (m_tree).find (key);
114
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700115 return lastItem->payload ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700116}
117
118const LoadStatsNode&
119StatsTree::WalkLeftRightRoot (tree_type *node)
120{
121 tree_type::point_iterator item (*node), end;
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700122
123 while (item != end)
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700124 {
125 node->payload () += WalkLeftRightRoot (&*item);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700126 item->payload ().Step ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700127
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700128 NS_LOG_DEBUG ("[" << item->key () << "] " << item->payload ());
129 // item->prune (); // will do only if necessary
130
131 tree_type::point_iterator prune_iterator = item;
132 item++;
133
134 prune_iterator->prune ();
135 }
136
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700137 return node->payload ();
138}
139
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700140void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700141StatsTree::RemoveFace (ns3::Ptr<ns3::NdnFace> face)
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700142{
143 tree_type::recursive_iterator item (&m_tree), end;
144 for (; item != end; item ++)
145 {
146 item->payload ().RemoveFace (face);
147 }
148}
149
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700150std::ostream &
151operator << (std::ostream &os, const StatsTree &tree)
152{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700153 // os << "[" << tree.m_tree.key () << "]: " << tree.m_tree.payload ();
154 os << tree.m_tree;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700155 return os;
156}
157
158
159}