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