liurunyu
2023-12-07 60bd6a383b3f7c0d4922517974da9525a0166b68
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
package com.dy.aceMw.server.tasks;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.dy.common.queue.Node;
import com.dy.common.mw.core.CoreTask;
import com.dy.aceMw.server.forTcp.TcpDownCommandCache;
import com.dy.aceMw.server.forTcp.TcpDownCommandObj;
import com.dy.aceMw.server.forTcp.TcpSessionCach;
 
/**
 * 处理RTU下行命令数据的恒久任务
 */
public class ToRtuConstantTask extends CoreTask {
    private static final Logger log = LogManager.getLogger(ToRtuConstantTask.class.getName());
 
    /**
     * 在单线程环境中运行
     */
    @Override
    public Integer excute() {
        try{
            Long now = System.currentTimeMillis() ;
            dealTcpSession(now) ;
            return dealDownCommand(now) ;
        }catch(Exception e){
            log.error(e);
        }
        return null ;
    }
    /**
     * 处理TCP缓存中的各个TCP Session的上行数据时刻
     */
    private void dealTcpSession(Long now){
        TcpSessionCach.updateUpDataTime(now) ;
    }
    
    /**
     * 处理下行命令
     */
    public Integer dealDownCommand(Long now) {
        Node first = TcpDownCommandCache.getFirstQueueNode() ;
        if(first != null){
            Integer count = TcpDownCommandCache.size() ;
            Node last = TcpDownCommandCache.getLastQueueNode() ;
            this.doDealDownCommand(now, first, last);
            return count ;
        }
        return null ;
    }
    /**
     * 处理缓存的下行命令节点
     * @param now 当前时刻
     * @param first 第一个节点
     * @param last 最后一个节点
     */
    private void doDealDownCommand1(Long now, Node first, Node last){
        if(first != null){
            //在dealNode方法中,可能要把first从队列中移除,这时first.next为空,所以提前把first.next取出来
            Node next = first.next ;
            dealNode(now, first) ;
            if(last != null && first != last){
                doDealDownCommand(now, next, last) ;
            }else if(last != null && first == last){
                //停止
            }else if(last == null){
                //这种情况不会存在
                doDealDownCommand(now, next, last) ;
            }
        }
    }
 
    /**
     * 处理缓存的下行命令节点
     * @param now 当前时刻
     * @param first 第一个节点
     * @param last 最后一个节点
     */
    private void doDealDownCommand(Long now, Node first, Node last){
        if(last != null){
            //在dealNode方法中,可能要把last从队列中移除,这时last.pre为空,所以提前把last.pre取出来
            Node pre = last.pre ;
            dealNode(now, last) ;
            if(first != null && first != last){
                doDealDownCommand(now, first, pre) ;
            }else if(first != null && first == last){
                //停止
            }else if(first == null){
                //这种情况不会存在
                doDealDownCommand(now, null, pre) ;
            }
        }
    }
 
    /**
     * 处理一个节点
     * @param now 现在时刻
     * @param node 节点
     */
    private void dealNode(Long now, Node node){
        TcpDownCommandObj obj = (TcpDownCommandObj)node.obj ;
        boolean removeNode = obj.dealSelf(now) ;
        if(removeNode){
            TcpDownCommandCache.removeNode(node);
        }
    }
 
 
}