-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGerenciadorDeClientes.java
More file actions
52 lines (41 loc) · 1.87 KB
/
GerenciadorDeClientes.java
File metadata and controls
52 lines (41 loc) · 1.87 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
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
public class GerenciadorDeClientes {
private Map<String, PrintWriter> clientes = new HashMap<>();
public synchronized void adicionarCliente(String nome, PrintWriter cliente) {
this.clientes.put(nome, cliente);
System.out.println("Gerenciador: " + nome + " adicionado. Total: " + clientes.size());
}
public synchronized void removerCliente(String nome) {
if (nome != null) {
this.clientes.remove(nome);
System.out.println("Gerenciador: " + nome + " removido. Total: " + clientes.size());
}
}
public synchronized void enviarBroadcast(String mensagem, String remetenteNome) {
for (Map.Entry<String, PrintWriter> entrada : clientes.entrySet()) {
String nomeDestino = entrada.getKey();
PrintWriter escritorDestino = entrada.getValue();
if (!nomeDestino.equals(remetenteNome)) {
escritorDestino.println(mensagem);
}
}
}
public synchronized void enviarMensagemPrivada(String destinatario, String mensagem, String remetente) {
PrintWriter escritorDestino = clientes.get(destinatario);
if (escritorDestino != null) {
escritorDestino.println("[Privado de " + remetente + "]: " + mensagem);
} else {
PrintWriter escritorRemetente = clientes.get(remetente);
if (escritorRemetente != null) {
escritorRemetente.println("Erro: Usuario " + destinatario + " nao encontrado.");
}
}
}
public synchronized String getListaConectados() {
int total = clientes.size();
String nomes = String.join(", ", clientes.keySet());
return "Total de " + total + " conectados: [ " + nomes + " ]";
}
}