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 | { |
| 40 | // walking the tree, aggregating and stepping on every node, starting the leaves |
| 41 | // for (trie_type:: |
| 42 | |
| 43 | WalkLeftRightRoot (&m_tree); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | StatsTree::NewPitEntry (const ns3::CcnxNameComponents &key) |
| 48 | { |
| 49 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
| 50 | NS_ASSERT (item.second == false); // should always return false |
| 51 | |
| 52 | item.first->payload ().NewPitEntry (); |
| 53 | NS_LOG_DEBUG ("NewPitEntry: " << item.first->payload ()); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | StatsTree::Incoming (const CcnxNameComponents &key, Ptr<CcnxFace> face) |
| 58 | { |
| 59 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
| 60 | NS_ASSERT (item.second == false); // should always return false |
| 61 | |
| 62 | item.first->payload ().AddIncoming (face); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | StatsTree::Outgoing (const CcnxNameComponents &key, Ptr<CcnxFace> face) |
| 67 | { |
| 68 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
| 69 | NS_ASSERT (item.second == false); // should always return false |
| 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 ()); |
| 78 | NS_ASSERT (item.second == false); // should always return false |
| 79 | |
| 80 | item.first->payload ().Satisfy (); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | StatsTree::Timeout (const CcnxNameComponents &key) |
| 85 | { |
| 86 | std::pair<tree_type::iterator, bool> item = m_tree.insert (key, LoadStatsNode ()); |
| 87 | NS_ASSERT (item.second == false); // should always return false |
| 88 | |
| 89 | item.first->payload ().Timeout (); |
| 90 | } |
| 91 | |
| 92 | const LoadStatsNode & |
| 93 | StatsTree::Get (const ns3::CcnxNameComponents &key) const |
| 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 | |
| 99 | NS_ASSERT_MSG (foundItem == lastItem, "Found item should always be the same as last item (same address)"); |
| 100 | |
| 101 | return foundItem->payload (); |
| 102 | } |
| 103 | |
| 104 | const LoadStatsNode& |
| 105 | StatsTree::WalkLeftRightRoot (tree_type *node) |
| 106 | { |
| 107 | tree_type::point_iterator item (*node), end; |
| 108 | |
| 109 | for (; item != end; item++) |
| 110 | { |
| 111 | node->payload () += WalkLeftRightRoot (&*item); |
| 112 | NS_LOG_DEBUG (node << ", " << node->payload ()); |
| 113 | } |
| 114 | |
| 115 | node->payload ().Step (); |
| 116 | return node->payload (); |
| 117 | } |
| 118 | |
| 119 | std::ostream & |
| 120 | operator << (std::ostream &os, const StatsTree &tree) |
| 121 | { |
| 122 | os << tree.m_tree.payload (); |
| 123 | return os; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | } |