Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 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 "ccnx-fib.h" |
| 22 | |
| 23 | #include "ccnx.h" |
| 24 | #include "ccnx-face.h" |
| 25 | #include "ccnx-interest-header.h" |
| 26 | |
| 27 | #include "ns3/node.h" |
| 28 | |
| 29 | //#define NDN_DEBUG_OSPF 0 |
| 30 | //#define NDN_DEBUG_OSPF_NODES 0 |
| 31 | |
| 32 | //#define NDN_DUMP_FIB 0 |
| 33 | namespace ns3 { |
| 34 | |
| 35 | |
| 36 | ////////////////////////////////////////////////////////////////////// |
| 37 | // Helpers |
| 38 | ////////////////////////////////////////////////////////////////////// |
| 39 | namespace __ccnx_private_fib { |
| 40 | |
| 41 | struct CcnxFibFaceMetricByFace |
| 42 | { |
| 43 | typedef CcnxFibFaceMetricContainer::type::index<i_face>::type |
| 44 | type; |
| 45 | }; |
| 46 | |
| 47 | struct ChangeStatus |
| 48 | { |
| 49 | ChangeStatus (uint8_t status) : m_status (status) { } |
| 50 | void operator() (CcnxFibFaceMetric &entry) |
| 51 | { |
| 52 | entry.m_status = m_status; |
| 53 | } |
| 54 | private: |
| 55 | uint8_t m_status; |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | struct SearchByFace { |
| 60 | /** |
| 61 | * \brief To perform effective searches by CcnxFace |
| 62 | */ |
| 63 | bool |
| 64 | operator() (const CcnxFibFaceMetric &m, const Ptr<CcnxFace> &face) const |
| 65 | { |
| 66 | return *(m.m_face) < *face; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * \brief To perform effective searches by CcnxFace |
| 71 | */ |
| 72 | bool |
| 73 | operator() (const Ptr<CcnxFace> &face, const CcnxFibFaceMetric &m) const |
| 74 | { |
| 75 | return *face < *(m.m_face); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | } |
| 80 | ////////////////////////////////////////////////////////////////////// |
| 81 | ////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | using namespace __ccnx_private_fib; |
| 84 | |
| 85 | void |
| 86 | CcnxFibEntry::UpdateStatus (Ptr<CcnxFace> face, uint8_t status) |
| 87 | { |
| 88 | CcnxFibFaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face, SearchByFace()); |
| 89 | NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (), "Update status can be performed only on existing faces of CcxnFibEntry"); |
| 90 | |
| 91 | m_faces.modify (record, ChangeStatus (status)); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 92 | |
| 93 | // reordering random access index same way as by metric index |
| 94 | m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ()); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 97 | |
| 98 | Ptr<CcnxFace> |
| 99 | CcnxFibEntry::FindBestCandidate (int skip/* = 0*/) |
| 100 | { |
| 101 | skip = skip % m_faces.size(); |
| 102 | return m_faces.get<i_nth> () [skip].GetFace (); |
| 103 | } |
| 104 | |
| 105 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 106 | CcnxFib::CcnxFib (Ptr<Ccnx> node) |
| 107 | : m_node (node) |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | std::pair<CcnxFibEntryContainer::type::iterator, bool> |
| 112 | CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const |
| 113 | { |
| 114 | const CcnxNameComponents &name = interest.GetName (); |
| 115 | for (size_t componentsCount = name.GetComponents ().size (); |
| 116 | componentsCount > 0; |
| 117 | componentsCount--) |
| 118 | { |
| 119 | CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount)); |
| 120 | CcnxFibEntryContainer::type::iterator match = m_fib.find (subPrefix); |
| 121 | if (match != m_fib.end()) |
| 122 | return std::pair<CcnxFibEntryContainer::type::iterator, bool> (match,true); |
| 123 | } |
| 124 | |
| 125 | return std::pair<CcnxFibEntryContainer::type::iterator, bool> (m_fib.end(),false); |
| 126 | } |
| 127 | |
| 128 | std::ostream& operator<< (std::ostream& os, const CcnxFib &fib) |
| 129 | { |
| 130 | os << "Node " << fib.m_node->GetObject<Node> ()->GetId () << "\n"; |
| 131 | os << " Dest prefix Interfaces(Costs) \n"; |
| 132 | os << "+-------------+--------------------------------------+\n"; |
| 133 | |
| 134 | for (CcnxFibEntryContainer::type::iterator entry = fib.m_fib.begin (); |
| 135 | entry != fib.m_fib.end (); |
| 136 | entry++) |
| 137 | { |
| 138 | os << *entry << "\n"; |
| 139 | } |
| 140 | return os; |
| 141 | } |
| 142 | |
| 143 | std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry) |
| 144 | { |
| 145 | os << entry.m_prefix << "\t"; |
| 146 | for (CcnxFibFaceMetricByFace::type::iterator metric = entry.m_faces.get<i_face> ().begin (); |
| 147 | metric != entry.m_faces.get<i_face> ().end (); |
| 148 | metric++) |
| 149 | { |
| 150 | if (metric != entry.m_faces.get<i_face> ().begin ()) |
| 151 | os << ", "; |
| 152 | |
| 153 | os << *metric; |
| 154 | } |
| 155 | return os; |
| 156 | } |
| 157 | |
| 158 | std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric) |
| 159 | { |
| 160 | static const std::string statusString[] = {"","g","y","r"}; |
| 161 | NS_ASSERT_MSG (1<=metric.m_status && metric.m_status<=3, "Status can be only GREEN, YELLOW, or RED"); |
| 162 | |
| 163 | os << metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")"; |
| 164 | return os; |
| 165 | } |
| 166 | |
| 167 | // void CcnxFib::resetProbing() |
| 168 | // { |
| 169 | // for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++) |
| 170 | // VALUE(fib).needsProbing = true; |
| 171 | // } |
| 172 | |
| 173 | // void CcnxFib::updateInterfaceStatus( int interface, int status ) |
| 174 | // { |
| 175 | // for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ ) |
| 176 | // { |
| 177 | // VALUE(fib).updateStatus( interface, status ); |
| 178 | // } |
| 179 | // } |
| 180 | |
| 181 | } // namespace ns3 |