Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -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 | |
| 22 | #ifndef BOOST_GRAPH_CCNX_GLOBAL_ROUTING_HELPER_H |
| 23 | #define BOOST_GRAPH_CCNX_GLOBAL_ROUTING_HELPER_H |
| 24 | |
| 25 | #include <boost/graph/graph_traits.hpp> |
| 26 | #include <boost/graph/properties.hpp> |
| 27 | |
| 28 | #include "ns3/node-list.h" |
| 29 | #include "ns3/channel-list.h" |
| 30 | #include "../model/ccnx-global-router.h" |
| 31 | #include <list> |
| 32 | #include <map> |
| 33 | |
| 34 | namespace boost |
| 35 | { |
| 36 | |
| 37 | class CcnxGlobalRouterGraph |
| 38 | { |
| 39 | public: |
| 40 | typedef ns3::Ptr< ns3::CcnxGlobalRouter > Vertice; |
| 41 | typedef uint16_t edge_property_type; |
| 42 | typedef uint32_t vertex_property_type; |
| 43 | |
| 44 | CcnxGlobalRouterGraph () |
| 45 | { |
| 46 | for (ns3::NodeList::Iterator node = ns3::NodeList::Begin (); node != ns3::NodeList::End (); node++) |
| 47 | { |
| 48 | ns3::Ptr<ns3::CcnxGlobalRouter> gr = (*node)->GetObject<ns3::CcnxGlobalRouter> (); |
| 49 | if (gr != 0) |
| 50 | m_vertices.push_back (gr); |
| 51 | } |
| 52 | |
| 53 | for (ns3::ChannelList::Iterator channel = ns3::ChannelList::Begin (); channel != ns3::ChannelList::End (); channel++) |
| 54 | { |
| 55 | ns3::Ptr<ns3::CcnxGlobalRouter> gr = (*channel)->GetObject<ns3::CcnxGlobalRouter> (); |
| 56 | if (gr != 0) |
| 57 | m_vertices.push_back (gr); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const std::list< Vertice > & |
| 62 | GetVertices () const |
| 63 | { |
| 64 | return m_vertices; |
| 65 | } |
| 66 | |
| 67 | public: |
| 68 | std::list< Vertice > m_vertices; |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | class ccnx_global_router_graph_category : |
| 73 | public virtual vertex_list_graph_tag, |
| 74 | public virtual incidence_graph_tag |
| 75 | { |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | template<> |
| 80 | struct graph_traits< CcnxGlobalRouterGraph > |
| 81 | { |
| 82 | // Graph concept |
| 83 | typedef CcnxGlobalRouterGraph::Vertice vertex_descriptor; |
| 84 | typedef ns3::CcnxGlobalRouter::Incidency edge_descriptor; |
| 85 | typedef directed_tag directed_category; |
| 86 | typedef disallow_parallel_edge_tag edge_parallel_category; |
| 87 | typedef ccnx_global_router_graph_category traversal_category; |
| 88 | |
| 89 | // VertexList concept |
| 90 | typedef std::list< vertex_descriptor >::const_iterator vertex_iterator; |
| 91 | typedef size_t vertices_size_type; |
| 92 | |
| 93 | // AdjacencyGraph concept |
| 94 | typedef ns3::CcnxGlobalRouter::IncidencyList::iterator out_edge_iterator; |
| 95 | typedef size_t degree_size_type; |
| 96 | |
| 97 | // typedef size_t edges_size_type; |
| 98 | }; |
| 99 | |
| 100 | } // namespace boost |
| 101 | |
| 102 | namespace boost |
| 103 | { |
| 104 | |
| 105 | inline |
| 106 | graph_traits< CcnxGlobalRouterGraph >::vertex_descriptor |
| 107 | source( |
| 108 | graph_traits< CcnxGlobalRouterGraph >::edge_descriptor e, |
| 109 | const CcnxGlobalRouterGraph& g) |
| 110 | { |
| 111 | return e.get<0> (); |
| 112 | } |
| 113 | |
| 114 | inline |
| 115 | graph_traits< CcnxGlobalRouterGraph >::vertex_descriptor |
| 116 | target( |
| 117 | graph_traits< CcnxGlobalRouterGraph >::edge_descriptor e, |
| 118 | const CcnxGlobalRouterGraph& g) |
| 119 | { |
| 120 | return e.get<2> (); |
| 121 | } |
| 122 | |
| 123 | inline |
| 124 | std::pair< graph_traits< CcnxGlobalRouterGraph >::vertex_iterator, |
| 125 | graph_traits< CcnxGlobalRouterGraph >::vertex_iterator > |
| 126 | vertices (const CcnxGlobalRouterGraph&g) |
| 127 | { |
| 128 | return make_pair (g.GetVertices ().begin (), g.GetVertices ().end ()); |
| 129 | } |
| 130 | |
| 131 | inline |
| 132 | graph_traits< CcnxGlobalRouterGraph >::vertices_size_type |
| 133 | num_vertices(const CcnxGlobalRouterGraph &g) |
| 134 | { |
| 135 | return g.GetVertices ().size (); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | inline |
| 140 | std::pair< graph_traits< CcnxGlobalRouterGraph >::out_edge_iterator, |
| 141 | graph_traits< CcnxGlobalRouterGraph >::out_edge_iterator > |
| 142 | out_edges( |
| 143 | graph_traits< CcnxGlobalRouterGraph >::vertex_descriptor u, |
| 144 | const CcnxGlobalRouterGraph& g) |
| 145 | { |
| 146 | return std::make_pair(u->GetIncidencies ().begin (), |
| 147 | u->GetIncidencies ().end ()); |
| 148 | } |
| 149 | |
| 150 | inline |
| 151 | graph_traits< CcnxGlobalRouterGraph >::degree_size_type |
| 152 | out_degree( |
| 153 | graph_traits< CcnxGlobalRouterGraph >::vertex_descriptor u, |
| 154 | const CcnxGlobalRouterGraph& g) |
| 155 | { |
| 156 | return u->GetIncidencies ().size (); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | ////////////////////////////////////////////////////////////// |
| 161 | // Property maps |
| 162 | |
| 163 | struct EdgeWeights |
| 164 | { |
| 165 | EdgeWeights (const CcnxGlobalRouterGraph &graph) |
| 166 | : m_graph (graph) |
| 167 | { |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | const CcnxGlobalRouterGraph &m_graph; |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | struct VertexIds |
| 176 | { |
| 177 | VertexIds (const CcnxGlobalRouterGraph &graph) |
| 178 | : m_graph (graph) |
| 179 | { |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | const CcnxGlobalRouterGraph &m_graph; |
| 184 | }; |
| 185 | |
| 186 | template<> |
| 187 | struct property_map< CcnxGlobalRouterGraph, edge_weight_t > |
| 188 | { |
| 189 | typedef const EdgeWeights const_type; |
| 190 | typedef EdgeWeights type; |
| 191 | }; |
| 192 | |
| 193 | template<> |
| 194 | struct property_map< CcnxGlobalRouterGraph, vertex_index_t > |
| 195 | { |
| 196 | typedef const VertexIds const_type; |
| 197 | typedef VertexIds type; |
| 198 | }; |
| 199 | |
| 200 | |
| 201 | template<> |
| 202 | struct property_traits< EdgeWeights > |
| 203 | { |
| 204 | // Metric property map |
| 205 | typedef uint16_t value_type; |
| 206 | typedef uint16_t reference; |
| 207 | typedef ns3::CcnxGlobalRouter::Incidency key_type; |
| 208 | typedef readable_property_map_tag category; |
| 209 | }; |
| 210 | |
| 211 | template<> |
| 212 | struct property_traits< VertexIds > |
| 213 | { |
| 214 | // Metric property map |
| 215 | typedef uint32_t value_type; |
| 216 | typedef uint32_t reference; |
| 217 | typedef ns3::Ptr< ns3::CcnxGlobalRouter > key_type; |
| 218 | typedef readable_property_map_tag category; |
| 219 | }; |
| 220 | |
| 221 | |
| 222 | inline EdgeWeights |
| 223 | get(edge_weight_t, |
| 224 | const CcnxGlobalRouterGraph &g) |
| 225 | { |
| 226 | return EdgeWeights (g); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | inline VertexIds |
| 231 | get(vertex_index_t, |
| 232 | const CcnxGlobalRouterGraph &g) |
| 233 | { |
| 234 | return VertexIds (g); |
| 235 | } |
| 236 | |
| 237 | template<class K, class V> |
| 238 | inline void |
| 239 | put (std::map<K,V> &mapp, |
| 240 | K a, V p) |
| 241 | { |
| 242 | mapp[a] = p; |
| 243 | } |
| 244 | |
| 245 | // void |
| 246 | // put (cref< std::map< ns3::Ptr<ns3::CcnxGlobalRouter>, ns3::Ptr<ns3::CcnxGlobalRouter> > > map, |
| 247 | |
| 248 | uint32_t |
| 249 | get (const boost::VertexIds&, ns3::Ptr<ns3::CcnxGlobalRouter> &gr) |
| 250 | { |
| 251 | return gr->GetId (); |
| 252 | } |
| 253 | |
| 254 | inline uint16_t |
| 255 | get(const boost::EdgeWeights&, ns3::CcnxGlobalRouter::Incidency &edge) |
| 256 | { |
| 257 | if (edge.get<1> () == 0) |
| 258 | return 0; |
| 259 | else |
| 260 | return edge.get<1> ()->GetMetric (); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | struct PredecessorsMap : |
| 265 | public std::map< ns3::Ptr< ns3::CcnxGlobalRouter >, ns3::Ptr< ns3::CcnxGlobalRouter > > |
| 266 | { |
| 267 | }; |
| 268 | |
| 269 | template<> |
| 270 | struct property_traits< PredecessorsMap > |
| 271 | { |
| 272 | // Metric property map |
| 273 | typedef ns3::Ptr< ns3::CcnxGlobalRouter > value_type; |
| 274 | typedef ns3::Ptr< ns3::CcnxGlobalRouter > reference; |
| 275 | typedef ns3::Ptr< ns3::CcnxGlobalRouter > key_type; |
| 276 | typedef read_write_property_map_tag category; |
| 277 | }; |
| 278 | |
| 279 | |
| 280 | struct DistancesMap : |
| 281 | public std::map< ns3::Ptr< ns3::CcnxGlobalRouter >, uint32_t > |
| 282 | { |
| 283 | }; |
| 284 | |
| 285 | template<> |
| 286 | struct property_traits< DistancesMap > |
| 287 | { |
| 288 | // Metric property map |
| 289 | typedef uint32_t value_type; |
| 290 | typedef uint32_t reference; |
| 291 | typedef ns3::Ptr< ns3::CcnxGlobalRouter > key_type; |
| 292 | typedef read_write_property_map_tag category; |
| 293 | }; |
| 294 | |
| 295 | inline uint32_t |
| 296 | get (DistancesMap &map, ns3::Ptr<ns3::CcnxGlobalRouter> key) |
| 297 | { |
| 298 | boost::DistancesMap::iterator i = map.find (key); |
| 299 | if (i == map.end ()) |
| 300 | return std::numeric_limits<uint32_t>::max (); |
| 301 | else |
| 302 | return i->second; |
| 303 | } |
| 304 | |
| 305 | } // namespace boost |
| 306 | |
| 307 | #endif // BOOST_GRAPH_CCNX_GLOBAL_ROUTING_HELPER_H |