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