Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions ns-3.35/src/ofswitch13/model/simple-controller-flex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,23 @@ void
SimpleControllerFlex::UpdateWeights ()
{
Graph topo = Topology::GetGraph ();
IntegerWeightCalc weightCalc = Topology::GetDefaultWeightCalc ();

boost::graph_traits<Graph>::edge_iterator edgeIt, edgeEnd;
for (boost::tie (edgeIt, edgeEnd) = boost::edges (topo); edgeIt != edgeEnd; ++edgeIt)
auto bmap = get (edge_bundle, topo);

for (auto ed : make_iterator_range (edges (topo)))
{
Edge ed = *edgeIt;
Ptr<Node> n1 = Topology::VertexToNode (ed.m_source);
Ptr<Node> n2 = Topology::VertexToNode (ed.m_target);
Edge &e = bmap[ed];
Ptr<Node> n1 = NodeList::GetNode (e.first);
Ptr<Node> n2 = NodeList::GetNode (e.second);

if (n1->IsSwitch () && n2->IsSwitch ())
{
int index = int (Simulator::Now ().GetMinutes ()) % 60;
float flex1 = EnergyAPI::GetFlexArray (Names::FindName (n1)).at (index);
float flex2 = EnergyAPI::GetFlexArray (Names::FindName (n2)).at (index);

Topology::UpdateEdgeWeight (n1, n2, flex1 + flex2);
weightCalc.UpdateEdgeWeight (e, flex1 + flex2);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions ns-3.35/src/parser/model/parse-links.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

#include "parse-links.h"
#include "parser.h"
#include "ns3/topology.h"
#include "toml.hpp"
#include "ns3/ipv4-address-helper.h"
#include "ns3/point-to-point-ethernet-helper.h"
Expand Down Expand Up @@ -62,16 +62,16 @@ installLinks (toml::table tbl, string outPath)
if (n0->IsHost ())
{
Ipv4InterfaceContainer ips = address.Assign (p2pDevices.Get (0));
Topology::AddHost (n0, ips.GetAddress (0));
Topology::AddNodeIpAddress (n0, ips.GetAddress (0));
}

if (n1->IsHost ())
{
Ipv4InterfaceContainer ips = address.Assign (p2pDevices.Get (1));
Topology::AddHost (n1, ips.GetAddress (0));
Topology::AddNodeIpAddress (n1, ips.GetAddress (0));
}

Topology::AddLink (n0, n1, channel);
Topology::AddEdge (n0, n1, channel);
}
}

Expand Down
1 change: 0 additions & 1 deletion ns-3.35/src/parser/model/parse-nodes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ parseNodes (string topoName)
node->SetAttribute ("CpuCapacity",
StringValue (configs["cpuCapacity"].value_or ("100Gbps")));
parseEnergyModels (configs, node);
Topology::AddSwitch (node);
}
else if (!nodeType.compare ("host"))
{
Expand Down
257 changes: 59 additions & 198 deletions ns-3.35/src/topology/model/topology.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,202 +21,94 @@
*/

#include "topology.h"
#include <boost/graph/detail/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/properties.hpp>
#include "ns3/node-list.h"

namespace ns3 {

NS_OBJECT_ENSURE_REGISTERED (Topology);

Graph Topology::m_graph = Graph ();
/* ########### IntegerWeightCalc ########### */

std::map<Ptr<Node>, Vertex> Topology::m_vertexes = std::map<Ptr<Node>, Vertex> ();
std::map<Vertex, Ptr<Node>> Topology::m_nodes = std::map<Vertex, Ptr<Node>> ();
std::map<Ipv4Address, Vertex> Topology::m_ip_to_vertex = std::map<Ipv4Address, Vertex> ();
std::map<Vertex, Ipv4Address> Topology::m_vertex_to_ip = std::map<Vertex, Ipv4Address> ();
std::map<Edge, Ptr<Channel>> Topology::m_channels = std::map<Edge, Ptr<Channel>> ();

TypeId
Topology::GetTypeId (void)
IntegerWeightCalc::IntegerWeightCalc () : m_weights ()
{
static TypeId tid = TypeId ("ns3::Topology").SetParent<Object> ().AddConstructor<Topology> ();
return tid;
}

Topology::Topology ()
IntegerWeightCalc::~IntegerWeightCalc ()
{
}

Topology::~Topology ()
int
IntegerWeightCalc::GetInitialWeight () const
{
}
return 0;
};

Vertex
Topology::AddNode (Ptr<Node> node)
int
IntegerWeightCalc::GetNonViableWeight () const
{
Vertex vd = add_vertex (node, m_graph);
m_vertexes[node] = vd;
m_nodes[vd] = node;
return vd;
}
return std::numeric_limits<int>::max ();
};

