root / src / peer / protocols / backup / Store.java @ 2
History | View | Annotate | Download (1014 Bytes)
1 | 2 | up20150644 | 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 | * Store
|
||
12 | */
|
||
13 | public class Store implements Runnable { |
||
14 | |||
15 | private Peer peer;
|
||
16 | private Chunk chunk;
|
||
17 | |||
18 | public Store(Peer peer, Chunk chunk) {
|
||
19 | this.peer = peer;
|
||
20 | this.chunk = chunk;
|
||
21 | } |
||
22 | |||
23 | public void sendStored() { |
||
24 | Message storedMessage = Message.parseStoredMessage(chunk, peer); |
||
25 | try {
|
||
26 | peer.sendToMc(storedMessage); |
||
27 | } catch (IOException e) { |
||
28 | e.printStackTrace(); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | public boolean storeChunk() { |
||
33 | try {
|
||
34 | Thread.sleep((long) (Math.random() * 400 + 1)); |
||
35 | } catch (InterruptedException e) { |
||
36 | e.printStackTrace(); |
||
37 | } |
||
38 | if (ChunkManagement.getInstance().getStores(chunk.getFileID(), chunk.getChunkNo()) < chunk.getRepDegree()) {
|
||
39 | if (peer.getDisk().storeChunk(chunk)) {
|
||
40 | sendStored(); |
||
41 | return true; |
||
42 | } |
||
43 | } |
||
44 | return false; |
||
45 | } |
||
46 | |||
47 | @Override
|
||
48 | public void run() { |
||
49 | storeChunk(); |
||
50 | } |
||
51 | |||
52 | } |