root / src / Message.java
History | View | Annotate | Download (6.29 KB)
1 |
import java.io.BufferedReader; |
---|---|
2 |
import java.io.ByteArrayInputStream; |
3 |
import java.io.IOException; |
4 |
import java.io.InputStreamReader; |
5 |
|
6 |
public class Message { |
7 |
|
8 |
public enum MessageType { |
9 |
PUTCHUNK, STORED, GETCHUNK, CHUNK, DELETE, REMOVED |
10 |
} |
11 |
|
12 |
public static String CRLF = "\r\n"; |
13 |
public static String SPACE = " "; |
14 |
|
15 |
// Header
|
16 |
private MessageType type = null; |
17 |
private Integer senderID = null; |
18 |
private String protocolVersion = null; |
19 |
private String fileID = null; |
20 |
private Integer chunkNo = null; |
21 |
private byte[] message; |
22 |
private Integer replicationDegree = null; |
23 |
|
24 |
// Body
|
25 |
private byte[] body = null; |
26 |
|
27 |
public byte[] getBody() { |
28 |
return body;
|
29 |
} |
30 |
public void setBody(byte[] body){ |
31 |
this.body=body;
|
32 |
} |
33 |
public void setMessageType(MessageType mgs){ |
34 |
this.type=mgs;
|
35 |
} |
36 |
public void setSenderId(Integer id){ |
37 |
this.senderID=id;
|
38 |
} |
39 |
|
40 |
public int getReplicationDegree(){ |
41 |
return replicationDegree;
|
42 |
} |
43 |
public void setReplicationDegree(int rep){ |
44 |
this.replicationDegree=rep;
|
45 |
} |
46 |
public String getProtocolVersion() { |
47 |
return protocolVersion;
|
48 |
} |
49 |
public void setProtocolVersion(String pv) { |
50 |
this.protocolVersion=pv;
|
51 |
} |
52 |
|
53 |
|
54 |
public Integer getSenderID() { |
55 |
return senderID;
|
56 |
} |
57 |
|
58 |
public String getFileID() { |
59 |
return fileID;
|
60 |
} |
61 |
public void setFileID(String fileId) { |
62 |
this.fileID=fileId;
|
63 |
} |
64 |
|
65 |
|
66 |
public Integer getChunkNo() { |
67 |
return chunkNo;
|
68 |
} |
69 |
public void setChunkNo(Integer no) { |
70 |
this.chunkNo= no;
|
71 |
} |
72 |
|
73 |
public MessageType getType(){
|
74 |
return this.type; |
75 |
} |
76 |
public byte[] getMessage(){ |
77 |
return message;
|
78 |
} |
79 |
|
80 |
// Processes a message from DatagramPacket (handles received messages)
|
81 |
public Message(byte[] message, int length) throws Exception{ |
82 |
|
83 |
this.message=message;
|
84 |
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(message); |
85 |
|
86 |
BufferedReader reader = new BufferedReader( |
87 |
new InputStreamReader(arrayInputStream)); |
88 |
|
89 |
String header = ""; |
90 |
|
91 |
try {
|
92 |
header = reader.readLine(); |
93 |
} catch (IOException e) { |
94 |
e.printStackTrace(); |
95 |
} |
96 |
if(header == "" ) { |
97 |
System.out.println("[ERROR] Message header badly formed!"); |
98 |
return;
|
99 |
} |
100 |
|
101 |
String[] headerSplit = header.split("\\s+"); |
102 |
|
103 |
this.protocolVersion = headerSplit[1]; //tem de ser igual a versao do cliente |
104 |
this.senderID = Integer.parseInt(headerSplit[2]); |
105 |
this.fileID = headerSplit[3]; |
106 |
|
107 |
|
108 |
switch (headerSplit[0]){ |
109 |
case "PUTCHUNK": |
110 |
if (headerSplit.length != 6) { |
111 |
System.out.println("[ERROR] No. of header fields does not follow <PUTCHUNK> message type."); |
112 |
return;
|
113 |
} |
114 |
this.chunkNo = Integer.parseInt(headerSplit[4]); |
115 |
this.type = MessageType.PUTCHUNK;
|
116 |
this.replicationDegree = Integer.parseInt(headerSplit[5]); |
117 |
break;
|
118 |
case "STORED": |
119 |
if (headerSplit.length != 5) { |
120 |
System.out.println("[ERROR] No. of header fields does not follow <STORE> message type."); |
121 |
return;
|
122 |
} |
123 |
this.chunkNo = Integer.parseInt(headerSplit[4]); |
124 |
this.type = MessageType.STORED;
|
125 |
break;
|
126 |
case "GETCHUNK": |
127 |
if (headerSplit.length != 5) { |
128 |
System.out.println("[ERROR] No. of header fields does not follow <GETCHUNK> message type."); |
129 |
return;
|
130 |
} |
131 |
this.chunkNo = Integer.parseInt(headerSplit[4]); |
132 |
this.type = MessageType.GETCHUNK;
|
133 |
break;
|
134 |
case "CHUNK": |
135 |
if (headerSplit.length != 5) { |
136 |
System.out.println("[ERROR] No. of header fields does not follow <CHUNK> message type."); |
137 |
return;
|
138 |
} |
139 |
this.chunkNo = Integer.parseInt(headerSplit[4]); |
140 |
this.type = MessageType.CHUNK;
|
141 |
break;
|
142 |
case "DELETE": |
143 |
if (headerSplit.length != 4) { |
144 |
System.out.println("[ERROR] No. of header fields does not follow <DELETE> message type."); |
145 |
return;
|
146 |
} |
147 |
this.type = MessageType.DELETE;
|
148 |
break;
|
149 |
case "REMOVED": |
150 |
if (headerSplit.length != 5) { |
151 |
System.out.println("[ERROR] No. of header fields does not follow <REMOVED> message type."); |
152 |
return;
|
153 |
} |
154 |
this.chunkNo = Integer.parseInt(headerSplit[4]); |
155 |
this.type = MessageType.REMOVED;
|
156 |
break;
|
157 |
default:
|
158 |
System.out.println("[ERROR] Not a valid message type."); |
159 |
} |
160 |
if(this.type == MessageType.PUTCHUNK || this.type == MessageType.CHUNK){ |
161 |
int bodyLength = length - header.length() - 4; |
162 |
this.body = new byte[bodyLength]; |
163 |
System.arraycopy(message, header.length()+4, this.body, 0, bodyLength); |
164 |
} |
165 |
} |
166 |
|
167 |
public String msgTypeToString(MessageType msgType){ |
168 |
switch(msgType){
|
169 |
case PUTCHUNK:
|
170 |
return "PUTCHUNK"; |
171 |
case STORED:
|
172 |
return "STORED"; |
173 |
case GETCHUNK:
|
174 |
return "GETCHUNK"; |
175 |
case CHUNK:
|
176 |
return "CHUNK"; |
177 |
case DELETE:
|
178 |
return "DELETE"; |
179 |
case REMOVED:
|
180 |
return "REMOVED"; |
181 |
default:
|
182 |
break;
|
183 |
} |
184 |
return ""; |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public String toString(){ |
189 |
StringBuilder string = new StringBuilder(); |
190 |
string.append(msgTypeToString(this.type)).append(SPACE).append(this.protocolVersion).append(SPACE).append(this.senderID).append(SPACE).append(this.fileID).append(SPACE); |
191 |
if (this.replicationDegree != null){ |
192 |
string.append(this.replicationDegree).append(SPACE);
|
193 |
} |
194 |
if (this.chunkNo != null){ |
195 |
string.append(this.chunkNo);
|
196 |
} |
197 |
string.append(CRLF+CRLF); |
198 |
if(this.body != null) |
199 |
string.append(new String(this.body)); |
200 |
|
201 |
return string.toString();
|
202 |
} |
203 |
|
204 |
|
205 |
} |