blob: ef04fac11da7d5e4068e9768ccc9d2b315f58ab9 [file] [log] [blame]
Alexander Afanasyevad3757f2012-04-17 10:27:59 -07001/* -*- 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 Afanasyeva5abcd92012-04-17 13:34:43 -070027#include <boost/ref.hpp>
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070028
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070029#include "ns3/ccnx-face.h"
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070030#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
36namespace boost
37{
38
39class CcnxGlobalRouterGraph
40{
41public:
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
69public:
70 std::list< Vertice > m_vertices;
71};
72
73
74class ccnx_global_router_graph_category :
75 public virtual vertex_list_graph_tag,
76 public virtual incidence_graph_tag
77{
78};
79
80
81template<>
82struct 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
104namespace boost
105{
106
107inline
108graph_traits< CcnxGlobalRouterGraph >::vertex_descriptor
109source(
110 graph_traits< CcnxGlobalRouterGraph >::edge_descriptor e,
111 const CcnxGlobalRouterGraph& g)
112{
113 return e.get<0> ();
114}
115
116inline
117graph_traits< CcnxGlobalRouterGraph >::vertex_descriptor
118target(
119 graph_traits< CcnxGlobalRouterGraph >::edge_descriptor e,
120 const CcnxGlobalRouterGraph& g)
121{
122 return e.get<2> ();
123}
124
125inline
126std::pair< graph_traits< CcnxGlobalRouterGraph >::vertex_iterator,
127 graph_traits< CcnxGlobalRouterGraph >::vertex_iterator >
128vertices (const CcnxGlobalRouterGraph&g)
129{
130 return make_pair (g.GetVertices ().begin (), g.GetVertices ().end ());
131}
132
133inline
134graph_traits< CcnxGlobalRouterGraph >::vertices_size_type
135num_vertices(const CcnxGlobalRouterGraph &g)
136{
137 return g.GetVertices ().size ();
138}
139
140
141inline
142std::pair< graph_traits< CcnxGlobalRouterGraph >::out_edge_iterator,
143 graph_traits< CcnxGlobalRouterGraph >::out_edge_iterator >
144out_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
152inline
153graph_traits< CcnxGlobalRouterGraph >::degree_size_type
154out_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
165struct EdgeWeights
166{
167 EdgeWeights (const CcnxGlobalRouterGraph &graph)
168 : m_graph (graph)
169 {
170 }
171
172private:
173 const CcnxGlobalRouterGraph &m_graph;
174};
175
176
177struct VertexIds
178{
179 VertexIds (const CcnxGlobalRouterGraph &graph)
180 : m_graph (graph)
181 {
182 }
183
184private:
185 const CcnxGlobalRouterGraph &m_graph;
186};
187
188template<>
189struct property_map< CcnxGlobalRouterGraph, edge_weight_t >
190{
191 typedef const EdgeWeights const_type;
192 typedef EdgeWeights type;
193};
194
195template<>
196struct property_map< CcnxGlobalRouterGraph, vertex_index_t >
197{
198 typedef const VertexIds const_type;
199 typedef VertexIds type;
200};
201
202
203template<>
204struct property_traits< EdgeWeights >
205{
206 // Metric property map
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700207 typedef tuple< ns3::Ptr<ns3::CcnxFace>, uint16_t > value_type;
208 typedef tuple< ns3::Ptr<ns3::CcnxFace>, uint16_t > reference;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700209 typedef ns3::CcnxGlobalRouter::Incidency key_type;
210 typedef readable_property_map_tag category;
211};
212
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700213const property_traits< EdgeWeights >::value_type WeightZero (0, 0);
214const property_traits< EdgeWeights >::value_type WeightInf (0, std::numeric_limits<uint16_t>::max ());
215
216struct 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
244struct 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 Afanasyevad3757f2012-04-17 10:27:59 -0700266template<>
267struct 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
277inline EdgeWeights
278get(edge_weight_t,
279 const CcnxGlobalRouterGraph &g)
280{
281 return EdgeWeights (g);
282}
283
284
285inline VertexIds
286get(vertex_index_t,
287 const CcnxGlobalRouterGraph &g)
288{
289 return VertexIds (g);
290}
291
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700292template<class M, class K, class V>
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700293inline void
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700294put (reference_wrapper< M > mapp,
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700295 K a, V p)
296{
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700297 mapp.get ()[a] = p;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700298}
299
300// void
301// put (cref< std::map< ns3::Ptr<ns3::CcnxGlobalRouter>, ns3::Ptr<ns3::CcnxGlobalRouter> > > map,
302
303uint32_t
304get (const boost::VertexIds&, ns3::Ptr<ns3::CcnxGlobalRouter> &gr)
305{
306 return gr->GetId ();
307}
308
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700309inline property_traits< EdgeWeights >::reference
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700310get(const boost::EdgeWeights&, ns3::CcnxGlobalRouter::Incidency &edge)
311{
312 if (edge.get<1> () == 0)
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700313 return property_traits< EdgeWeights >::reference (0, 0);
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700314 else
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700315 return property_traits< EdgeWeights >::reference (edge.get<1> (), edge.get<1> ()->GetMetric ());
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700316}
317
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700318struct PredecessorsMap :
319 public std::map< ns3::Ptr< ns3::CcnxGlobalRouter >, ns3::Ptr< ns3::CcnxGlobalRouter > >
320{
321};
322
323template<>
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700324struct property_traits< reference_wrapper<PredecessorsMap> >
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700325{
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
334struct DistancesMap :
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700335 public std::map< ns3::Ptr< ns3::CcnxGlobalRouter >, tuple< ns3::Ptr<ns3::CcnxFace>, uint32_t > >
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700336{
337};
338
339template<>
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700340struct property_traits< reference_wrapper<DistancesMap> >
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700341{
342 // Metric property map
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700343 typedef tuple< ns3::Ptr<ns3::CcnxFace>, uint32_t > value_type;
344 typedef tuple< ns3::Ptr<ns3::CcnxFace>, uint32_t > reference;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700345 typedef ns3::Ptr< ns3::CcnxGlobalRouter > key_type;
346 typedef read_write_property_map_tag category;
347};
348
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700349inline tuple< ns3::Ptr<ns3::CcnxFace>, uint32_t >
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700350get (DistancesMap &map, ns3::Ptr<ns3::CcnxGlobalRouter> key)
351{
352 boost::DistancesMap::iterator i = map.find (key);
353 if (i == map.end ())
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700354 return tuple< ns3::Ptr<ns3::CcnxFace>, uint32_t > (0, std::numeric_limits<uint32_t>::max ());
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700355 else
356 return i->second;
357}
358
359} // namespace boost
360
361#endif // BOOST_GRAPH_CCNX_GLOBAL_ROUTING_HELPER_H