mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 19:15:35 +08:00
update 将桩与连接channel的关系改为一对多
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
package com.jsowell.common.enums.ykc;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.jsowell.common.util.StringUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelId;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
@@ -18,6 +24,9 @@ public class PileChannelEntity {
|
||||
* 管理一个全局map,保存连接进服务端的通道数量
|
||||
*/
|
||||
private static final ConcurrentHashMap<String, ChannelHandlerContext> manager = new ConcurrentHashMap<>();
|
||||
|
||||
// 桩号--channelId 一对多
|
||||
public static final ConcurrentHashMap<String, List<ChannelHandlerContext>> pileMap = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 校验channel是否保存
|
||||
@@ -49,6 +58,38 @@ public class PileChannelEntity {
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkChannelV2(String pileSn, ChannelHandlerContext ctx) {
|
||||
List<ChannelHandlerContext> list = pileMap.get(pileSn);
|
||||
// 如果该桩当前没有保存过该channel,则进行保存
|
||||
if (!list.contains(ctx)) {
|
||||
list.add(ctx);
|
||||
pileMap.put(pileSn, list);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除某台桩的某个channel连接
|
||||
* @param pileSn
|
||||
* @param ctx
|
||||
*/
|
||||
public static void deleteChannel(String pileSn, ChannelHandlerContext ctx) {
|
||||
String channelId = ctx.channel().id().asLongText();
|
||||
// 从map中删除该ctx
|
||||
List<ChannelHandlerContext> list = pileMap.get(pileSn);
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return;
|
||||
}
|
||||
for (ChannelHandlerContext channelHandlerContext : list) {
|
||||
String id = channelHandlerContext.channel().id().asLongText();
|
||||
if (StringUtils.equals(id, channelId)) {
|
||||
// 传来的channelId与已保存的channel中有一致的,进行删除
|
||||
list.remove(channelHandlerContext);
|
||||
// 同时关闭连接
|
||||
channelHandlerContext.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过桩编号获取channel链接信息
|
||||
* @param pileSn
|
||||
|
||||
Reference in New Issue
Block a user