root / RemovedTask.java @ 1
History | View | Annotate | Download (2.91 KB)
1 | 1 | up20150476 | import java.io.IOException; |
---|---|---|---|
2 | import java.net.DatagramPacket; |
||
3 | import java.net.DatagramSocket; |
||
4 | import java.util.Random; |
||
5 | |||
6 | /**
|
||
7 | * RemovedTask
|
||
8 | */
|
||
9 | public class RemovedTask implements Runnable { |
||
10 | private static final String CRLF = Integer.toHexString(0xD) + Integer.toHexString(0xA); |
||
11 | |||
12 | private Peer peer;
|
||
13 | private String fileId; |
||
14 | private int chunkNo; |
||
15 | private MCListener mc;
|
||
16 | |||
17 | private String version; |
||
18 | |||
19 | // REMOVED <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
|
||
20 | RemovedTask(Peer peer, String[] args, MCListener mc) { |
||
21 | this.peer = peer;
|
||
22 | this.fileId = args[3]; |
||
23 | this.version = args[1]; |
||
24 | this.chunkNo = Integer.parseInt(args[4]); |
||
25 | this.mc = mc;
|
||
26 | |||
27 | this.mc.print("starting REMOVED protocol: [fileId] " + fileId + "[chunkNo] " + chunkNo); |
||
28 | } |
||
29 | |||
30 | @Override
|
||
31 | public void run() { |
||
32 | this.peer.repDegMap.get(this.fileId).put(this.chunkNo, this.peer.repDegMap.get(this.fileId).get(this.chunkNo)-1); |
||
33 | |||
34 | try{
|
||
35 | if(this.peer.repDegMap.get(this.fileId).get(chunkNo) < this.peer.chunksMap.get(fileId).getDesiredReplicationDegree()){ |
||
36 | |||
37 | Random random = new Random(); |
||
38 | |||
39 | //Random delay between [0-400] ms to avoid overlapping messages
|
||
40 | Thread.sleep(random.nextInt(401)); |
||
41 | |||
42 | if(this.peer.repDegMap.get(this.fileId).get(chunkNo) >= this.peer.chunksMap.get(fileId).getDesiredReplicationDegree()){ |
||
43 | return; //já outro peer tem o chunk |
||
44 | } |
||
45 | byte[] chunk = this.peer.chunksMap.get(fileId).getChunksMap().get(chunkNo).getData(); |
||
46 | |||
47 | String message = "PUTCHUNK " + version + " " + peer.getSenderId() + " " + fileId + " " + chunkNo + " " |
||
48 | + this.peer.chunksMap.get(fileId).getDesiredReplicationDegree() + " " + CRLF + CRLF; |
||
49 | |||
50 | byte[] packetData = new byte[message.getBytes().length + chunk.length]; |
||
51 | System.arraycopy(message.getBytes(), 0, packetData, 0, message.getBytes().length); |
||
52 | System.arraycopy(chunk, 0, packetData, message.getBytes().length, chunk.length); |
||
53 | |||
54 | DatagramSocket socket = new DatagramSocket(); |
||
55 | |||
56 | DatagramPacket packet = new DatagramPacket(packetData, packetData.length, this.peer.getMDBaddress(), this.peer.getMDBport()); |
||
57 | int interval = 1000; |
||
58 | while (interval <= 31000) { //31 = 1+2+4+8+16 |
||
59 | this.peer.print("sending PUTCHUNK message"); |
||
60 | socket.send(packet); |
||
61 | Thread.sleep(interval);
|
||
62 | if (this.peer.repDegMap.containsKey(fileId) && this.peer.repDegMap.get(fileId).get(chunkNo) >= this.peer.chunksMap.get(fileId).getDesiredReplicationDegree()) |
||
63 | break;
|
||
64 | interval *= 2;
|
||
65 | } |
||
66 | socket.close(); |
||
67 | |||
68 | } |
||
69 | |||
70 | this.mc.print("REMOVED protocol finished"); |
||
71 | } |
||
72 | catch (InterruptedException | IOException e) |
||
73 | { |
||
74 | e.printStackTrace(); |
||
75 | } |
||
76 | |||
77 | } |
||
78 | } |