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

📄 sender.java

📁 飞信的j2me源代码
💻 JAVA
字号:
import java.io.*;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

class Sender implements Runnable
{
    static String runState = "init";
    private Transform transform;
    private volatile boolean aborting;
    private boolean polling;
    private Vector packageQueue;
    byte errorTimer;
    public static final int ERROR_TIME = 5;
    private DataPackage lastSendDataPackage;

    Sender(Transform transform1)
    {
        transform = Transform.instance;
        aborting = false;
        polling = false;
        packageQueue = new Vector(2);
        errorTimer = 0;
        lastSendDataPackage = null;
        transform = transform1;
        (new Thread(this)).start();
    }

    public void sendDataPackage(DataPackage datapackage)
    {
        packageQueue.addElement(datapackage);
        synchronized(this)
        {
            notify();
        }
    }

    void autoPolling(boolean flag)
    {
        polling = flag;
        if(polling && isQueueEmpty())
            sendDataPackage(transform.getPollPackage());
    }

    boolean isAutoPolling()
    {
        return polling;
    }

    public void output(String s)
    {
        System.out.println(s);
    }

    public void run()
    {
        DataPackage datapackage;
        output("# get to func Sender.run()");
        datapackage = null;
        while(!aborting){
            output("# get to func Sender.run(),line 0");
            try{
                synchronized(this){
                    output("# get to func Sender.run(),line 1");
                    output("# packageQueue.size()" + packageQueue.size());
                    while(isQueueEmpty()){
                        try{
                            output("# get to func Sender.run(),line 2");
                            wait();
                            output("# get to func Sender.run(),line 3");
                            if(aborting)
                                return;
                        }
                        finally{}
                    }
                }
                datapackage =(DataPackage) packageQueue.elementAt(0);
                
            }
            catch(Exception ex){
                packageQueue.removeAllElements();
                MMIDlet.rpcode = 200;
                MMIDlet.instance.notifyAndExit(Integer.toHexString(datapackage.type));
                continue;
            }
            
            if(datapackage.delayTime > 0)
            {
                output("# get to func Sender.run(),line 6");
                synchronized(this)
                {
                    try
                    {
                        output("# get to func Sender.run(),line 7");
                        wait(datapackage.delayTime * 1000);
                        output("# get to func Sender.run(),line 8");
                    }
                    catch(Exception exception3) { }
                }
            }
            
            if(datapackage.type == 0 && !packageQueue.lastElement().equals(datapackage))
            {
                output("# get to func Sender.run(),line 10");
                packageQueue.removeElementAt(0);
                datapackage = (DataPackage)packageQueue.elementAt(0);
            }      
            
            if(doSend(datapackage))
            {
                output("# get to func Sender.run(),line 11");
                errorTimer = 0;
                packageQueue.removeElementAt(0);
                if(isQueueEmpty() && polling)
                    sendDataPackage(transform.getPollPackage());
                continue; 
            }
            output("# get to func Sender.run(),line 12");
            
            if(7 == datapackage.type)
            {
                MMIDlet.instance.notifyDestroyed();
                return;
            }
            
            try
            {
                errorTimer++;
                if(errorTimer >= 4)
                {
                    if(errorTimer == 4)
                        transform.on_NetworkError(datapackage);
                    try
                    {
                        Thread.sleep(5000L);
                    }
                    catch(Exception exception) { }
                }
                if(errorTimer % 6 == 0)
                    MMIDlet.instance.showAlert("发生异常,请稍候片刻再使用该程序。" + MMIDlet.errorCode(Integer.toHexString(datapackage.type)), null);
            }
            catch(Exception exception1)
            {
                packageQueue.removeAllElements();
                MMIDlet.rpcode = 200;
                MMIDlet.instance.notifyAndExit(Integer.toHexString(datapackage.type));
            }
        }
    }

    private boolean doSend(DataPackage datapackage)
    {
        HttpConnection httpconnection;
        DataInputStream datainputstream;
        DataOutputStream dataoutputstream;
        byte abyte0[];
        output("# get to func Sender.doSend(),line 1");
        lastSendDataPackage = datapackage;
        httpconnection = null;
        datainputstream = null;
        dataoutputstream = null;
        MMIDlet.ioError = false;
        MMIDlet.responseError = false;
        abyte0 = null;
        try{
            httpconnection = (HttpConnection)Connector.open(MMIDlet.serverUrl);
            output("# get to func Sender.doSend(),line 2");
            if(MMIDlet.isProxy)
                httpconnection.setRequestProperty("X-Online-Host", MMIDlet.realHostIP);
            httpconnection.setRequestMethod("POST");
            httpconnection.setRequestProperty("Content-Length", String.valueOf(datapackage.allData.length));
            String s = MMIDlet.instance.getAppProperty("cellphone");
            if(s != null){
                if(MMIDlet.user.cellPhone == null){
                    httpconnection.setRequestProperty(MMIDlet.mobileHeader,s);
                }
                else{
                    httpconnection.setRequestProperty(MMIDlet.mobileHeader, MMIDlet.user.cellPhone);
                }
            }
            dataoutputstream = httpconnection.openDataOutputStream();
            dataoutputstream.write(datapackage.allData);
            datainputstream = httpconnection.openDataInputStream();
            int i = (int)httpconnection.getLength();
            MMIDlet.rpcode = httpconnection.getResponseCode();
            if(MMIDlet.rpcode >= 200 && MMIDlet.rpcode < 300)
                abyte0 = MMIDlet.reading(datainputstream, i, (byte)2);
            else
                MMIDlet.responseError = true;
            if(datainputstream != null)
                try
                {
                    datainputstream.close();
                }
                catch(IOException ioexception) { }
            if(dataoutputstream != null)
                try
                {
                    dataoutputstream.close();
                }
                catch(IOException ioexception1) { }
            if(httpconnection != null)
                try
                {
                    httpconnection.close();
                }
                catch(IOException ioexception2) { }
        }
        catch(Exception ex){
            MMIDlet.ioError = true;
            output("# Exception happen, at func Sender.doSend()");
            if(datainputstream != null)
                try
                {
                    datainputstream.close();
                }
                catch(IOException ioexception3) { }
            if(dataoutputstream != null)
                try
                {
                    dataoutputstream.close();
                }
                catch(IOException ioexception4) { }
            if(httpconnection != null)
                try
                {
                    httpconnection.close();
                }
                catch(IOException ioexception5) { }
            return false;
        }
        if(MMIDlet.ioError)
            return false;
        if(MMIDlet.responseError)
            return false;
        if(abyte0[3] != -1)
        {
            transform.receive(new DataPackage(abyte0));
        } else
        {
            byte byte0 = abyte0[4];
            int k = 5;
            for(int l = 0; l < byte0; l++)
            {
                int j = (abyte0[k] & 0xff) << 16 | (abyte0[k + 1] & 0xff) << 8 | abyte0[k + 2] & 0xff;
                byte abyte1[] = new byte[j];
                System.arraycopy(abyte0, k, abyte1, 0, j);
                transform.receive(new DataPackage(abyte1));
                k += j;
            }

        }
        return true;
    }

    void abort()
    {
        aborting = true;
        synchronized(this)
        {
            notify();
        }
    }

    public boolean isQueueEmpty()
    {
        output("# get to func Sender.isQueueEmpty()");
        output("# packageQueue.size():" + packageQueue.size());
        return packageQueue.size() == 0;
    }

    public int queueSize()
    {
        return packageQueue.size();
    }

    public void sendLastDataPackage()
    {
        sendDataPackage(lastSendDataPackage);
    }
}

⌨️ 快捷键说明

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