-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainEngine.java
More file actions
91 lines (74 loc) · 3.37 KB
/
MainEngine.java
File metadata and controls
91 lines (74 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.*;
public class MainEngine {
public static void main(String[] args) {
System.out.println(" Starting Routing Simulator...\n");
// STEP 1: Load routers from config file
// List<Router> routers = Initializer.loadRouters("config.txt");
// Change this filename if you want to java MainEnginetest different configs!
String configFile = "config3.txt"; // Try: config.txt, config2.txt, config3.txt
List<Router> routers = Initializer.loadRouters(configFile);
if (routers.isEmpty()) {
System.out.println(" No routers loaded. Check your config.txt file!");
return;
}
// STEP 2: Run Builder and Listener (simulate routing protocol)
System.out.println(" Routers exchanging routing information...\n");
// Run multiple rounds to let routing tables converge
for (int round = 1; round <= 5; round++) {
System.out.println("--- Round " + round + " ---");
// Each router builds and sends packets
for (Router sender : routers) {
String packet = sender.builder();
// All other routers listen to this packet
for (Router receiver : routers) {
if (!receiver.getName().equals(sender.getName())) {
receiver.listener(packet, routers);
}
}
}
}
System.out.println("\n Routing tables converged!\n");
// Display all routing tables
for (Router router : routers) {
router.displayTable();
}
// STEP 3: Test change_cost
System.out.println("\n Testing CHANGE_COST...\n");
// Find R1 and change cost to its second neighbor
Router r1 = findRouter(routers, "R1");
if (r1 != null) {
// Get R1's neighbors
List<String> neighborNames = new ArrayList<>(r1.getNeighbors().keySet());
if (neighborNames.size() >= 2) {
String secondNeighbor = neighborNames.get(1);
System.out.println("Changing cost between R1 and " + secondNeighbor + " to 400\n");
r1.changeCost(secondNeighbor, 400);
// Re-run routing protocol after change
for (int round = 1; round <= 5; round++) {
for (Router sender : routers) {
String packet = sender.builder();
for (Router receiver : routers) {
if (!receiver.getName().equals(sender.getName())) {
receiver.listener(packet, routers);
}
}
}
}
System.out.println("\n Updated Routing Tables:\n");
for (Router router : routers) {
router.displayTable();
}
}
}
System.out.println(" Simulation complete!");
}
// Helper method to find a router by name
private static Router findRouter(List<Router> routers, String name) {
for (Router r : routers) {
if (r.getName().equals(name)) {
return r;
}
}
return null;
}
}