edu.tsinghua.lumaqq.qq.net.TCPHttpPort.java Source code

Java tutorial

Introduction

Here is the source code for edu.tsinghua.lumaqq.qq.net.TCPHttpPort.java

Source

/*
* 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.net;

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

import edu.tsinghua.lumaqq.qq.QQ;
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.PacketParseException;

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

/**
 * <pre>
 * TCPHTTP?
 * </pre>
 * 
 * @author luma
 */
public class TCPHttpPort extends AbstractPort implements IProxyHandler {
    /** Log */
    private static final Log log = LogFactory.getLog(TCPHttpPort.class);
    /** proxy???channel */
    private SocketChannel channel;

    /** HTTP? */
    private HttpProxy proxy;
    /** ??? */
    private boolean ready;
    /**
     * true?
     */
    private boolean remoteClosed;

    /**
     * 
     * 
    * @param policy
    *       ?
    * @param serverAddress
    *       ??
    * @throws IOException
    *       port
     */
    public TCPHttpPort(IConnectionPolicy policy, InetSocketAddress serverAddress) throws IOException {
        super(policy);
        ready = false;
        remoteClosed = false;
        remoteAddress = serverAddress;
        // ?
        proxy = new HttpProxy(this, policy.getProxyUsername(), policy.getProxyPassword());
        proxy.setProxyAddress(policy.getProxy());
        proxy.setRemoteAddress(serverAddress);
        channel = (SocketChannel) proxy.channel();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.net.IPort#dispose()
     */
    public void dispose() {
        proxy.dispose();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.net.IPort#start()
     */
    public void start() {
        proxy.start();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.net.AbstractPort#getNIOHandler()
     */
    @Override
    public INIOHandler getNIOHandler() {
        if (ready)
            return this;
        else
            return proxy;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxyHandler#proxyReady(java.net.InetSocketAddress)
     */
    public void proxyReady(InetSocketAddress bindAddress) throws IOException {
        ready = true;
        channel = (SocketChannel) proxy.channel();
        ((PortGate) getPool()).getPorter().register(this, SelectionKey.OP_READ);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IProxyHandler#proxyAuthFail()
     */
    public void proxyAuthFail() {
        proxyError("Proxy Auth Fail");
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.net.IProxyHandler#proxyError(java.lang.String)
     */
    public void proxyError(String err) {
        ErrorPacket packet = policy.createErrorPacket(ErrorPacket.ERROR_PROXY, getId());
        packet.errorMessage = err;
        policy.pushIn(packet);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IPort#channel()
     */
    public SelectableChannel channel() {
        return channel;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IPort#receive()
     */
    public void receive() throws IOException, PacketParseException {
        if (remoteClosed)
            return;
        //?       
        int oldPos = receiveBuf.position();
        for (int r = channel.read(receiveBuf); r > 0; r = channel.read(receiveBuf))
            ;
        // ??
        int pos = receiveBuf.position();
        receiveBuf.flip();
        // ?0??      
        if (oldPos == pos) {
            ErrorPacket packet = policy.createErrorPacket(ErrorPacket.ERROR_CONNECTION_BROKEN, getId());
            policy.pushIn(packet);
            remoteClosed = true;
            return;
        }
        // ?
        while (true) {
            /* ?? */
            // ?
            InPacket packet = null;
            try {
                packet = policy.parseIn(receiveBuf, false);
            } catch (PacketParseException e) {
                adjustBuffer(pos);
                throw e;
            }
            if (packet == null) {
                /*
                 * packetnull????
                 * ????parser?
                 * ???
                 */
                if (!policy.relocate(receiveBuf))
                    break;
            }
            policy.pushIn(packet);
        }
        adjustBuffer(pos);
    }

    /**
     * buffer
     * 
     * @param pos
     */
    private void adjustBuffer(int pos) {
        // 0??pos?
        if (receiveBuf.position() > 0) {
            receiveBuf.compact();
            receiveBuf.limit(receiveBuf.capacity());
        } else {
            receiveBuf.limit(receiveBuf.capacity());
            receiveBuf.position(pos);
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IPort#send()
     */
    public void send() throws IOException {
        while (!isEmpty()) {
            sendBuf.clear();
            OutPacket packet = remove();
            packet.fill(sendBuf);
            sendBuf.flip();
            if (packet.needAck()) {
                channel.write(sendBuf);
                // ??
                packet.setTimeout(System.currentTimeMillis() + QQ.QQ_TIMEOUT_SEND);
                policy.pushResend(packet, getId());
                log.debug("?? - " + packet.toString());
            } else {
                int count = packet.getSendCount();
                for (int i = 0; i < count; i++) {
                    sendBuf.rewind();
                    channel.write(sendBuf);
                    log.debug("?? - " + packet.toString());
                }
            }
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.net.IPort#send(edu.tsinghua.lumaqq.qq.packets.OutPacket)
     */
    public void send(OutPacket packet) {
        try {
            sendBuf.clear();
            packet.fill(sendBuf);
            sendBuf.flip();
            if (packet.needAck()) {
                channel.write(sendBuf);
                log.debug("?? - " + packet.toString());
            } else {
                int count = packet.getSendCount();
                for (int i = 0; i < count; i++) {
                    sendBuf.rewind();
                    channel.write(sendBuf);
                    log.debug("?? - " + packet.toString());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IPort#send(java.nio.ByteBuffer)
     */
    public void send(ByteBuffer buffer) {
        try {
            channel.write(buffer);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IPort#isConnected()
     */
    public boolean isConnected() {
        return ready;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.INIOHandler#processConnect(java.nio.channels.SelectionKey)
     */
    public void processConnect(SelectionKey sk) throws IOException {
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.INIOHandler#processRead(java.nio.channels.SelectionKey)
     */
    public void processRead(SelectionKey sk) throws IOException, PacketParseException {
        receive();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.INIOHandler#processWrite()
     */
    public void processWrite() throws IOException {
        send();
    }
}