Project

General

Profile

Statistics
| Revision:

root / src / protocols / RestoreProtocol.java

History | View | Annotate | Download (2.82 KB)

1 1 up20140508
package protocols;
2
3
4
import peer.Peer;
5
import utils.FileController;
6
import utils.Utils;
7
8
import java.io.*;
9
import java.net.DatagramPacket;
10
import java.util.ArrayList;
11
12
public class RestoreProtocol {
13
    String hashId;
14
    String name;
15
    int numberOfChunks;
16
17
    public RestoreProtocol(String[] args) throws IOException, InterruptedException {
18
        this.name = args[1];
19
        this.numberOfChunks = 0;
20
21
        //check in hash database
22
        this.hashId = Utils.hashDatabase.get(this.name);
23
24
        if(this.hashId == null) {
25
            System.out.println("no such file exists to restore, exiting ...");
26
            return;
27
        }
28
29
        getNumberOfChunks();
30
31
        System.out.println("RESTORE Protocol  ||  Restoring: " + this.name + "  ||  IdHashed: " + this.hashId);
32
33
        Utils.restorePeer = Peer.getSenderId();
34
35
        //send get chunck message to MC channel
36
        sendGetChunk(0);
37
38
        Thread.sleep(10000); //waits a bit for chunks to be stored in the folder before merging them
39
        System.out.println("All chunks stored, merging into one single file");
40
        mergeChunks();
41
    }
42
43
    private void getNumberOfChunks() {
44
        for (String name: Utils.storedChunks.keySet()){
45
            String [] key = name.split(":");
46
            if(this.hashId.equals(key[0])) this.numberOfChunks++;
47
        }
48
    }
49
50
    private void sendGetChunk(int chunkNo) throws IOException {
51
52
        if(chunkNo > this.numberOfChunks -1) return;
53
54
        System.out.println("Sending getchunk message to mc...");
55
        String msg = "GETCHUNK" + " " + Utils.version + " " + Peer.getSenderId() + " " + this.hashId + " " + chunkNo + Utils.crlf;
56
57
        DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.getBytes().length, Peer.getMcAddress(),Peer.getMcPort());
58
59
        Utils.mcSocket.send(packet);
60
        chunkNo += 1;
61
        sendGetChunk(chunkNo);
62
    }
63
64
    private void mergeChunks() throws IOException {
65
        File outputFile = new File("storage/Restored/Peer" + Peer.getSenderId() + "/" + this.name);
66
        outputFile.getParentFile().mkdirs();
67
        outputFile.createNewFile();
68
69
        FileOutputStream outStream = new FileOutputStream(outputFile);
70
71
        for(int i = 0; i < this.numberOfChunks; i++) {
72
            File inputFile = new File("storage/Restored/Peer" + Peer.getSenderId() + "/" + i);
73
                FileInputStream inStream = new FileInputStream(inputFile);
74
                byte[] buf = new byte[64000];
75
                int readBytes = inStream.read(buf);
76
                outStream.write(buf, 0, readBytes);
77
78
                inStream.close();
79
80
        }
81
82
        for(int i = 0; i < this.numberOfChunks; i++) { //delete chunks (we dont need them anymore)
83
            File inputFile = new File("storage/Restored/Peer" + Peer.getSenderId() + "/" + i);
84
            inputFile.delete();
85
        }
86
87
        outStream.close();
88
    }
89
}