root / proj / src / MessageParser.java @ 1
History | View | Annotate | Download (4.75 KB)
1 |
import java.io.IOException; |
---|---|
2 |
import java.io.UnsupportedEncodingException; |
3 |
import java.net.DatagramPacket; |
4 |
import java.net.UnknownHostException; |
5 |
import java.nio.charset.StandardCharsets; |
6 |
import java.util.Arrays; |
7 |
import java.util.Random; |
8 |
import java.util.concurrent.TimeUnit; |
9 |
|
10 |
public class MessageParser implements Runnable { |
11 |
|
12 |
Peer peer; |
13 |
DatagramPacket packet;
|
14 |
|
15 |
public MessageParser(DatagramPacket packet, Peer peer) { |
16 |
this.peer = peer;
|
17 |
this.packet = packet;
|
18 |
|
19 |
} |
20 |
|
21 |
public void run() { |
22 |
|
23 |
String str = new String(packet.getData(), StandardCharsets.UTF_8); |
24 |
String rest = null, fileID = null, messageType = null, version = null; |
25 |
int i = 0, next = 0, chunkNo = 0, replicationDeg = 0, senderID=0; |
26 |
|
27 |
if(str.contains(" ")){ |
28 |
i= str.indexOf(" ");
|
29 |
messageType= str.substring(0,i);
|
30 |
next = str.indexOf(" ", i+1); |
31 |
version = str.substring(i,next).replace(" ",""); |
32 |
i = next; |
33 |
next = str.indexOf(" ", i+1); |
34 |
senderID = Integer.parseInt(str.substring(i,next).replace(" ","")); |
35 |
i = next; |
36 |
next = str.indexOf(" ", i+1); |
37 |
fileID = str.substring(i,next).replace(" ",""); |
38 |
i = next; |
39 |
next = str.indexOf(" ",i+1); |
40 |
chunkNo = Integer.parseInt(str.substring(i,next).replace(" ","")); |
41 |
i = next; |
42 |
next = str.indexOf(" ",i+1); |
43 |
replicationDeg = Integer.parseInt(str.substring(i,next).replace(" ","")); |
44 |
rest = str.substring(next); |
45 |
} |
46 |
|
47 |
|
48 |
if(peer.getPeerID() == senderID) {
|
49 |
return;
|
50 |
} |
51 |
|
52 |
|
53 |
String CRLF = "" + (char) 0xD + (char) 0xA; |
54 |
|
55 |
String bodyString = rest.replaceFirst(CRLF+CRLF,""); |
56 |
|
57 |
String bodyString2 = bodyString.replaceAll("\0", ""); |
58 |
|
59 |
bodyString = bodyString2.replaceFirst(" ", ""); |
60 |
|
61 |
byte[] body = bodyString.getBytes( StandardCharsets.UTF_8); |
62 |
|
63 |
|
64 |
switch(messageType) {
|
65 |
|
66 |
case "PUTCHUNK": |
67 |
|
68 |
Chunk chunk = new Chunk(fileID, chunkNo, body);
|
69 |
System.out.println("CHUNK N: "+ chunk.getChunkN()+ " SIZE: " + chunk.getContent().length); |
70 |
if(peer.getStorage().addChunk(chunk)) {
|
71 |
|
72 |
Message stored = new Message("STORED",peer.getVersion(),peer.getPeerID(),fileID, chunkNo, 0, null); |
73 |
byte[] reply = stored.sendable(); |
74 |
|
75 |
try {
|
76 |
|
77 |
Random rand = new Random(); |
78 |
int n = rand.nextInt(400) + 1; |
79 |
|
80 |
|
81 |
try {
|
82 |
Thread.sleep(n);
|
83 |
} catch (InterruptedException e) { |
84 |
e.printStackTrace(); |
85 |
} |
86 |
|
87 |
peer.getMC().sendMessage(reply); |
88 |
System.out.println(stored.messageToStringPrintable());
|
89 |
} catch (UnknownHostException e) { |
90 |
// TODO Auto-generated catch block
|
91 |
e.printStackTrace(); |
92 |
} |
93 |
|
94 |
try {
|
95 |
peer.saveChunks(); |
96 |
} catch (IOException e) { |
97 |
// TODO Auto-generated catch block
|
98 |
e.printStackTrace(); |
99 |
} |
100 |
|
101 |
} |
102 |
break;
|
103 |
|
104 |
|
105 |
case "STORED": |
106 |
peer.getStorage().getBackUps().add(new BackUpInfo(fileID, chunkNo, peer.getPeerID()));
|
107 |
|
108 |
|
109 |
break;
|
110 |
|
111 |
case "GETCHUNK": //CHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF><Body> |
112 |
|
113 |
for(Chunk chunkSend: peer.getStorage().getChunks()) {
|
114 |
|
115 |
if(chunkSend.getFileID().equals(fileID) && chunkSend.getChunkN()==chunkNo) {
|
116 |
|
117 |
Random rand = new Random(); |
118 |
int n = rand.nextInt(400) + 1; |
119 |
|
120 |
|
121 |
try {
|
122 |
Thread.sleep(n);
|
123 |
} catch (InterruptedException e) { |
124 |
e.printStackTrace(); |
125 |
} |
126 |
|
127 |
Message sendChunk = new Message("CHUNK",peer.getVersion(),peer.getPeerID(),fileID, chunkNo, 0, chunkSend.getContent()); |
128 |
byte[] reply = sendChunk.sendable(); |
129 |
System.out.println(sendChunk.messageToStringPrintable());
|
130 |
|
131 |
try {
|
132 |
peer.getMDR().sendMessage(reply); |
133 |
} catch (UnknownHostException e) { |
134 |
// TODO Auto-generated catch block
|
135 |
e.printStackTrace(); |
136 |
} |
137 |
} |
138 |
|
139 |
} |
140 |
|
141 |
|
142 |
|
143 |
break;
|
144 |
|
145 |
case "CHUNK": //GETCHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF> |
146 |
Chunk gotChunk = new Chunk(fileID, chunkNo, body);
|
147 |
peer.getStorage().addChunk(gotChunk); |
148 |
if(gotChunk.getContent().length < 64000) |
149 |
peer.lastChunk(); |
150 |
break;
|
151 |
|
152 |
case "REMOVED": |
153 |
Random rand = new Random(); |
154 |
int n = rand.nextInt(400) + 1; |
155 |
|
156 |
|
157 |
try {
|
158 |
Thread.sleep(n);
|
159 |
} catch (InterruptedException e) { |
160 |
e.printStackTrace(); |
161 |
} |
162 |
peer.getStorage().getBackUps().remove(new BackUpInfo(fileID, chunkNo, peer.getPeerID()));
|
163 |
break;
|
164 |
|
165 |
case "DELETE": |
166 |
//peer.getStorage().getFileInfo().remove(new FileInfo(fileID, peer.getFileInfo.getDateModified(), FileInfo.getFilename(), peer.getPeerID()));
|
167 |
break;
|
168 |
} |
169 |
} |
170 |
static byte[] trim(byte[] bytes) |
171 |
{ |
172 |
int i = bytes.length - 1; |
173 |
while (i >= 0 && bytes[i] == 0) |
174 |
{ |
175 |
--i; |
176 |
} |
177 |
|
178 |
return Arrays.copyOf(bytes, i + 1); |
179 |
} |
180 |
|
181 |
|
182 |
} |
183 |
|
184 |
|
185 |
|