root / threads / ManagePutchunk.java @ 15
History | View | Annotate | Download (1.22 KB)
1 | 15 | up20160473 | package threads; |
---|---|---|---|
2 | |||
3 | |||
4 | import java.io.IOException; |
||
5 | import java.util.concurrent.TimeUnit; |
||
6 | import server.Server; |
||
7 | |||
8 | public class ManagePutchunk implements Runnable { |
||
9 | |||
10 | private byte[] message; |
||
11 | private int time; |
||
12 | private String key; |
||
13 | private int replicationDegree; |
||
14 | private int tries; |
||
15 | |||
16 | public ManagePutchunk(byte[] message, int time, String fileID, int chunkNr, int replicationDegree) { |
||
17 | this.message = message;
|
||
18 | this.time = time;
|
||
19 | this.key = fileID + '.' + chunkNr; |
||
20 | this.replicationDegree = replicationDegree;
|
||
21 | this.tries = 1; |
||
22 | } |
||
23 | |||
24 | @Override
|
||
25 | public void run() { |
||
26 | |||
27 | int occurrences = Server.getStorage().getStoredTimes().get(this.key); |
||
28 | |||
29 | if (occurrences < replicationDegree) {
|
||
30 | try {
|
||
31 | Server.getmdb().sendMessage(message); |
||
32 | } catch (IOException e) { |
||
33 | e.printStackTrace(); |
||
34 | } |
||
35 | System.out.println("Sent PUTCHUNK try: " + tries); |
||
36 | System.out.println(key);
|
||
37 | this.time = 2 * this.time; |
||
38 | this.tries++;
|
||
39 | |||
40 | if (this.tries < 5) |
||
41 | Server.getThreadLauncher().schedule(this, this.time, TimeUnit.SECONDS); |
||
42 | |||
43 | |||
44 | |||
45 | |||
46 | } |
||
47 | |||
48 | |||
49 | |||
50 | } |
||
51 | } |