From d20d38e22b06559d758c568769017e2acf632583 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期三, 23 四月 2025 14:06:48 +0800
Subject: [PATCH] Merge branch 'master' of http://8.140.179.55:20000/r/pipIrr-SV
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/PrefixedDataAvailableHandleImp.java | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 215 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/PrefixedDataAvailableHandleImp.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/PrefixedDataAvailableHandleImp.java
new file mode 100644
index 0000000..3b99830
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/PrefixedDataAvailableHandleImp.java
@@ -0,0 +1,215 @@
+package com.dy.common.mw.protocol;
+
+import java.util.Collection;
+import java.util.HashMap;
+
+import com.dy.common.mw.channel.tcp.TcpIoSessionAttrIdIsRtuAddr;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.mina.core.buffer.IoBuffer;
+
+import com.dy.common.util.ByteUtil;
+import com.dy.common.mw.channel.tcp.PrefixedDataAvailableHandle;
+import com.dy.common.mw.channel.tcp.PrefixedDataAvailableStatus;
+import org.apache.mina.core.session.IoSession;
+
+public class PrefixedDataAvailableHandleImp implements PrefixedDataAvailableHandle {
+
+ private static final Logger log = LogManager.getLogger(PrefixedDataAvailableHandleImp.class) ;
+
+
+ /**
+ * 鍦ㄥ绾跨▼鐜涓繍琛�
+ * 鍒嗘瀽涓婄嚎鏁版嵁(缃戠粶杩炴帴鍚庣涓�鍖呮暟鎹�)鏄惁鍙幏寰�
+ * @param in IoBuffer
+ * @return PrefixedDataAvailableStatus
+ */
+ public PrefixedDataAvailableStatus forOnLine(IoSession ioSession, IoBuffer in){
+ int remain = in.remaining() ;
+ if(remain == 0){
+ return new PrefixedDataAvailableStatus().breaked() ;
+ }else{
+ try{
+ PrefixedDataAvailableStatus pds = null ;
+ PrefixedDataAvailable pda = null ;
+
+ //浠庝細璇濈紦瀛樺緱鍒颁笂娆′笂琛屾暟鎹‘瀹氱殑鍗忚
+ String protocolName = (String) ioSession.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName) ;
+ Short protocolVersion = (Short) ioSession.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolVersion) ;
+ //鐢变細璇濅腑鐨勫崗璁緱鍒板鐞嗙被
+ //鍒╃敤缂撳瓨鐨勫崗璁紝杩涜�屽涓嶇鍚堝崗璁殑涓婅鏁版嵁涔熻兘鐢ㄨ鍗忚澶勭悊浜嗭紝渚嬪RTU閲囩敤涓嶅悓鏁版嵁鍗忚鐨勫祵鍏ュ紡杞欢鍗囩骇鍗忚
+ //鍗砇TU绗竴鍖呮暟鎹‘瀹氫笅鏉ュ崗璁紝鍚庨潰鏁版嵁閮界敤杩欎釜鍗忚澶勭悊锛岄偅涔堟暟鎹彲浠ュ彉鍖栦簡锛屽彲浠ヤ笉绗﹀悎绗竴鍖呮暟鎹伒瀹堢殑鍗忚浜�
+ Object[] objs = this.getClassObjAndAnnotationVo(protocolName, protocolVersion) ;
+ if(objs != null && objs[0] != null && objs[1] != null){
+ pda = (PrefixedDataAvailable)objs[0] ;
+ //澶勭悊瀹屾暣鎬ф鏌�
+ pds = pda.forOnLine(ioSession, in, remain, ((AnnotationPrefixedDataAvailableVo)objs[1]).onLineDataMinLength, ((AnnotationPrefixedDataAvailableVo)objs[1]).errorMaxLength) ;
+ //鐢ㄥ畬瀵硅薄鍚庯紝鏀惧洖姹犱腑
+ PrefixedDataAvailablePool.freeInstance(objs[0].getClass(), pda);
+ }
+ if(pds == null){
+ //濡傛灉浼氳瘽涓紦瀛樼殑鍗忚鏈兘姝g‘澶勭悊
+ HashMap<String, AnnotationPrefixedDataAvailableVo> prefixedDataAvailableMap = ProtocolCache.getPrefixedDataAvailableMap() ;
+ Collection<AnnotationPrefixedDataAvailableVo> set = prefixedDataAvailableMap.values() ;
+ if(set.size() == 0){
+ throw new Exception("涓婄嚎鏁版嵁瀹屾暣鎬ф鏌ユ椂锛屽緱鍒扮殑鍗忚瀹屾暣鎬ф鏌ョ被闆嗗悎涓虹┖銆�") ;
+ }
+ int priority = ProtocolConstant.firstPriority ;
+
+ while(true){
+ objs = this.getClassObjAndAnnotationVo(priority, set) ;
+ pda = (PrefixedDataAvailable)objs[0] ;
+ if(pda == null && priority == ProtocolConstant.firstPriority){
+ throw new Exception("涓婄嚎鏁版嵁瀹屾暣鎬ф鏌ユ椂锛屾湭寰楀埌浼樺厛绾т负" + priority + "涓婄嚎鏁版嵁瀹屾暣鎬ф鏌ョ被锛�") ;
+ }else if(pda == null){
+ //璇存槑涓婄嚎鏁版嵁瀹屾暣鎬ф鏌ョ被闆嗗悎宸茬粡閬嶅巻瀹屼簡銆�
+ break ;
+ }
+ //澶勭悊瀹屾暣鎬ф鏌�
+ pds = pda.forOnLine(ioSession, in, remain, ((AnnotationPrefixedDataAvailableVo)objs[1]).onLineDataMinLength, ((AnnotationPrefixedDataAvailableVo)objs[1]).errorMaxLength) ;
+ //鐢ㄥ畬瀵硅薄鍚庯紝鏀惧洖姹犱腑
+ //2024-11-25涓嬮潰涓�琛岃繘琛屼簡淇敼
+ //PrefixedDataAvailablePool.freeInstance(((AnnotationPrefixedDataAvailableVo)objs[1]).clazz, pda);
+ PrefixedDataAvailablePool.freeInstance(objs[0].getClass(), pda);
+ if(pds == null){
+ //璇存槑涓嶆槸瀵瑰簲鐨勫崗璁暟鎹紝闇�瑕佸彟澶栫殑鍗忚鏉ュ鐞嗕笂绾�
+ //寰幆缁х画
+ priority++ ;
+ }else{
+ //鍋滄寰幆锛岃繑鍥炵粨鏋�
+ break ;
+ }
+ }
+ }
+ if(pds == null){
+ //璇存槑鏁版嵁涓嶅睘浜庝换浣曞崗璁紝涓�鑸负Rtu鏁版嵁鍑洪敊锛屾垨缃戠粶鏀诲嚮鏁版嵁
+ byte[] preByte = new byte[remain];
+ in.get(preByte) ;
+ in.position(0) ;
+ log.error("涓婄嚎绗竴鍖呮暟鎹湭鎵惧埌瀵瑰簲鐨勮В鏋愬崗璁�傛暟鎹槸锛�" + ByteUtil.bytes2Hex(preByte, true)) ;
+ //璁や负鏄畬鏁村寘鏁版嵁锛屽悗缁鍑猴紝闃叉鍗犵敤鍐呭瓨鍙婃寰幆IO璇绘暟鎹�
+ pds = new PrefixedDataAvailableStatus().rubbish(remain) ;
+ }
+ return pds ;
+ }catch(Exception e){
+ //澶勭悊杩囩▼涓彂鐢熷紓甯革紝涓婅鏁版嵁灏辨槸鍨冨溇鏁版嵁浜嗭紝鍚庣画浠ュ瀮鍦炬暟鎹鐞嗭紝鍗虫妸鏁版嵁浠嶪O涓彇鍑烘潵锛屼互闃叉姝诲惊鐜疘O璇绘暟鎹�
+ return new PrefixedDataAvailableStatus().rubbish(remain) ;
+ }
+ }
+ }
+
+ /**
+ * 鍦ㄥ绾跨▼鐜涓繍琛�
+ * 鍒嗘瀽涓婅鏁版嵁(缃戠粶杩炴帴鍚庣浜�(鍖呭惈)鍖呬互鍚庢暟鎹�)鏄惁鍙幏寰�
+ * @param in IoBuffer
+ * @return PrefixedDataAvailableStatus
+ */
+ public PrefixedDataAvailableStatus forUpData(IoSession ioSession, IoBuffer in){
+ int remain = in.remaining() ;
+ if(remain == 0){
+ return new PrefixedDataAvailableStatus().breaked() ;
+ }else{
+ try{
+ PrefixedDataAvailableStatus pds = null ;
+ PrefixedDataAvailable pda = null ;
+
+ //浠庝細璇濈紦瀛樺緱鍒颁笂娆′笂琛屾暟鎹‘瀹氱殑鍗忚
+ String protocolName = (String) ioSession.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName) ;
+ Short protocolVersion = (Short) ioSession.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolVersion) ;
+ //鐢变細璇濅腑鐨勫崗璁緱鍒板鐞嗙被
+ Object[] objs = this.getClassObjAndAnnotationVo(protocolName, protocolVersion) ;
+ if(objs != null && objs[0] != null && objs[1] != null){
+ pda = (PrefixedDataAvailable)objs[0] ;
+ //澶勭悊瀹屾暣鎬ф鏌�
+ pds = pda.forOnLine(ioSession, in, remain, ((AnnotationPrefixedDataAvailableVo)objs[1]).onLineDataMinLength, ((AnnotationPrefixedDataAvailableVo)objs[1]).errorMaxLength) ;
+ //鐢ㄥ畬瀵硅薄鍚庯紝鏀惧洖姹犱腑
+ PrefixedDataAvailablePool.freeInstance(objs[0].getClass(), pda);
+ }
+ if(pds == null) {
+ //濡傛灉浼氳瘽涓紦瀛樼殑鍗忚鏈兘姝g‘澶勭悊
+ HashMap<String, AnnotationPrefixedDataAvailableVo> prefixedDataAvailableMap = ProtocolCache.getPrefixedDataAvailableMap() ;
+ Collection<AnnotationPrefixedDataAvailableVo> set = prefixedDataAvailableMap.values() ;
+ if(set.size() == 0){
+ throw new Exception("涓婄嚎鏁版嵁瀹屾暣鎬ф鏌ユ椂锛屽緱鍒扮殑鍗忚闆嗗悎涓虹┖銆�") ;
+ }
+ int priority = ProtocolConstant.firstPriority ;
+
+ while(true){
+ objs = this.getClassObjAndAnnotationVo(priority, set) ;
+ pda = (PrefixedDataAvailable)objs[0] ;
+ if(pda == null && priority == ProtocolConstant.firstPriority){
+ throw new Exception("涓婄嚎鏁版嵁瀹屾暣鎬ф鏌ユ椂锛屾湭寰楀埌浼樺厛绾т负" + priority + "涓婄嚎鏁版嵁瀹屾暣鎬ф鏌ョ被锛�") ;
+ }else if(pda == null){
+ //璇存槑涓婄嚎鏁版嵁瀹屾暣鎬ф鏌ョ被闆嗗悎宸茬粡閬嶅巻瀹屼簡銆�
+ break ;
+ }
+ //澶勭悊瀹屾暣鎬ф鏌�
+ pds = pda.forUpData(ioSession, in, remain, ((AnnotationPrefixedDataAvailableVo)objs[1]).headMinLength, ((AnnotationPrefixedDataAvailableVo)objs[1]).errorMaxLength) ;
+ //鐢ㄥ畬瀵硅薄鍚庯紝鏀惧洖姹犱腑
+ PrefixedDataAvailablePool.freeInstance(objs[0].getClass(), pda);
+ if(pds == null){
+ //璇存槑涓嶆槸瀵瑰簲鐨勫崗璁暟鎹紝闇�瑕佸彟澶栫殑鍗忚鏉ュ鐞嗕笂绾�
+ //寰幆缁х画
+ priority++ ;
+ }else{
+ //鍋滄寰幆锛岃繑鍥炵粨鏋�
+ break ;
+ }
+ }
+ }
+ if(pds == null){
+ //璇存槑鏁版嵁涓嶅睘浜庝换浣曞崗璁紝涓�鑸负Rtu鏁版嵁鍑洪敊锛屾垨缃戠粶鏀诲嚮鏁版嵁
+ byte[] preByte = new byte[remain];
+ in.get(preByte) ;
+ in.position(0) ;
+ log.error("涓婅鏁版嵁鏈壘鍒板搴旂殑瑙f瀽鍗忚銆傛暟鎹槸锛�" + ByteUtil.bytes2Hex(preByte, true)) ;
+ //璁や负鏄畬鏁村寘鏁版嵁锛屽悗缁鍑猴紝闃叉鍗犵敤鍐呭瓨鍙婃寰幆IO璇绘暟鎹�
+ pds = new PrefixedDataAvailableStatus().rubbish(remain) ;
+ }
+ return pds ;
+ }catch(Exception e){
+ //澶勭悊杩囩▼涓彂鐢熷紓甯革紝涓婅鏁版嵁灏辨槸鍨冨溇鏁版嵁浜嗭紝鍚庣画浠ュ瀮鍦炬暟鎹鐞嗭紝鍗虫妸鏁版嵁浠嶪O涓彇鍑烘潵锛屼互闃叉姝诲惊鐜疘O璇绘暟鎹�
+ return new PrefixedDataAvailableStatus().rubbish(remain) ;
+ }
+ }
+ }
+
+
+
+ /**
+ * 寰楀埌澶勭悊绫诲璞�
+ * @param prority 浼樺厛绾�
+ * @param set 闆嗗悎
+ * @return Object[]
+ * @throws Exception 寮傚父
+ */
+ private Object[] getClassObjAndAnnotationVo(int prority, Collection<AnnotationPrefixedDataAvailableVo> set) throws Exception{
+ PrefixedDataAvailable obj = null ;
+ AnnotationPrefixedDataAvailableVo rVo = null ;
+ for(AnnotationPrefixedDataAvailableVo vo : set){
+ if(prority == vo.priority){
+ obj = PrefixedDataAvailablePool.getInstance(vo.clazz) ;
+ rVo = vo ;
+ break ;
+ }
+ }
+ return new Object[]{obj, rVo} ;
+ }
+ /**
+ * 寰楀埌澶勭悊绫诲璞�
+ * @param protocolName 閫氫俊鍗忚鍚嶇О
+ * @param protocolVersion 閫氫俊鍗忚鐗堟湰鍙�
+ * @return Object[]
+ * @throws Exception 寮傚父
+ */
+ private Object[] getClassObjAndAnnotationVo(String protocolName, Short protocolVersion) throws Exception{
+ PrefixedDataAvailable obj = null ;
+ AnnotationPrefixedDataAvailableVo rVo = null ;
+ if(protocolName != null && !protocolName.trim().equals("") && protocolVersion != null){
+ rVo = ProtocolCache.getAnnotationPrefixedDataAvailable(protocolName, protocolVersion) ;
+ obj = PrefixedDataAvailablePool.getInstance(rVo.clazz) ;
+ }
+ return new Object[]{obj, rVo} ;
+ }
+
+}
--
Gitblit v1.8.0