root / src / protocols / Restore.java
History | View | Annotate | Download (2.29 KB)
1 | 1 | up20150524 | package protocols; |
---|---|---|---|
2 | |||
3 | import chord.ChordManager; |
||
4 | import chord.ConnectionInfo; |
||
5 | import files.FileHandler; |
||
6 | import messages.*; |
||
7 | import peer.Peer; |
||
8 | |||
9 | import java.io.IOException; |
||
10 | import java.math.BigInteger; |
||
11 | import java.net.InetAddress; |
||
12 | import java.net.UnknownHostException; |
||
13 | import java.security.NoSuchAlgorithmException; |
||
14 | import java.util.concurrent.ExecutionException; |
||
15 | |||
16 | public class Restore implements Runnable { |
||
17 | private String filename; |
||
18 | |||
19 | public Restore(String filename) { |
||
20 | this.filename = filename;
|
||
21 | } |
||
22 | |||
23 | @Override
|
||
24 | public void run() { |
||
25 | |||
26 | BigInteger hashFile = null; |
||
27 | try {
|
||
28 | String [] params = new String[] {filename, FileHandler.getFileSize(filename)}; |
||
29 | hashFile = ChordManager.encrypt(params); |
||
30 | } catch (Exception e) { |
||
31 | e.printStackTrace(); |
||
32 | } |
||
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 | FileHandler.writeFile("./peerDisk/peer" + Peer.getPeerAccessPoint() + "-" + ChordManager.peerHash + "/restored/" + filename, content); |
||
39 | } catch (IOException e) { |
||
40 | e.printStackTrace(); |
||
41 | } catch (ExecutionException e) { |
||
42 | e.printStackTrace(); |
||
43 | } catch (InterruptedException e) { |
||
44 | e.printStackTrace(); |
||
45 | } |
||
46 | |||
47 | } else {
|
||
48 | |||
49 | ConnectionInfo ci = null;
|
||
50 | try {
|
||
51 | ci = new ConnectionInfo(hashFile, InetAddress.getLocalHost().getHostAddress(), Peer.port); |
||
52 | } catch (UnknownHostException e) { |
||
53 | e.printStackTrace(); |
||
54 | } |
||
55 | |||
56 | Message res = ChordManager.searchSuccessor2(ci); |
||
57 | if (res instanceof SucessorMessage) { |
||
58 | MessageForwarder.sendMessage(new RestoreMessage(ci, hashFile, this.filename, ((SucessorMessage) res).getCi().getIp(), ((SucessorMessage) res).getCi().getPort())); |
||
59 | } else if (res instanceof LookupMessage) { |
||
60 | MessageForwarder.sendMessage(new RestoreInitMessage(ci, hashFile, this.filename, res.getIpAddress(), res.getPort())); |
||
61 | } |
||
62 | |||
63 | } |
||
64 | } |
||
65 | } |