root / src / messages / ReclaimBackup.java
History | View | Annotate | Download (2.21 KB)
1 |
package messages; |
---|---|
2 |
|
3 |
import chord.ChordManager; |
4 |
import chord.ConnectionInfo; |
5 |
import files.FileHandler; |
6 |
import peer.Peer; |
7 |
import protocols.Backup; |
8 |
|
9 |
import java.io.IOException; |
10 |
import java.math.BigInteger; |
11 |
import java.util.concurrent.ExecutionException; |
12 |
|
13 |
public class ReclaimBackup extends Message { |
14 |
private int repDegree; |
15 |
private BigInteger hashFile; |
16 |
private ConnectionInfo ci;
|
17 |
private byte[] body; |
18 |
private String ipAddress; |
19 |
private int port; |
20 |
|
21 |
public ReclaimBackup(ConnectionInfo ci, BigInteger hashFile, int repDegree, byte[] body,String ipAddress,int port) { |
22 |
this.ci = ci;
|
23 |
this.hashFile = hashFile;
|
24 |
this.repDegree = repDegree;
|
25 |
this.body = body;
|
26 |
this.ipAddress = ipAddress;
|
27 |
this.port = port;
|
28 |
} |
29 |
|
30 |
@Override
|
31 |
public String getIpAddress() { |
32 |
return this.ipAddress; |
33 |
} |
34 |
|
35 |
@Override
|
36 |
public int getPort() { |
37 |
return this.port; |
38 |
} |
39 |
@Override
|
40 |
public String toString() { |
41 |
return "RECLAIM_BACKUP " + this.hashFile + " " + this.repDegree; |
42 |
} |
43 |
|
44 |
public void handleMessage() { |
45 |
if (Peer.storage.getSpaceOcupied() + body.length > Peer.storage.getSpaceReserved()) {
|
46 |
System.out.println("Not enough space to save file"); |
47 |
MessageForwarder.sendMessage(new ReclaimBackup(ci, hashFile, repDegree, body, ChordManager.getFingerTable().get(0).getIp(), ChordManager.getFingerTable().get(0).getPort())); |
48 |
} else {
|
49 |
if(FileHandler.checkFileExists("./peerDisk/peer" + Peer.getPeerAccessPoint() + "-" + ChordManager.peerHash + "/backup/" + hashFile)){ |
50 |
MessageForwarder.sendMessage(new ReclaimBackup(ci, hashFile, repDegree, body, ChordManager.getFingerTable().get(0).getIp(), ChordManager.getFingerTable().get(0).getPort())); |
51 |
} else {
|
52 |
|
53 |
try {
|
54 |
FileHandler.writeFile("./peerDisk/peer" + Peer.getPeerAccessPoint() + "-" + ChordManager.peerHash + "/backup/" + hashFile, body); |
55 |
} catch (IOException e) { |
56 |
e.printStackTrace(); |
57 |
} catch (ExecutionException e) { |
58 |
e.printStackTrace(); |
59 |
} catch (InterruptedException e) { |
60 |
e.printStackTrace(); |
61 |
} |
62 |
} |
63 |
} |
64 |
} |
65 |
} |