package com.dashitech.callcenter.webSocket.core;

import org.atmosphere.cpr.AtmosphereResource;
import org.jboss.netty.util.internal.ConcurrentHashMap;


import java.util.Map;

/**
 * WebSocket缓存类
 * Created by CX on 2018/8/9 0009.
 */
public class SessionCache {
    /**
     * 关键字缓存
     */
    private final ConcurrentHashMap<String, AtmosphereResource> indentityCache = new ConcurrentHashMap<String, AtmosphereResource>();

    /**
     * 所有浏览器的缓存，如果有需要可以扩展
     */
//    private final ConcurrentHashMap<String, AtmosphereResource> allCache = new ConcurrentHashMap<String, AtmosphereResource>();
    public SessionCache() {
    }

    public ConcurrentHashMap<String, AtmosphereResource> getIndentityCache() {
        return indentityCache;
    }

    public AtmosphereResource get(String identity) {
        return this.indentityCache.get(identity);
    }

    public boolean contains(String identity) {
        return this.indentityCache.containsKey(identity);
    }

    public void put(String identity, AtmosphereResource atmosphereResource) {
        this.indentityCache.put(identity, atmosphereResource);
    }

    public AtmosphereResource remove(String identity) {
        return indentityCache.remove(identity);
    }

    public AtmosphereResource removeByUUID(String uuid) {
        for (Map.Entry<String, AtmosphereResource> entry : indentityCache.entrySet()) {
            if (entry.getValue().uuid().equals(uuid)) {
                return indentityCache.remove(entry.getKey());
            }
        }
        return null;
    }


}
