blob: 37112787ed634466c5a3dd8ae5fcbcc361830581 [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>
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
34namespace boost
35{
36
37class CcnxGlobalRouterGraph
38{
39public:
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
67public:
68 std::list< Vertice > m_vertices;
69};
70
71
72class ccnx_global_router_graph_category :
73 public virtual vertex_list_graph_tag,
74 public virtual incidence_graph_tag
75{
76};
77
78
79template<>
80struct 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
102namespace boost
103{
104
105inline
106graph_traits< CcnxGlobalRouterGraph >::vertex_descriptor
107source(
108 graph_traits< CcnxGlobalRouterGraph >::edge_descriptor e,
109 const CcnxGlobalRouterGraph& g)
110{
111 return e.get<0> ();
112}
113
114inline
115graph_traits< CcnxGlobalRouterGraph >::vertex_descriptor
116target(
117 graph_traits< CcnxGlobalRouterGraph >::edge_descriptor e,
118 const CcnxGlobalRouterGraph& g)
119{
120 return e.get<2> ();
121}
122
123inline
124std::pair< graph_traits< CcnxGlobalRouterGraph >::vertex_iterator,
125 graph_traits< CcnxGlobalRouterGraph >::vertex_iterator >
126vertices (const CcnxGlobalRouterGraph&g)
127{
128 return make_pair (g.GetVertices ().begin (), g.GetVertices ().end ());
129}
130
131inline
132graph_traits< CcnxGlobalRouterGraph >::vertices_size_type
133num_vertices(const CcnxGlobalRouterGraph &g)
134{
135 return g.GetVertices ().size ();
136}
137
138
139inline
140std::pair< graph_traits< CcnxGlobalRouterGraph >::out_edge_iterator,
141 graph_traits< CcnxGlobalRouterGraph >::out_edge_iterator >
142out_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
150inline
151graph_traits< CcnxGlobalRouterGraph >::degree_size_type
152out_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
163struct EdgeWeights
164{
165 EdgeWeights (const CcnxGlobalRouterGraph &graph)
166 : m_graph (graph)
167 {
168 }
169
170private:
171 const CcnxGlobalRouterGraph &m_graph;
172};
173
174
175struct VertexIds
176{
177 VertexIds (const CcnxGlobalRouterGraph &graph)
178 : m_graph (graph)
179 {
180 }
181
182private:
183 const CcnxGlobalRouterGraph &m_graph;
184};
185
186template<>
187struct property_map< CcnxGlobalRouterGraph, edge_weight_t >
188{
189 typedef const EdgeWeights const_type;
190 typedef EdgeWeights type;
191};
192
193template<>
194struct property_map< CcnxGlobalRouterGraph, vertex_index_t >
195{
196 typedef const VertexIds const_type;
197 typedef VertexIds type;
198};
199
200
201template<>
202struct 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
211template<>
212struct 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
222inline EdgeWeights
223get(edge_weight_t,
224 const CcnxGlobalRouterGraph &g)
225{
226 return EdgeWeights (g);
227}
228
229
230inline VertexIds
231get(vertex_index_t,
232 const CcnxGlobalRouterGraph &g)
233{
234 return VertexIds (g);
235}
236
237template<class K, class V>
238inline void
239put (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
248uint32_t
249get (const boost::VertexIds&, ns3::Ptr<ns3::CcnxGlobalRouter> &gr)
250{
251 return gr->GetId ();
252}
253
254inline uint16_t
255get(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
264struct PredecessorsMap :
265 public std::map< ns3::Ptr< ns3::CcnxGlobalRouter >, ns3::Ptr< ns3::CcnxGlobalRouter > >
266{
267};
268
269template<>
270struct 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
280struct DistancesMap :
281 public std::map< ns3::Ptr< ns3::CcnxGlobalRouter >, uint32_t >
282{
283};
284
285template<>
286struct 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
295inline uint32_t
296get (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