root / BackupTask.java @ 1
History | View | Annotate | Download (3.75 KB)
1 |
import java.io.File; |
---|---|
2 |
import java.io.FileInputStream; |
3 |
import java.io.IOException; |
4 |
import java.net.DatagramPacket; |
5 |
import java.net.DatagramSocket; |
6 |
import java.net.InetAddress; |
7 |
import java.security.NoSuchAlgorithmException; |
8 |
import java.util.ArrayList; |
9 |
import java.util.Arrays; |
10 |
|
11 |
/**
|
12 |
* BackupTask
|
13 |
*/
|
14 |
public class BackupTask implements Runnable { |
15 |
private static final int MAX_CHUNK_SIZE = 64000; // 64KBytes |
16 |
private static final String CRLF = Integer.toHexString(0xD) + Integer.toHexString(0xA); |
17 |
|
18 |
private String filePath; |
19 |
private String version; |
20 |
private int replicationDeg; |
21 |
private InetAddress MDBaddress; |
22 |
private int MDBport; |
23 |
|
24 |
private Peer peer;
|
25 |
|
26 |
BackupTask(String pathname, int replicationDeg, String version, Peer peer) { |
27 |
this.filePath = pathname;
|
28 |
this.version = version;
|
29 |
this.replicationDeg = replicationDeg;
|
30 |
this.MDBaddress = peer.getMDBaddress();
|
31 |
this.MDBport = peer.getMDBport();
|
32 |
this.peer = peer;
|
33 |
} |
34 |
|
35 |
@Override
|
36 |
public void run() { |
37 |
File file = new File(this.filePath); |
38 |
try {
|
39 |
BackupFile backupFile; |
40 |
FileInputStream stream = new FileInputStream(file); |
41 |
byte[] data = new byte[(int) file.length()]; |
42 |
stream.read(data); |
43 |
stream.close(); |
44 |
|
45 |
ArrayList<Chunk> chunks = new ArrayList<>(); |
46 |
String fileId = BackupFile.generateFileId(this.filePath, file.lastModified()); |
47 |
DatagramSocket socket = new DatagramSocket(); |
48 |
long interval;
|
49 |
|
50 |
backupFile = new BackupFile(this.filePath, this.replicationDeg); |
51 |
|
52 |
int i;
|
53 |
for (i = 0; i < data.length / MAX_CHUNK_SIZE; i++) { |
54 |
chunks.add(new Chunk(backupFile, i, Arrays.copyOfRange(data, i * MAX_CHUNK_SIZE, (i + 1) * MAX_CHUNK_SIZE))); |
55 |
backupFile.addChunk(i, new Chunk(backupFile, i, Arrays.copyOfRange(data, i * MAX_CHUNK_SIZE, (i + 1) * MAX_CHUNK_SIZE))); |
56 |
} |
57 |
chunks.add(new Chunk(backupFile, i, Arrays.copyOfRange(data, i * MAX_CHUNK_SIZE, data.length))); |
58 |
backupFile.addChunk(i, new Chunk(backupFile, i, Arrays.copyOfRange(data, i * MAX_CHUNK_SIZE, data.length))); |
59 |
this.peer.addToFileMap(backupFile.getPathname(), backupFile);
|
60 |
|
61 |
for (i = 0; i < chunks.size(); i++) { |
62 |
interval = 1000;
|
63 |
byte[] chunk = chunks.get(i).getData(); |
64 |
String message = "PUTCHUNK " + version + " " + peer.getSenderId() + " " + fileId + " " + i + " " |
65 |
+ this.replicationDeg + " " + CRLF + CRLF; |
66 |
|
67 |
byte[] packetData = new byte[message.getBytes().length + chunk.length]; |
68 |
System.arraycopy(message.getBytes(), 0, packetData, 0, message.getBytes().length); |
69 |
System.arraycopy(chunk, 0, packetData, message.getBytes().length, chunk.length); |
70 |
|
71 |
DatagramPacket packet = new DatagramPacket(packetData, packetData.length, MDBaddress, MDBport); |
72 |
|
73 |
while (interval <= 31000) { //31 = 1+2+4+8+16 |
74 |
this.peer.print("sending PUTCHUNK message"); |
75 |
socket.send(packet); |
76 |
Thread.sleep(interval);
|
77 |
if (this.peer.repDegMap.containsKey(fileId) && this.peer.repDegMap.get(fileId).get(i) >= this.replicationDeg) |
78 |
break;
|
79 |
interval *= 2;
|
80 |
} |
81 |
if(interval > 31000) { |
82 |
this.peer.print("Couldn't achieve the desired replication degree."); |
83 |
this.peer.fileMap.remove(this.filePath); |
84 |
return;
|
85 |
} |
86 |
} |
87 |
socket.close(); |
88 |
this.peer.print("BACKUP protocol finished"); |
89 |
|
90 |
} catch (IOException | NoSuchAlgorithmException | InterruptedException e) { |
91 |
e.printStackTrace(); |
92 |
} |
93 |
} |
94 |
} |