root / DistributedBackupService / src / handlers / Message.java @ 2
History | View | Annotate | Download (5.29 KB)
1 | 1 | up20130859 | package handlers; |
---|---|---|---|
2 | |||
3 | import utils.Utils; |
||
4 | import utils.Utils.MessageType; |
||
5 | |||
6 | import java.io.*; |
||
7 | |||
8 | public class Message { |
||
9 | |||
10 | private int argsNo; |
||
11 | |||
12 | private int chunkNo; |
||
13 | private int senderID; |
||
14 | private String fileID; |
||
15 | private String version; |
||
16 | private int replication; |
||
17 | private MessageType msgType;
|
||
18 | |||
19 | private byte [] body; |
||
20 | |||
21 | public Message(byte[] information, int length) { |
||
22 | String header;
|
||
23 | String headerWithoutStuff;
|
||
24 | String [] splittedHeader; |
||
25 | |||
26 | header= readHeader(information); |
||
27 | |||
28 | headerWithoutStuff = header.trim().replaceAll("\\s+", " "); |
||
29 | splittedHeader = headerWithoutStuff.split("\\s+");
|
||
30 | translateHeader(splittedHeader); |
||
31 | |||
32 | if (msgType == MessageType.CHUNK)
|
||
33 | this.body = readBody(information, header.length(), length);
|
||
34 | |||
35 | if(msgType == MessageType.PUTCHUNK)
|
||
36 | this.body = readBody(information, header.length(), length);
|
||
37 | } |
||
38 | |||
39 | public Message(MessageType type, String[] args) { |
||
40 | this.msgType = type;
|
||
41 | version = args[0];
|
||
42 | senderID = Integer.parseInt(args[1]); |
||
43 | fileID = args[2];
|
||
44 | |||
45 | if (type != MessageType.DELETE)
|
||
46 | chunkNo = Integer.parseInt(args[3]); |
||
47 | |||
48 | if (type == MessageType.PUTCHUNK) {
|
||
49 | replication = Integer.parseInt(args[4]); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | public Message(MessageType type, String[] args, byte[] information) { |
||
54 | this(type, args);
|
||
55 | body = information; |
||
56 | } |
||
57 | |||
58 | private String readHeader(byte[] information) { |
||
59 | ByteArrayInputStream stream;
|
||
60 | BufferedReader reader;
|
||
61 | String header = ""; |
||
62 | |||
63 | stream = new ByteArrayInputStream(information); |
||
64 | reader = new BufferedReader(new InputStreamReader(stream)); |
||
65 | |||
66 | try {
|
||
67 | header = reader.readLine(); |
||
68 | } catch (IOException e) { |
||
69 | e.printStackTrace(); |
||
70 | } |
||
71 | return header;
|
||
72 | } |
||
73 | |||
74 | private byte[] readBody(byte[] information, int headerLength, int informationLength) { |
||
75 | int readBytes;
|
||
76 | ByteArrayInputStream message;
|
||
77 | byte[] bodyContent; |
||
78 | |||
79 | readBytes = informationLength - headerLength - 4;
|
||
80 | message = new ByteArrayInputStream(information, headerLength + 4, readBytes); |
||
81 | bodyContent = new byte[readBytes]; |
||
82 | |||
83 | message.read(bodyContent, 0, readBytes);
|
||
84 | |||
85 | return bodyContent;
|
||
86 | } |
||
87 | |||
88 | private void translateHeader(String[] headerSplit) { |
||
89 | switch (headerSplit[0]) { |
||
90 | case "PUTCHUNK": |
||
91 | { |
||
92 | msgType = MessageType.PUTCHUNK; |
||
93 | argsNo = 6;
|
||
94 | } break;
|
||
95 | case "STORED": |
||
96 | { |
||
97 | msgType = MessageType.STORED; |
||
98 | argsNo = 5;
|
||
99 | } |
||
100 | break;
|
||
101 | case "GETCHUNK": |
||
102 | { |
||
103 | msgType = MessageType.GETCHUNK; |
||
104 | argsNo = 5;
|
||
105 | } break;
|
||
106 | case "CHUNK": |
||
107 | { msgType = MessageType.CHUNK; |
||
108 | argsNo = 5;
|
||
109 | } break;
|
||
110 | case "DELETE": |
||
111 | { |
||
112 | msgType = MessageType.DELETE; |
||
113 | argsNo = 4;
|
||
114 | } break;
|
||
115 | case "REMOVED": |
||
116 | { |
||
117 | msgType = MessageType.REMOVED; |
||
118 | argsNo = 5;
|
||
119 | } break;
|
||
120 | default:
|
||
121 | return;
|
||
122 | } |
||
123 | |||
124 | if (headerSplit.length != argsNo)
|
||
125 | return;
|
||
126 | |||
127 | version = headerSplit[1];
|
||
128 | senderID = Integer.parseInt(headerSplit[2]); |
||
129 | fileID = headerSplit[3];
|
||
130 | |||
131 | if (argsNo > 4) |
||
132 | chunkNo = Integer.parseInt(headerSplit[4]); |
||
133 | |||
134 | if (msgType == MessageType.PUTCHUNK)
|
||
135 | replication = Integer.parseInt(headerSplit[5]); |
||
136 | |||
137 | } |
||
138 | |||
139 | public String headerString() { |
||
140 | String string;
|
||
141 | if(msgType == MessageType.PUTCHUNK)
|
||
142 | string = msgType + " " + version + " " + senderID + " " + fileID + " " + chunkNo + " " + replication + " " + Utils.CRLF + Utils.CRLF; |
||
143 | else if(msgType == MessageType.DELETE) |
||
144 | string = msgType + " " + version + " " + senderID + " " + fileID + " " + Utils.CRLF + Utils.CRLF; |
||
145 | else
|
||
146 | string = msgType + " " + version + " " + senderID + " " + fileID + " " + chunkNo + " " + Utils.CRLF + Utils.CRLF; |
||
147 | return string;
|
||
148 | } |
||
149 | |||
150 | public byte[] getBytes() throws IOException { |
||
151 | byte header[]; |
||
152 | header = headerString().getBytes(); |
||
153 | |||
154 | if (body == null) |
||
155 | return header;
|
||
156 | else {
|
||
157 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||
158 | out.write(header); |
||
159 | out.write(body); |
||
160 | return out.toByteArray();
|
||
161 | } |
||
162 | } |
||
163 | |||
164 | 2 | up20130859 | @Override
|
165 | public String toString() { |
||
166 | String str;
|
||
167 | if(msgType == MessageType.PUTCHUNK)
|
||
168 | { |
||
169 | str = "<" + msgType +">" + " " + "<" + version+">" + " " + "<" + senderID+">" + " " +"<" + fileID+">" + " " + "<" +chunkNo+">"; |
||
170 | } |
||
171 | else if(msgType == MessageType.DELETE) |
||
172 | { |
||
173 | str = "<" + msgType +">" + " " + "<" + version+">" + " " + "<" + senderID+">" + " " +"<" + fileID+">"; |
||
174 | } |
||
175 | else
|
||
176 | { |
||
177 | str = "<" + msgType +">" + " " + "<" + version+">" + " " + "<" + senderID+">" + " " +"<" + fileID+">" + " " + "<" +chunkNo+">"; |
||
178 | } |
||
179 | return str;
|
||
180 | } |
||
181 | |||
182 | // Getters and Setters
|
||
183 | |||
184 | 1 | up20130859 | public String getVersion() { |
185 | return version;
|
||
186 | } |
||
187 | |||
188 | public int getSenderID() { |
||
189 | return senderID;
|
||
190 | } |
||
191 | |||
192 | public String getFileID() { |
||
193 | return fileID;
|
||
194 | } |
||
195 | |||
196 | public int getChunkNo() { |
||
197 | return chunkNo;
|
||
198 | } |
||
199 | |||
200 | public int getReplicationDegree() { |
||
201 | return replication;
|
||
202 | } |
||
203 | |||
204 | public byte[] getBody() { |
||
205 | return body;
|
||
206 | } |
||
207 | |||
208 | public MessageType getType() {
|
||
209 | return msgType;
|
||
210 | } |
||
211 | |||
212 | 2 | up20130859 | public int getArgsNo() { |
213 | return argsNo;
|
||
214 | 1 | up20130859 | } |
215 | |||
216 | 2 | up20130859 | public int getReplication() { |
217 | return replication;
|
||
218 | } |
||
219 | |||
220 | public MessageType getMsgType() {
|
||
221 | return msgType;
|
||
222 | } |
||
223 | |||
224 | public void setArgsNo(int argsNo) { |
||
225 | this.argsNo = argsNo;
|
||
226 | } |
||
227 | |||
228 | public void setChunkNo(int chunkNo) { |
||
229 | this.chunkNo = chunkNo;
|
||
230 | } |
||
231 | |||
232 | public void setSenderID(int senderID) { |
||
233 | this.senderID = senderID;
|
||
234 | } |
||
235 | |||
236 | public void setFileID(String fileID) { |
||
237 | this.fileID = fileID;
|
||
238 | } |
||
239 | |||
240 | public void setVersion(String version) { |
||
241 | this.version = version;
|
||
242 | } |
||
243 | |||
244 | public void setReplication(int replication) { |
||
245 | this.replication = replication;
|
||
246 | } |
||
247 | |||
248 | public void setMsgType(MessageType msgType) { |
||
249 | this.msgType = msgType;
|
||
250 | } |
||
251 | |||
252 | public void setBody(byte[] body) { |
||
253 | this.body = body;
|
||
254 | } |
||
255 | |||
256 | 1 | up20130859 | } |