liurunyu
2023-11-18 c1ddfd71223c1a7d704b6f21b669fbfcb37adc82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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 TcpDownCommandCach {
    
    //TCP下行命令缓存队列
    private static Queue cachQueue = new Queue("tcpDownComandQueue") ;
    
    private static TcpDownCommandCach instance = new TcpDownCommandCach() ;
    
    private TcpDownCommandCach(){
        cachQueue.setLimit(990000, 1000000);
    }
    
    public static TcpDownCommandCach getInstance(){
        return instance ;
    }
 
    /**
     * 缓存命令
     * @param result
     * @throws Exception
     */
    public static void cachCommand(MidResultToRtu result) throws Exception{
        if(result != null){
            if(result.maxSendTimes == null){
                //设置最大发送次数
                result.maxSendTimes = ServerProperties.downComandMaxResendTimes ;
            }
            if(result.isSendFirst){
                cachQueue.pushHead(new TcpDownCommandObj(result));
            }else{
                cachQueue.pushTail(new TcpDownCommandObj(result));
            }
        }
    }
    
    /**
     * 匹配命令结果
     * @param rsFromRtu
     * @return
     */
    public static MidResultToRtu matchFromHead(MidResultFromRtu rsFromRtu){
        MidResultToRtu res = null ;
        TcpDownCommandObj obj = null ;
        Node node = cachQueue.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 = cachQueue.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 cachQueue.getFirstNode() ;
    }
    
    /**
     * 得到最后一个节点
     * @return
     */
    public static Node getLastQueueNode(){
        return cachQueue.getLastNode() ;
    }
    
    /**
     * 移除节点
     * @param node
     */
    public static void removeNode(Node node){
        cachQueue.remove(node);
    }
 
    /**
     * 缓存的节点数
     * @param node
     */
    public static Integer size(){
        return cachQueue.size() ;
    }
 
}