-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializer.java
More file actions
74 lines (57 loc) · 2.71 KB
/
Copy pathInitializer.java
File metadata and controls
74 lines (57 loc) · 2.71 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
import java.io.*;
import java.util.*;
public class Initializer {
// Read the config file and create routers
public static List<Router> loadRouters(String filename) {
List<Router> routers = new ArrayList<>();
try {
// Open the file
BufferedReader reader = new BufferedReader(new FileReader(filename));
// Read first line: number of routers
String line = reader.readLine();
int numRouters = Integer.parseInt(line.trim());
System.out.println(" Loading " + numRouters + " routers...\n");
// Read each router line
for (int i = 0; i < numRouters; i++) {
line = reader.readLine();
if (line == null) break;
// Parse: "R1: (R2, 140), (R4, 180)"
String[] parts = line.split(":");
String routerName = parts[0].trim();
// Create the router
Router router = new Router(routerName);
// Parse neighbors: "(R2, 140), (R4, 180)"
if (parts.length > 1) {
String neighborsStr = parts[1].trim();
String[] neighborPairs = neighborsStr.split("\\),");
for (String pair : neighborPairs) {
// Remove parentheses and spaces
pair = pair.replace("(", "").replace(")", "").trim();
String[] neighborInfo = pair.split(",");
if (neighborInfo.length == 2) {
String neighborName = neighborInfo[0].trim();
int cost = Integer.parseInt(neighborInfo[1].trim());
router.addNeighbor(neighborName, cost);
}
}
}
routers.add(router);
System.out.println(" Created " + routerName);
}
reader.close();
// Give each router the list of ALL routers
List<String> allRouterNames = new ArrayList<>();
for (Router r : routers) {
allRouterNames.add(r.getName());
}
for (Router r : routers) {
r.setAllRouters(allRouterNames);
}
System.out.println("\n All routers loaded successfully!\n");
} catch (Exception e) {
System.out.println(" Error reading file: " + e.getMessage());
e.printStackTrace();
}
return routers;
}
}