blob: 5fde9cbd7090a7a6f189c4b5fb7e660044f3516d [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) 2011 UCLA
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#include "ccnx-global-routing-helper.h"
22
23#include "ns3/ccnx.h"
24#include "../model/ccnx-net-device-face.h"
25#include "../model/ccnx-global-router.h"
26#include "ns3/ccnx-name-components.h"
27
28#include "ns3/node.h"
29#include "ns3/net-device.h"
30#include "ns3/channel.h"
31#include "ns3/log.h"
32#include "ns3/assert.h"
33#include "ns3/names.h"
34#include "ns3/node-list.h"
35#include "ns3/channel-list.h"
36
37#include <boost/lexical_cast.hpp>
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070038#include <boost/concept/assert.hpp>
39// #include <boost/graph/graph_concepts.hpp>
40// #include <boost/graph/adjacency_list.hpp>
41#include <boost/graph/dijkstra_shortest_paths.hpp>
42
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070043#include "boost-graph-ccnx-global-routing-helper.h"
44
45NS_LOG_COMPONENT_DEFINE ("CcnxGlobalRoutingHelper");
46
47using namespace std;
48using namespace boost;
49
50namespace ns3 {
51
52void
53CcnxGlobalRoutingHelper::Install (Ptr<Node> node)
54{
55 NS_LOG_LOGIC ("Node: " << node->GetId ());
56
57 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
58 NS_ASSERT_MSG (ccnx != 0, "Cannot install CcnxGlobalRoutingHelper before Ccnx is installed on a node");
59
60 Ptr<CcnxGlobalRouter> gr = node->GetObject<CcnxGlobalRouter> ();
61 if (gr != 0)
62 {
63 NS_LOG_DEBUG ("CcnxGlobalRouter is already installed: " << gr);
64 return; // already installed
65 }
66
67 gr = CreateObject<CcnxGlobalRouter> ();
68 node->AggregateObject (gr);
69
70 for (uint32_t faceId = 0; faceId < ccnx->GetNFaces (); faceId++)
71 {
72 Ptr<CcnxNetDeviceFace> face = DynamicCast<CcnxNetDeviceFace> (ccnx->GetFace (faceId));
73 if (face == 0)
74 {
75 NS_LOG_DEBUG ("Skipping non-netdevice face");
76 continue;
77 }
78
79 Ptr<NetDevice> nd = face->GetNetDevice ();
80 if (nd == 0)
81 {
82 NS_LOG_DEBUG ("Not a NetDevice associated with CcnxNetDeviceFace");
83 continue;
84 }
85
86 Ptr<Channel> ch = nd->GetChannel ();
87
88 if (ch == 0)
89 {
90 NS_LOG_DEBUG ("Channel is not associated with NetDevice");
91 continue;
92 }
93
94 if (ch->GetNDevices () == 2) // e.g., point-to-point channel
95 {
96 for (uint32_t deviceId = 0; deviceId < ch->GetNDevices (); deviceId ++)
97 {
98 Ptr<NetDevice> otherSide = ch->GetDevice (deviceId);
99 if (nd == otherSide) continue;
100
101 Ptr<Node> otherNode = otherSide->GetNode ();
102 NS_ASSERT (otherNode != 0);
103
104 Ptr<CcnxGlobalRouter> otherGr = otherNode->GetObject<CcnxGlobalRouter> ();
105 if (otherGr == 0)
106 {
107 Install (otherNode);
108 }
109 otherGr = otherNode->GetObject<CcnxGlobalRouter> ();
110 NS_ASSERT (otherGr != 0);
111 gr->AddIncidency (face, otherGr);
112 }
113 }
114 else
115 {
116 Ptr<CcnxGlobalRouter> grChannel = ch->GetObject<CcnxGlobalRouter> ();
117 if (grChannel == 0)
118 {
119 Install (ch);
120 }
121 grChannel = ch->GetObject<CcnxGlobalRouter> ();
122
123 gr->AddIncidency (0, grChannel);
124 }
125 }
126}
127
128void
129CcnxGlobalRoutingHelper::Install (Ptr<Channel> channel)
130{
131 NS_LOG_LOGIC ("Channel: " << channel->GetId ());
132
133 Ptr<CcnxGlobalRouter> gr = channel->GetObject<CcnxGlobalRouter> ();
134 if (gr != 0)
135 return;
136
137 gr = CreateObject<CcnxGlobalRouter> ();
138 channel->AggregateObject (gr);
139
140 for (uint32_t deviceId = 0; deviceId < channel->GetNDevices (); deviceId ++)
141 {
142 Ptr<NetDevice> dev = channel->GetDevice (deviceId);
143
144 Ptr<Node> node = dev->GetNode ();
145 NS_ASSERT (node != 0);
146
147 Ptr<CcnxGlobalRouter> grOther = node->GetObject<CcnxGlobalRouter> ();
148 if (grOther == 0)
149 {
150 Install (node);
151 }
152 grOther = node->GetObject<CcnxGlobalRouter> ();
153 NS_ASSERT (grOther != 0);
154
155 gr->AddIncidency (0, grOther);
156 }
157}
158
159void
160CcnxGlobalRoutingHelper::AddOrigin (const std::string &prefix, Ptr<Node> node)
161{
162 Ptr<CcnxGlobalRouter> gr = node->GetObject<CcnxGlobalRouter> ();
163 NS_ASSERT_MSG (gr != 0,
164 "CcnxGlobalRouter is not installed on the node");
165
166 Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> (boost::lexical_cast<CcnxNameComponents> (prefix));
167 gr->AddLocalPrefix (name);
168}
169
170void
171CcnxGlobalRoutingHelper::AddOrigin (const std::string &prefix, const std::string &nodeName)
172{
173 Ptr<Node> node = Names::Find<Node> (nodeName);
174 NS_ASSERT_MSG (node != 0, nodeName << "is not a Node");
175
176 AddOrigin (prefix, node);
177}
178
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700179void
180CcnxGlobalRoutingHelper::CalculateRoutes ()
181{
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700182 NS_LOG_DEBUG ("Enter");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700183 BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< CcnxGlobalRouterGraph > ));
184 BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< CcnxGlobalRouterGraph > ));
185 // BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept< CcnxGlobalRouterGraph,
186 // graph_traits < CcnxGlobalRouterGraph >::edge_descriptor > ));
187 // BOOST_CONCEPT_ASSERT(( PropertyGraphConcept< CcnxGlobalRouterGraph,
188 // graph_traits < CcnxGlobalRouterGraph >::edge_descriptor,
189 // edge_weight_t> ));
190 // BOOST_CONCEPT_ASSERT(( PropertyMapConcept< CcnxGlobalRouterGraph, edge_weight_t,
191 // graph_traits < CcnxGlobalRouterGraph >::edge_descriptor> ));
192
193
194 CcnxGlobalRouterGraph graph;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700195 typedef graph_traits < CcnxGlobalRouterGraph >::vertex_descriptor vertex_descriptor;
196
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700197 for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++)
198 {
199 Ptr<CcnxGlobalRouter> source = (*node)->GetObject<CcnxGlobalRouter> ();
200 if (source == 0)
201 {
202 NS_LOG_DEBUG ("Node " << (*node)->GetId () << " does not export CcnxGlobalRouter interface");
203 continue;
204 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700205
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700206 // PredecessorsMap predecessors;
207 DistancesMap distances;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700208
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700209 dijkstra_shortest_paths (graph, source,
210 // predecessor_map (boost::ref(predecessors))
211 // .
212 distance_map (boost::ref(distances))
213 .
214 distance_inf (WeightInf)
215 .
216 distance_zero (WeightZero)
217 .
218 distance_compare (boost::WeightCompare ())
219 .
220 distance_combine (boost::WeightCombine ())
221 );
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700222
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700223 // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ());
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700224
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700225 cout << "Reachability from Node: " << source->GetObject<Node> ()->GetId () << endl;
226 for (DistancesMap::iterator i = distances.begin ();
227 i != distances.end ();
228 i++)
229 {
230 if (i->first == source)
231 continue;
232 else
233 {
234 cout << " Node " << i->first->GetObject<Node> ()->GetId ();
235 if (distances[i->first].get<0> () == 0)
236 cout << " is unreachable" << endl;
237 else
238 cout << " reachable via face " << *i->second.get<0> ()
239 << " with distance " << i->second.get<1> () << endl;
240 }
241 }
242
243 }
244 // NS_LOG_DEBUG ("Exit");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700245}
246
247
248}