-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseFile.java
More file actions
100 lines (86 loc) · 3.56 KB
/
Copy pathParseFile.java
File metadata and controls
100 lines (86 loc) · 3.56 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
import java.io.*;
import java.util.*;
public class ParseFile {
// Initializes the array list of ports by reading from the file config_peer.txt
public static void loadConfigPeerFile() {
try {
File configPeerFile = new File("config_peer.txt");
BufferedReader br = new BufferedReader(new FileReader(configPeerFile));
String portStr;
int id = 0;
while ((portStr = br.readLine()) != null)
TcpSocketController.availableTcpSockets.add(new TcpSocket(Integer.parseInt(portStr), id++));
}
catch (IOException e) {
System.out.println("Error reading from config_peer.txt");
}
}
/**
* The static method that updates the config_peer.txt file with the available sockets
* This will be called every time there is a modification to the list of available sockets
*/
public static void writeConfigPeerFile() {
try {
FileWriter configPeerWriter = new FileWriter("config_peer.txt", false);
for (TcpSocket socket : TcpSocketController.availableTcpSockets) {
configPeerWriter.write(socket.getLocalPort() + "\n");
}
configPeerWriter.flush();
configPeerWriter.close();
}
catch (IOException e) {
System.out.println("Error writing to config_peer.txt");
}
}
/**
* The static method that updates the config_neighbors.txt file with the connected sockets
* This will be called every time there is a modification to the list of connected sockets
*/
public static void writeConfigNeighborsFile() {
try {
FileWriter configNeighborsWriter = new FileWriter("config_neighbors.txt", false);
for (TcpSocket socket : TcpSocketController.connectedTcpSockets) {
configNeighborsWriter.write(socket.getConnectionSocket().getRemoteSocketAddress().toString().substring(1) + "\n");
}
configNeighborsWriter.flush();
configNeighborsWriter.close();
}
catch (IOException e) {
System.out.println("Error writing to config_neighbors.txt");
}
}
/**
* The static method that searches whether the indicated file exists
* @param searchedName the name of the file in the form of string
* @return true if the file is found in the shared folder
*/
public static boolean fileFound(String searchedName) {
final String filename = searchedName;
File sharedDir = new File("shared");
File[] searchResult = sharedDir.listFiles(new FilenameFilter() {
public boolean accept(File file, String name) {
return name.startsWith(filename);
}
});
return searchResult.length > 0;
}
/**
* The static method that returns the list of files with matched filenames
* @param searchedName the name of the file in the form of string
* @return the list of file names found in the shared folder
*/
public static ArrayList<String> getExactNames(String searchedName) {
final String filename = searchedName;
File sharedDir = new File("shared");
File[] searchResult = sharedDir.listFiles(new FilenameFilter() {
public boolean accept(File file, String name) {
return name.startsWith(filename);
}
});
ArrayList<String> matches = new ArrayList<String>();
for (File match : searchResult) {
matches.add(match.getName());
}
return matches;
}
}