Project

General

Profile

Statistics
| Revision:

root / project / src / main / java / protocols / Reclaim.java @ 1

History | View | Annotate | Download (1.48 KB)

1 1 up20120064
package main.java.protocols;
2
3
import main.java.file.FileChunkID;
4
import main.java.file.FileID;
5
import main.java.listeners.Broker;
6
import main.java.peer.Peer;
7
8
import java.io.File;
9
10
import static main.java.utils.Constants.MAX_WAITING_TIME;
11
12
public class Reclaim implements Runnable {
13
14
    private int amount;
15
16
    public Reclaim(int amount){
17
        this.amount = amount;
18
19
    }
20
    @Override
21
    public void run() {
22
23
        Peer.getDisk().setCapacity(amount);
24
25
        while (Peer.getDisk().getFreeSpace() < 0){
26
            FileChunkID chunkID = Peer.getDb().getHighestPerceivedRepDegChunk();
27
28
            System.out.println("CHUNKID: " + chunkID.toString());
29
            if(chunkID != null){
30
                Broker.sendREMOVED(chunkID);
31
32
33
                File f = new File("peer"+Peer.getID()+"/Backup/"+
34
                        chunkID.getFileID().split("\\.")[0]+ "/"+chunkID.toString());
35
36
               // File f = new File("/debug/"+chunkID.toString());
37
38
39
40
                Peer.getDisk().removeFile(f.length());
41
                Peer.getDb().decreasePerceivedRepDeg(chunkID, Peer.getID());
42
                /* Peer.getDb().removeFile(new FileID(chunkID.getFileID(), -1)); */
43
44
                try{
45
                    //System.out.println("\tWaiting...");
46
                    Thread.sleep((long)(Math.random() * MAX_WAITING_TIME));
47
                } catch (InterruptedException ie){
48
                    ie.printStackTrace();
49
                }
50
51
                f.delete();
52
53
54
            }
55
56
        }
57
58
    }
59
}