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