pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/tasks/SendMsConstantTask.java
New file
@@ -0,0 +1,81 @@
package com.dy.rtuMw.server.tasks;
import com.dy.common.mw.core.CoreTask;
import com.dy.common.queue.Node;
import com.dy.rtuMw.server.forMs.MsObj4Ding;
import com.dy.rtuMw.server.forMs.SendMsCache;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
 * @Author: liurunyu
 * @Date: 2024/7/31 18:45
 * @Description
 */
public class SendMsConstantTask  extends CoreTask {
    private static final Logger log = LogManager.getLogger(SendMsConstantTask.class.getName());
    /**
     * 在单线程环境中运行
     */
    @Override
    public Integer execute() {
        try{
            //log.info("当前消息队列中还有结点数量:" + SendMsCache.size());
            dealMs() ;
        }catch(Exception e){
            log.error(e);
        }
        return SendMsCache.size()>0?0:1 ;
    }
    /**
     * 处理下行命令
     */
    public void dealMs() {
        Node first = SendMsCache.getFirstQueueNode() ;
        if(first != null){
            Node last = SendMsCache.getLastQueueNode() ;
            while (last != null){
                last = this.doDealMs(first, last);
            }
        }
    }
    /**
     * 处理缓存的下行节点
     * @param first 第一个节点
     * @param last 最后一个节点
     */
    private Node doDealMs(Node first, Node last){
        if(last != null){
            //在dealNode方法中,可能要把last从队列中移除,这时last.pre为空,所以提前把last.pre取出来
            Node pre = last.pre ;
            dealNode(last) ;
            if(first != last){
                return pre ;
            }else{
                //停止
                return null ;
            }
        }else{
            return null ;
        }
    }
    /**
     * 处理一个节点
     * @param node 节点
     */
    private void dealNode(Node node){
        if(node != null && node.obj != null){
            MsObj4Ding obj = (MsObj4Ding)node.obj ;
            boolean removeNode = obj.dealSelf() ;
            if(removeNode){
                SendMsCache.removeNode(node);
            }
        }
    }
}