root / src / peer / protocols / backup / BackupChunk.java @ 3
History | View | Annotate | Download (1.22 KB)
1 |
package peer.protocols.backup; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
|
5 |
import chunk.Chunk; |
6 |
import disk.ChunkManagement; |
7 |
import message.Message; |
8 |
import peer.Peer; |
9 |
|
10 |
/**
|
11 |
* BackupChunk
|
12 |
*/
|
13 |
public class BackupChunk implements Runnable { |
14 |
|
15 |
public static final int backupTries = 10; |
16 |
|
17 |
private Chunk chunk;
|
18 |
private Peer peer;
|
19 |
|
20 |
public BackupChunk(Peer peer, Chunk chunk) {
|
21 |
this.chunk = chunk;
|
22 |
this.peer = peer;
|
23 |
} |
24 |
|
25 |
public void sendPutChunk() { |
26 |
Message message = Message.parsePutChunkMessage(chunk, peer); |
27 |
try {
|
28 |
peer.sendToMdb(message); |
29 |
} catch (IOException e) { |
30 |
e.printStackTrace(); |
31 |
} |
32 |
} |
33 |
|
34 |
public boolean backupChunk() { |
35 |
sendPutChunk(); |
36 |
try {
|
37 |
Thread.sleep(400); |
38 |
} catch (InterruptedException e) { |
39 |
e.printStackTrace(); |
40 |
} |
41 |
if (ChunkManagement.getInstance().getStores(chunk.getFileID(), chunk.getChunkNo()) >= chunk.getRepDegree()) {
|
42 |
return true; |
43 |
} |
44 |
return false; |
45 |
} |
46 |
|
47 |
@Override
|
48 |
public void run() { |
49 |
try {
|
50 |
Thread.sleep(400 * chunk.getChunkNo()); |
51 |
} catch (InterruptedException e) { |
52 |
// TODO Auto-generated catch block
|
53 |
e.printStackTrace(); |
54 |
} |
55 |
boolean done = false; |
56 |
int tries = 0; |
57 |
while(!done) {
|
58 |
done = backupChunk(); |
59 |
if (!done) {
|
60 |
tries++; |
61 |
if (tries > backupTries) {
|
62 |
break;
|
63 |
} |
64 |
} |
65 |
} |
66 |
} |
67 |
} |