root / src / peer / channels / Handler.java @ 2
History | View | Annotate | Download (3.43 KB)
1 |
package peer.channels; |
---|---|
2 |
|
3 |
import java.net.DatagramPacket; |
4 |
|
5 |
import chunk.Chunk; |
6 |
import disk.ChunkManagement; |
7 |
import peer.Peer; |
8 |
import peer.protocols.backup.Store; |
9 |
import peer.protocols.reclaim.Reclaim; |
10 |
import peer.protocols.restore.GetChunk; |
11 |
import message.*; |
12 |
|
13 |
public class Handler implements Runnable { |
14 |
|
15 |
Peer peer; |
16 |
DatagramPacket packet;
|
17 |
Message msg; |
18 |
MessageHeader msgHeader; |
19 |
|
20 |
public Handler(Peer peer, DatagramPacket packet) { |
21 |
this.peer = peer;
|
22 |
this.packet = packet;
|
23 |
} |
24 |
|
25 |
public void parsePacket() { |
26 |
int packetLength = packet.getLength();
|
27 |
byte[] rawData = new byte[packetLength]; |
28 |
byte[] packetData = this.packet.getData(); |
29 |
System.arraycopy(packetData, packet.getOffset(), rawData, 0, packetLength); |
30 |
this.msg = new Message(rawData); |
31 |
this.msgHeader = msg.getHeader();
|
32 |
} |
33 |
|
34 |
@Override
|
35 |
public void run() { |
36 |
|
37 |
parsePacket(); |
38 |
|
39 |
if (this.peer.getPeerId() != this.msgHeader.getSenderId()) |
40 |
switch (this.msgHeader.getMessageType()) { |
41 |
case "PUTCHUNK": |
42 |
handlePUTCHUNK(); |
43 |
break;
|
44 |
case "GETCHUNK": |
45 |
handleGETCHUNK(); |
46 |
break;
|
47 |
case "CHUNK": |
48 |
handleCHUNK(); |
49 |
break;
|
50 |
case "DELETE": |
51 |
handleDELETE(); |
52 |
break;
|
53 |
case "REMOVED": |
54 |
handleREMOVED(); |
55 |
break;
|
56 |
default:
|
57 |
break;
|
58 |
} |
59 |
} |
60 |
|
61 |
private void handleREMOVED() { |
62 |
String fileId = this.msgHeader.getFileId(); |
63 |
int chunkNo = this.msgHeader.getChunkNo(); |
64 |
ChunkManagement.getInstance().registerRemoved(fileId, chunkNo); |
65 |
|
66 |
Thread t = new Thread(new Reclaim(peer, fileId, chunkNo)); |
67 |
t.start(); |
68 |
try {
|
69 |
t.join(); |
70 |
} catch (InterruptedException e) { |
71 |
e.printStackTrace(); |
72 |
} |
73 |
} |
74 |
|
75 |
public void handlePUTCHUNK() { |
76 |
if (this.peer.getDisk().getFreeSpace() < (packet.getLength())) { |
77 |
System.out.println("Peer " + this.peer.getPeerId() + "- PUTCHUNK request: Not enough space to store chunk"); |
78 |
return;
|
79 |
} |
80 |
|
81 |
Chunk chunk = new Chunk(this.msgHeader.getFileId(), this.msgHeader.getChunkNo(), this.msgHeader.getReplicaDeg(), |
82 |
this.msg.getBody());
|
83 |
|
84 |
Thread t = new Thread(new Store(peer, chunk)); |
85 |
t.start(); |
86 |
try {
|
87 |
t.join(); |
88 |
} catch (InterruptedException e) { |
89 |
e.printStackTrace(); |
90 |
} |
91 |
|
92 |
} |
93 |
|
94 |
public void handleGETCHUNK() { |
95 |
String fileId = this.msgHeader.getFileId(); |
96 |
int chunkNo = this.msgHeader.getChunkNo(); |
97 |
Thread t = new Thread(new GetChunk(peer, fileId, chunkNo)); |
98 |
t.start(); |
99 |
try {
|
100 |
t.join(); |
101 |
} catch (InterruptedException e) { |
102 |
e.printStackTrace(); |
103 |
} |
104 |
} |
105 |
|
106 |
public void handleCHUNK() { |
107 |
Chunk chunk = new Chunk(this.msgHeader.getFileId(), this.msgHeader.getChunkNo(), this.msgHeader.getReplicaDeg(), |
108 |
this.msg.getBody());
|
109 |
|
110 |
ChunkManagement.getInstance().addRestoreChunk(chunk); |
111 |
} |
112 |
|
113 |
public void handleDELETE() { |
114 |
String fileID = msgHeader.getFileId();
|
115 |
this.peer.getDisk().deleteFileDirectory(fileID);
|
116 |
ChunkManagement.getInstance().deleteStores(fileID); |
117 |
} |
118 |
} |