root / src / messages / RestoreMessage.java @ 1
History | View | Annotate | Download (1.91 KB)
1 | 1 | up20150524 | package messages; |
---|---|---|---|
2 | |||
3 | import chord.ChordManager; |
||
4 | import chord.ConnectionInfo; |
||
5 | import files.FileHandler; |
||
6 | import peer.Peer; |
||
7 | |||
8 | import java.io.IOException; |
||
9 | import java.math.BigInteger; |
||
10 | import java.util.concurrent.ExecutionException; |
||
11 | |||
12 | public class RestoreMessage extends Message { |
||
13 | private ConnectionInfo ci;
|
||
14 | private BigInteger hashFile; |
||
15 | private String filename; |
||
16 | private String ipAddress; |
||
17 | private int port; |
||
18 | |||
19 | public RestoreMessage(ConnectionInfo ci, BigInteger hashFile, String filename,String ipAddress,int port) { |
||
20 | this.ci = ci;
|
||
21 | this.hashFile = hashFile;
|
||
22 | this.filename = filename;
|
||
23 | this.ipAddress = ipAddress;
|
||
24 | this.port = port;
|
||
25 | } |
||
26 | |||
27 | @Override
|
||
28 | public String toString() { |
||
29 | return "RESTORE " + this.ci + " " + this.hashFile + " " + this.filename; |
||
30 | } |
||
31 | |||
32 | public void handleMessage() { |
||
33 | |||
34 | if(FileHandler.checkFileExists("./peerDisk/peer" + Peer.getPeerAccessPoint() + "-" + ChordManager.peerHash + "/backup/" + hashFile)) { |
||
35 | try {
|
||
36 | byte[] content = FileHandler.readFromFile("./peerDisk/peer" + Peer.getPeerAccessPoint() + "-" + ChordManager.peerHash + "/backup/" + hashFile); |
||
37 | |||
38 | MessageForwarder.sendMessage(new RestoreFile(ci, hashFile, filename, content, ci.getIp(), ci.getPort()));
|
||
39 | } catch (IOException e) { |
||
40 | e.printStackTrace(); |
||
41 | } catch (ExecutionException e) { |
||
42 | e.printStackTrace(); |
||
43 | } catch (InterruptedException e) { |
||
44 | e.printStackTrace(); |
||
45 | } |
||
46 | } else {
|
||
47 | MessageForwarder.sendMessage(new RestoreSucessor(ci, ChordManager.peerHash , hashFile, filename, ChordManager.getFingerTable().get(0).getIp(), ChordManager.getFingerTable().get(0).getPort())); |
||
48 | } |
||
49 | } |
||
50 | @Override
|
||
51 | public String getIpAddress() { |
||
52 | return this.ipAddress; |
||
53 | } |
||
54 | |||
55 | @Override
|
||
56 | public int getPort() { |
||
57 | return this.port; |
||
58 | } |
||
59 | } |