package com.zy.acs.gateway.cache; import com.alibaba.fastjson.JSON; import io.netty.channel.Channel; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * Channel缓存 ==>> { * key: agv编号 * value: Channel * } * Created by vincent on 2019-04-02 */ @Slf4j @RestController public class ChannelCache { private static final Map channelGroup = new ConcurrentHashMap<>(); public static void setChannel(String uniqueno, Channel channel){ // todo 缓存标记 // String hostName = SystemProperties.HOST_NAME; if (getChannel(uniqueno) == channel){ return; } removeChannel(uniqueno); channelGroup.put(uniqueno, channel); } public static Boolean hasChannel(String uniqueno){ return channelGroup.get(uniqueno) != null; } public static Channel getChannel(String uniqueno){ return channelGroup.get(uniqueno); } public static void removeChannel(String uniqueno) { Channel channel = getChannel(uniqueno); if (null == channel){ return; } channelGroup.remove(uniqueno); channel.close(); } public static String removeChannel(Channel channel){ String key = null; for (Map.Entry entry : channelGroup.entrySet()){ if (entry.getValue() == channel){ key = entry.getKey(); break; } } if (null != key){ channelGroup.remove(key); return key; } return null; } @GetMapping("/channel/all") public String getAllCache(@RequestParam(required = false)String type, @RequestParam String pwd){ if ("xltys1995".equals(pwd)){ if (null != type && "info".equals(type)){ return JSON.toJSONString(channelGroup); } Map map = new HashMap<>(); map.put("channels", channelGroup.keySet().toString()); map.put("count", channelGroup.size()); return JSON.toJSONString(map); } return "请输入密码"; } }