Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame^] | 1 | /* -*- 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> |
| 38 | #include "boost-graph-ccnx-global-routing-helper.h" |
| 39 | |
| 40 | NS_LOG_COMPONENT_DEFINE ("CcnxGlobalRoutingHelper"); |
| 41 | |
| 42 | using namespace std; |
| 43 | using namespace boost; |
| 44 | |
| 45 | namespace ns3 { |
| 46 | |
| 47 | void |
| 48 | CcnxGlobalRoutingHelper::Install (Ptr<Node> node) |
| 49 | { |
| 50 | NS_LOG_LOGIC ("Node: " << node->GetId ()); |
| 51 | |
| 52 | Ptr<Ccnx> ccnx = node->GetObject<Ccnx> (); |
| 53 | NS_ASSERT_MSG (ccnx != 0, "Cannot install CcnxGlobalRoutingHelper before Ccnx is installed on a node"); |
| 54 | |
| 55 | Ptr<CcnxGlobalRouter> gr = node->GetObject<CcnxGlobalRouter> (); |
| 56 | if (gr != 0) |
| 57 | { |
| 58 | NS_LOG_DEBUG ("CcnxGlobalRouter is already installed: " << gr); |
| 59 | return; // already installed |
| 60 | } |
| 61 | |
| 62 | gr = CreateObject<CcnxGlobalRouter> (); |
| 63 | node->AggregateObject (gr); |
| 64 | |
| 65 | for (uint32_t faceId = 0; faceId < ccnx->GetNFaces (); faceId++) |
| 66 | { |
| 67 | Ptr<CcnxNetDeviceFace> face = DynamicCast<CcnxNetDeviceFace> (ccnx->GetFace (faceId)); |
| 68 | if (face == 0) |
| 69 | { |
| 70 | NS_LOG_DEBUG ("Skipping non-netdevice face"); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | Ptr<NetDevice> nd = face->GetNetDevice (); |
| 75 | if (nd == 0) |
| 76 | { |
| 77 | NS_LOG_DEBUG ("Not a NetDevice associated with CcnxNetDeviceFace"); |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | Ptr<Channel> ch = nd->GetChannel (); |
| 82 | |
| 83 | if (ch == 0) |
| 84 | { |
| 85 | NS_LOG_DEBUG ("Channel is not associated with NetDevice"); |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if (ch->GetNDevices () == 2) // e.g., point-to-point channel |
| 90 | { |
| 91 | for (uint32_t deviceId = 0; deviceId < ch->GetNDevices (); deviceId ++) |
| 92 | { |
| 93 | Ptr<NetDevice> otherSide = ch->GetDevice (deviceId); |
| 94 | if (nd == otherSide) continue; |
| 95 | |
| 96 | Ptr<Node> otherNode = otherSide->GetNode (); |
| 97 | NS_ASSERT (otherNode != 0); |
| 98 | |
| 99 | Ptr<CcnxGlobalRouter> otherGr = otherNode->GetObject<CcnxGlobalRouter> (); |
| 100 | if (otherGr == 0) |
| 101 | { |
| 102 | Install (otherNode); |
| 103 | } |
| 104 | otherGr = otherNode->GetObject<CcnxGlobalRouter> (); |
| 105 | NS_ASSERT (otherGr != 0); |
| 106 | gr->AddIncidency (face, otherGr); |
| 107 | } |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | Ptr<CcnxGlobalRouter> grChannel = ch->GetObject<CcnxGlobalRouter> (); |
| 112 | if (grChannel == 0) |
| 113 | { |
| 114 | Install (ch); |
| 115 | } |
| 116 | grChannel = ch->GetObject<CcnxGlobalRouter> (); |
| 117 | |
| 118 | gr->AddIncidency (0, grChannel); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | CcnxGlobalRoutingHelper::Install (Ptr<Channel> channel) |
| 125 | { |
| 126 | NS_LOG_LOGIC ("Channel: " << channel->GetId ()); |
| 127 | |
| 128 | Ptr<CcnxGlobalRouter> gr = channel->GetObject<CcnxGlobalRouter> (); |
| 129 | if (gr != 0) |
| 130 | return; |
| 131 | |
| 132 | gr = CreateObject<CcnxGlobalRouter> (); |
| 133 | channel->AggregateObject (gr); |
| 134 | |
| 135 | for (uint32_t deviceId = 0; deviceId < channel->GetNDevices (); deviceId ++) |
| 136 | { |
| 137 | Ptr<NetDevice> dev = channel->GetDevice (deviceId); |
| 138 | |
| 139 | Ptr<Node> node = dev->GetNode (); |
| 140 | NS_ASSERT (node != 0); |
| 141 | |
| 142 | Ptr<CcnxGlobalRouter> grOther = node->GetObject<CcnxGlobalRouter> (); |
| 143 | if (grOther == 0) |
| 144 | { |
| 145 | Install (node); |
| 146 | } |
| 147 | grOther = node->GetObject<CcnxGlobalRouter> (); |
| 148 | NS_ASSERT (grOther != 0); |
| 149 | |
| 150 | gr->AddIncidency (0, grOther); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | CcnxGlobalRoutingHelper::AddOrigin (const std::string &prefix, Ptr<Node> node) |
| 156 | { |
| 157 | Ptr<CcnxGlobalRouter> gr = node->GetObject<CcnxGlobalRouter> (); |
| 158 | NS_ASSERT_MSG (gr != 0, |
| 159 | "CcnxGlobalRouter is not installed on the node"); |
| 160 | |
| 161 | Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> (boost::lexical_cast<CcnxNameComponents> (prefix)); |
| 162 | gr->AddLocalPrefix (name); |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | CcnxGlobalRoutingHelper::AddOrigin (const std::string &prefix, const std::string &nodeName) |
| 167 | { |
| 168 | Ptr<Node> node = Names::Find<Node> (nodeName); |
| 169 | NS_ASSERT_MSG (node != 0, nodeName << "is not a Node"); |
| 170 | |
| 171 | AddOrigin (prefix, node); |
| 172 | } |
| 173 | |
| 174 | } // namespace ns3 |
| 175 | |
| 176 | |
| 177 | |
| 178 | #include <boost/concept/assert.hpp> |
| 179 | #include <boost/graph/graph_concepts.hpp> |
| 180 | // #include <boost/graph/adjacency_list.hpp> |
| 181 | #include <boost/graph/dijkstra_shortest_paths.hpp> |
| 182 | |
| 183 | namespace ns3 { |
| 184 | |
| 185 | void |
| 186 | CcnxGlobalRoutingHelper::CalculateRoutes () |
| 187 | { |
| 188 | BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< CcnxGlobalRouterGraph > )); |
| 189 | BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< CcnxGlobalRouterGraph > )); |
| 190 | // BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept< CcnxGlobalRouterGraph, |
| 191 | // graph_traits < CcnxGlobalRouterGraph >::edge_descriptor > )); |
| 192 | // BOOST_CONCEPT_ASSERT(( PropertyGraphConcept< CcnxGlobalRouterGraph, |
| 193 | // graph_traits < CcnxGlobalRouterGraph >::edge_descriptor, |
| 194 | // edge_weight_t> )); |
| 195 | // BOOST_CONCEPT_ASSERT(( PropertyMapConcept< CcnxGlobalRouterGraph, edge_weight_t, |
| 196 | // graph_traits < CcnxGlobalRouterGraph >::edge_descriptor> )); |
| 197 | |
| 198 | |
| 199 | CcnxGlobalRouterGraph graph; |
| 200 | Ptr<CcnxGlobalRouter> source = (*NodeList::Begin ())->GetObject<CcnxGlobalRouter> (); |
| 201 | |
| 202 | typedef graph_traits < CcnxGlobalRouterGraph >::vertex_descriptor vertex_descriptor; |
| 203 | |
| 204 | PredecessorsMap predecessors; |
| 205 | DistancesMap distances; |
| 206 | // std::map< vertex_descriptor, int > distances; |
| 207 | // std::vector<uint32_t> d (num_vertices (graph)); |
| 208 | // std::vector<int> distances; |
| 209 | // std::map< vertex_descriptor, std:: |
| 210 | |
| 211 | dijkstra_shortest_paths (graph, source, |
| 212 | predecessor_map (predecessors) |
| 213 | . |
| 214 | distance_map (distances) |
| 215 | ); |
| 216 | |
| 217 | // BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<CcnxGlobalRouterGraph> )); |
| 218 | // BOOST_CONCEPT_ASSERT(( MutableGraphConcept<CcnxGlobalRouterGraph> )); |
| 219 | |
| 220 | // typedef adjacency_list < listS, vecS, undirectedS, no_property, property < edge_weight_t, uint16_t > > Graph; |
| 221 | // typedef Graph::vertex_descriptor Vertex; |
| 222 | |
| 223 | // class Graph |
| 224 | // { |
| 225 | // public: |
| 226 | // typedef Ptr<CcnxGlobalRouter> vertex_descriptor; |
| 227 | // typedef pair< Ptr<CcnxGlobalRouter>, Ptr<CcnxGlobalRouter> > edge_descriptor; |
| 228 | // typedef directed_tag directed_category; |
| 229 | // typedef disallow_parallel_edge_tag edge_parallel_category; |
| 230 | // typedef adjacency_graph_tag traversal_category; |
| 231 | |
| 232 | // typedef CcnxGlobalRouter::Incidency adjacency_iterator; |
| 233 | |
| 234 | // // null_vertex() ??? |
| 235 | // // adjacent_vertices(v, g) ??? |
| 236 | // }; |
| 237 | |
| 238 | // Graph graph; |
| 239 | |
| 240 | // for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++) |
| 241 | // { |
| 242 | // Ptr<CcnxGlobalRouter> gr = (*node)->GetObject<CcnxGlobalRouter> (); |
| 243 | // if (gr == 0) |
| 244 | // continue; |
| 245 | |
| 246 | // for (CcnxGlobalRouter::IncidencyList::const_iterator i = gr->GetIncidencies ().begin (); |
| 247 | // i != gr->GetIncidencies ().end (); |
| 248 | // i++) |
| 249 | // { |
| 250 | // add_edge (gr, i->get<1> (), 10, graph); |
| 251 | // } |
| 252 | // break; |
| 253 | // } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | |
| 258 | } |