root / proj / src / MessageParser.java @ 2
History | View | Annotate | Download (5.98 KB)
1 | 1 | up20160559 | import java.io.IOException; |
---|---|---|---|
2 | import java.io.UnsupportedEncodingException; |
||
3 | import java.net.DatagramPacket; |
||
4 | import java.net.UnknownHostException; |
||
5 | 2 | up20160559 | import java.nio.charset.Charset; |
6 | 1 | up20160559 | import java.nio.charset.StandardCharsets; |
7 | import java.util.Arrays; |
||
8 | import java.util.Random; |
||
9 | import java.util.concurrent.TimeUnit; |
||
10 | |||
11 | public class MessageParser implements Runnable { |
||
12 | |||
13 | Peer peer; |
||
14 | DatagramPacket packet;
|
||
15 | |||
16 | public MessageParser(DatagramPacket packet, Peer peer) { |
||
17 | this.peer = peer;
|
||
18 | this.packet = packet;
|
||
19 | |||
20 | } |
||
21 | |||
22 | public void run() { |
||
23 | |||
24 | 2 | up20160559 | |
25 | String str = null; |
||
26 | |||
27 | str = new String(packet.getData(),0, packet.getLength()); |
||
28 | |||
29 | |||
30 | 1 | up20160559 | String rest = null, fileID = null, messageType = null, version = null; |
31 | int i = 0, next = 0, chunkNo = 0, replicationDeg = 0, senderID=0; |
||
32 | 2 | up20160559 | byte[] body = null; |
33 | 1 | up20160559 | |
34 | if(str.contains(" ")){ |
||
35 | i= str.indexOf(" ");
|
||
36 | messageType= str.substring(0,i);
|
||
37 | next = str.indexOf(" ", i+1); |
||
38 | version = str.substring(i,next).replace(" ",""); |
||
39 | i = next; |
||
40 | next = str.indexOf(" ", i+1); |
||
41 | senderID = Integer.parseInt(str.substring(i,next).replace(" ","")); |
||
42 | i = next; |
||
43 | next = str.indexOf(" ", i+1); |
||
44 | fileID = str.substring(i,next).replace(" ",""); |
||
45 | i = next; |
||
46 | next = str.indexOf(" ",i+1); |
||
47 | chunkNo = Integer.parseInt(str.substring(i,next).replace(" ","")); |
||
48 | i = next; |
||
49 | next = str.indexOf(" ",i+1); |
||
50 | replicationDeg = Integer.parseInt(str.substring(i,next).replace(" ","")); |
||
51 | rest = str.substring(next); |
||
52 | } |
||
53 | |||
54 | |||
55 | if(peer.getPeerID() == senderID) {
|
||
56 | return;
|
||
57 | } |
||
58 | |||
59 | |||
60 | String CRLF = "" + (char) 0xD + (char) 0xA; |
||
61 | |||
62 | String bodyString = rest.replaceFirst(CRLF+CRLF,""); |
||
63 | |||
64 | 2 | up20160559 | bodyString = bodyString.replaceFirst(" ", ""); |
65 | 1 | up20160559 | |
66 | 2 | up20160559 | |
67 | body = bodyString.getBytes(); |
||
68 | |||
69 | 1 | up20160559 | |
70 | |||
71 | 2 | up20160559 | |
72 | 1 | up20160559 | switch(messageType) {
|
73 | |||
74 | case "PUTCHUNK": |
||
75 | |||
76 | Chunk chunk = new Chunk(fileID, chunkNo, body);
|
||
77 | 2 | up20160559 | chunk.setGoalRepDeg(replicationDeg); |
78 | 1 | up20160559 | if(peer.getStorage().addChunk(chunk)) {
|
79 | |||
80 | Message stored = new Message("STORED",peer.getVersion(),peer.getPeerID(),fileID, chunkNo, 0, null); |
||
81 | byte[] reply = stored.sendable(); |
||
82 | |||
83 | try {
|
||
84 | |||
85 | Random rand = new Random(); |
||
86 | int n = rand.nextInt(400) + 1; |
||
87 | |||
88 | |||
89 | try {
|
||
90 | Thread.sleep(n);
|
||
91 | } catch (InterruptedException e) { |
||
92 | e.printStackTrace(); |
||
93 | } |
||
94 | |||
95 | peer.getMC().sendMessage(reply); |
||
96 | System.out.println(stored.messageToStringPrintable());
|
||
97 | } catch (UnknownHostException e) { |
||
98 | // TODO Auto-generated catch block
|
||
99 | e.printStackTrace(); |
||
100 | } |
||
101 | |||
102 | try {
|
||
103 | peer.saveChunks(); |
||
104 | } catch (IOException e) { |
||
105 | // TODO Auto-generated catch block
|
||
106 | e.printStackTrace(); |
||
107 | } |
||
108 | |||
109 | } |
||
110 | break;
|
||
111 | |||
112 | |||
113 | case "STORED": |
||
114 | peer.getStorage().getBackUps().add(new BackUpInfo(fileID, chunkNo, peer.getPeerID()));
|
||
115 | 2 | up20160559 | peer.getStorage().increaseChunkRepDeg(fileID, chunkNo); |
116 | 1 | up20160559 | |
117 | |||
118 | break;
|
||
119 | |||
120 | case "GETCHUNK": //CHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF><Body> |
||
121 | |||
122 | 2 | up20160559 | Random rand = new Random(); |
123 | int n = rand.nextInt(400) + 1; |
||
124 | |||
125 | |||
126 | try {
|
||
127 | Thread.sleep(n);
|
||
128 | } catch (InterruptedException e) { |
||
129 | e.printStackTrace(); |
||
130 | } |
||
131 | |||
132 | 1 | up20160559 | for(Chunk chunkSend: peer.getStorage().getChunks()) {
|
133 | |||
134 | if(chunkSend.getFileID().equals(fileID) && chunkSend.getChunkN()==chunkNo) {
|
||
135 | |||
136 | |||
137 | |||
138 | Message sendChunk = new Message("CHUNK",peer.getVersion(),peer.getPeerID(),fileID, chunkNo, 0, chunkSend.getContent()); |
||
139 | 2 | up20160559 | |
140 | 1 | up20160559 | byte[] reply = sendChunk.sendable(); |
141 | System.out.println(sendChunk.messageToStringPrintable());
|
||
142 | 2 | up20160559 | |
143 | 1 | up20160559 | |
144 | try {
|
||
145 | peer.getMDR().sendMessage(reply); |
||
146 | } catch (UnknownHostException e) { |
||
147 | // TODO Auto-generated catch block
|
||
148 | e.printStackTrace(); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | } |
||
153 | |||
154 | |||
155 | |||
156 | break;
|
||
157 | |||
158 | case "CHUNK": //GETCHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF> |
||
159 | Chunk gotChunk = new Chunk(fileID, chunkNo, body);
|
||
160 | 2 | up20160559 | |
161 | 1 | up20160559 | peer.getStorage().addChunk(gotChunk); |
162 | 2 | up20160559 | if(gotChunk.getContent().length < 58000) |
163 | 1 | up20160559 | peer.lastChunk(); |
164 | break;
|
||
165 | |||
166 | case "REMOVED": |
||
167 | 2 | up20160559 | if(peer.getStorage().lowerRepDeg(fileID, chunkNo)) {
|
168 | Random rand2 = new Random(); |
||
169 | int n2 = rand2.nextInt(400) + 1; |
||
170 | 1 | up20160559 | |
171 | |||
172 | try {
|
||
173 | 2 | up20160559 | Thread.sleep(n2);
|
174 | 1 | up20160559 | } catch (InterruptedException e) { |
175 | e.printStackTrace(); |
||
176 | } |
||
177 | 2 | up20160559 | for(Chunk chunkSend: peer.getStorage().getChunks())
|
178 | { |
||
179 | if(chunkSend.getFileID().equals(fileID) && chunkSend.getChunkN() == chunkNo)
|
||
180 | { |
||
181 | boolean stored = false; |
||
182 | |||
183 | Message msg = new Message("PUTCHUNK", version, peer.getPeerID(),chunkSend.getFileID(),chunkNo, chunkSend.getGoalRepDeg()-chunkSend.getRepDeg(), |
||
184 | chunkSend.getContent()); |
||
185 | byte[] msgByte = msg.sendable(); |
||
186 | |||
187 | System.out.println(msg.messageToStringPrintable());
|
||
188 | try {
|
||
189 | peer.getMDB().sendMessage(msgByte); |
||
190 | } catch (UnknownHostException e2) { |
||
191 | // TODO Auto-generated catch block
|
||
192 | e2.printStackTrace(); |
||
193 | } |
||
194 | |||
195 | for(int j = 0; j < 5 && !stored; j++) { |
||
196 | |||
197 | try {
|
||
198 | TimeUnit.SECONDS.sleep(1+j); |
||
199 | } catch (InterruptedException e1) { |
||
200 | // TODO Auto-generated catch block
|
||
201 | e1.printStackTrace(); |
||
202 | } |
||
203 | |||
204 | if(peer.storedCheck(chunkNo, fileID) >= chunkSend.getGoalRepDeg()) {
|
||
205 | stored = true;
|
||
206 | } |
||
207 | else {
|
||
208 | try {
|
||
209 | peer.getMDB().sendMessage(msgByte); |
||
210 | } catch (UnknownHostException e) { |
||
211 | // TODO Auto-generated catch block
|
||
212 | e.printStackTrace(); |
||
213 | } |
||
214 | System.out.println(msg.messageToStringPrintable());
|
||
215 | |||
216 | } |
||
217 | } |
||
218 | stored = false;
|
||
219 | } |
||
220 | |||
221 | } |
||
222 | } |
||
223 | |||
224 | 1 | up20160559 | break;
|
225 | |||
226 | case "DELETE": |
||
227 | 2 | up20160559 | peer.getStorage().deleteChunkByFileID(fileID); |
228 | peer.deleteFileFolder(fileID); |
||
229 | 1 | up20160559 | break;
|
230 | } |
||
231 | } |
||
232 | static byte[] trim(byte[] bytes) |
||
233 | { |
||
234 | int i = bytes.length - 1; |
||
235 | while (i >= 0 && bytes[i] == 0) |
||
236 | { |
||
237 | --i; |
||
238 | } |
||
239 | |||
240 | return Arrays.copyOf(bytes, i + 1); |
||
241 | } |
||
242 | |||
243 | |||
244 | } |
||
245 | |||
246 |