root / src / Message / Message.java @ 2
History | View | Annotate | Download (5.55 KB)
1 | 2 | up20160340 | package Message; |
---|---|---|---|
2 | |||
3 | |||
4 | import java.io.BufferedReader; |
||
5 | import java.io.ByteArrayInputStream; |
||
6 | import java.io.IOException; |
||
7 | import java.io.InputStreamReader; |
||
8 | import java.net.DatagramPacket; |
||
9 | |||
10 | import static Global.Globals.*; |
||
11 | |||
12 | public class Message { |
||
13 | |||
14 | protected MessageType type;
|
||
15 | protected String version; |
||
16 | protected int senderId; |
||
17 | protected String fileID; |
||
18 | protected Integer chunkNo; |
||
19 | protected Integer repDegree; |
||
20 | protected byte[] body; |
||
21 | |||
22 | /**
|
||
23 | * @param type Message.Message type
|
||
24 | * @param version
|
||
25 | * @param senderId
|
||
26 | * @param fileID
|
||
27 | * @param chunkNo
|
||
28 | * @param repDegree
|
||
29 | * @param body
|
||
30 | */
|
||
31 | public Message(MessageType type, String version, int senderId, String fileID, Integer chunkNo, Integer repDegree, byte[] body) { |
||
32 | this.type = type;
|
||
33 | this.version = version;
|
||
34 | this.senderId = senderId;
|
||
35 | this.fileID = fileID;
|
||
36 | this.chunkNo = chunkNo;
|
||
37 | this.repDegree = repDegree;
|
||
38 | this.body = body;
|
||
39 | |||
40 | } |
||
41 | |||
42 | /**
|
||
43 | * Translate UDP datagram packet
|
||
44 | *
|
||
45 | * @param message Message.Message that comes fr
|
||
46 | * om UDP Datagram
|
||
47 | */
|
||
48 | public Message(DatagramPacket message) { |
||
49 | |||
50 | if (message.getData().length == 0 || message.getLength() == 0) { |
||
51 | return;
|
||
52 | } |
||
53 | int messageLength = message.getLength();
|
||
54 | byte[] data = message.getData(); |
||
55 | byte[] header = new byte[messageLength]; |
||
56 | System.arraycopy(data, 0, header, 0, messageLength); |
||
57 | ByteArrayInputStream getHeader = new ByteArrayInputStream(header); |
||
58 | |||
59 | BufferedReader buffer = new BufferedReader(new InputStreamReader(getHeader)); |
||
60 | |||
61 | try {
|
||
62 | String head = buffer.readLine();
|
||
63 | int size = head.length() + 4; |
||
64 | |||
65 | |||
66 | this.body = new byte[messageLength - size]; |
||
67 | System.arraycopy(data, size, this.body, 0, messageLength - size); |
||
68 | |||
69 | |||
70 | String[] l = head.split(" "); |
||
71 | ms(l); |
||
72 | |||
73 | this.version = l[1]; |
||
74 | this.senderId = Integer.parseInt(l[2]); |
||
75 | this.fileID = l[3]; |
||
76 | |||
77 | |||
78 | } catch (IOException e) { |
||
79 | //e.printStackTrace();
|
||
80 | } |
||
81 | |||
82 | } |
||
83 | |||
84 | void ms(String[] header) { |
||
85 | |||
86 | switch (header[0]) { |
||
87 | case "PUTCHUNK": |
||
88 | this.type = MessageType.PUTCHUNK;
|
||
89 | this.chunkNo = Integer.parseInt(header[4]); |
||
90 | this.repDegree = Integer.parseInt(header[5]); |
||
91 | break;
|
||
92 | case "STORED": |
||
93 | this.type = MessageType.STORED;
|
||
94 | this.chunkNo = Integer.parseInt(header[4]); |
||
95 | this.repDegree = null; |
||
96 | |||
97 | break;
|
||
98 | case "REMOVED": |
||
99 | this.type = MessageType.REMOVED;
|
||
100 | this.chunkNo = Integer.parseInt(header[4]); |
||
101 | this.repDegree = null; |
||
102 | break;
|
||
103 | case "GETCHUNK": |
||
104 | this.type = MessageType.GETCHUNK;
|
||
105 | this.chunkNo = Integer.parseInt(header[4]); |
||
106 | this.repDegree = null; |
||
107 | |||
108 | break;
|
||
109 | case "DELETE": |
||
110 | this.type = MessageType.DELETE;
|
||
111 | this.chunkNo = null; |
||
112 | this.repDegree = null; |
||
113 | break;
|
||
114 | case "CHUNK": |
||
115 | this.type = MessageType.CHUNK;
|
||
116 | this.chunkNo = Integer.parseInt(header[4]); |
||
117 | this.repDegree = null; |
||
118 | break;
|
||
119 | default:
|
||
120 | System.out.println("Something wrong"); |
||
121 | |||
122 | return;
|
||
123 | } |
||
124 | |||
125 | } |
||
126 | |||
127 | |||
128 | public byte[] messagePacket() { |
||
129 | try {
|
||
130 | String message = ""; |
||
131 | String type = typeToString(this.type); |
||
132 | String senderId = Integer.toString(this.senderId); |
||
133 | message += type + " " + this.version + " " + senderId + " " + this.fileID; |
||
134 | String chunkNo;
|
||
135 | |||
136 | |||
137 | if (this.chunkNo != null) { |
||
138 | chunkNo = (this.chunkNo).toString();
|
||
139 | message += " " + chunkNo;
|
||
140 | } |
||
141 | |||
142 | if (this.repDegree != null) |
||
143 | message += " " + this.repDegree; |
||
144 | message += " " + CRLF + CRLF;
|
||
145 | |||
146 | byte[] messageByte = message.getBytes(); |
||
147 | byte[] result; |
||
148 | |||
149 | if (this.body != null) { |
||
150 | result = new byte[messageByte.length + this.body.length]; |
||
151 | System.arraycopy(messageByte, 0, result, 0, messageByte.length); |
||
152 | System.arraycopy(this.body, 0, result, messageByte.length, this.body.length); |
||
153 | |||
154 | return result;
|
||
155 | } |
||
156 | |||
157 | return messageByte;
|
||
158 | } catch (Exception e) { |
||
159 | // e.printStackTrace();
|
||
160 | } |
||
161 | return null; |
||
162 | } |
||
163 | |||
164 | |||
165 | String typeToString(MessageType type) {
|
||
166 | |||
167 | switch (type) {
|
||
168 | case PUTCHUNK:
|
||
169 | return "PUTCHUNK"; |
||
170 | case STORED:
|
||
171 | return "STORED"; |
||
172 | case REMOVED:
|
||
173 | return "REMOVED"; |
||
174 | case GETCHUNK:
|
||
175 | return "GETCHUNK"; |
||
176 | case DELETE:
|
||
177 | return "DELETE"; |
||
178 | case CHUNK:
|
||
179 | return "CHUNK"; |
||
180 | default:
|
||
181 | System.out.println("Something wrong"); |
||
182 | |||
183 | return "NULL"; |
||
184 | } |
||
185 | |||
186 | } |
||
187 | |||
188 | public int getSenderId() { |
||
189 | return senderId;
|
||
190 | } |
||
191 | |||
192 | public Integer getChunkNo() { |
||
193 | return chunkNo;
|
||
194 | } |
||
195 | |||
196 | public Integer getRepDegree() { |
||
197 | return repDegree;
|
||
198 | } |
||
199 | |||
200 | public byte[] getBody() { |
||
201 | return body;
|
||
202 | } |
||
203 | |||
204 | public String getVersion() { |
||
205 | return version;
|
||
206 | } |
||
207 | |||
208 | public String getFileID() { |
||
209 | return fileID;
|
||
210 | } |
||
211 | |||
212 | public MessageType getType() {
|
||
213 | return type;
|
||
214 | } |
||
215 | } |