NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
boost-graph-ndn-global-routing-helper.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #ifndef BOOST_GRAPH_NDN_GLOBAL_ROUTING_HELPER_H
21 #define BOOST_GRAPH_NDN_GLOBAL_ROUTING_HELPER_H
22 
24 
25 #include "ns3/ndnSIM/model/ndn-common.hpp"
26 
27 #include <boost/graph/graph_traits.hpp>
28 #include <boost/graph/properties.hpp>
29 #include <boost/ref.hpp>
30 
31 #include "ns3/ndnSIM/model/ndn-global-router.hpp"
32 
33 #include "ns3/node.h"
34 #include "ns3/node-list.h"
35 #include "ns3/channel.h"
36 #include "ns3/channel-list.h"
37 
38 #include <list>
39 #include <map>
40 
41 namespace boost {
42 
43 class NdnGlobalRouterGraph {
44 public:
45  typedef ns3::Ptr<ns3::ndn::GlobalRouter> Vertice;
46  typedef uint16_t edge_property_type;
47  typedef uint32_t vertex_property_type;
48 
49  NdnGlobalRouterGraph()
50  {
51  for (ns3::NodeList::Iterator node = ns3::NodeList::Begin(); node != ns3::NodeList::End();
52  node++) {
53  ns3::Ptr<ns3::ndn::GlobalRouter> gr = (*node)->GetObject<ns3::ndn::GlobalRouter>();
54  if (gr != 0)
55  m_vertices.push_back(gr);
56  }
57 
58  for (ns3::ChannelList::Iterator channel = ns3::ChannelList::Begin();
59  channel != ns3::ChannelList::End(); channel++) {
60  ns3::Ptr<ns3::ndn::GlobalRouter> gr = (*channel)->GetObject<ns3::ndn::GlobalRouter>();
61  if (gr != 0)
62  m_vertices.push_back(gr);
63  }
64  }
65 
66  const std::list<Vertice>&
67  GetVertices() const
68  {
69  return m_vertices;
70  }
71 
72 public:
73  std::list<Vertice> m_vertices;
74 };
75 
76 class ndn_global_router_graph_category : public virtual vertex_list_graph_tag,
77  public virtual incidence_graph_tag {
78 };
79 
80 template<>
81 struct graph_traits<NdnGlobalRouterGraph> {
82  // Graph concept
83  typedef NdnGlobalRouterGraph::Vertice vertex_descriptor;
84  typedef ns3::ndn::GlobalRouter::Incidency edge_descriptor;
85  typedef directed_tag directed_category;
86  typedef disallow_parallel_edge_tag edge_parallel_category;
87  typedef ndn_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::ndn::GlobalRouter::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 inline graph_traits<NdnGlobalRouterGraph>::vertex_descriptor
105 source(graph_traits<NdnGlobalRouterGraph>::edge_descriptor e, const NdnGlobalRouterGraph& g)
106 {
107  return std::get<0>(e);
108 }
109 
110 inline graph_traits<NdnGlobalRouterGraph>::vertex_descriptor
111 target(graph_traits<NdnGlobalRouterGraph>::edge_descriptor e, const NdnGlobalRouterGraph& g)
112 {
113  return std::get<2>(e);
114 }
115 
116 inline std::pair<graph_traits<NdnGlobalRouterGraph>::vertex_iterator,
117  graph_traits<NdnGlobalRouterGraph>::vertex_iterator>
118 vertices(const NdnGlobalRouterGraph& g)
119 {
120  return make_pair(g.GetVertices().begin(), g.GetVertices().end());
121 }
122 
123 inline graph_traits<NdnGlobalRouterGraph>::vertices_size_type
124 num_vertices(const NdnGlobalRouterGraph& g)
125 {
126  return g.GetVertices().size();
127 }
128 
129 inline std::pair<graph_traits<NdnGlobalRouterGraph>::out_edge_iterator,
130  graph_traits<NdnGlobalRouterGraph>::out_edge_iterator>
131 out_edges(graph_traits<NdnGlobalRouterGraph>::vertex_descriptor u, const NdnGlobalRouterGraph& g)
132 {
133  return std::make_pair(u->GetIncidencies().begin(), u->GetIncidencies().end());
134 }
135 
136 inline graph_traits<NdnGlobalRouterGraph>::degree_size_type
137 out_degree(graph_traits<NdnGlobalRouterGraph>::vertex_descriptor u, const NdnGlobalRouterGraph& g)
138 {
139  return u->GetIncidencies().size();
140 }
141 
143 // Property maps
144 
145 struct EdgeWeights {
146  EdgeWeights(const NdnGlobalRouterGraph& graph)
147  : m_graph(graph)
148  {
149  }
150 
151 private:
152  const NdnGlobalRouterGraph& m_graph;
153 };
154 
155 struct VertexIds {
156  VertexIds(const NdnGlobalRouterGraph& graph)
157  : m_graph(graph)
158  {
159  }
160 
161 private:
162  const NdnGlobalRouterGraph& m_graph;
163 };
164 
165 template<>
166 struct property_map<NdnGlobalRouterGraph, edge_weight_t> {
167  typedef const EdgeWeights const_type;
168  typedef EdgeWeights type;
169 };
170 
171 template<>
172 struct property_map<NdnGlobalRouterGraph, vertex_index_t> {
173  typedef const VertexIds const_type;
174  typedef VertexIds type;
175 };
176 
177 template<>
178 struct property_traits<EdgeWeights> {
179  // Metric property map
180  typedef std::tuple<std::shared_ptr<nfd::Face>, uint16_t, double> value_type;
181  typedef std::tuple<std::shared_ptr<nfd::Face>, uint16_t, double> reference;
182  typedef ns3::ndn::GlobalRouter::Incidency key_type;
183  typedef readable_property_map_tag category;
184 };
185 
186 const property_traits<EdgeWeights>::value_type WeightZero(nullptr, 0, 0.0);
187 const property_traits<EdgeWeights>::value_type
188  WeightInf(nullptr, std::numeric_limits<uint16_t>::max(), 0.0);
189 
190 struct WeightCompare : public std::binary_function<property_traits<EdgeWeights>::reference,
191  property_traits<EdgeWeights>::reference, bool> {
192  bool
193  operator()(std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double> a,
194  std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double> b) const
195  {
196  return std::get<1>(a) < std::get<1>(b);
197  }
198 
199  bool
200  operator()(property_traits<EdgeWeights>::reference a, uint32_t b) const
201  {
202  return std::get<1>(a) < b;
203  }
204 
205  bool
206  operator()(uint32_t a, uint32_t b) const
207  {
208  return a < b;
209  }
210 };
211 
212 struct WeightCombine
213  : public std::binary_function<uint32_t, property_traits<EdgeWeights>::reference, uint32_t> {
214  uint32_t
215  operator()(uint32_t a, property_traits<EdgeWeights>::reference b) const
216  {
217  return a + std::get<1>(b);
218  }
219 
220  std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double>
221  operator()(std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double> a,
222  property_traits<EdgeWeights>::reference b) const
223  {
224  if (std::get<0>(a) == nullptr)
225  return std::make_tuple(std::get<0>(b), std::get<1>(a) + std::get<1>(b),
226  std::get<2>(a) + std::get<2>(b));
227  else
228  return std::make_tuple(std::get<0>(a), std::get<1>(a) + std::get<1>(b),
229  std::get<2>(a) + std::get<2>(b));
230  }
231 };
232 
233 template<>
234 struct property_traits<VertexIds> {
235  // Metric property map
236  typedef uint32_t value_type;
237  typedef uint32_t reference;
238  typedef ns3::Ptr<ns3::ndn::GlobalRouter> key_type;
239  typedef readable_property_map_tag category;
240 };
241 
242 inline EdgeWeights
243 get(edge_weight_t, const NdnGlobalRouterGraph& g)
244 {
245  return EdgeWeights(g);
246 }
247 
248 inline VertexIds
249 get(vertex_index_t, const NdnGlobalRouterGraph& g)
250 {
251  return VertexIds(g);
252 }
253 
254 template<class M, class K, class V>
255 inline void
256 put(reference_wrapper<M> mapp, K a, V p)
257 {
258  mapp.get()[a] = p;
259 }
260 
261 // void
262 // put (cref< std::map< ns3::Ptr<ns3::ndn::GlobalRouter>, ns3::Ptr<ns3::ndn::GlobalRouter> > > map,
263 
264 inline uint32_t
265 get(const boost::VertexIds&, ns3::Ptr<ns3::ndn::GlobalRouter>& gr)
266 {
267  return gr->GetId();
268 }
269 
270 inline property_traits<EdgeWeights>::reference
271 get(const boost::EdgeWeights&, ns3::ndn::GlobalRouter::Incidency& edge)
272 {
273  if (std::get<1>(edge) == 0)
274  return property_traits<EdgeWeights>::reference(nullptr, 0, 0.0);
275  else {
276  return property_traits<EdgeWeights>::reference(std::get<1>(edge),
277  static_cast<uint16_t>(
278  std::get<1>(edge)->getMetric()),
279  0.0);
280  }
281 }
282 
283 struct PredecessorsMap
284  : public std::map<ns3::Ptr<ns3::ndn::GlobalRouter>, ns3::Ptr<ns3::ndn::GlobalRouter>> {
285 };
286 
287 template<>
288 struct property_traits<reference_wrapper<PredecessorsMap>> {
289  // Metric property map
290  typedef ns3::Ptr<ns3::ndn::GlobalRouter> value_type;
291  typedef ns3::Ptr<ns3::ndn::GlobalRouter> reference;
292  typedef ns3::Ptr<ns3::ndn::GlobalRouter> key_type;
293  typedef read_write_property_map_tag category;
294 };
295 
296 struct DistancesMap : public std::map<ns3::Ptr<ns3::ndn::GlobalRouter>,
297  std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double>> {
298 };
299 
300 template<>
301 struct property_traits<reference_wrapper<DistancesMap>> {
302  // Metric property map
303  typedef std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double> value_type;
304  typedef std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double> reference;
305  typedef ns3::Ptr<ns3::ndn::GlobalRouter> key_type;
306  typedef read_write_property_map_tag category;
307 };
308 
309 } // boost
310 
311 namespace std {
312 template<>
313 class numeric_limits<std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double>>
314 {
315 public:
316  typedef std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double> value;
317  static value
318  max()
319  {
320  return boost::WeightInf;
321  }
322 };
323 }
324 
325 namespace boost {
326 
327 inline std::tuple<std::shared_ptr<nfd::Face>, uint32_t, double>
328 get(DistancesMap& map, ns3::Ptr<ns3::ndn::GlobalRouter> key)
329 {
330  boost::DistancesMap::iterator i = map.find(key);
331  if (i == map.end())
332  return std::tuple<std::shared_ptr<nfd::Face>, uint32_t,
333  double>(nullptr, std::numeric_limits<uint32_t>::max(), 0.0);
334  else
335  return i->second;
336 }
337 
338 } // namespace boost
339 
341 
342 #endif // BOOST_GRAPH_NDN_GLOBAL_ROUTING_HELPER_H
nonstd::variants::get
R & get(variant< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 > &v, nonstd_lite_in_place_type_t(R)=nonstd_lite_in_place_type(R))
Definition: variant.hpp:1753
boost
Definition: block.hpp:31
ns3::ndn::GlobalRouter
Class representing global router interface for ndnSIM.
Definition: ndn-global-router.hpp:43
ns3::ndn::GlobalRouter::Incidency
std::tuple< Ptr< GlobalRouter >, shared_ptr< Face >, Ptr< GlobalRouter > > Incidency
Graph edge.
Definition: ndn-global-router.hpp:48