blob: f84d902f719a7e06e804da90938d6f1c59d7197b [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 Afanasyev2b4c9472012-08-09 15:00:38 -070082StatsTree::Timeout (const NameComponents &key)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070083{
84 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -070085
86 item.first->payload ().Timeout ();
87}
88
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070089void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090StatsTree::Rx (const NameComponents &key, Ptr<Face> face, uint32_t amount)
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070091{
92 std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ());
93
94 item.first->payload ().Rx (face, amount);
95}
96
97void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098StatsTree::Tx (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 ().Tx (face, amount);
103}
104
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700105// const LoadStatsNode &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106// StatsTree::Get (const NameComponents &key) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700107const LoadStatsNode &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700108StatsTree::operator [] (const NameComponents &key) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700109{
110 tree_type::iterator foundItem, lastItem;
111 bool reachLast;
112 boost::tie (foundItem, reachLast, lastItem) = const_cast<tree_type&> (m_tree).find (key);
113
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700114 return lastItem->payload ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700115}
116
117const LoadStatsNode&
118StatsTree::WalkLeftRightRoot (tree_type *node)
119{
120 tree_type::point_iterator item (*node), end;
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700121
122 while (item != end)
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700123 {
124 node->payload () += WalkLeftRightRoot (&*item);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700125 item->payload ().Step ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700126
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700127 NS_LOG_DEBUG ("[" << item->key () << "] " << item->payload ());
128 // item->prune (); // will do only if necessary
129
130 tree_type::point_iterator prune_iterator = item;
131 item++;
132
Alexander Afanasyev70426a02012-08-15 15:39:18 -0700133 prune_iterator->prune_node ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700134 }
135
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700136 return node->payload ();
137}
138
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700139void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700140StatsTree::RemoveFace (Ptr<Face> face)
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700141{
142 tree_type::recursive_iterator item (&m_tree), end;
143 for (; item != end; item ++)
144 {
145 item->payload ().RemoveFace (face);
146 }
147}
148
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700149std::ostream &
150operator << (std::ostream &os, const StatsTree &tree)
151{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700152 // os << "[" << tree.m_tree.key () << "]: " << tree.m_tree.payload ();
153 os << tree.m_tree;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700154 return os;
155}
156
157
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700158} // namespace ndnSIM
159} // namespace ndn
160} // namespace ns3
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700161