| New file | 
 |  |  | 
 |  |  | package com.dy.aceMw.server.forTcp; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | import com.dy.common.queue.Node; | 
 |  |  | import com.dy.common.queue.Queue; | 
 |  |  | import com.dy.common.mw.protocol.MidResultFromRtu; | 
 |  |  | import com.dy.common.mw.protocol.MidResultToRtu; | 
 |  |  | import com.dy.aceMw.server.ServerProperties; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 非线程安全的,只能在单线程中运行 | 
 |  |  |  */ | 
 |  |  | public class TcpDownCommandCache { | 
 |  |  | 	 | 
 |  |  |    //TCP下行命令缓存队列 | 
 |  |  |    private static Queue cacheQueue = new Queue("tcpDownCommandQueue") ; | 
 |  |  | 	 | 
 |  |  |    private static TcpDownCommandCache instance = new TcpDownCommandCache() ; | 
 |  |  | 	 | 
 |  |  |    private TcpDownCommandCache(){ | 
 |  |  |       cacheQueue.setLimit(ServerProperties.cacheUpDownDataWarnCount, ServerProperties.cacheUpDownDataMaxCount); | 
 |  |  |    } | 
 |  |  | 	 | 
 |  |  |    public static TcpDownCommandCache getInstance(){ | 
 |  |  |       return instance ; | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 缓存命令 | 
 |  |  |     * @param result | 
 |  |  |     * @throws Exception | 
 |  |  |     */ | 
 |  |  |    public static void cacheCommand(MidResultToRtu result) throws Exception{ | 
 |  |  |       if(result != null){ | 
 |  |  |          if(result.maxSendTimes == null){ | 
 |  |  |             //设置最大发送次数 | 
 |  |  |             result.maxSendTimes = ServerProperties.downComandMaxResendTimes ; | 
 |  |  |          } | 
 |  |  |          if(result.isSendFirst){ | 
 |  |  |             cacheQueue.pushHead(new TcpDownCommandObj(result)); | 
 |  |  |          }else{ | 
 |  |  |             cacheQueue.pushTail(new TcpDownCommandObj(result)); | 
 |  |  |          } | 
 |  |  |       } | 
 |  |  |    } | 
 |  |  | 	 | 
 |  |  |    /** | 
 |  |  |     * 匹配命令结果 | 
 |  |  |     * @param rsFromRtu | 
 |  |  |     * @return | 
 |  |  |     */ | 
 |  |  |    public static MidResultToRtu matchFromHead(MidResultFromRtu rsFromRtu){ | 
 |  |  |       MidResultToRtu res = null ; | 
 |  |  |       TcpDownCommandObj obj = null ; | 
 |  |  |       Node node = cacheQueue.getFirstNode() ; | 
 |  |  |       while(node != null && node.obj != null){ | 
 |  |  |          obj = (TcpDownCommandObj)node.obj; | 
 |  |  |          res = obj.result ; | 
 |  |  |          if(res != null  | 
 |  |  |                && res.rtuAddr.equals(rsFromRtu.rtuAddr) | 
 |  |  |                && res.downCode.equals(rsFromRtu.upCode)){ | 
 |  |  |             obj.onceReceivedResult = true ;//标识已经收到命令结果 | 
 |  |  |             return res ; | 
 |  |  |          }else{ | 
 |  |  |             node = node.pre ; | 
 |  |  |          } | 
 |  |  |       } | 
 |  |  | 		 | 
 |  |  |       return null ; | 
 |  |  |    } | 
 |  |  | 	 | 
 |  |  |    /** | 
 |  |  |     * 匹配命令结果 | 
 |  |  |     * @param rsFromRtu | 
 |  |  |     * @return | 
 |  |  |     */ | 
 |  |  |    public static MidResultToRtu matchFromTail(MidResultFromRtu rsFromRtu){ | 
 |  |  |       MidResultToRtu res = null ; | 
 |  |  |       TcpDownCommandObj obj = null ; | 
 |  |  |       Node node = cacheQueue.getLastNode() ; | 
 |  |  |       while(node != null && node.obj != null){ | 
 |  |  |          obj = (TcpDownCommandObj)node.obj; | 
 |  |  |          res = obj.result ; | 
 |  |  |          if(res != null  | 
 |  |  |                && res.rtuAddr.equals(rsFromRtu.rtuAddr) | 
 |  |  |                && res.downCode.equals(rsFromRtu.upCode)){ | 
 |  |  |             obj.onceReceivedResult = true ;//标识已经收到命令结果 | 
 |  |  |             return res ; | 
 |  |  |          }else{ | 
 |  |  |             node = node.pre ; | 
 |  |  |          } | 
 |  |  |       } | 
 |  |  | 		 | 
 |  |  |       return null ; | 
 |  |  |    } | 
 |  |  | 	 | 
 |  |  |    /** | 
 |  |  |     * 得到第一个节点 | 
 |  |  |     * @return | 
 |  |  |     */ | 
 |  |  |    public static Node getFirstQueueNode(){ | 
 |  |  |       return cacheQueue.getFirstNode() ; | 
 |  |  |    } | 
 |  |  | 	 | 
 |  |  |    /** | 
 |  |  |     * 得到最后一个节点 | 
 |  |  |     * @return | 
 |  |  |     */ | 
 |  |  |    public static Node getLastQueueNode(){ | 
 |  |  |       return cacheQueue.getLastNode() ; | 
 |  |  |    } | 
 |  |  | 	 | 
 |  |  |    /** | 
 |  |  |     * 移除节点 | 
 |  |  |     * @param node | 
 |  |  |     */ | 
 |  |  |    public static void removeNode(Node node){ | 
 |  |  |       cacheQueue.remove(node); | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 缓存的节点数 | 
 |  |  |     * @Return 缓存节点数 | 
 |  |  |     */ | 
 |  |  |    public static Integer size(){ | 
 |  |  |       return cacheQueue.size() ; | 
 |  |  |    } | 
 |  |  |  | 
 |  |  | } |