void
Topology::AddSwitch (Ptr<Node> sw)
int
IntegerWeightCalc::GetWeight (Edge &e) const
{
AddNode (sw);
}
auto iter = m_weights.find (e);
if (iter == m_weights.end ())
return 1;

void
Topology::AddHost (Ptr<Node> host, Ipv4Address ip)
{
Vertex vd = AddNode (host);
m_ip_to_vertex[ip] = vd;
m_vertex_to_ip[vd] = ip;
}
return (*iter).second;
};

void
Topology::AddLink (Ptr<Node> n1, Ptr<Node> n2, Ptr<Channel> channel)
{
Vertex vd1 = m_vertexes[n1];
Vertex vd2 = m_vertexes[n2];
Edge e = add_edge (vd1, vd2, 1, m_graph).first;
m_channels[e] = channel;
}

std::vector<Vertex>
Topology::DijkstraShortestPathsInternal (Vertex src)
IntegerWeightCalc::UpdateEdgeWeight (Edge &e, int weight)
{
std::vector<Vertex> predecessors (num_vertices (m_graph));
dijkstra_shortest_paths (m_graph, src,
predecessor_map (boost::make_iterator_property_map (
predecessors.begin (), get (boost::vertex_index, m_graph)))

);
return predecessors;
m_weights[e] = weight;
}

std::vector<Vertex>
Topology::DijkstraShortestPathInternal (Vertex src, Vertex dst)
{
std::vector<Vertex> predecessors = DijkstraShortestPathsInternal (src);
std::vector<Vertex> path;
Vertex currentVertex = dst;
/* ########### Topology ########### */

while (currentVertex != src)
{
path.push_back (currentVertex);
currentVertex = predecessors[currentVertex];
}
path.push_back (src);
std::reverse (path.begin (), path.end ());
Graph Topology::m_graph{};
IntegerWeightCalc Topology::m_default_calc{};

return path;
}
std::map<Ipv4Address, Ptr<Node>> Topology::m_ip_to_node{};
std::map<Edge, Ptr<Channel>> Topology::m_channels{};

std::vector<Ptr<Node>>
Topology::VertexToNode (std::vector<Vertex> path)
{
std::vector<Ptr<Node>> path_nodes = std::vector<Ptr<Node>> ();
for (auto i = path.begin (); i != path.end (); i++)
path_nodes.push_back (m_nodes[*i]);
return path_nodes;
}

std::vector<Ptr<Node>>
Topology::DijkstraShortestPath (Ptr<Node> src, Ptr<Node> dst)
{
return VertexToNode (DijkstraShortestPathInternal (m_vertexes[src], m_vertexes[dst]));
}

std::vector<Ptr<Node>>
Topology::DijkstraShortestPath (Ptr<Node> src, Ipv4Address dst)
{
return DijkstraShortestPath (src, m_nodes[m_ip_to_vertex[dst]]);
}

std::vector<Ptr<Node>>
Topology::DijkstraShortestPath (Ipv4Address src, Ptr<Node> dst)
TypeId
Topology::GetTypeId (void)
{
return DijkstraShortestPath (m_nodes[m_ip_to_vertex[src]], dst);
static TypeId tid = TypeId ("ns3::Topology").SetParent<Object> ().AddConstructor<Topology> ();
return tid;
}

std::vector<Ptr<Node>>
Topology::DijkstraShortestPath (Ipv4Address src, Ipv4Address dst)
Topology::Topology ()
{
return DijkstraShortestPath (m_nodes[m_ip_to_vertex[src]], m_nodes[m_ip_to_vertex[dst]]);
}

std::vector<Ptr<Node>>
Topology::DijkstraShortestPath (std::string src, std::string dst)
Topology::~Topology ()
{
Ptr<Node> srcNode = Names::Find<Node> (src);
Ptr<Node> dstNode = Names::Find<Node> (dst);
return DijkstraShortestPath (srcNode, dstNode);
}

std::vector<Ptr<Node>>
Topology::DijkstraShortestPaths (Ptr<Node> src)
void
Topology::AddNodeIpAddress (Ptr<Node> node, Ipv4Address ip)
{
return VertexToNode (DijkstraShortestPathsInternal (m_vertexes[src]));
m_ip_to_node[ip] = node;
}

