Project

General

Profile

Statistics
| Revision:

root / src / protocols / Backup.java @ 1

History | View | Annotate | Download (1.97 KB)

1
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.File;
10
import java.io.IOException;
11
import java.math.BigInteger;
12
import java.net.InetAddress;
13
import java.net.UnknownHostException;
14
import java.security.NoSuchAlgorithmException;
15
import java.util.concurrent.ExecutionException;
16

    
17
public class Backup implements Runnable{
18
    private String filename;
19
    private int repDegree;
20

    
21
    public Backup(String filename, int repDegree) {
22
        this.filename = filename;
23
        this.repDegree = repDegree;
24
    }
25

    
26
    @Override
27
    public void run() {
28

    
29
        BigInteger hashFile = null;
30
        try {
31
            String [] params = new String[] {filename, FileHandler.getFileSize(filename)};
32
            hashFile = ChordManager.encrypt(params);
33
        } catch (Exception e) {
34
            e.printStackTrace();
35
        }
36

    
37
        ConnectionInfo ci = null;
38
        try {
39
            ci = new ConnectionInfo(hashFile, InetAddress.getLocalHost().getHostAddress(), Peer.port);
40
        } catch (UnknownHostException e) {
41
            e.printStackTrace();
42
        }
43

    
44
        Message res = ChordManager.searchSuccessor2(ci);
45

    
46
        if(res instanceof SucessorMessage) {
47
            try {
48
                byte[] content = FileHandler.readFromFile("./testFiles/" + filename);
49
                MessageForwarder.sendMessage(new BackupMessage(ci, hashFile, repDegree, content, ((SucessorMessage) res).getCi().getIp(), ((SucessorMessage) res).getCi().getPort()));
50
            } catch (IOException e) {
51
                e.printStackTrace();
52
            } catch (ExecutionException e) {
53
                e.printStackTrace();
54
            } catch (InterruptedException e) {
55
                e.printStackTrace();
56
            }
57

    
58
        } else if(res instanceof LookupMessage){
59
            MessageForwarder.sendMessage(new BackupInitMessage(ci, hashFile, this.repDegree, this.filename, res.getIpAddress(), res.getPort()));
60
        }
61

    
62
    }
63
}