-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFXMLController.java
More file actions
144 lines (126 loc) · 4.76 KB
/
MainFXMLController.java
File metadata and controls
144 lines (126 loc) · 4.76 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
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
139
140
141
142
143
144
package webcrawler;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.function.BiConsumer;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class MainFXMLController extends Application implements Initializable {
@FXML
private TextField threadCount;
@FXML
private Button startButton;
@FXML
private TextField startUrl;
@FXML
private TextArea webOutput;
private final ArrayList<Runner> runners = new ArrayList<>();
private final ArrayList<Thread> threads = new ArrayList<>();
@FXML
private Label numberOfLinksVisitedText;
@FXML
private Label numberOfUniqueWebsitesText;
@FXML
private Label numberOfWebsitesSkippedText;
@FXML
private TextField skipWebNumber;
@FXML
private Label linksPerSecond;
private boolean isRunning = false;
private long startTime;
@Override
public void initialize(URL url, ResourceBundle rb) {
numberOfLinksVisitedText.setText("Number of Links Visited: 0");
numberOfUniqueWebsitesText.setText("Number of Unique Websites: 0");
numberOfWebsitesSkippedText.setText("Number of Websites Skipped: 0");
linksPerSecond.setText("Links per second: 0");
Runner.HOSTS.addConsumer((String t, Integer u) -> {
Platform.runLater(() -> {
if (u <= 1) {
String output = (t + "\n");
webOutput.appendText(output);
}
});
});
Runner.getNumberOfLinksVisited().addListener(() -> {
numberOfLinksVisitedText.setText("Number of "
+ "links visited: " + Runner.getNumberOfLinksVisited());
updateLinksPerSecond(Runner.getNumberOfLinksVisited().get());
});
Runner.getNumberOfUniqueWebsites().addListener(() -> {
numberOfUniqueWebsitesText.setText("Number of "
+ "unique websites visited: " + Runner.getNumberOfUniqueWebsites());
});
Runner.getNumberOfSkippedLinks().addListener(() -> {
numberOfWebsitesSkippedText.setText("Number of "
+ "links skipped: " + Runner.getNumberOfSkippedLinks());
});
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainFXML.fxml"));
Scene scene = new Scene(loader.load());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
@FXML
private void onStartPressed(ActionEvent event) {
if (!isRunning) {
isRunning = true;
if (startTime == 0) {
startTime = System.currentTimeMillis();
}
String startUrlString = startUrl.getText();
int numberOfThreads = Integer.parseInt(threadCount.getText());
int cutoff = Integer.parseInt(skipWebNumber.getText());
Runner.setCutoffNumber(cutoff);
startButton.setText("Stop");
startThreads(startUrlString, numberOfThreads);
} else {
isRunning = false;
stopThreads();
startButton.setText("Start");
}
}
public void stopThreads() {
for (Runner runner : runners) {
runner.setRunning(false);
}
}
public void updateLinksPerSecond(long distance) {
final DecimalFormat formatter = new DecimalFormat("#.##");
long time = System.currentTimeMillis() - startTime;
double rate = ((double) distance) / (time / 1000.);
linksPerSecond.setText("Links per second: " + formatter.format(rate));
}
public void startThreads(String startUrl, int numThreads) {
runners.clear();
threads.clear();
runners.add(new Runner(new Webpage(startUrl)));
for (int i = 1; i < numThreads; i++) {
runners.add(new Runner());
}
for (int i = 0; i < runners.size(); i++) {
Runner runner = runners.get(i);
Thread thread = new Thread(runner);
threads.add(thread);
thread.setDaemon(true);
thread.setName("Crawler thread " + Integer.toString(i));
thread.start();
}
}
}