std::vector<Ptr<Node>>
Topology::DijkstraShortestPaths (Ipv4Address src)
void
Topology::AddEdge (Ptr<Node> n1, Ptr<Node> n2, Ptr<Channel> channel)
{
return DijkstraShortestPaths (m_nodes[m_ip_to_vertex[src]]);
Edge e (n1->GetId (), n2->GetId ());
add_edge (e.first, e.second, e, m_graph);
m_channels[e] = channel;
}

std::vector<Ptr<Node>>
Topology::DijkstraShortestPaths (std::string src)
{
Ptr<Node> srcNode = Names::Find<Node> (src);
return DijkstraShortestPaths (srcNode);
}

// NOTE: Code partially generated by ChatGPT
std::vector<std::pair<std::vector<Ptr<Node>>, int>>
Topology::DijkstraShortestPaths (Ptr<Node> src, Ptr<Node> dst)
Topology::VertexToNode (std::vector<Vertex> path)
{
std::vector<Vertex> predecessors (num_vertices (m_graph));
std::vector<int> distances (num_vertices (m_graph));
Vertex source = NodeToVertex (src);
Vertex target = NodeToVertex (dst);

dijkstra_shortest_paths (m_graph, source,
predecessor_map (make_iterator_property_map (
predecessors.begin (), get (vertex_index, m_graph)))
.distance_map (make_iterator_property_map (
distances.begin (), get (vertex_index, m_graph))));

// Create a vector to store the paths and their distances
std::vector<std::pair<std::vector<Ptr<Node>>, int>> paths;

// Loop through all vertices and store the path and distance from the source
for (Vertex v = 0; v < num_vertices (m_graph); ++v)
{
if (v == source || v == target)
continue;

std::vector<Ptr<Node>> path;
for (Vertex u = v; u != source; u = predecessors[u])
path.push_back (VertexToNode (u));

path.push_back (VertexToNode (source));
std::reverse (path.begin (), path.end ());
path.push_back (VertexToNode (target));

paths.emplace_back (std::move (path), distances[v]);
}

// Sort the paths by distance in ascending order
std::sort (paths.begin (), paths.end (),
[] (const auto &lhs, const auto &rhs) { return lhs.second < rhs.second; });

// Now, the 'paths' vector contains all paths between 'source' and 'target'
// sorted by their distances in ascending order.
// Each entry in the 'paths' vector is a pair (path, distance).
return paths;
std::vector<Ptr<Node>> path_nodes = std::vector<Ptr<Node>> ();
for (auto i = path.begin (); i != path.end (); i++)
path_nodes.push_back (NodeList::GetNode (*i));
return path_nodes;
}

Graph
Expand All @@ -225,66 +117,35 @@ Topology::GetGraph ()
return m_graph;
}

Ptr<Node>
Topology::VertexToNode (Vertex vd)
{
return m_nodes[vd];
}

Vertex
Topology::NodeToVertex (Ptr<Node> node)
{
return m_vertexes[node];
}

void
Topology::UpdateEdgeWeight (Ptr<Node> n1, Ptr<Node> n2, int newWeight)
IntegerWeightCalc &
Topology::GetDefaultWeightCalc ()
{
std::pair<Edge, bool> pair = boost::edge (m_vertexes[n1], m_vertexes[n2], m_graph);

if (pair.second)
Topology::UpdateEdgeWeight (pair.first, newWeight);
return m_default_calc;
}

void
Topology::UpdateEdgeWeight (Edge ed, int newWeight)
{
// Prevent negative weights
if (newWeight < 0)
newWeight = 0;
put (edge_weight_t (), m_graph, ed, newWeight);
}

int
Topology::GetEdgeWeight (Ptr<Node> n1, Ptr<Node> n2)
{
std::pair<Edge, bool> pair = boost::edge (m_vertexes[n1], m_vertexes[n2], m_graph);

if (pair.second)
return Topology::GetEdgeWeight (pair.first);
return -1;
}

int
Topology::GetEdgeWeight (Edge e)
Ptr<Node>
Topology::VertexToNode (Vertex vd)
{
return get (edge_weight_t (), m_graph, e);
return NodeList::GetNode (vd);
}

Ptr<Channel>
Topology::GetChannel (Ptr<Node> n1, Ptr<Node> n2)
{
std::pair<Edge, bool> pair = boost::edge (m_vertexes[n1], m_vertexes[n2], m_graph);
Edge e (n1->GetId (), n2->GetId ());

if (pair.second)
return Topology::GetChannel (pair.first);
return NULL;
return Topology::GetChannel (e);
}

Ptr<Channel>
Topology::GetChannel (Edge e)
{
return m_channels[e];

auto iter = m_channels.find (e);
if (iter == m_channels.end ())
return NULL;

return (*iter).second;
}

} // namespace ns3
Loading