Project

General

Profile

Statistics
| Revision:

root / RestoreTask.java @ 1

History | View | Annotate | Download (2.73 KB)

1
import java.io.File;
2
import java.io.IOException;
3
import java.net.DatagramPacket;
4
import java.net.DatagramSocket;
5
import java.net.InetAddress;
6
import java.security.NoSuchAlgorithmException;
7

    
8
/**
9
 * BackupTask
10
 */
11
public class RestoreTask implements Runnable {
12
    private static final int MAX_CHUNK_SIZE = 64000; // 64KBytes
13
    private static final String CRLF = Integer.toHexString(0xD) + Integer.toHexString(0xA);
14

    
15
    private String filePath;
16
    private InetAddress MCaddress;
17
    private int MCport;
18

    
19
    private Peer peer;
20
    private String version;
21

    
22
    RestoreTask(String pathname, String version, Peer peer) {
23
        this.filePath = pathname;
24
        this.MCaddress = peer.getMCaddress();
25
        this.MCport = peer.getMCport();
26
        this.version = version;
27
        this.peer = peer;
28
    }
29
    
30
    @Override
31
    public void run() {
32
        File file = new File(this.filePath);
33
        try {
34
            String fileId = BackupFile.generateFileId(this.filePath, file.lastModified());
35
            long interval;
36
            RestoreFile restoreFile = new RestoreFile(new File("Peer-" + this.peer.getSenderId() + File.separator + "Backups" + File.separator + this.filePath), fileId, file.length()/MAX_CHUNK_SIZE + 1);
37
            this.peer.restoreMap.put(fileId, restoreFile);
38
            DatagramSocket socket = new DatagramSocket();
39

    
40
            //GETCHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
41
            for (int i = 0; i <= file.length()/MAX_CHUNK_SIZE; i++) {
42
                interval = 1000;
43
                String message = "GETCHUNK " + this.version + " " + peer.getSenderId() + " " + fileId + " " + i + " " + CRLF + CRLF;
44
                
45
                DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), this.MCaddress, this.MCport);
46

    
47
                while (interval <= 31000) { //31 = 1+2+4+8+16
48
                    this.peer.print("sending GETCHUNK message");
49
                    socket.send(packet);
50
                    Thread.sleep(interval);
51
                    if (this.peer.restoreMap.get(fileId).hasChunk(i)){
52
                        break;
53
                    }
54
                    interval *= 2;
55
                }
56
                                if(interval > 31000) {
57
                                        this.peer.print("Couldn't get all chunks");
58
                                        return;
59
                                }
60
            }
61
            
62
            if (!restoreFile.restoreFile(file)) {
63
                this.peer.print("The file couldn't be restored due to lack of chunks");
64
            } else {
65
                this.peer.print("The file was restored successfully");
66
            }
67
            
68
            socket.close();
69
            this.peer.print("RESTORE protocol finished");
70
        } catch (IOException | NoSuchAlgorithmException | InterruptedException e) {
71
            e.printStackTrace();
72
        }
73
    }
74
}