sdis1819-t7g02 / protocols / BackupChunkProtocol.java
History | View | Annotate | Download (1.24 KB)
1 |
package protocols; |
---|---|
2 |
|
3 |
import java.util.concurrent.Callable; |
4 |
|
5 |
import channels.Message; |
6 |
import service.Chunk; |
7 |
import service.Cloud; |
8 |
import service.Constants; |
9 |
|
10 |
public class BackupChunkProtocol implements Callable<Object> { |
11 |
Cloud storage; |
12 |
Chunk objective; |
13 |
Message builtChunkMessage; |
14 |
int repDegree;
|
15 |
|
16 |
public BackupChunkProtocol(Cloud c, Chunk chunksToBeStored, Message m, int replication_degree) { |
17 |
storage = c; |
18 |
objective = chunksToBeStored; |
19 |
repDegree = replication_degree; |
20 |
builtChunkMessage = m; |
21 |
} |
22 |
|
23 |
@Override
|
24 |
public Object call() throws Exception { |
25 |
|
26 |
int numberOfTries = 0; |
27 |
int multiplier = 1; |
28 |
|
29 |
if(storage.insertAwaitingStoreFile(builtChunkMessage.getHeader())) {
|
30 |
while(numberOfTries < Constants.MAX_PUTCHUNK_MESSAGES) {
|
31 |
storage.sendBackupMessage(builtChunkMessage); |
32 |
// System.out.println("Trying " + multiplier + " seconds");
|
33 |
|
34 |
Thread.sleep(1000 * multiplier); |
35 |
|
36 |
if(storage.wasTheRepDegreeMatched(objective)) {
|
37 |
storage.addChunk(objective); |
38 |
System.out.println(objective.getChunkNumber() + " replication degree matched!"); |
39 |
return true; |
40 |
} |
41 |
|
42 |
multiplier *= 2;
|
43 |
numberOfTries++; |
44 |
} |
45 |
} |
46 |
|
47 |
System.out.println("ERROR: " + objective.getChunkId() + " replication degree not matched!"); |
48 |
return false; |
49 |
} |
50 |
} |