package com.willalp.control.netty.model;

import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;

/**
 * @author hs
 */
public class DeviceChannelMap {

    private static Map<String, ChannelMsgModel> map = new ConcurrentHashMap<>();

    public static void add(String clientId, ChannelMsgModel channelMsgModel) {
        map.put(clientId, channelMsgModel);
    }

    public static ChannelMsgModel get(String clientId) {
        return map.get(clientId);
    }

    public static Map<String, ChannelMsgModel> getAll() {
        return map;
    }

    public static void remove(Channel socketChannel) {
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == socketChannel) {
                map.remove(entry.getKey());
            }
        }
    }

    public static void remove(String key){
        map.remove(key);
    }

    public static void clear(){
        map.clear();
    }




    private static Map<String, Channel> channelMap = new ConcurrentHashMap<>();
    public static void setChannel(String key, Channel channel){
        channelMap.put(key,channel);
    }

    public static Channel getChannel(String key){
        return channelMap.get(key);
    }

    private static Map<String,Lock> channelLockMap = new ConcurrentHashMap<>();
    public static void setChannelLock(String key,Lock lock){
        channelLockMap.put(key,lock);
    }

    public static Lock getChannelLock(String key){
        return channelLockMap.get(key);
    }
}
