diff --git a/jsowell-common/src/main/java/com/jsowell/common/core/redis/RedisCache.java b/jsowell-common/src/main/java/com/jsowell/common/core/redis/RedisCache.java index 2a4aa935e..c6c95953c 100644 --- a/jsowell-common/src/main/java/com/jsowell/common/core/redis/RedisCache.java +++ b/jsowell-common/src/main/java/com/jsowell/common/core/redis/RedisCache.java @@ -8,7 +8,6 @@ import org.springframework.data.redis.core.*; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Component; -import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.TimeUnit; @@ -354,22 +353,33 @@ public class RedisCache { * * @param pattern 表达式,如:abc*,找出所有以abc开始的键 */ - public Set scan(String pattern) { - return (Set) redisTemplate.execute((RedisCallback>) connection -> { - Set keysTmp = new HashSet<>(); - try (Cursor cursor = connection.scan(new ScanOptions.ScanOptionsBuilder() - .match(pattern) - .count(10000).build())) { + // public Set scan(String pattern) { + // return (Set) redisTemplate.execute((RedisCallback>) connection -> { + // Set keysTmp = new HashSet<>(); + // try (Cursor cursor = connection.scan(new ScanOptions.ScanOptionsBuilder() + // .match(pattern) + // .count(10000).build())) { + // + // while (cursor.hasNext()) { + // keysTmp.add(new String(cursor.next(), StandardCharsets.UTF_8)); + // } + // } catch (Exception e) { + // logger.error(e.getMessage(), e); + // throw new RuntimeException(e); + // } + // return keysTmp; + // }); + // } - while (cursor.hasNext()) { - keysTmp.add(new String(cursor.next(), StandardCharsets.UTF_8)); - } - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw new RuntimeException(e); - } - return keysTmp; - }); + /** + * setnx + * @param key 键 + * @param value 值 + * @param expireTime 过期时间,单位秒 + * @return + */ + public Boolean setnx(String key, String value, long expireTime) { + return redisTemplate.opsForValue().setIfAbsent(key, value, expireTime, TimeUnit.SECONDS); } /** @@ -385,7 +395,7 @@ public class RedisCache { //在一定时间内获取锁,超时则返回错误 for (; ; ) { //Set命令返回OK,则证明获取锁成功 - Boolean ret = redisTemplate.opsForValue().setIfAbsent(lockKey, requestId, expireTime, TimeUnit.SECONDS); + Boolean ret = setnx(lockKey, requestId, expireTime); if (ret != null && ret) { return true; } diff --git a/jsowell-netty/src/main/java/com/jsowell/netty/handler/AbstractHandler.java b/jsowell-netty/src/main/java/com/jsowell/netty/handler/AbstractHandler.java index 8622c9947..ac74245b1 100644 --- a/jsowell-netty/src/main/java/com/jsowell/netty/handler/AbstractHandler.java +++ b/jsowell-netty/src/main/java/com/jsowell/netty/handler/AbstractHandler.java @@ -74,4 +74,19 @@ public abstract class AbstractHandler implements InitializingBean { PileChannelEntity.checkChannel(pileSn, channel); } + /** + * 阻止重复帧 + */ + protected boolean verifyTheDuplicateRequest(YKCDataProtocol ykcDataProtocol, Channel channel) { + // 获取序列号域 + int serialNumber = BytesUtil.bytesToIntLittle(ykcDataProtocol.getSerialNumber()); + // 获取channelId + String channelId = channel.id().asShortText(); + + String redisKey = "Request_" + channelId + "_" + serialNumber; + + + return false; + } + } \ No newline at end of file diff --git a/jsowell-netty/src/main/java/com/jsowell/netty/handler/LoginRequestHandler.java b/jsowell-netty/src/main/java/com/jsowell/netty/handler/LoginRequestHandler.java index fdfb4ef90..9f89984ba 100644 --- a/jsowell-netty/src/main/java/com/jsowell/netty/handler/LoginRequestHandler.java +++ b/jsowell-netty/src/main/java/com/jsowell/netty/handler/LoginRequestHandler.java @@ -7,7 +7,6 @@ import com.jsowell.common.constant.Constants; import com.jsowell.common.core.domain.ykc.LoginRequestData; import com.jsowell.common.core.domain.ykc.YKCDataProtocol; import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode; -import com.jsowell.common.core.redis.RedisCache; import com.jsowell.common.util.BytesUtil; import com.jsowell.common.util.StringUtils; import com.jsowell.common.util.YKCUtils; @@ -30,6 +29,7 @@ import java.util.concurrent.CompletableFuture; @Slf4j @Component public class LoginRequestHandler extends AbstractHandler { + private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.LOGIN_CODE.getBytes()); @Autowired private IPileBasicInfoService pileBasicInfoService; @@ -40,11 +40,6 @@ public class LoginRequestHandler extends AbstractHandler { @Autowired private IPileMsgRecordService pileMsgRecordService; - @Autowired - private RedisCache redisCache; - - private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.LOGIN_CODE.getBytes()); - private List newProgramVersionList = Lists.newArrayList("c6-30"); @Override @@ -111,7 +106,6 @@ public class LoginRequestHandler extends AbstractHandler { @Override public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) { - // log.info("[===执行登录逻辑===] param:{}, channel:{}", JSONObject.toJSONString(ykcDataProtocol), channel.toString()); // 获取消息体 byte[] msgBody = ykcDataProtocol.getMsgBody(); @@ -121,7 +115,6 @@ public class LoginRequestHandler extends AbstractHandler { // 桩编码 byte[] pileSnByte = BytesUtil.copyBytes(msgBody, startIndex, length); String pileSn = BytesUtil.binary(pileSnByte, 16); - // log.info("桩号:{}", pileSn); // 保存时间 saveLastTimeAndCheckChannel(pileSn, channel); @@ -140,7 +133,6 @@ public class LoginRequestHandler extends AbstractHandler { // 通信协议版本 版本号乘 10,v1.0 表示 0x0A startIndex += length; byte[] communicationVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length); - // int i = Integer.parseInt(BytesUtil.bcd2Str(communicationVersionByteArr)); // 0F --> 15 BigDecimal bigDecimal = new BigDecimal(BytesUtil.bcd2Str(communicationVersionByteArr)); BigDecimal communicationVersionTemp = bigDecimal.divide(new BigDecimal(10)); String communicationVersion = "v" + communicationVersionTemp; @@ -170,6 +162,10 @@ public class LoginRequestHandler extends AbstractHandler { byte[] businessTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length); String business = BytesUtil.bcd2Str(businessTypeByteArr); + // *********************** 字段解析完成,下面进行逻辑处理 *********************** // + verifyTheDuplicateRequest(ykcDataProtocol, channel); + + LoginRequestData loginRequestData = LoginRequestData.builder() .pileSn(pileSn) .pileType(pileType)