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 | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame^] | 106 | CcnxFib::CcnxFib () |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 107 | { |
| 108 | } |
| 109 | |
| 110 | std::pair<CcnxFibEntryContainer::type::iterator, bool> |
| 111 | CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const |
| 112 | { |
| 113 | const CcnxNameComponents &name = interest.GetName (); |
| 114 | for (size_t componentsCount = name.GetComponents ().size (); |
| 115 | componentsCount > 0; |
| 116 | componentsCount--) |
| 117 | { |
| 118 | CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount)); |
| 119 | CcnxFibEntryContainer::type::iterator match = m_fib.find (subPrefix); |
| 120 | if (match != m_fib.end()) |
| 121 | return std::pair<CcnxFibEntryContainer::type::iterator, bool> (match,true); |
| 122 | } |
| 123 | |
| 124 | return std::pair<CcnxFibEntryContainer::type::iterator, bool> (m_fib.end(),false); |
| 125 | } |
| 126 | |
| 127 | std::ostream& operator<< (std::ostream& os, const CcnxFib &fib) |
| 128 | { |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame^] | 129 | // os << "Node " << fib.m_node->GetObject<Node> ()->GetId () << "\n"; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 130 | os << " Dest prefix Interfaces(Costs) \n"; |
| 131 | os << "+-------------+--------------------------------------+\n"; |
| 132 | |
| 133 | for (CcnxFibEntryContainer::type::iterator entry = fib.m_fib.begin (); |
| 134 | entry != fib.m_fib.end (); |
| 135 | entry++) |
| 136 | { |
| 137 | os << *entry << "\n"; |
| 138 | } |
| 139 | return os; |
| 140 | } |
| 141 | |
| 142 | std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry) |
| 143 | { |
| 144 | os << entry.m_prefix << "\t"; |
| 145 | for (CcnxFibFaceMetricByFace::type::iterator metric = entry.m_faces.get<i_face> ().begin (); |
| 146 | metric != entry.m_faces.get<i_face> ().end (); |
| 147 | metric++) |
| 148 | { |
| 149 | if (metric != entry.m_faces.get<i_face> ().begin ()) |
| 150 | os << ", "; |
| 151 | |
| 152 | os << *metric; |
| 153 | } |
| 154 | return os; |
| 155 | } |
| 156 | |
| 157 | std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric) |
| 158 | { |
| 159 | static const std::string statusString[] = {"","g","y","r"}; |
| 160 | NS_ASSERT_MSG (1<=metric.m_status && metric.m_status<=3, "Status can be only GREEN, YELLOW, or RED"); |
| 161 | |
| 162 | os << metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")"; |
| 163 | return os; |
| 164 | } |
| 165 | |
| 166 | // void CcnxFib::resetProbing() |
| 167 | // { |
| 168 | // for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++) |
| 169 | // VALUE(fib).needsProbing = true; |
| 170 | // } |
| 171 | |
| 172 | // void CcnxFib::updateInterfaceStatus( int interface, int status ) |
| 173 | // { |
| 174 | // for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ ) |
| 175 | // { |
| 176 | // VALUE(fib).updateStatus( interface, status ); |
| 177 | // } |
| 178 | // } |
| 179 | |
| 180 | } // namespace ns3 |