-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
117 lines (102 loc) · 3.93 KB
/
ChatServer.java
File metadata and controls
117 lines (102 loc) · 3.93 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
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ChatServer {
private static HashMap<String, String> users = new HashMap<String, String>();
private static Set<PrintWriter> writers = new HashSet<>();
public static void main(String[] args) throws Exception {
System.out.println("O servidor está rodando...");
ExecutorService pool = Executors.newFixedThreadPool(500);
try (ServerSocket listener = new ServerSocket(59898)) {
while (true) {
pool.execute(new Handler(listener.accept()));
}
}
}
private static class Handler implements Runnable {
private String name;
private String color;
private Socket socket;
private Scanner in;
private PrintWriter out;
public Handler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream(), true);
// Fica fazendo requisições de nome até receber um nome único.
while (true) {
out.println("SUBMITNAME");
name = in.nextLine();
if (name == null || name.isEmpty()) {
out.println("SUBMITNAME");
return;
}
synchronized (users) {
if (!name.isEmpty() && users.get(name) == null) {
users.put(name, null);
break;
}
}
}
// Fica fazendo requisições de cor até receber uma.
while (true) {
out.println("USERCOLOR");
color = in.nextLine();
if (color == null) {
return;
} else {
users.put(name, color);
break;
}
}
// Identifica que o usuário foi aceito com sucesso.
out.println("NAMEACCEPTED " + name);
// Escreve para todos os clientes que o usuário entrou no chat.
for (PrintWriter writer : writers) {
Color c = Color.BLACK;
writer.println("MESSAGE;"+ c.getRGB() + "; >>>>" + name + ";" + " entrou no chat.");
}
writers.add(out);
// Transmite as mensagens para os usuários
while (true) {
String input = in.nextLine();
if (input.toLowerCase().startsWith("/quit")) {
return;
}
for (PrintWriter writer : writers) {
writer.println("MESSAGE;"+ color + ";" + name + ": " + ";" + input);
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
if (out != null) {
writers.remove(out);
}
if (name != null) {
System.out.println(name + " is leaving");
users.remove(name);
for (PrintWriter writer : writers) {
Color c = Color.BLACK;
writer.println("MESSAGE;"+ c.getRGB() + "; >>>>" + name + ";" + " saiu do chat.");
}
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}