⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resendtrigger.java

📁 MilyQQ是一个使用控制台的QQ客户端,基于LumaQQ的核心JQL包开发,只有一个可执行的jar包(MilyQQ.jar),携带方便,由于是Java程序,因此理论上应该可以运行于所有平台,不过基于
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//package edu.tsinghua.lumaqq.qq;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

//import edu.tsinghua.lumaqq.qq.packets.ErrorPacket;
//import edu.tsinghua.lumaqq.qq.packets.InPacket;
//import edu.tsinghua.lumaqq.qq.packets.OutPacket;
//import edu.tsinghua.lumaqq.qq.packets.PacketHistory;

public class ResendTrigger<T> implements Callable<T> {
	private QQClient client;

    // 检查应答包是否到达的类
    private PacketHistory history;
    // 超时队列
    private Queue<OutPacket> timeoutQueue;
    // 包 to Port的映射
    private Map<OutPacket, String> toPort;

    // temp variable
    private String portName;

	public ResendTrigger(QQClient client) {
        this.client = client;
        history = client.getHistory();
        timeoutQueue = new LinkedList<OutPacket>();
        toPort = new HashMap<OutPacket, String>();
        QQClient.executor.schedule(this, QQ.QQ_TIMEOUT_SEND, TimeUnit.MILLISECONDS);
	}

    /**
     * 添加一个包到超时队列
     *
     * @param packet
     * 		发送包对象
     * @param name
     * 		port名称
     */
    public synchronized void add(OutPacket packet, String name) {
        timeoutQueue.offer(packet);
        toPort.put(packet, name);
    }

    /**
     * 清空重发队列
     */
    public synchronized void clear() {
    	timeoutQueue.clear();
    	toPort.clear();
    }

    /**
     * 得到超时队列的第一个包,不把它从队列中删除
     *
     * @return
     * 		超时队列的第一个包,如果没有,返回null
     */
    public synchronized OutPacket get() {
		return timeoutQueue.peek();
    }

    /**
     * 得到超时队列的第一个包,并把它从队列中删除
     *
     * @return
     * 		超时队列的第一个包,如果没有,返回null
     */
    public synchronized OutPacket remove() {
        OutPacket ret = timeoutQueue.poll();
        if(ret != null)
        	portName = toPort.remove(ret);
        return ret;
    }

    /**
     * 删除ack对应的请求包
     *
     * @param ack
     */
    public synchronized void remove(InPacket ack) {
        int hash = ack.hashCode();
        for(Iterator<OutPacket> i = timeoutQueue.iterator(); i.hasNext(); ) {
        	OutPacket packet = i.next();
        	if(packet.hashCode() == hash) {
        		toPort.remove(packet);
        		i.remove();
        		break;
        	}
        }
    }

    /**
     * 得到下一个包的超时时间
     *
     * @return
     * 		下一个包的超时时间,如果队列为空,返回一个固定值
     */
    private long getTimeoutLeft() {
        OutPacket packet = get();
        if(packet == null)
            return QQ.QQ_TIMEOUT_SEND;
        else
            return packet.getTimeout() - System.currentTimeMillis();
    }

    /**
     * 触发超时事件
     * @param p
     */
    private void fireOperationTimeOutEvent(OutPacket packet) {
    	ErrorPacket error = new ErrorPacket(ErrorPacket.ERROR_TIMEOUT, client.getUser());
    	error.timeoutPacket = packet;
    	error.setHeader(packet.getHeader());
    	client.addIncomingPacket(error);
    }

	/* (non-Javadoc)
	 * @see java.util.concurrent.Callable#call()
	 */
	public T call() throws Exception {
    	long t = getTimeoutLeft();
		while(t <= 0) {
			OutPacket packet = remove();

			// 发送
			if(packet != null && !history.check(packet, false)) {
				if(packet.needResend()) {
					// 重发次数未到最大,重发
					client.sendPacketAnyway(packet, portName);
				} else {
					// 触发操作超时事件
					fireOperationTimeOutEvent(packet);
				}
			}
			t = getTimeoutLeft();
		}
		QQClient.executor.schedule(this, t, TimeUnit.MILLISECONDS);

    	return null;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -