-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageParser.java
More file actions
338 lines (296 loc) · 11.3 KB
/
Copy pathMessageParser.java
File metadata and controls
338 lines (296 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package Chat;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringEscapeUtils;
/**
* The message parser for a specified socket
*
* @author joar
*/
public class MessageParser implements Runnable {
private Controller controller;
private InputStreamReader input;
private String message;
private Conversation conversation;
private Socket socket;
private Connection connection;
private boolean accepted;
/**
* Creates a message parser for a specified socket
*
* @param socket the specified socket
* @param conversation the conversation to add messages to
* @param controller the controller
* @param connection the client/server connection
* @param accepted if the client has been accepted by the server to the
* conversation
* @throws IOException
*/
public MessageParser(Socket socket, Conversation conversation,
Controller controller, Connection connection, boolean accepted)
throws IOException {
this.accepted = accepted;
this.socket = socket;
this.input = new InputStreamReader(socket.getInputStream(), "UTF-8");
this.controller = controller;
this.conversation = conversation;
this.connection = connection;
}
@Override
public void run() {
while (true) {
String name = getName();
if (!name.equals("")) {
connection.setName(socket, name);
}
if (message == null) {
conversation.addInfo("Lost connection with: "
+ connection.getName(socket));
connection.sockets.remove(socket);
break;
} else if (hasTags("", Constants.DISCONNECT)) {
conversation.addInfo(connection.getName(socket)
+ " has loged off");
try {
connection.sendDisconnect(socket, name, controller.name());
} catch (Exception ex) {
Logger.getLogger(MessageParser.class.getName())
.log(Level.SEVERE, null, ex);
}
connection.sockets.remove(socket);
break;
} else if (!accepted) {
if (hasTags(Constants.REQUEST, Constants.REQUEST_STOP)) {
parseJoinRequest();
controller.handelJoinRequest(socket, message, false);
} else {
controller.handelJoinRequest(socket,
"A primitive chat application is trying to connect",
true);
}
accepted = true;
continue;
}
if (hasTags(Constants.ENCRYPTED_TYPE,
Constants.ENCRYPTED_STOP)) {
if (has("key")) {
encryptionStart();
} else {
decodeEncryption();
}
}
if (message.equals(Constants.BROKEN_ENCRYPTION)) {
conversation.addMessage(message,
connection.getName(socket), "");
} else if (hasTags(Constants.REQUEST_ANS, Constants.REQUEST_STOP)) {
String ans = parseJoinReply();
conversation.addInfo("Answer to join request: " + ans);
} else if (hasTags(Constants.KEY_REQUEST,
Constants.KEY_REQUEST_STOP)) {
parseKeyRequest();
} else if (hasTags(Constants.KEY_RESPONSE,
Constants.KEY_RESPONSE_STOP)) {
parseKeyResponse();
} else if (hasTags(Constants.FILE_REQUEST_NAME,
Constants.FILE_REQUEST_STOP)) {
parseFileRequest();
} else if (hasTags(Constants.FILE_RESPONES,
Constants.FILE_RESPONSE_STOP)) {
parseFileResponse();
} else {
if (message.equals(Constants.BROKEN_ENCRYPTION)) {
conversation.addMessage(message,
connection.getName(socket), "");
continue;
}
String color = getColor();
removeTags(Constants.BOLD_START, Constants.BOLD_STOP);
removeTags(Constants.KURSIVE_START, Constants.KURSIVE_STOP);
removeUnknownTags();
conversation.addMessage(message, name, color);
if (connection.multiConversation) {
try {
connection.sendOtherClients(
socket, StringEscapeUtils.unescapeHtml3(message),
name, color);
} catch (Exception ex) {
}
}
}
}
}
private String getName() {
StringBuilder tmpMessage = new StringBuilder();
message = null;
try {
int tmpChar = input.read();
while (tmpChar != -1) {
tmpMessage.append((char) tmpChar);
message = tmpMessage.toString();
//no name
if (hasTags(Constants.MESSAGE_START, Constants.MESSAGE_STOP)) {
removeTags(Constants.MESSAGE_START, Constants.MESSAGE_STOP);
return "";
} //name
else if (hasTags(Constants.MESSAGE_NAME,
Constants.MESSAGE_STOP)) {
String name = getAtribute("name");
removeTags(Constants.MESSAGE_NAME, Constants.MESSAGE_STOP);
splitFirst(">");
return name;
} else if (message.contains(Constants.MESSAGE_STOP)) {
message = Constants.BROKEN;
return "";
}
tmpChar = input.read();
}
} catch (Exception e) {
message = null;
}
return "";
}
private String getColor() {
if (hasTags(Constants.TEXT_START, Constants.TEXT_STOP)) {
removeTags(Constants.TEXT_START, Constants.TEXT_STOP);
return "";
} else if (hasTags(Constants.TEXT_COLOR, Constants.TEXT_STOP)) {
String color = getAtribute("color");
removeTags(Constants.TEXT_COLOR, Constants.TEXT_STOP);
splitFirst(">");
return color;
} else {
message = Constants.BROKEN;
return "";
}
}
private boolean has(String atribute) {
return message.contains(atribute)
&& message.indexOf(atribute) < message.indexOf(">");
}
private String getAtribute(String atribute) {
atribute = atribute + "=";
if (message.contains(atribute)
&& message.indexOf(atribute) < message.indexOf(">")) {
int index = message.indexOf(atribute) + atribute.length();
int end;
if (message.indexOf(" ", index) != -1
&& message.indexOf(" ", index) < message.indexOf(">", index)) {
end = message.indexOf(" ", index);
} else {
end = message.indexOf(">", index);
}
if (index == end) {
return "";
} else {
String name = message.substring(index, end);
return name;
}
} else {
return "";
}
}
private String splitFirst(String str) {
int index = message.indexOf(str);
if (index == -1) {
message = Constants.BROKEN;
return "";
}
String name = message.substring(0, index);
message = message.substring(index + str.length());
return name;
}
private void removeUnknownTags() {
while (message.indexOf("<") == 0) {
String tag;
if (has(" ")) {
tag = splitFirst(" ");
} else {
tag = splitFirst(">");
}
splitFirst("/" + tag + ">");
}
if (message.contains("<")) {
message = splitFirst("<");
}
}
private void removeTags(String startTag, String endTag) {
int index = message.indexOf(endTag);
if (hasTags(startTag, endTag)) {
message = message.substring(startTag.length(), index);
}
}
private boolean hasTags(String startTag, String endTag) {
return message.startsWith(startTag) && message.contains(endTag)
&& message.indexOf(endTag)
+ endTag.length() == message.length();
}
private void parseKeyRequest() {
String type = getAtribute("type");
removeTags(Constants.KEY_REQUEST, Constants.KEY_REQUEST_STOP);
splitFirst(">");
controller.keyRequest(socket, message, connection.getName(socket), type);
}
private void parseKeyResponse() {
String type = getAtribute("type");
String key = getAtribute("key");
removeTags(Constants.KEY_RESPONSE, Constants.KEY_RESPONSE_STOP);
splitFirst(">");
controller.handleKeyReply(socket, type, key);
}
private void parseFileRequest() {
String fileName = getAtribute("name");
String size = getAtribute("size");
removeTags(Constants.FILE_REQUEST_NAME, Constants.FILE_REQUEST_STOP);
splitFirst(">");
controller.handleFileRequest(socket, message, fileName, size);
}
private void parseFileResponse() {
String answer = getAtribute("reply");
String port = getAtribute("port");
String type = getAtribute("type");
String key = getAtribute("key");
removeTags(Constants.FILE_RESPONES, Constants.FILE_RESPONSE_STOP);
splitFirst(">");
controller.handleFileResponse(socket, message, answer,
port, type, key);
}
private void encryptionStart() {
try {
String type = getAtribute("type");
String key = getAtribute("key");
removeTags(Constants.ENCRYPTED_TYPE, Constants.ENCRYPTED_STOP);
splitFirst(">");
connection.setCrypto(socket, type);
Crypto crypto = connection.getCrypto(socket);
crypto.setKey(key);
message = crypto.decodeMessage(message);
conversation.addInfo("A new encryption of type: "
+ crypto.getType() + " initiated by: "
+ connection.getName(socket));
} catch (Exception ex) {
message = Constants.BROKEN_ENCRYPTION;
}
}
private void decodeEncryption() {
try {
removeTags(Constants.ENCRYPTED_TYPE, Constants.ENCRYPTED_STOP);
splitFirst(">");
Crypto crypto = connection.getCrypto(socket);
message = crypto.decodeMessage(message);
} catch (Exception ex) {
message = Constants.BROKEN_ENCRYPTION;
}
}
private void parseJoinRequest() {
removeTags(Constants.REQUEST, Constants.REQUEST_STOP);
}
private String parseJoinReply() {
String ans = getAtribute("ans");
removeTags(Constants.REQUEST_ANS, Constants.REQUEST_STOP);
splitFirst(">");
return ans;
}
}