-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.java
138 lines (108 loc) · 4.46 KB
/
Controller.java
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class Controller {
private static final String ipDirectory = "inputs/", opDirectory = "outputs/";
private int duration;
private File topologyFile;
private HashMap<Integer, ArrayList<Integer>> topology;
Controller(int duration) {
this.duration = duration;
topologyFile = new File("topology.txt");
topology = new HashMap<Integer, ArrayList<Integer>>(10);
}
// main method which builds the topology of the network
private void buildTopology() {
try {
BufferedReader tReader = new BufferedReader(new FileReader(topologyFile));
String line = null;
while ((line = tReader.readLine()) != null) {
String[] lineSplit = line.split(" ");
int src = Integer.parseInt(lineSplit[0]);
int dest = Integer.parseInt(lineSplit[1]);
if (!topology.containsKey(src)) {
topology.put(src, new ArrayList<Integer>());
}
ArrayList<Integer> l = topology.get(src);
l.add(dest);
topology.put(src, l);
}
tReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// class for files of each node
class NodeFile {
File file;
int fileNo;
long lastLine;
NodeFile(int fileNo, File file) {
this.fileNo = fileNo;
this.file = file;
lastLine = 0;
}
// method which read output file and writes to input file
void readAndProcess() {
try {
if (lastLine < file.length()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
reader.skip(lastLine);
String line = null;
while ((line = reader.readLine()) != null) {
for (Integer node : topology.get(fileNo)) {
String ipPath = ipDirectory+"input_"+node+".txt";
File input = new File(ipPath);
if (input.exists()) {
BufferedWriter writer = new BufferedWriter(new FileWriter(input, true));
writer.write(line);
writer.write(System.lineSeparator());
writer.close();
}
}
}
lastLine = file.length();
reader.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void startController() {
// read the topology file and create the topology graph
buildTopology();
NodeFile[] nodeFiles = new NodeFile[10];
for (int f = 0; f < 10; f++) nodeFiles[f] = null;
long currTime = System.currentTimeMillis();
while (System.currentTimeMillis() < currTime+(1000*duration)) {
for (int id = 0; id < 10; id++) {
// check if node files exists, that means the node is running
String outputPath = opDirectory+"output_"+id+".txt";
if (new File(outputPath).exists()) {
// create file pointers to node files
if (nodeFiles[id] == null) {
File opFile = new File(outputPath);
nodeFiles[id] = new NodeFile(id, opFile);
}
if (nodeFiles[id] != null) nodeFiles[id].readAndProcess();
}
}
}
}
public static void main(String[] args) {
int time = Integer.parseInt(args[0]);
Controller controller = new Controller(time);
controller.startController();
}
}