sdis1819-t7g02 / channels / MessageHeader.java @ 1
History | View | Annotate | Download (5.01 KB)
1 | 1 | up20150366 | package channels; |
---|---|---|---|
2 | |||
3 | import java.util.ArrayList; |
||
4 | |||
5 | import service.Constants; |
||
6 | |||
7 | public class MessageHeader { |
||
8 | private String messageType; |
||
9 | private String version; |
||
10 | private String senderId; |
||
11 | private String fileId; |
||
12 | private String chunkNumber; |
||
13 | private String degree; |
||
14 | |||
15 | public MessageHeader(byte[] msg, int length) { |
||
16 | |||
17 | byte[] header = getHeaderPart(msg, length); |
||
18 | |||
19 | String headerString = new String(header); |
||
20 | |||
21 | String[] headerElements = headerString.split("\\s+"); |
||
22 | |||
23 | if(!analyseHeader(headerElements)) {
|
||
24 | throw new IllegalArgumentException("Minimum header arguments not matched"); |
||
25 | } |
||
26 | } |
||
27 | |||
28 | public MessageHeader(String[] headerComponents) { |
||
29 | if(!analyseHeader(headerComponents)) {
|
||
30 | throw new IllegalArgumentException("Minimum header arguments not matched"); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | @Override
|
||
35 | public String toString() { |
||
36 | if(messageType.equals(Constants.PUTCHUNK)) {
|
||
37 | return messageType + " " + version + " " + senderId + " " + fileId + " " + chunkNumber + " " + degree; |
||
38 | } |
||
39 | else {
|
||
40 | return messageType + " " + version + " " + senderId + " " + fileId + " " + chunkNumber; |
||
41 | } |
||
42 | } |
||
43 | |||
44 | public String getMessageType() { |
||
45 | return messageType;
|
||
46 | } |
||
47 | |||
48 | private void setMessageType(String messageType) { |
||
49 | this.messageType = messageType;
|
||
50 | } |
||
51 | |||
52 | public String getVersion() { |
||
53 | return version;
|
||
54 | } |
||
55 | |||
56 | private void setVersion(String version) { |
||
57 | this.version = version;
|
||
58 | } |
||
59 | |||
60 | public String getSenderId() { |
||
61 | return senderId;
|
||
62 | } |
||
63 | |||
64 | private boolean setSenderId(String senderId) { |
||
65 | try {
|
||
66 | Integer.parseInt(senderId);
|
||
67 | this.senderId = senderId;
|
||
68 | } catch(Exception e) { |
||
69 | return false; |
||
70 | } |
||
71 | |||
72 | return true; |
||
73 | } |
||
74 | |||
75 | public String getFileId() { |
||
76 | return fileId;
|
||
77 | } |
||
78 | |||
79 | private void setFileId(String fileId) { |
||
80 | this.fileId = fileId;
|
||
81 | } |
||
82 | |||
83 | public String getChunkNumber() { |
||
84 | return chunkNumber;
|
||
85 | } |
||
86 | |||
87 | private void setChunkNumber(String chunkNumber) { |
||
88 | this.chunkNumber = chunkNumber;
|
||
89 | } |
||
90 | |||
91 | public String getDegree() { |
||
92 | if(degree == null) { |
||
93 | return "1"; |
||
94 | } |
||
95 | return degree;
|
||
96 | } |
||
97 | |||
98 | private void setDegree(String degree) { |
||
99 | this.degree = degree;
|
||
100 | } |
||
101 | |||
102 | private byte[] getHeaderPart(byte[] msg, int length) { |
||
103 | byte[] headerPart = new byte[length]; |
||
104 | |||
105 | for(int i = 0; i < length; i++) { |
||
106 | headerPart[i] = msg[i]; |
||
107 | } |
||
108 | |||
109 | return headerPart;
|
||
110 | } |
||
111 | |||
112 | public ArrayList<Byte> toBytes() { |
||
113 | ArrayList<Byte> headerBytes = new ArrayList<Byte>(); |
||
114 | Byte space = new String(" ").getBytes()[0]; |
||
115 | |||
116 | for(Byte b : getMessageType().getBytes()) { |
||
117 | headerBytes.add(b); |
||
118 | } |
||
119 | |||
120 | headerBytes.add(space); |
||
121 | |||
122 | for(Byte b : getVersion().getBytes()) { |
||
123 | headerBytes.add(b); |
||
124 | } |
||
125 | |||
126 | headerBytes.add(space); |
||
127 | |||
128 | for(Byte b : getSenderId().getBytes()) { |
||
129 | headerBytes.add(b); |
||
130 | } |
||
131 | |||
132 | headerBytes.add(space); |
||
133 | |||
134 | for(Byte b : getFileId().getBytes()) { |
||
135 | headerBytes.add(b); |
||
136 | } |
||
137 | |||
138 | headerBytes.add(space); |
||
139 | |||
140 | if(!messageType.equals(Constants.DELETE)) {
|
||
141 | for(Byte b : getChunkNumber().getBytes()) { |
||
142 | headerBytes.add(b); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | headerBytes.add(space); |
||
147 | |||
148 | if(messageType.equals(Constants.PUTCHUNK)) {
|
||
149 | for(Byte b : getDegree().getBytes()) { |
||
150 | headerBytes.add(b); |
||
151 | } |
||
152 | } |
||
153 | headerBytes.add(space); |
||
154 | |||
155 | headerBytes.add(Constants.CR); |
||
156 | headerBytes.add(Constants.LF); |
||
157 | headerBytes.add(Constants.CR); |
||
158 | headerBytes.add(Constants.LF); |
||
159 | |||
160 | return headerBytes;
|
||
161 | } |
||
162 | |||
163 | public byte[] headerToBytes() { |
||
164 | ArrayList<Byte> headerBytes = toBytes(); |
||
165 | byte[] toReturn = new byte[headerBytes.size()]; |
||
166 | int i = 0; |
||
167 | |||
168 | for(Byte b: headerBytes) { |
||
169 | toReturn[i] = b; |
||
170 | i++; |
||
171 | } |
||
172 | |||
173 | return toReturn;
|
||
174 | } |
||
175 | |||
176 | private boolean analyseHeader(String[] h) { |
||
177 | |||
178 | switch(h[0]) { |
||
179 | case Constants.PUTCHUNK:
|
||
180 | if(h.length != Constants.PUTCHUNK_N_ARGS) {
|
||
181 | return false; |
||
182 | } |
||
183 | setMessageType(h[0]);
|
||
184 | setVersion(h[1]);
|
||
185 | if(!setSenderId(h[2])) { |
||
186 | return false; |
||
187 | } |
||
188 | setFileId(h[3]);
|
||
189 | setChunkNumber(h[4]);
|
||
190 | setDegree(h[5]);
|
||
191 | break;
|
||
192 | case Constants.STORED:
|
||
193 | if(h.length != Constants.STORED_N_ARGS) {
|
||
194 | return false; |
||
195 | } |
||
196 | setMessageType(h[0]);
|
||
197 | setVersion(h[1]);
|
||
198 | if(!setSenderId(h[2])) { |
||
199 | return false; |
||
200 | } |
||
201 | setFileId(h[3]);
|
||
202 | setChunkNumber(h[4]);
|
||
203 | break;
|
||
204 | case Constants.GETCHUNK:
|
||
205 | if(h.length != Constants.GETCHUNK_N_ARGS) {
|
||
206 | return false; |
||
207 | } |
||
208 | setMessageType(h[0]);
|
||
209 | setVersion(h[1]);
|
||
210 | if(!setSenderId(h[2])) { |
||
211 | return false; |
||
212 | } |
||
213 | setFileId(h[3]);
|
||
214 | setChunkNumber(h[4]);
|
||
215 | break;
|
||
216 | case Constants.CHUNK:
|
||
217 | if(h.length != Constants.CHUNK_N_ARGS) {
|
||
218 | return false; |
||
219 | } |
||
220 | setMessageType(h[0]);
|
||
221 | setVersion(h[1]);
|
||
222 | if(!setSenderId(h[2])) { |
||
223 | return false; |
||
224 | } |
||
225 | setFileId(h[3]);
|
||
226 | setChunkNumber(h[4]);
|
||
227 | break;
|
||
228 | case Constants.DELETE:
|
||
229 | if(h.length != Constants.DELETE_N_ARGS) {
|
||
230 | return false; |
||
231 | } |
||
232 | setMessageType(h[0]);
|
||
233 | setVersion(h[1]);
|
||
234 | if(!setSenderId(h[2])) { |
||
235 | return false; |
||
236 | } |
||
237 | setFileId(h[3]);
|
||
238 | break;
|
||
239 | case Constants.REMOVED:
|
||
240 | if(h.length != Constants.REMOVED_N_ARGS) {
|
||
241 | return false; |
||
242 | } |
||
243 | setMessageType(h[0]);
|
||
244 | setVersion(h[1]);
|
||
245 | if(!setSenderId(h[2])) { |
||
246 | return false; |
||
247 | } |
||
248 | setFileId(h[3]);
|
||
249 | setChunkNumber(h[4]);
|
||
250 | break;
|
||
251 | default:
|
||
252 | return false; |
||
253 | } |
||
254 | |||
255 | return true; |
||
256 | } |
||
257 | } |