root / project / src / main / java / protocols / BackupChunk.java @ 1
History | View | Annotate | Download (2.28 KB)
1 | 1 | up20120064 | package main.java.protocols; |
---|---|---|---|
2 | |||
3 | |||
4 | import main.java.file.FileChunk; |
||
5 | import main.java.file.FileChunkID; |
||
6 | import main.java.file.FileID; |
||
7 | import main.java.listeners.Broker; |
||
8 | import main.java.peer.Peer; |
||
9 | |||
10 | import java.io.IOException; |
||
11 | |||
12 | public class BackupChunk implements Runnable { |
||
13 | |||
14 | public FileChunk chunk;
|
||
15 | public boolean repDegReached; |
||
16 | |||
17 | public BackupChunk(FileChunk chunk) {
|
||
18 | this.chunk = chunk;
|
||
19 | repDegReached = false;
|
||
20 | } |
||
21 | |||
22 | @Override
|
||
23 | public void run() { |
||
24 | int attempt = 0; |
||
25 | |||
26 | long timeToRetry = 500; |
||
27 | FileID fID = new FileID(chunk.getFileID().toString(),chunk.getReplicationDegree());
|
||
28 | |||
29 | |||
30 | FileChunkID fileChunkID = new FileChunkID(fID.toString(), chunk.getChunkNo());
|
||
31 | |||
32 | |||
33 | |||
34 | while(!repDegReached) {
|
||
35 | |||
36 | Peer.getDb().addNewRepDegCounter(new FileChunkID(fID.toString(), chunk.getChunkNo()), chunk.getReplicationDegree());
|
||
37 | |||
38 | timeToRetry = timeToRetry *2;
|
||
39 | |||
40 | if(Peer.getMCListener().isCounting(fileChunkID))
|
||
41 | Peer.getMCListener().clearCount(fileChunkID); |
||
42 | |||
43 | try {
|
||
44 | Broker.sendPUTCHUNK(chunk); |
||
45 | Peer.getMCListener().startCountingStoreds(fileChunkID); |
||
46 | System.out.println("Listening for STOREDs for " + timeToRetry + "ms"); |
||
47 | Thread.sleep(timeToRetry);
|
||
48 | } catch (InterruptedException e) { |
||
49 | e.printStackTrace(); |
||
50 | } |
||
51 | |||
52 | |||
53 | int perceivedRepDeg = Peer.getMCListener().getCount(fileChunkID);
|
||
54 | |||
55 | System.out.println("Replication degree count: " + perceivedRepDeg + "/" + chunk.getReplicationDegree()); |
||
56 | |||
57 | |||
58 | if(perceivedRepDeg < chunk.getReplicationDegree()) {
|
||
59 | attempt++; |
||
60 | |||
61 | if(attempt > 4) { |
||
62 | System.out.println("The Desired Replication Degree wasn't reached in 5 tries, skipping chunk..."); |
||
63 | repDegReached = true;
|
||
64 | return;
|
||
65 | |||
66 | |||
67 | } |
||
68 | else {
|
||
69 | System.out.println("The Desired Replication Degree was not reached, trying again..."); |
||
70 | |||
71 | } |
||
72 | |||
73 | } else {
|
||
74 | System.out.println("Desired Replication Degree accomplished!"); |
||
75 | repDegReached = true;
|
||
76 | } |
||
77 | |||
78 | } |
||
79 | |||
80 | |||
81 | |||
82 | |||
83 | |||
84 | Peer.getMCListener().stopCounting(fileChunkID); |
||
85 | } |
||
86 | } |