blob: 14c6138ff83f6b88528f74d46431792517ffce20 [file] [log] [blame]
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -07001/* -*- 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
33namespace ns3 {
34
35
36//////////////////////////////////////////////////////////////////////
37// Helpers
38//////////////////////////////////////////////////////////////////////
39namespace __ccnx_private_fib {
40
41struct CcnxFibFaceMetricByFace
42{
43 typedef CcnxFibFaceMetricContainer::type::index<i_face>::type
44 type;
45};
46
47struct ChangeStatus
48{
49 ChangeStatus (uint8_t status) : m_status (status) { }
50 void operator() (CcnxFibFaceMetric &entry)
51 {
52 entry.m_status = m_status;
53 }
54private:
55 uint8_t m_status;
56};
57
58
59struct 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
83using namespace __ccnx_private_fib;
84
85void
86CcnxFibEntry::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));
92}
93
94CcnxFib::CcnxFib (Ptr<Ccnx> node)
95 : m_node (node)
96{
97}
98
99std::pair<CcnxFibEntryContainer::type::iterator, bool>
100CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const
101{
102 const CcnxNameComponents &name = interest.GetName ();
103 for (size_t componentsCount = name.GetComponents ().size ();
104 componentsCount > 0;
105 componentsCount--)
106 {
107 CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount));
108 CcnxFibEntryContainer::type::iterator match = m_fib.find (subPrefix);
109 if (match != m_fib.end())
110 return std::pair<CcnxFibEntryContainer::type::iterator, bool> (match,true);
111 }
112
113 return std::pair<CcnxFibEntryContainer::type::iterator, bool> (m_fib.end(),false);
114}
115
116std::ostream& operator<< (std::ostream& os, const CcnxFib &fib)
117{
118 os << "Node " << fib.m_node->GetObject<Node> ()->GetId () << "\n";
119 os << " Dest prefix Interfaces(Costs) \n";
120 os << "+-------------+--------------------------------------+\n";
121
122 for (CcnxFibEntryContainer::type::iterator entry = fib.m_fib.begin ();
123 entry != fib.m_fib.end ();
124 entry++)
125 {
126 os << *entry << "\n";
127 }
128 return os;
129}
130
131std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry)
132{
133 os << entry.m_prefix << "\t";
134 for (CcnxFibFaceMetricByFace::type::iterator metric = entry.m_faces.get<i_face> ().begin ();
135 metric != entry.m_faces.get<i_face> ().end ();
136 metric++)
137 {
138 if (metric != entry.m_faces.get<i_face> ().begin ())
139 os << ", ";
140
141 os << *metric;
142 }
143 return os;
144}
145
146std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric)
147{
148 static const std::string statusString[] = {"","g","y","r"};
149 NS_ASSERT_MSG (1<=metric.m_status && metric.m_status<=3, "Status can be only GREEN, YELLOW, or RED");
150
151 os << metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")";
152 return os;
153}
154
155// void CcnxFib::resetProbing()
156// {
157// for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++)
158// VALUE(fib).needsProbing = true;
159// }
160
161// void CcnxFib::updateInterfaceStatus( int interface, int status )
162// {
163// for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ )
164// {
165// VALUE(fib).updateStatus( interface, status );
166// }
167// }
168
169} // namespace ns3