sdis1819-t7g02 / protocols / RestoreChunksProtocol.java
History | View | Annotate | Download (1.05 KB)
1 |
package protocols; |
---|---|
2 |
|
3 |
import java.util.concurrent.Callable; |
4 |
|
5 |
import channels.MessageHeader; |
6 |
import service.Cloud; |
7 |
import service.Constants; |
8 |
|
9 |
|
10 |
public class RestoreChunksProtocol implements Callable<Object>{ |
11 |
Cloud cloud; |
12 |
MessageHeader message_header; |
13 |
|
14 |
public RestoreChunksProtocol(Cloud c, MessageHeader mh)
|
15 |
{ |
16 |
this.cloud = c;
|
17 |
this.message_header = mh;
|
18 |
} |
19 |
|
20 |
@Override
|
21 |
public Object call() throws Exception { |
22 |
|
23 |
int numberOfTries = 0; |
24 |
int multiplier = 1; |
25 |
|
26 |
while(numberOfTries < Constants.MAX_PUTCHUNK_MESSAGES) {
|
27 |
|
28 |
cloud.sendHeader(this.message_header);
|
29 |
// System.out.println("Trying " + multiplier + " seconds");
|
30 |
|
31 |
Thread.sleep(1000 * multiplier); |
32 |
|
33 |
if(cloud.checkIfChunkWasRestored(this.message_header.getFileId(), this.message_header.getChunkNumber())) |
34 |
{ |
35 |
System.out.println(this.message_header.getChunkNumber() + " restored"); |
36 |
return true; |
37 |
} |
38 |
|
39 |
multiplier *= 2;
|
40 |
numberOfTries++; |
41 |
} |
42 |
|
43 |
System.out.println("ERROR: " + this.message_header.getChunkNumber() + " could not be restored!"); |
44 |
return false; |
45 |
} |
46 |
} |