root / DeleteTask.java @ 1
History | View | Annotate | Download (1.68 KB)
1 |
import java.io.File; |
---|---|
2 |
import java.io.IOException; |
3 |
import java.net.DatagramPacket; |
4 |
import java.net.DatagramSocket; |
5 |
import java.net.InetAddress; |
6 |
import java.security.NoSuchAlgorithmException; |
7 |
|
8 |
/**
|
9 |
* DeleteTask
|
10 |
*/
|
11 |
public class DeleteTask implements Runnable { |
12 |
private static final String CRLF = Integer.toHexString(0xD) + Integer.toHexString(0xA); |
13 |
|
14 |
private String filePath; |
15 |
private InetAddress MCaddress; |
16 |
private int MCport; |
17 |
|
18 |
private Peer peer;
|
19 |
private String version; |
20 |
|
21 |
DeleteTask(String pathname, String version, Peer peer) { |
22 |
this.filePath = pathname;
|
23 |
this.MCaddress = peer.getMCaddress();
|
24 |
this.MCport = peer.getMCport();
|
25 |
this.version = version;
|
26 |
this.peer = peer;
|
27 |
} |
28 |
|
29 |
@Override
|
30 |
public void run() { |
31 |
File file = new File(this.filePath); |
32 |
try {
|
33 |
String fileId = BackupFile.generateFileId(this.filePath, file.lastModified()); |
34 |
DeleteFile deleteFile = new DeleteFile(fileId);
|
35 |
this.peer.deleteMap.put(fileId, deleteFile);
|
36 |
DatagramSocket socket = new DatagramSocket(); |
37 |
|
38 |
//DELETE <Version> <SenderId> <FileId> <CRLF><CRLF>
|
39 |
String message = "DELETE " + this.version + " " + peer.getSenderId() + " " + fileId + " " + CRLF + CRLF; |
40 |
|
41 |
DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), this.MCaddress, this.MCport); |
42 |
this.peer.print("sending DELETE message"); |
43 |
socket.send(packet); |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
socket.close(); |
49 |
this.peer.print("DELETE protocol finished"); |
50 |
} catch (IOException | NoSuchAlgorithmException e) { |
51 |
e.printStackTrace(); |
52 |
} |
53 |
} |
54 |
} |