前言
1.临时客户端池
用于存放未经过身份验证的客户端。
package edu.xupt.cs.core;
import java.util.LinkedList;
import java.util.List;
public class TempClientPool {
private List<ServerConversation> clients;
{
clients = new LinkedList<>();
}
public void addClient(ServerConversation client) {
if (!clients.contains(client)) {
clients.add(client);
}
}
public boolean removeClient(ServerConversation client) {
return clients.remove(client);
}
public ServerConversation popClient() {
int index = getClientCount() - 1;
return clients.remove(index);
}
public int getClientCount() {
return clients.size();
}
public boolean isEmpty() {
return clients.isEmpty();
}
}
2.客户端池
存放经过验证的客户端。
package edu.xupt.cs.core;
import java.util.*;
public class ClientPool {
private final Map<String,ServerConversation> clientPool;
{
clientPool = new HashMap<String,ServerConversation>();
}
public void addClient(ServerConversation client) {
String id = client.getId();
if (clientPool.containsKey(id)) {
return;
}
clientPool.put(id,client);
}
public void removeClient(String id) {
clientPool.remove(id);
}
public List<ServerConversation> clientList() {
List<ServerConversation> clients = new LinkedList<>();
for (String id : clientPool.keySet()) {
clients.add(clientPool.get(id));
}
return clients;
}
public List<ServerConversation> clientsExceptOne(String fireClientId) {
List<ServerConversation> clients = new LinkedList<>();
for (Map.Entry<String, ServerConversation> entry : clientPool.entrySet()) {
if (entry.getKey().equalsIgnoreCase(fireClientId)) {
continue;
}
clients.add(entry.getValue());
}
return clients;
}
public ServerConversation getClient(String id) {
return clientPool.get(id);
}
public int getClientCount() {
return clientPool.size();
}
}