Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 22 | #include "ns3/ndn-face.h" |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 23 | #include "ns3/log.h" |
| 24 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 25 | NS_LOG_COMPONENT_DEFINE ("ndn.StatsTree"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 27 | namespace ns3 { |
| 28 | namespace ndn { |
| 29 | namespace ndnSIM { |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 30 | |
| 31 | StatsTree::StatsTree () |
| 32 | : m_tree ("") |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | void |
| 37 | StatsTree::Step () |
| 38 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 39 | NS_LOG_FUNCTION (this); |
| 40 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 41 | // walking the tree, aggregating and stepping on every node, starting the leaves |
| 42 | // for (trie_type:: |
| 43 | |
| 44 | WalkLeftRightRoot (&m_tree); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 45 | m_tree.payload ().Step (); |
| 46 | NS_LOG_DEBUG ("[" << m_tree.key () << "] " << m_tree.payload ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 50 | StatsTree::NewPitEntry (const NameComponents &key) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 51 | { |
| 52 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 53 | |
| 54 | item.first->payload ().NewPitEntry (); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 58 | StatsTree::Incoming (const NameComponents &key, Ptr<Face> face) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 59 | { |
| 60 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 61 | |
| 62 | item.first->payload ().AddIncoming (face); |
| 63 | } |
| 64 | |
| 65 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 66 | StatsTree::Outgoing (const NameComponents &key, Ptr<Face> face) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 67 | { |
| 68 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 69 | |
| 70 | item.first->payload ().AddOutgoing (face); |
| 71 | } |
| 72 | |
| 73 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 74 | StatsTree::Satisfy (const NameComponents &key) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 75 | { |
| 76 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 77 | |
| 78 | item.first->payload ().Satisfy (); |
| 79 | } |
| 80 | |
| 81 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 82 | StatsTree::Timeout (const NameComponents &key) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 83 | { |
| 84 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 85 | |
| 86 | item.first->payload ().Timeout (); |
| 87 | } |
| 88 | |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 89 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 90 | StatsTree::Rx (const NameComponents &key, Ptr<Face> face, uint32_t amount) |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 91 | { |
| 92 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
| 93 | |
| 94 | item.first->payload ().Rx (face, amount); |
| 95 | } |
| 96 | |
| 97 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 98 | StatsTree::Tx (const NameComponents &key, Ptr<Face> face, uint32_t amount) |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 99 | { |
| 100 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
| 101 | |
| 102 | item.first->payload ().Tx (face, amount); |
| 103 | } |
| 104 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 105 | // const LoadStatsNode & |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 106 | // StatsTree::Get (const NameComponents &key) const |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 107 | const LoadStatsNode & |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 108 | StatsTree::operator [] (const NameComponents &key) const |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 109 | { |
| 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 Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 114 | return lastItem->payload (); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | const LoadStatsNode& |
| 118 | StatsTree::WalkLeftRightRoot (tree_type *node) |
| 119 | { |
| 120 | tree_type::point_iterator item (*node), end; |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 121 | |
| 122 | while (item != end) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 123 | { |
| 124 | node->payload () += WalkLeftRightRoot (&*item); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 125 | item->payload ().Step (); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 127 | 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 | |
| 133 | prune_iterator->prune (); |
| 134 | } |
| 135 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 136 | return node->payload (); |
| 137 | } |
| 138 | |
Alexander Afanasyev | 33364b6 | 2012-07-26 17:53:56 -0700 | [diff] [blame] | 139 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 140 | StatsTree::RemoveFace (Ptr<Face> face) |
Alexander Afanasyev | 33364b6 | 2012-07-26 17:53:56 -0700 | [diff] [blame] | 141 | { |
| 142 | tree_type::recursive_iterator item (&m_tree), end; |
| 143 | for (; item != end; item ++) |
| 144 | { |
| 145 | item->payload ().RemoveFace (face); |
| 146 | } |
| 147 | } |
| 148 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 149 | std::ostream & |
| 150 | operator << (std::ostream &os, const StatsTree &tree) |
| 151 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 152 | // os << "[" << tree.m_tree.key () << "]: " << tree.m_tree.payload (); |
| 153 | os << tree.m_tree; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 154 | return os; |
| 155 | } |
| 156 | |
| 157 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 158 | } // namespace ndnSIM |
| 159 | } // namespace ndn |
| 160 | } // namespace ns3 |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 161 | |