-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
183 lines (151 loc) · 6.09 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
package ue08_tcp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/*
Philipp Theurer
3CI
4/2/19
*/
public class Server {
private static int server_port = 10_023;
private static Set<ClientHandler> connectedClients = new HashSet<>();
private static Map<String, Integer> messageCount = new LinkedHashMap<>();
public static void main(String[] args) {
try {
// Scanner scanner = new Scanner(System.in);
// System.out.println("Enter Port: [Leave blank for default (10023)]");
//TODO uncomment for port selection
// String temp = scanner.nextLine();
// if (!temp.isEmpty()) {
// server_port = Integer.parseInt(temp);
// }
ServerSocket srvSocket = new ServerSocket(server_port);
System.out.println("Server started on Port: " + server_port);
new Thread(() -> {
try {
while (true) {
ClientHandler chatClientHandler = new ClientHandler(srvSocket.accept());
addClient(chatClientHandler);
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
int seconds = 30;
System.out.println("Enter \"quit\" if you want to quit!");
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String input;
while ((input = console.readLine()) != null) {
if (input.equals("quit")) {
System.out.println("Beginning Shutdown!");
sendServerMessage("Server is shutting down in 30 seconds!");
for (int i = 0; i < 6; i++) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Shutting down in " + seconds + " seconds!");
seconds -= 5;
}
System.out.println("Shutting down now!");
sendServerMessage("Bye!");
for (ClientHandler connectedClient : connectedClients) {
connectedClient.disconnect();
}
srvSocket.close();
} else if (input.equals("list")) {
System.out.print("Connected Clients: ");
for (ClientHandler client : connectedClients) {
System.out.print(client.getUsername() + "; ");
}
System.out.println();
} else if (input.equals("stat")) {
System.out.println(Arrays.toString(messageCount.entrySet().toArray()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean addClient(ClientHandler clientHandler) {
synchronized (connectedClients) {
return connectedClients.add(clientHandler);
}
}
public static void removeClient(ClientHandler clientHandler) {
synchronized (connectedClients) {
connectedClients.remove(clientHandler);
}
}
public static void sendMessageToAll(ClientHandler sourceClient, String message) {
if (!message.isEmpty()) {
Collection<ClientHandler> clients;
synchronized (connectedClients) {
clients = new ArrayList<>(connectedClients);
}
Matcher m = Pattern.compile("(\\w+):(.+)").matcher(message);
if (m.matches()) {
for (ClientHandler clientHandler : clients) {
if (clientHandler.getUsername().matches(m.group(1))) {
sendPM("PM from " + sourceClient.getUsername() + ": " + m.group(2), clientHandler);
}
}
} else {
for (ClientHandler clientHandler1 : clients) {
if (clientHandler1 != sourceClient) {
clientHandler1.sendMessage(sourceClient.getUsername() + ": " + message, true);
} else {
messageCount.put(clientHandler1.getUsername(), messageCount.get(clientHandler1.getUsername()) + 1);
}
}
}
}
}
public static void sendJoinMessage(String message, ClientHandler source) {
if (!message.isEmpty()) {
Collection<ClientHandler> clients;
synchronized (connectedClients) {
clients = new ArrayList<>(connectedClients);
}
for (ClientHandler clientHandler : clients) {
if (clientHandler != source) {
clientHandler.sendMessage(message, true);
}
}
}
}
public static void sendServerMessage(String message) {
if (!message.isEmpty()) {
Collection<ClientHandler> clients;
synchronized (connectedClients) {
clients = new ArrayList<>(connectedClients);
}
for (ClientHandler clientHandler : clients) {
clientHandler.sendMessage(message, true);
}
}
}
public static void sendPM(String message, ClientHandler dest) {
if (!message.isEmpty()) {
dest.sendMessage(message, true);
}
}
public static Collection<String> getUsernames() {
synchronized (connectedClients) {
return connectedClients.stream().map(ClientHandler::getUsername).collect(Collectors.toList());
}
}
public static Map<String, Integer> getMessageCount() {
return messageCount;
}
public static void setMessageCount(Map<String, Integer> messageCount) {
Server.messageCount = messageCount;
}
}