-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.java
More file actions
58 lines (50 loc) · 1.85 KB
/
Server.java
File metadata and controls
58 lines (50 loc) · 1.85 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
package com.yurii.salimov.lesson12.task01;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author Yuriy Salimov (yuriy.alex.salimov@gmail.com)
* @version 1.0
*/
public final class Server {
private final String filePath;
private final String reportPath;
public Server(final String filePath, final String reportPath) throws FileNotFoundException {
this.filePath = filePath;
this.reportPath = reportPath;
final File file = new File(this.filePath);
if (!file.exists() || !file.isFile()) {
throw new FileNotFoundException(filePath);
}
}
public void getStatusConnectionReport() throws InterruptedException, IOException {
final StringBuilder sb = new StringBuilder();
sb.append(new Date())
.append("\r\nServer - status:\r\n");
for (String address : getAddressList()) {
int status = getStatusServer(address);
sb.append(address)
.append(" - ")
.append((status == 0 ? "reachable\r\n" : "unreachable\r\n"));
}
Files.write(this.reportPath, sb);
}
private int getStatusServer(final String address) throws InterruptedException, IOException {
final Runtime runtime = Runtime.getRuntime();
final Process process = runtime.exec("ping " + address);
return process.waitFor();
}
public List<String> getAddressList() throws FileNotFoundException {
final String addresses = Files.read(this.filePath).replace("https://", "").replace("http://", "");
return Arrays.asList(addresses.split("\n"));
}
public String getFilePath() {
return this.filePath;
}
public String getReportPath() {
return reportPath;
}
}