Project

General

Profile

Statistics
| Revision:

root / src / messages / RestoreSucessor.java

History | View | Annotate | Download (2.26 KB)

1
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.net.UnknownHostException;
11
import java.util.concurrent.ExecutionException;
12

    
13
public class RestoreSucessor extends Message {
14
    private ConnectionInfo ci;
15
    private BigInteger originalHash;
16
    private BigInteger hashFile;
17
    private String filename;
18
    private String ipAddress;
19
    private int port;
20

    
21
    RestoreSucessor(ConnectionInfo ci, BigInteger originalHash, BigInteger hashFile, String filename, String ipAddress, int port){
22
        this.ci = ci;
23
        this.originalHash = originalHash;
24
        this.hashFile = hashFile;
25
        this.filename = filename;
26
        this.ipAddress = ipAddress;
27
        this.port = port;
28
    }
29

    
30
    @Override
31
    public String toString() {
32
        return "RESTORE-SUCESSOR: " + this.ci + " " + this.hashFile + " " + this.filename;
33
    }
34

    
35
    @Override
36
    public void handleMessage() throws UnknownHostException {
37

    
38
        if(ChordManager.peerHash.compareTo(originalHash) == 0) {
39
            System.out.println("Restore failed, there is no such file in the system");
40
            return;
41
        }
42

    
43
        if(FileHandler.checkFileExists("./peerDisk/peer" + Peer.getPeerAccessPoint() + "-" + ChordManager.peerHash + "/backup/" + hashFile)) {
44
            try {
45
                byte[] content = FileHandler.readFromFile("./peerDisk/peer" + Peer.getPeerAccessPoint() + "-" + ChordManager.peerHash  + "/backup/" + hashFile);
46

    
47
                MessageForwarder.sendMessage(new RestoreFile(ci, hashFile, filename, content, ci.getIp(), ci.getPort()));
48
            } catch (IOException e) {
49
                e.printStackTrace();
50
            } catch (ExecutionException e) {
51
                e.printStackTrace();
52
            } catch (InterruptedException e) {
53
                e.printStackTrace();
54
            }
55
        } else {
56
            MessageForwarder.sendMessage(new RestoreSucessor(ci, originalHash, hashFile, filename, ChordManager.getFingerTable().get(0).getIp(), ChordManager.getFingerTable().get(0).getPort()));
57
        }
58
    }
59

    
60
    @Override
61
    public String getIpAddress() {
62
        return ipAddress;
63
    }
64

    
65
    @Override
66
    public int getPort() {
67
        return port;
68
    }
69
}