public void Receive(NetworkNode node, RoutingTable receivedTable) { lock (ThreadLock) { foreach (RoutingState n_state in receivedTable.GetRoutingStates()) { if (n_state.NextNode.GetIP() == "0.0.0.0") //Other router does not know the way to the target (yet?) { continue; } List <RoutingState> cstates = Table.GetRoutingStates(); for (int i = 0; i < cstates.Count; i++) { RoutingState curState = cstates[i]; if (curState.DestinationNode == n_state.DestinationNode) { int targetMetric = n_state.Metric + 1; if (targetMetric < curState.Metric) { curState.Metric = targetMetric; curState.NextNode = node; cstates[i] = curState; } } } } } }
public void RemoveLink(string ip1, string ip2) { NetworkNode selected1 = FindNode(ip1); NetworkNode selected2 = FindNode(ip2); if (selected1 == null || selected2 == null) { Console.WriteLine("One or both routers are not found!"); return; } Edge sEdge = null; foreach (Edge e in selected1.GetEdges()) { if (e.GetLeft() == selected2 || e.GetRight() == selected2) { sEdge = e; } } if (sEdge == null) { Console.WriteLine("Link not found!"); return; } selected1.RemoveEdge(sEdge); selected2.RemoveEdge(sEdge); Console.WriteLine("Link removed!"); }
public void RemoveRouter(string ip) { NetworkNode selected = FindNode(ip); if (selected == null) { Console.WriteLine("Router not found!"); return; } NetworkNodes.Remove(selected); foreach (NetworkNode ns in NetworkNodes) { List <Edge> edges = ns.GetEdges(); List <Edge> toRemove = new List <Edge>(); foreach (Edge ee in edges) { if (ee.GetLeft() == selected || ee.GetRight() == selected) { toRemove.Add(ee); } } foreach (Edge ee in toRemove) { edges.Remove(ee); } } Console.WriteLine("Router {0} removed!", ip); }
private void Broadcast() { foreach (Edge e in Edges) { NetworkNode n = e.GetLeft() == this ? e.GetRight() : e.GetLeft(); //Get neighboring node and not ourselves n.Receive(this, Table); } }
public void PrintTable(string ip) { NetworkNode selected = FindNode(ip); if (selected == null) { Console.WriteLine("Router not found!"); return; } selected.PrintTable(); }
public void AddRouter(string ip) { NetworkNode selected = FindNode(ip); if (selected != null) { Console.WriteLine("Router already exists!"); return; } NetworkNodes.Add(new NetworkNode(ip, this)); Console.WriteLine("Router {0} addded!", ip); }
private NetworkNode FindNode(string ip) { NetworkNode selected = null; foreach (NetworkNode ns in NetworkNodes) { if (ns.GetIP() == ip) { selected = ns; break; } } return(selected); }
private void InitStates() { States.Clear(); foreach (NetworkNode node in CurrentNetwork.GetNetworkNodes()) { if (node == ParentNode) { continue; } NetworkNode nextNode = new NetworkNode(); int targetMetric = MAX_HOPS + 1; foreach (Edge en in ParentNode.GetEdges()) { if (en.GetLeft() == node) { if (en.GetLeft().GetIP() == "0.0.0.0") { continue; } nextNode = en.GetLeft(); targetMetric = 1; break; } if (en.GetRight() == node) { if (en.GetRight().GetIP() == "0.0.0.0") { continue; } nextNode = en.GetRight(); targetMetric = 1; break; } } States.Add(new RoutingState() { NextNode = nextNode, DestinationNode = node, Metric = targetMetric }); } }
public void PrintNodes() { foreach (NetworkNode n in NetworkNodes) { Console.WriteLine("Router: {0}", n.GetIP()); Console.WriteLine("Connections:"); foreach (Edge e in n.GetEdges()) { NetworkNode en = e.GetLeft() == n?e.GetRight() : e.GetLeft(); Console.WriteLine("{0} -- {1}", n.GetIP(), en.GetIP()); } Console.WriteLine(); } }
public void AddLink(string ip1, string ip2) { NetworkNode selected1 = FindNode(ip1); NetworkNode selected2 = FindNode(ip2); if (selected1 == null || selected2 == null) { Console.WriteLine("One or both routers are not found!"); return; } Edge e = new Edge(selected1, selected2); selected1.AddEdge(e); selected2.AddEdge(e); Console.WriteLine("Link added!"); }
public void Generate(int Networks) { if (Networks <= 1) { throw new ArgumentException("Cannot create a network with less than two networks!"); } NetworkNodes.Clear(); //Generate network point nodes List <string> uniqueIPs = GenerateUniqueIPs(Networks); Console.WriteLine("Generated IP Addresses: "); foreach (string ip in uniqueIPs) { Console.WriteLine(ip); NetworkNodes.Add(new NetworkNode(ip, this)); } Console.WriteLine("Generated connections: "); //Generate a randomly connected network graph for (int i = 0; i < Networks; i++) { //Generate a random network to target int r = RandomGenerator.Next(Networks); while (r == i) { r = RandomGenerator.Next(Networks); } NetworkNode currentNetwork = NetworkNodes[i]; NetworkNode targetNetwork = NetworkNodes[r]; Edge e = new Edge(currentNetwork, targetNetwork); currentNetwork.AddEdge(e); targetNetwork.AddEdge(e); Console.WriteLine("{0} -- {1}", currentNetwork.GetIP(), targetNetwork.GetIP()); } }
public void SetLeft(NetworkNode e) { Left = e; }
public void SetRight(NetworkNode e) { Right = e; }
public Edge(NetworkNode Left, NetworkNode Right) { this.Left = Left; this.Right = Right; }
public RoutingTable(NetworkNode node, Network net) { CurrentNetwork = net; ParentNode = node; InitStates(); }