-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.java
231 lines (183 loc) · 6.21 KB
/
Server.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Server {
private ServerSocket serverSocket;
private List<Client> clients;
// der server hat fŸr jeden client genau einen writer, Ÿber den
// er den clients nachrichten senden kann
private Map<Client, PrintWriter> writers = null;
private class ConnectionHandler implements Runnable {
private Client client;
private PrintWriter writer;
/**
* Kommandos: (1) /w username some text here (2) /infos username (3)
* /ignore username (4) /unignore username
*/
private void handleCommandResponse(String command) {
String header = "";
String body = "";
// '/' entfernen
header = command.substring(1);
if (header.startsWith("infos")) {
body = header.substring("infos".length() + 1);
for (Client client : clients) {
if (client.getNickname().equals(body)) {
String infoString = (client.getNickname()+"\n"+client.getVorname()+" "+client.getNachname()+"\n"+client.getEmail()+"\n");
// nachricht an den user senden, der den request geschickt hat
writers.get(this.client).println(infoString);
}
}
}
if (header.startsWith("w")) {
body = header.substring("w".length() + 1);
// username und message extrahieren
String username = "";
String message = "";
for (int i = 0; i < body.length(); i++) {
if (body.charAt(i) == ' ') {
username = body.substring(0, i);
message = body.substring(i+1);
break;
}
}
// message an adressierten user senden
for (Client client : clients) {
if (client.getNickname().equals(username) && !client.getIgnoreList().contains(this.client))
writers.get(client).println(message);
}
}
if (header.startsWith("ignore")) {
body = header.substring("ignore".length() + 1);
for (Client client : clients) {
if (client.getNickname().equals(body)) {
List<Client> ignoreList = this.client.getIgnoreList();
if (!ignoreList.contains(client))
ignoreList.add(client);
}
}
}
if (header.startsWith("unignore")) {
body = header.substring("unignore".length() + 1);
for (Client client : clients) {
if (client.getNickname().equals(body)) {
List<Client> ignoreList = this.client.getIgnoreList();
if (ignoreList.contains(client))
ignoreList.remove(client);
}
}
}
//writers.get(client).println("Dieses Kommando gibt es nicht...");
}
public ConnectionHandler(Client client, PrintWriter writer) {
this.client = client;
this.writer = writer;
}
@Override
public void run() {
handleConversation(client);
}
public void handleConversation(Client client) {
BufferedReader bufferedReader = null;
try {
// Reader initialisieren um Nachrichten vom Client auslesen zu
// kšnnen
bufferedReader = new BufferedReader(new InputStreamReader(
client.getSocket().getInputStream()));
// Blockieren, bis Client alle Infos rŸbergeschickt hat
client.setNickname(bufferedReader.readLine());
client.setVorname(bufferedReader.readLine());
client.setNachname(bufferedReader.readLine());
client.setEmail(bufferedReader.readLine());
// Horchen, ob Client etwas sendet
String prefix = (client.getNickname() + ": ");
String message = "";
while ((message = bufferedReader.readLine()) != null) {
// Falls user manuell eine "disconnect" Nachricht sendet
// wird er entfernt
if (message.equals("disconnect")) {
System.out
.println("Habe Disconnect Nachricht erhalten: "
+ client.getNickname());
break;
}
// Command-Nachrichten sepparat behandeln und versenden
if (message.charAt(0) == '/') {
handleCommandResponse(message);
} else {
// Textnachrichte an alle anderen clients weitersenden
for (Client otherClient : clients) {
if (!otherClient.equals(client)) {
if (!otherClient.getIgnoreList().contains(
client)) {
writers.get(otherClient).println(
prefix + message);
}
}
}
}
// Nachricht auf der Konsole des Servers ausgeben
System.out.println(prefix + message);
}
} catch (IOException e) {
System.out.println("Fehler beim abhorchen des clients: "
+ e.getMessage());
} finally {
// Verbindungen schlie§en und client vom Server lšschen
try {
clients.get(clients.indexOf(client)).getSocket().close();
clients.remove(client);
System.out.println("User wurde entfernt. Anzahl: "
+ clients.size());
// Die Reader / Writer mŸssen UNBEDINGT nach dem socket
// geschlossen werden,
// da sie die sockets sonst frŸhzeitig SCHLIESSEN (=>
// Exception)
writers.get(client).close();
writers.remove(writer);
bufferedReader.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
public Server(int port) throws IOException {
this.serverSocket = new ServerSocket(port);
System.out.println("Server Socket wurde erfolgreich gešffnet.");
this.clients = new ArrayList<Client>();
this.writers = new HashMap<Client, PrintWriter>();
}
public void run() {
System.out.println("Warte darauf, dass Clients connecten...");
while (true) {
try {
Client client = new Client("", "", "", "");
Socket socket = serverSocket.accept();
client.setSocket(socket);
clients.add(client);
// Jeder Client bekommt einen eigenen writer, Ÿber den ihm der
// server
// Nachrichten zusenden kann
PrintWriter writer = new PrintWriter(client.getSocket()
.getOutputStream(), true);
writers.put(client, writer);
writer.println("Der Server hat dich hinzugefuegt.");
writer.println("send_infos_to_server");
System.out.println("Neuer Client connected. Anzahl: "
+ clients.size());
// fŸr jede Verbindung einen eigenen Thread eršffnen
new Thread(new ConnectionHandler(client, writer)).start();
} catch (IOException e) {
System.out.println("Fehler beim accepten.");
}
}
}
}