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

📄 abstractproxy.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;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <pre>
 * 实现Proxy的一些共同的部分
 * </pre>
 * 
 * @author 马若劼
 */
public abstract class AbstractProxy implements IProxy, INIOHandler {
    /** Log类 */
	private static final Log log = LogFactory.getLog(AbstractProxy.class);

    /** 连接Proxy的channel */
    protected SocketChannel channel;
    /** 这个proxy从属的Porter */
    protected Porter porter;
    /** 代理事件处理器 */
    protected IProxyHandler handler;
    /** byte buffer */
    protected ByteBuffer buffer;
    /** 用户名 */
    protected String username;
    /** 密码 */
    protected String password;
    /** 要连接的服务器地址 */
    protected InetSocketAddress serverAddress;
    /** 代理服务器地址 */
    protected InetSocketAddress proxyAddress;
    /** 当前状态 */
    protected int status;
    /** proxy是否已经连接上 */
    protected boolean connected;
    /** 是否udp,该字段只对Socks5代理有效 */
    protected boolean udp;
    /** 客户端端口,该字段制作UDP Socks5时有效 */
    protected int clientPort;
    
    /**
     * 构造函数
     * @param porter
     */
    public AbstractProxy(Porter porter, IProxyHandler handler) {
        this.porter = porter;
        this.handler = handler;
        buffer = ByteBuffer.allocateDirect(300);
        username = password = "";
        connected = false;
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxy#channel()
     */
    public SocketChannel channel() {
        return channel;
    }
    
    /**
     * 发送
     */
    protected void send() {
        try {
            if(connected)
                channel.write(buffer);
        } catch (IOException e) {
            log.error(e.getMessage());
            handler.proxyError(e);
        }
    }
    
    /**
     * 接收数据
     * @throws IOException
     */
    protected void receive() {
        try {
            buffer.clear();
            for (int len = channel.read(buffer); len > 0; len = channel.read(buffer));
            buffer.flip();
        } catch (IOException e) {
            log.error(e.getMessage());
            handler.proxyError(e);
        }
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxy#start()
     */
    public void start() {
        log.trace("AbstractProxy start");
        try {
            // 打开连接代理的channel
            porter.setNIOHandler(this);
            channel = SocketChannel.open();
            channel.configureBlocking(false);
            porter.register(channel);
            channel.connect(proxyAddress);
        } catch (Exception e) {
            log.error(e.getMessage());
            handler.proxyError(e);
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxy#dispose()
     */
    public void dispose() throws IOException {
        channel.close();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.INIOHandler#processConnect(java.nio.channels.SelectionKey)
     */
    public void processConnect(SelectionKey sk) throws IOException {
        log.debug("Abstract Proxy NIOHandler processConnect, connected = " + String.valueOf(connected));
        if(connected) return;
        //完成SocketChannel的连接
        channel.finishConnect();
        while(!channel.isConnected()) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                // 没有什么要做的
            }            
            channel.finishConnect();
        }
    	sk.interestOps(SelectionKey.OP_READ);
    	connected = true;
    	log.debug("已连接上代理服务器");
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.INIOHandler#processError(java.lang.Exception)
     */
    public void processError(Exception e) {
        handler.proxyError(e);
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxy#setProxyHandler(edu.tsinghua.lumaqq.qq.IProxyHandler)
     */
    public void setProxyHandler(IProxyHandler handler) {
        this.handler = handler;
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxy#setProxyAddress(java.net.InetSocketAddress)
     */
    public void setProxyAddress(InetSocketAddress proxyAddress) {
        this.proxyAddress = proxyAddress;
    }
        
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxy#setServerAddress(java.net.InetSocketAddress)
     */
    public void setServerAddress(InetSocketAddress serverAddress) {
        this.serverAddress = serverAddress;
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxy#setUdp(boolean)
     */
    public void setUdp(boolean udp) {
        this.udp = udp;
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxy#setClientPort(int)
     */
    public void setClientPort(int p) {
        clientPort = p;
    }
    
    /**
     * @param password The password to set.
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    /**
     * @param username The username to set.
     */
    public void setUsername(String username) {
        this.username = username;
    }
}

⌨️ 快捷键说明

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