blob: 87d02f5863f461df7dcae11bc28652016f43f6ff [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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#ifndef BOOST_GRAPH_NDN_GLOBAL_ROUTING_HELPER_H
22#define BOOST_GRAPH_NDN_GLOBAL_ROUTING_HELPER_H
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070023
Alexander Afanasyev6315ef72012-06-01 20:56:31 -070024/// @cond include_hidden
25
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070026#include "ns3/ndnSIM/model/ndn-common.hpp"
27
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070028#include <boost/graph/graph_traits.hpp>
29#include <boost/graph/properties.hpp>
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070030#include <boost/ref.hpp>
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070031
Alexander Afanasyev0c395372014-12-20 15:54:02 -080032#include "ns3/ndn-face.hpp"
33#include "ns3/ndn-limits.hpp"
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070034#include "ns3/node-list.h"
35#include "ns3/channel-list.h"
Alexander Afanasyev0c395372014-12-20 15:54:02 -080036#include "../model/ndn-global-router.hpp"
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070037#include <list>
38#include <map>
39
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040namespace boost {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070041
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042class NdnGlobalRouterGraph {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070043public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 typedef ns3::Ptr<ns3::ndn::GlobalRouter> Vertice;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070045 typedef uint16_t edge_property_type;
46 typedef uint32_t vertex_property_type;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070047
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 NdnGlobalRouterGraph()
49 {
50 for (ns3::NodeList::Iterator node = ns3::NodeList::Begin(); node != ns3::NodeList::End();
51 node++) {
52 ns3::Ptr<ns3::ndn::GlobalRouter> gr = (*node)->GetObject<ns3::ndn::GlobalRouter>();
53 if (gr != 0)
54 m_vertices.push_back(gr);
55 }
56
57 for (ns3::ChannelList::Iterator channel = ns3::ChannelList::Begin();
58 channel != ns3::ChannelList::End(); channel++) {
59 ns3::Ptr<ns3::ndn::GlobalRouter> gr = (*channel)->GetObject<ns3::ndn::GlobalRouter>();
60 if (gr != 0)
61 m_vertices.push_back(gr);
62 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070063 }
64
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 const std::list<Vertice>&
66 GetVertices() const
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070067 {
68 return m_vertices;
69 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070071public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 std::list<Vertice> m_vertices;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070073};
74
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075class ndn_global_router_graph_category : public virtual vertex_list_graph_tag,
76 public virtual incidence_graph_tag {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070077};
78
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070079template<>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080struct graph_traits<NdnGlobalRouterGraph> {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070081 // Graph concept
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070082 typedef NdnGlobalRouterGraph::Vertice vertex_descriptor;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070083 typedef ns3::ndn::GlobalRouter::Incidency edge_descriptor;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070084 typedef directed_tag directed_category;
85 typedef disallow_parallel_edge_tag edge_parallel_category;
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070086 typedef ndn_global_router_graph_category traversal_category;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070087
88 // VertexList concept
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 typedef std::list<vertex_descriptor>::const_iterator vertex_iterator;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070090 typedef size_t vertices_size_type;
91
92 // AdjacencyGraph concept
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070093 typedef ns3::ndn::GlobalRouter::IncidencyList::iterator out_edge_iterator;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070094 typedef size_t degree_size_type;
95
96 // typedef size_t edges_size_type;
97};
98
99} // namespace boost
100
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101namespace boost {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700102
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103inline graph_traits<NdnGlobalRouterGraph>::vertex_descriptor
104source(graph_traits<NdnGlobalRouterGraph>::edge_descriptor e, const NdnGlobalRouterGraph& g)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700105{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 return e.get<0>();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700107}
108
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109inline graph_traits<NdnGlobalRouterGraph>::vertex_descriptor
110target(graph_traits<NdnGlobalRouterGraph>::edge_descriptor e, const NdnGlobalRouterGraph& g)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700111{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 return e.get<2>();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700113}
114
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115inline std::pair<graph_traits<NdnGlobalRouterGraph>::vertex_iterator,
116 graph_traits<NdnGlobalRouterGraph>::vertex_iterator>
117vertices(const NdnGlobalRouterGraph& g)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700118{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 return make_pair(g.GetVertices().begin(), g.GetVertices().end());
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700120}
121
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122inline graph_traits<NdnGlobalRouterGraph>::vertices_size_type
123num_vertices(const NdnGlobalRouterGraph& g)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700124{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 return g.GetVertices().size();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700126}
127
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128inline std::pair<graph_traits<NdnGlobalRouterGraph>::out_edge_iterator,
129 graph_traits<NdnGlobalRouterGraph>::out_edge_iterator>
130out_edges(graph_traits<NdnGlobalRouterGraph>::vertex_descriptor u, const NdnGlobalRouterGraph& g)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700131{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 return std::make_pair(u->GetIncidencies().begin(), u->GetIncidencies().end());
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700133}
134
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135inline graph_traits<NdnGlobalRouterGraph>::degree_size_type
136out_degree(graph_traits<NdnGlobalRouterGraph>::vertex_descriptor u, const NdnGlobalRouterGraph& g)
137{
138 return u->GetIncidencies().size();
139}
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700140
141//////////////////////////////////////////////////////////////
142// Property maps
143
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800144struct EdgeWeights {
145 EdgeWeights(const NdnGlobalRouterGraph& graph)
146 : m_graph(graph)
147 {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700148 }
149
150private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 const NdnGlobalRouterGraph& m_graph;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700152};
153
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154struct VertexIds {
155 VertexIds(const NdnGlobalRouterGraph& graph)
156 : m_graph(graph)
157 {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700158 }
159
160private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800161 const NdnGlobalRouterGraph& m_graph;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700162};
163
164template<>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800165struct property_map<NdnGlobalRouterGraph, edge_weight_t> {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700166 typedef const EdgeWeights const_type;
167 typedef EdgeWeights type;
168};
169
170template<>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800171struct property_map<NdnGlobalRouterGraph, vertex_index_t> {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700172 typedef const VertexIds const_type;
173 typedef VertexIds type;
174};
175
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700176template<>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800177struct property_traits<EdgeWeights> {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700178 // Metric property map
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800179 typedef tuple<ns3::Ptr<ns3::ndn::Face>, uint16_t, double> value_type;
180 typedef tuple<ns3::Ptr<ns3::ndn::Face>, uint16_t, double> reference;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700181 typedef ns3::ndn::GlobalRouter::Incidency key_type;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700182 typedef readable_property_map_tag category;
183};
184
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800185const property_traits<EdgeWeights>::value_type WeightZero(0, 0, 0.0);
186const property_traits<EdgeWeights>::value_type
187 WeightInf(0, std::numeric_limits<uint16_t>::max(), 0.0);
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700188
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800189struct WeightCompare : public std::binary_function<property_traits<EdgeWeights>::reference,
190 property_traits<EdgeWeights>::reference, bool> {
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700191 bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800192 operator()(tuple<ns3::Ptr<ns3::ndn::Face>, uint32_t, double> a,
193 tuple<ns3::Ptr<ns3::ndn::Face>, uint32_t, double> b) const
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700194 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195 return a.get<1>() < b.get<1>();
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700196 }
197
198 bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800199 operator()(property_traits<EdgeWeights>::reference a, uint32_t b) const
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700200 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201 return a.get<1>() < b;
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700202 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700204 bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800205 operator()(uint32_t a, uint32_t b) const
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700206 {
207 return a < b;
208 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700209};
210
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800211struct WeightCombine
212 : public std::binary_function<uint32_t, property_traits<EdgeWeights>::reference, uint32_t> {
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700213 uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800214 operator()(uint32_t a, property_traits<EdgeWeights>::reference b) const
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700215 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800216 return a + b.get<1>();
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700217 }
218
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800219 tuple<ns3::Ptr<ns3::ndn::Face>, uint32_t, double>
220 operator()(tuple<ns3::Ptr<ns3::ndn::Face>, uint32_t, double> a,
221 property_traits<EdgeWeights>::reference b) const
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700222 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800223 if (a.get<0>() == 0)
224 return make_tuple(b.get<0>(), a.get<1>() + b.get<1>(), a.get<2>() + b.get<2>());
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700225 else
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800226 return make_tuple(a.get<0>(), a.get<1>() + b.get<1>(), a.get<2>() + b.get<2>());
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700227 }
228};
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800229
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700230template<>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800231struct property_traits<VertexIds> {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700232 // Metric property map
233 typedef uint32_t value_type;
234 typedef uint32_t reference;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800235 typedef ns3::Ptr<ns3::ndn::GlobalRouter> key_type;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700236 typedef readable_property_map_tag category;
237};
238
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700239inline EdgeWeights
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800240get(edge_weight_t, const NdnGlobalRouterGraph& g)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700241{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800242 return EdgeWeights(g);
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700243}
244
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700245inline VertexIds
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800246get(vertex_index_t, const NdnGlobalRouterGraph& g)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700247{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800248 return VertexIds(g);
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700249}
250
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700251template<class M, class K, class V>
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700252inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800253put(reference_wrapper<M> mapp, K a, V p)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700254{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800255 mapp.get()[a] = p;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700256}
257
258// void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700259// put (cref< std::map< ns3::Ptr<ns3::ndn::GlobalRouter>, ns3::Ptr<ns3::ndn::GlobalRouter> > > map,
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700260
Alexander Afanasyevb2a11fe2012-12-12 11:38:39 -0800261inline uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800262get(const boost::VertexIds&, ns3::Ptr<ns3::ndn::GlobalRouter>& gr)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700263{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800264 return gr->GetId();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700265}
266
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800267inline property_traits<EdgeWeights>::reference
268get(const boost::EdgeWeights&, ns3::ndn::GlobalRouter::Incidency& edge)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700269{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800270 if (edge.get<1>() == 0)
271 return property_traits<EdgeWeights>::reference(0, 0, 0.0);
272 else {
273 ns3::Ptr<ns3::ndn::Limits> limits = edge.get<1>()->GetObject<ns3::ndn::Limits>();
274 double delay = 0.0;
275 if (limits != 0) // valid limits object
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800276 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800277 delay = limits->GetLinkDelay();
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800278 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800279 return property_traits<EdgeWeights>::reference(edge.get<1>(), edge.get<1>()->GetMetric(),
280 delay);
281 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700282}
283
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800284struct PredecessorsMap
285 : public std::map<ns3::Ptr<ns3::ndn::GlobalRouter>, ns3::Ptr<ns3::ndn::GlobalRouter>> {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700286};
287
288template<>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800289struct property_traits<reference_wrapper<PredecessorsMap>> {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700290 // Metric property map
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800291 typedef ns3::Ptr<ns3::ndn::GlobalRouter> value_type;
292 typedef ns3::Ptr<ns3::ndn::GlobalRouter> reference;
293 typedef ns3::Ptr<ns3::ndn::GlobalRouter> key_type;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700294 typedef read_write_property_map_tag category;
295};
296
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800297struct DistancesMap : public std::map<ns3::Ptr<ns3::ndn::GlobalRouter>,
298 tuple<ns3::Ptr<ns3::ndn::Face>, uint32_t, double>> {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700299};
300
301template<>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800302struct property_traits<reference_wrapper<DistancesMap>> {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700303 // Metric property map
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800304 typedef tuple<ns3::Ptr<ns3::ndn::Face>, uint32_t, double> value_type;
305 typedef tuple<ns3::Ptr<ns3::ndn::Face>, uint32_t, double> reference;
306 typedef ns3::Ptr<ns3::ndn::GlobalRouter> key_type;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700307 typedef read_write_property_map_tag category;
308};
309
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800310inline tuple<ns3::Ptr<ns3::ndn::Face>, uint32_t, double>
311get(DistancesMap& map, ns3::Ptr<ns3::ndn::GlobalRouter> key)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700312{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800313 boost::DistancesMap::iterator i = map.find(key);
314 if (i == map.end())
315 return tuple<ns3::Ptr<ns3::ndn::Face>, uint32_t, double>(0,
316 std::numeric_limits<uint32_t>::max(),
317 0.0);
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700318 else
319 return i->second;
320}
321
322} // namespace boost
323
Alexander Afanasyev6315ef72012-06-01 20:56:31 -0700324/// @endcond
325
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700326#endif // BOOST_GRAPH_NDN_GLOBAL_ROUTING_HELPER_H