【Java框架】CSFreamwork:客户端池

前言

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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值