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

📄 statusholder.java

📁 类似于MSN
💻 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.filetrans;

/**
 * <pre>
 * 文件传输状态监管类,负责处理状态的改变。
 * 文件传输有三个阶段:请求阶段,连接阶段,传送阶段。
 * 
 * 对于文件发送者来说:
 * 请求阶段始于发送请求,终于对方接受请求
 * 连接阶段始于0x003B,终于0x003E或者0x0032或者0x0040或者请求中转
 * 传送阶段为连接阶段之后的阶段
 * 
 * 对于文件接受者来说:
 * 请求阶段始于收到请求,终于同意请求
 * 连接阶段始于0x003B,终于0x003D或者0x0031或者0x004B
 * 传送阶段为连接阶段之后的阶段
 * 
 * 不同的阶段又可能有不同的状态,StatusHolder负责这些状态之间的判断和转换
 * 这些状态之间采用位或,因为并非同时只能有一个状态。基本上每一个包类型都被
 * 当作一个状态来处理
 * </pre>
 * 
 * @author 马若劼
 */
public class StatusHolder {  
    // TODO update plugin    
    // 当前状态
    protected int status;
    
    /** 初始状态 */
    public static final int FT_INIT = 0x1;
    
    // 请求阶段状态常量
    /** 请求传送文件 */
    public static final int FT_REQUESTING = 0x2;

    // 连接阶段状态常量
    /** 通知对方IP和端口信息 */
    public static final int FT_NOTIFING_IP = 0x4;
    /** Ping or Pong */
    public static final int FT_PING_PONG = 0x8;
    /** 防火墙试探中 */
    public static final int FT_FIREWALL_TESTING = 0x10;
    /** 正在发送Hello */
    public static final int FT_SAYING_HELLO = 0x20;
    /** 正在请求中转服务 */
    public static final int FT_REQUESTING_AGENT = 0x40;
    /** It is time to transfering data */
    public static final int FT_IT_IS_TIME = 0x80;
    /** I am ready */
    public static final int FT_I_AM_READY = 0x100;
    
    // 传送阶段状态常量
    /** 发送文件基本信息 */
    public static final int FT_SENDING_BASIC = 0x200;
    /** 发送文件数据分片 */
    public static final int FT_SENDING_FRAGMENT = 0x400;
    /** 发送文件EOF信息 */
    public static final int FT_SENDING_EOF = 0x800;
    /** 发送传送结束信息 */
    public static final int FT_FINISHING = 0x1000;
    /** 收到结束信息的确认,发送正式结束 */
    public static final int FT_FINISHED = 0x2000;
    
    /** 状态转换映射,把StatusHolder中状态的大小做为索引,比如0x20,为第四个 */
    protected static final int[] statusMap = new int[] {
            // from FT_INIT
            FT_REQUESTING,
            // from FT_REQUESTING
            FT_NOTIFING_IP | FT_PING_PONG | FT_FIREWALL_TESTING,
            // from FT_NOTIFING_IP
            FT_PING_PONG | FT_FIREWALL_TESTING | FT_REQUESTING_AGENT,
            // from FT_PING_PONG
            FT_SAYING_HELLO | FT_REQUESTING_AGENT,
            // from FT_FIREWALL_TESTING
            FT_SAYING_HELLO | FT_REQUESTING_AGENT,
            // from FT_SAYING_HELLO
            FT_SENDING_BASIC | FT_SENDING_FRAGMENT,
            // from FT_REQUESTING_AGENT
            FT_IT_IS_TIME,
            // from FT_IT_IS_TIME
            FT_I_AM_READY,
            // from FT_I_AM_READY
            FT_SENDING_BASIC | FT_SENDING_FRAGMENT,
            // from FT_SENDING_BASIC
            FT_SENDING_FRAGMENT,
            // from FT_SENDING_FRAGMENT
            FT_SENDING_FRAGMENT | FT_SENDING_EOF,
            // from FT_SENDING_EOF
            FT_FINISHING,
            // from FT_FINISHING
            FT_FINISHED,
            // from FT_FINISHED
            0
    };
    
    /**
     * 构造函数
     */
    public StatusHolder() {
        status = FT_INIT;
    }
    
    /**
     * 判断从当前状态转到另一个状态是否是允许的
     * @param destStatus 目标状态
     * @return true表示运行,false表示不运行
     */
    public synchronized boolean isPermitted(int destStatus) {
        int temp = status;
        int i = 0;
        while(temp != 0) {
            if((temp & 1) != 0 && (statusMap[i] & destStatus) != 0)
                return true;
            temp >>= 1;
            i++;
        }
        return false;
    }
    
    /**
     * 设置当前状态,原有的状态将被清除。设置之前将会进行检查
     * @param destStatus 目标状态
     * @return true表示状态改变成功,false表示改变失败
     */
    public synchronized boolean setStatus(int destStatus) {
        if(isPermitted(destStatus)) {
            status = destStatus;
            return true;
        } else
            return false;
    }
    
    /**
     * 设置状态,不做许可检查
     * @param destStatus 目标状态
     */
    public synchronized void resetStatus(int destStatus) {
        status = destStatus;
    }
    
    /**
     * 添加状态,原有的状态将保存,于是这形成了多状态的情况。添加之前将做许可检查
     * @param destStatus 要添加的状态位
     * @return true表示添加成功,false表示失败
     */
    public synchronized boolean addStatus(int destStatus) {
        if(isPermitted(destStatus)) {
            status |= destStatus;
            return true;
        } else 
            return false;
    }
    
    /**
     * 移除状态位,不做检查
     * @param destStatus 要移除的状态
     */
    public synchronized void removeStatus(int destStatus) {
        status ^= destStatus;
    }
}

⌨️ 快捷键说明

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