Project

General

Profile

Statistics
| Revision:

root / src / messages / BackupReadyMessage.java

History | View | Annotate | Download (2.69 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.net.InetAddress;
11
import java.security.NoSuchAlgorithmException;
12
import java.util.concurrent.ExecutionException;
13
14
public class BackupReadyMessage extends Message {
15
    private ConnectionInfo ci;
16
    private String filename;
17
    private int repDegree;
18
    private BigInteger hashFile;
19
    private String ipAddress;
20
    private int port;
21
22
    public BackupReadyMessage(ConnectionInfo ci, BigInteger hashFile, int repDegree, String filename,String ipAddress,int port) {
23
        this.hashFile = hashFile;
24
        this.repDegree = repDegree;
25
        this.ci = ci;
26
        this.filename = filename;
27
        this.ipAddress = ipAddress;
28
        this.port = port;
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 "BACKUP-READY" + this.hashFile + " " + " " + this.filename + " " + this.repDegree;
42
    }
43
44
    public void handleMessage() {
45
46
        try {
47
            byte[] content = FileHandler.readFromFile("./testFiles/" + filename);
48
49
            if ((this.ci.getPort() == Peer.port) && (this.ci.getIp().equals(InetAddress.getLocalHost().getHostAddress()))) {
50
51
                try {
52
                    FileHandler.writeFile("./peerDisk/peer" + Peer.getPeerAccessPoint() + "-" + ChordManager.peerHash + "/backup/" + hashFile, content);
53
                } catch (IOException e) {
54
                    e.printStackTrace();
55
                } catch (ExecutionException e) {
56
                    e.printStackTrace();
57
                } catch (InterruptedException e) {
58
                    e.printStackTrace();
59
                }
60
61
                if(repDegree - 1 > 0)
62
                    MessageForwarder.sendMessage(new BackupMessage(ci, hashFile, repDegree - 1, content, ChordManager.getFingerTable().get(0).getIp(), ChordManager.getFingerTable().get(0).getPort()));
63
                MessageForwarder.sendMessage(new BackupCompleteMessage(this.hashFile, this.repDegree, ci.getIp(), ci.getPort()));
64
            } else {
65
66
                MessageForwarder.sendMessage(new BackupMessage(new ConnectionInfo(ChordManager.peerHash, InetAddress.getLocalHost().getHostAddress(), Peer.port), hashFile, repDegree, content, ci.getIp(), ci.getPort()));
67
            }
68
        } catch(IOException e){
69
                e.printStackTrace();
70
            } catch(ExecutionException e){
71
                e.printStackTrace();
72
            } catch(InterruptedException e){
73
                e.printStackTrace();
74
            }
75
    }
76
}