root / ReclaimTask.java @ 1
History | View | Annotate | Download (3.09 KB)
1 | 1 | up20150476 | import java.io.IOException; |
---|---|---|---|
2 | import java.net.DatagramPacket; |
||
3 | import java.net.DatagramSocket; |
||
4 | import java.net.InetAddress; |
||
5 | import java.util.concurrent.ConcurrentHashMap; |
||
6 | |||
7 | /**
|
||
8 | * ReclaimTask
|
||
9 | */
|
||
10 | public class ReclaimTask implements Runnable { |
||
11 | private static final String CRLF = Integer.toHexString(0xD) + Integer.toHexString(0xA); |
||
12 | |||
13 | private InetAddress MCaddress; |
||
14 | private int MCport; |
||
15 | |||
16 | private Peer peer;
|
||
17 | private String version; |
||
18 | private int diskspace; |
||
19 | |||
20 | ReclaimTask(int diskspace, Peer peer) {
|
||
21 | this.MCaddress = peer.getMCaddress();
|
||
22 | this.MCport = peer.getMCport();
|
||
23 | this.diskspace = diskspace;
|
||
24 | this.peer = peer;
|
||
25 | } |
||
26 | |||
27 | @Override
|
||
28 | public void run() { |
||
29 | try {
|
||
30 | long storageSize = this.peer.getStorageUsed(); |
||
31 | diskspace = diskspace*1000; //change from KByte to Byte |
||
32 | if (diskspace > storageSize)
|
||
33 | System.out.println("This amount of KB hasn't been reached yet."); |
||
34 | else if (diskspace == storageSize) |
||
35 | System.out.println("This amount of KB is the current amount in the peer."); |
||
36 | else {
|
||
37 | DatagramSocket socket = new DatagramSocket(); |
||
38 | |||
39 | while(diskspace < this.peer.getStorageUsed()){ |
||
40 | |||
41 | for(String i : this.peer.chunksMap.keySet()){ |
||
42 | if(diskspace >= this.peer.getStorageUsed()) |
||
43 | break;
|
||
44 | BackupFile fileToCheck = this.peer.chunksMap.get(i);
|
||
45 | ConcurrentHashMap<Integer, Chunk> chunkMapToCheck = fileToCheck.getChunksMap(); |
||
46 | |||
47 | |||
48 | for(Integer j : chunkMapToCheck.keySet()) { |
||
49 | if(diskspace >= this.peer.getStorageUsed()) |
||
50 | break;
|
||
51 | if (this.peer.repDegMap.get(i).get(j) > fileToCheck.getDesiredReplicationDegree()){ |
||
52 | |||
53 | BackupFile newBackup = this.peer.chunksMap.get(fileToCheck.getFileId());
|
||
54 | newBackup.removeChunk(j, this.peer);
|
||
55 | this.peer.repDegMap.get(fileToCheck.getFileId()).put(j, this.peer.repDegMap.get(fileToCheck.getFileId()).get(j)-1); |
||
56 | this.peer.chunksMap.put(fileToCheck.getFileId(), newBackup); //apaga chunk do peer atual |
||
57 | |||
58 | //REMOVED <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
|
||
59 | String message = "REMOVED " + this.version + " " + peer.getSenderId() + " " + fileToCheck.getFileId() + " " + j + " " + CRLF + CRLF; |
||
60 | DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), this.MCaddress, this.MCport); |
||
61 | |||
62 | socket.send(packet); |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | socket.close(); |
||
68 | } |
||
69 | this.peer.setStorageCapacity(diskspace);
|
||
70 | this.peer.print("RECLAIM protocol finished"); |
||
71 | } catch (IOException e) { |
||
72 | e.printStackTrace(); |
||
73 | } |
||
74 | } |
||
75 | } |