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" |
| 22 | #include "ns3/ccnx-face.h" |
| 23 | #include "ns3/log.h" |
| 24 | |
| 25 | using namespace ns3; |
| 26 | |
| 27 | NS_LOG_COMPONENT_DEFINE ("StatsTree"); |
| 28 | |
| 29 | namespace ndnSIM |
| 30 | { |
| 31 | |
| 32 | StatsTree::StatsTree () |
| 33 | : m_tree ("") |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | StatsTree::Step () |
| 39 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 40 | NS_LOG_FUNCTION (this); |
| 41 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 42 | // walking the tree, aggregating and stepping on every node, starting the leaves |
| 43 | // for (trie_type:: |
| 44 | |
| 45 | WalkLeftRightRoot (&m_tree); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 46 | m_tree.payload ().Step (); |
| 47 | NS_LOG_DEBUG ("[" << m_tree.key () << "] " << m_tree.payload ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
| 51 | StatsTree::NewPitEntry (const ns3::CcnxNameComponents &key) |
| 52 | { |
| 53 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 54 | |
| 55 | item.first->payload ().NewPitEntry (); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void |
| 59 | StatsTree::Incoming (const CcnxNameComponents &key, Ptr<CcnxFace> face) |
| 60 | { |
| 61 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 62 | |
| 63 | item.first->payload ().AddIncoming (face); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | StatsTree::Outgoing (const CcnxNameComponents &key, Ptr<CcnxFace> face) |
| 68 | { |
| 69 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 70 | |
| 71 | item.first->payload ().AddOutgoing (face); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | StatsTree::Satisfy (const CcnxNameComponents &key) |
| 76 | { |
| 77 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 78 | |
| 79 | item.first->payload ().Satisfy (); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | StatsTree::Timeout (const CcnxNameComponents &key) |
| 84 | { |
| 85 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 86 | |
| 87 | item.first->payload ().Timeout (); |
| 88 | } |
| 89 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 90 | // const LoadStatsNode & |
| 91 | // StatsTree::Get (const ns3::CcnxNameComponents &key) const |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 92 | const LoadStatsNode & |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 93 | StatsTree::operator [] (const ns3::CcnxNameComponents &key) const |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 94 | { |
| 95 | tree_type::iterator foundItem, lastItem; |
| 96 | bool reachLast; |
| 97 | boost::tie (foundItem, reachLast, lastItem) = const_cast<tree_type&> (m_tree).find (key); |
| 98 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 99 | return lastItem->payload (); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | const LoadStatsNode& |
| 103 | StatsTree::WalkLeftRightRoot (tree_type *node) |
| 104 | { |
| 105 | tree_type::point_iterator item (*node), end; |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 106 | |
| 107 | while (item != end) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 108 | { |
| 109 | node->payload () += WalkLeftRightRoot (&*item); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 110 | item->payload ().Step (); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 111 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 112 | NS_LOG_DEBUG ("[" << item->key () << "] " << item->payload ()); |
| 113 | // item->prune (); // will do only if necessary |
| 114 | |
| 115 | tree_type::point_iterator prune_iterator = item; |
| 116 | item++; |
| 117 | |
| 118 | prune_iterator->prune (); |
| 119 | } |
| 120 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 121 | return node->payload (); |
| 122 | } |
| 123 | |
| 124 | std::ostream & |
| 125 | operator << (std::ostream &os, const StatsTree &tree) |
| 126 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 127 | // os << "[" << tree.m_tree.key () << "]: " << tree.m_tree.payload (); |
| 128 | os << tree.m_tree; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 129 | return os; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | } |