Revision 3
Update
Handler.java | ||
---|---|---|
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 |
} |
|
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 |
} |
|
118 | 119 |
} |
Also available in: Unified diff