-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeer.java
408 lines (316 loc) · 14.2 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.Thread;
import Mensagem.*;
@SuppressWarnings({ "resource", "unused" }) // removes never closed and not used warnings
public class Peer {
public static void main(String args[]) throws Exception {
// default server IP and port
InetAddress serverAddress = InetAddress.getByName("127.0.0.1");
int serverPort = 10098;
Scanner input = new Scanner(System.in);
// peer address
System.out.print("Entre com o endereco IP deste peer: ");
InetAddress peerAddress = InetAddress.getByName(input.nextLine());
// peer port
System.out.print("Entre com a porta deste peer: ");
int peerPort = Integer.parseInt(input.nextLine());
// peer folder
System.out.print("Entre com o endereco da pasta: ");
String folderPath = input.nextLine();
File[] listFiles = new File(folderPath).listFiles();
while (listFiles == null) {
System.out.print("Endereco invalido, insira novamente: ");
folderPath = input.nextLine();
listFiles = new File(folderPath).listFiles();
}
// peer file names
ArrayList<String> fileNames = new ArrayList<String>();
for (File f : listFiles) {
if (f.isFile())
fileNames.add(f.getName());
}
// desired file
String filetoDownload = "";
ArrayList<String> peerswithFile = new ArrayList<String>();
DatagramSocket clientSocket = new DatagramSocket(peerPort);
// initialize a thread for peer download requests
PeerRequests pr = new PeerRequests(peerPort, folderPath);
pr.start();
// initialize a thread for server messages
ServerHandler server = new ServerHandler(clientSocket);
server.start();
// options menu
Scanner in = new Scanner(System.in);
while (true) {
System.out.print("MENU:\n[1] JOIN\n[2] SEARCH\n[3] DOWNLOAD\n[4] LEAVE\nEntre com a opcao: ");
try {
int option = in.nextInt();
if (option == 1) { // JOIN
// send request and list of files
Mensagem requisicao = new Mensagem();
requisicao.setMessage("JOIN");
requisicao.setList(fileNames);
// waits for server response
Mensagem resposta = waitResponse("JOIN_OK", server, requisicao, clientSocket, serverAddress,
serverPort); // blocking
System.out.print("Sou peer " + resposta.getAddress().toString().replace("/", "") + ":"
+ resposta.getPort() + " com arquivos");
printList(fileNames);
} else if (option == 2) { // SEARCH
// send request and name of file to search
Mensagem requisicao = new Mensagem();
requisicao.setMessage("SEARCH");
ArrayList<String> l = new ArrayList<String>();
System.out.print("Entre com o nome do arquivo desejado: ");
filetoDownload = input.nextLine();
l.add(filetoDownload);
requisicao.setList(l);
// waits for server response
Mensagem resposta = waitResponse("SEARCH_OK", server, requisicao, clientSocket, serverAddress,
serverPort); // blocking
peerswithFile = resposta.getList();
System.out.print("Peers com arquivo solicitado:");
printList(resposta.getList());
} else if (option == 3) { // DOWNLOAD
// download available only after search and if exists peers with file
if (!filetoDownload.isEmpty() && !peerswithFile.isEmpty()) {
Boolean received = false;
// try to download file from all peers
// ask again if all peers deny
while (!received) {
for (int i = 0; i < peerswithFile.size(); i++) {
String ip = peerswithFile.get(i).split(":")[0];
int port = Integer.parseInt(peerswithFile.get(i).split(":")[1]);
// tcp connection
Socket s = new Socket(ip, port);
OutputStream os = s.getOutputStream();
DataOutputStream writer = new DataOutputStream(os);
// send name of the desired file
writer.writeBytes(filetoDownload + "\n");
DataInputStream is = new DataInputStream(s.getInputStream());
FileOutputStream fos = new FileOutputStream(
new File(folderPath, filetoDownload).getAbsolutePath());
byte[] buffer = new byte[1024];
// receives data until EOF
int read;
while ((read = is.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
is.close();
// read header to check if download was denied
byte[] b = new byte[15];
InputStream fis = new FileInputStream(
new File(folderPath, filetoDownload).getAbsolutePath());
fis.read(b);
if (!new String(b).equals("DOWNLOAD_NEGADO")) {
received = true;
fileNames.add(filetoDownload);
System.out.println("Arquivo " + filetoDownload + " baixado com sucesso na pasta "
+ folderPath);
break;
} else {
// delete temporary file with DOWNLOAD_NEGADO message
new File(folderPath, filetoDownload).delete();
int index = i == peerswithFile.size() - 1 ? 0 : i + 1;
System.out.println(
"Peer " + ip + ":" + port + " negou o download, pedindo agora para o peer "
+ peerswithFile.get(index));
}
fis.close();
}
// waits 10 seconds to start requesting again
if (!received)
Thread.sleep(10000);
}
// send request to update
Mensagem requisicao = new Mensagem();
ArrayList<String> l = new ArrayList<String>();
l.add(filetoDownload);
requisicao.setMessage("UPDATE");
requisicao.setList(l);
// waits for server response
Mensagem resposta = waitResponse("UPDATE_OK", server, requisicao, clientSocket, serverAddress,
serverPort); // blocking
}
} else if (option == 4) { // LEAVE
// send request to leave
Mensagem requisicao = new Mensagem();
requisicao.setMessage("LEAVE");
// waits for server response
Mensagem resposta = waitResponse("LEAVE_OK", server, requisicao, clientSocket, serverAddress,
serverPort); // blocking
// nothing to print
} else {
System.out.println("Opcao invalida!");
}
} catch (Exception e) {
System.out.println("Opcao invalida!");
}
}
}
// serialize and send Mensagem object
public static void send(Mensagem m, DatagramSocket s, InetAddress a, int p) {
try {
// serialize Mensagem object
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(byteStream));
objectOut.flush();
objectOut.writeObject(m);
objectOut.flush();
byte[] sendData = byteStream.toByteArray();
// send packet
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, a, p);
s.send(sendPacket);
objectOut.close();
} catch (Exception e) {
}
}
// waits for server response expected
// send new requests in timeout
private static Mensagem waitResponse(String r, ServerHandler sh, Mensagem m, DatagramSocket s, InetAddress a,
int p) {
int timeout = 15; // time in seconds to send a new request
// first request
send(m, s, a, p);
// periodically check response and sends new request on timeout
int t = 0;
while (!sh.getResposta().getMessage().equals(r)) {
try {
Thread.sleep(100);
t++;
} catch (Exception e) {
}
if (t == timeout * 10) {
send(m, s, a, p);
t = 0;
}
}
Mensagem resposta = sh.getResposta();
sh.setResposta(new Mensagem());
return resposta;
}
private static void printList(ArrayList<String> lista) {
for (String item : lista) {
System.out.print(" " + item);
}
System.out.print("\n");
}
}
class ServerHandler extends Thread {
private Mensagem resposta;
private DatagramSocket clientSocket;
public ServerHandler(DatagramSocket socket) {
this.resposta = new Mensagem();
this.clientSocket = socket;
}
public void run() {
try {
// waits server contact
while (true) {
byte[] recBuffer = new byte[1024];
DatagramPacket recPacket = new DatagramPacket(recBuffer, recBuffer.length);
clientSocket.receive(recPacket); // blocking
// deserialize Mensagem object
ByteArrayInputStream byteStream = new ByteArrayInputStream(recBuffer);
ObjectInputStream objectIn = new ObjectInputStream(new BufferedInputStream(byteStream));
Mensagem resposta = (Mensagem) objectIn.readObject();
if (resposta.getMessage().equals("ALIVE")) {
AliveHandler answer = new AliveHandler(this.clientSocket, recPacket.getAddress(),
recPacket.getPort());
answer.start();
} else {
setResposta(resposta);
}
}
} catch (Exception e) {
}
}
public void setResposta(Mensagem r) {
this.resposta = r;
}
public Mensagem getResposta() {
return this.resposta;
}
}
class AliveHandler extends Thread {
private DatagramSocket clientSocket;
private InetAddress serverAddress;
private int serverPort;
public AliveHandler(DatagramSocket socket, InetAddress address, int port) {
this.clientSocket = socket;
this.serverAddress = address;
this.serverPort = port;
}
public void run() {
try {
Mensagem resposta = new Mensagem();
resposta.setMessage("ALIVE_OK");
// send message with alive confirmation
Peer.send(resposta, this.clientSocket, this.serverAddress, this.serverPort);
} catch (Exception e) {
}
}
}
@SuppressWarnings("resource") // removes never closed warnings
class PeerRequests extends Thread {
private int port;
private String folderPath;
public PeerRequests(int p, String f) {
this.port = p;
this.folderPath = f;
}
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(this.port);
while (true) {
try {
Socket no = serverSocket.accept(); // blocking
// start a thread to handle the new peer request
PeerHandler peer = new PeerHandler(no, this.folderPath);
peer.start();
} catch (Exception e) {
}
}
} catch (Exception e) {
}
}
}
class PeerHandler extends Thread {
private Socket no;
private String folderPath;
public PeerHandler(Socket no, String f) {
this.no = no;
this.folderPath = f;
}
public void run() {
try {
InputStreamReader is = new InputStreamReader(this.no.getInputStream());
BufferedReader reader = new BufferedReader(is);
// receive the name of the file to send
String filetoSend = reader.readLine();
DataOutputStream os = new DataOutputStream(this.no.getOutputStream());
byte[] buffer = new byte[1024];
// randomly choose to send or not the file
Random random = new Random();
// if random is true and this peer has the file on the folder then send
if (random.nextBoolean() && new File(folderPath, filetoSend).isFile()) {
FileInputStream fis = new FileInputStream(new File(folderPath, filetoSend).getAbsolutePath());
// send file
int read = 0;
while ((read = fis.read(buffer)) > 0) {
os.write(buffer, 0, read);
}
fis.close();
} else { // send string DOWNLOAD_NEGADO
os.writeBytes("DOWNLOAD_NEGADO\n");
}
os.flush();
os.close();
this.no.close();
} catch (Exception e) {
}
}
}