-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientHandler.java
More file actions
146 lines (129 loc) · 5.91 KB
/
ClientHandler.java
File metadata and controls
146 lines (129 loc) · 5.91 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
package ue08_tcp;
import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class ClientHandler implements Comparable<ClientHandler> {
public static final Charset CHARSET = Charset.forName("ISO-8859-1");
private final Socket cltSocket;
private BufferedWriter out = null;
private BufferedReader in = null;
private String username;
private static final String STZ_DAVOR = new String(new byte[]{0x0b, 0x1b, '[', '1', 'A', 0x1b, '7', 0x1b, '[', '1', 'L', '\r'});
private static final String STZ_DANACH = new String(new byte[]{0x1b, '8', 0x1b, '[', '1', 'B'});
public static boolean closed = false;
public static final Pattern statPattern = Pattern.compile("stat((\\d)*([+-]))?");
public ClientHandler(Socket cltSocket) {
this.cltSocket = cltSocket;
new Thread(this::handleClient).start();
}
//log
public ClientHandler(Socket cltSocket, String username) {
this.cltSocket = cltSocket;
this.username = username;
}
private void handleClient() {
try {
out = new BufferedWriter(new OutputStreamWriter(cltSocket.getOutputStream(), CHARSET));
in = new BufferedReader(new InputStreamReader(cltSocket.getInputStream(), CHARSET));
this.sendMessage("Willkommen beim Chat-Server der 3CI\r\n\r\nUm die Verbindung zu beenden gib quit ein.\r\n\r\nWelchen Spitznamen moechtest du haben: ", false);
while (this.getUsername() == null) {
String temp = in.readLine().trim();
if (!Server.getUsernames().contains(temp) && !temp.trim().equals("")) {
this.username = temp;
} else {
this.sendMessage("Dieser Nickname ist entweder schon vergeben oder nicht erlaubt!\r\n" +
"Anderer Nickname: ", false);
}
}
Map<String, Integer> tempMap = Server.getMessageCount();
tempMap.put(this.username, 0);
Server.setMessageCount(tempMap);
Server.sendJoinMessage("\"" + this.getUsername() + "\" hat den Raum betreten.", this);
System.out.println("New User: \"" + username + "\" connected! (IP: " + cltSocket.getInetAddress().getHostAddress() + "; Port: " + cltSocket.getPort() + ")");
while (!closed) {
out.write(this.username + ">");
out.flush();
String temp = in.readLine();
Matcher matcher = statPattern.matcher(temp);
if (temp.equals("quit")) {
out.write("Bye!\n");
out.flush();
cltSocket.close();
Server.sendServerMessage("\"" + this.getUsername() + "\" hat den Raum verlassen.");
break;
} else if (matcher.matches()) {
//TODO - not working
if (matcher.group(1) != null) {
if (matcher.group(3).equals("+")) {
Map<String, Integer> sorted = Server.getMessageCount()
.entrySet().stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new
));
out.write(Arrays.toString(Arrays.copyOfRange(sorted.entrySet().toArray(), 0, Integer.min(Integer.parseInt(matcher.group(2)), Server.getMessageCount().keySet().size()))) + "\n");
} else if (matcher.group(3).equals("-")) {
Map<String, Integer> sorted = Server.getMessageCount()
.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new
));
out.write(Arrays.toString(Arrays.copyOfRange(sorted.entrySet().toArray(), 0, Integer.min(Integer.parseInt(matcher.group(2)), Server.getMessageCount().keySet().size()))) + "\n");
}
} else {
out.write(Arrays.toString(Server.getMessageCount().entrySet().toArray()) + "\n");
}
out.flush();
} else {
Server.sendMessageToAll(this, temp);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int compareTo(ClientHandler other) {
return username.compareToIgnoreCase(other.username);
}
public String getUsername() {
return username;
}
public void sendMessage(String message, boolean isMessage) {
try {
if (isMessage) {
out.write(STZ_DAVOR + message + STZ_DANACH);
} else {
out.write(message);
}
out.flush();
} catch (
IOException e) {
try {
cltSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void disconnect() {
try {
closed = true;
this.in.close();
this.out.close();
this.cltSocket.close();
Server.removeClient(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}