root / src / peer / protocols / restore / GetChunk.java @ 3
History | View | Annotate | Download (1.3 KB)
1 |
package peer.protocols.restore; |
---|---|
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 |
* GetChunk
|
12 |
*/
|
13 |
public class GetChunk implements Runnable{ |
14 |
|
15 |
private Peer peer;
|
16 |
private String fileId; |
17 |
private int chunkNo; |
18 |
|
19 |
public GetChunk(Peer peer, String fileId, int chunkNo) { |
20 |
this.peer = peer;
|
21 |
this.fileId = fileId;
|
22 |
this.chunkNo = chunkNo;
|
23 |
} |
24 |
|
25 |
public void sendChunk(Chunk chunk) { |
26 |
Message message = Message.parseChunkMessage(chunk,peer); |
27 |
try {
|
28 |
peer.sendToMc(message); |
29 |
} catch (IOException e) { |
30 |
e.printStackTrace(); |
31 |
} |
32 |
} |
33 |
|
34 |
public boolean getChunk() { |
35 |
Chunk chunk = peer.getDisk().getChunk(fileId, chunkNo); |
36 |
if (chunk!= null){ |
37 |
try {
|
38 |
Thread.sleep((long) (Math.random() * 400 + 1)); |
39 |
} catch (InterruptedException e) { |
40 |
e.printStackTrace(); |
41 |
} |
42 |
if (ChunkManagement.getInstance().getRestoreChunks().contains(chunk)){
|
43 |
ChunkManagement.getInstance().getRestoreChunks().remove(chunk); |
44 |
return false; |
45 |
} |
46 |
sendChunk(chunk); |
47 |
return true; |
48 |
} |
49 |
return false; |
50 |
} |
51 |
|
52 |
@Override
|
53 |
public void run() { |
54 |
getChunk(); |
55 |
} |
56 |
|
57 |
|
58 |
} |