-
Notifications
You must be signed in to change notification settings - Fork 1
/
Peer.java
109 lines (89 loc) · 2.63 KB
/
Peer.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
import java.util.HashMap;
import java.nio.file.*;
import java.io.IOException;
import java.util.stream.Stream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Map;
import java.io.Serializable;
import java.net.*;
import java.io.*;
public class Peer implements Serializable {
private static final long serialVersionUID = 1L;
public String name; // client name
public String address; // client address
public HashMap<String, String> files; // array for all hash and path files
public Peer(String[] args) {
name = args[1];
address = args[2];
files = new HashMap<String, String>();
saveAllFilesPath(args[3]); // "./share"
}
public void requestFile(String hashFile, Peer host) throws IOException {
String pathFile = host.pathByHash(hashFile);
Socket socket = new Socket(host.getAddress(), 4444);
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
// escreve 1, para dizer que é uma requisição de arquivo
dOut.writeByte(1);
// escreve o caminho do arquivo que quero
dOut.writeUTF(pathFile);
dOut.flush(); // Send off the data
// encerra
dOut.writeByte(-1);
dOut.flush(); // Send off the data
dOut.close();
}
public void saveAllFilesPath(String rootPath) {
System.out.println("Reading your files to share...");
try (Stream<Path> paths = Files.walk(Paths.get(rootPath))) {
paths.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
try {
files.put(getHashFile(filePath.toString()), filePath.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
System.out.println("Files ready");
} catch (IOException e) {
e.printStackTrace();
}
}
public String getHashFile(String path) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(Files.readAllBytes(Paths.get(path)));
String checksum = new BigInteger(1, md.digest()).toString(16);
return checksum;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean haveFileByHash(String hash) {
for (String fileHash : files.keySet()) {
if (fileHash.equals(hash)) {
return true;
}
}
return false;
}
public String pathByHash(String hash) {
for (String fileHash : files.keySet()) {
if (fileHash.equals(hash)) {
return files.get(fileHash);
}
}
return null;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public Map<String, String> getFiles() {
return files;
}
}