FileDocCategorySizeDatePackage
HttpCirChannel.javaAPI DocAndroid 1.5 API3474Wed May 06 22:42:46 BST 2009com.android.im.imps

HttpCirChannel

public class HttpCirChannel extends CirChannel implements Runnable
An implementation of CIR channel with standalone HTTP binding.

Fields Summary
private static final int
HTTP_CIR_PING_INTERVAL
private DataChannel
mDataChannel
private boolean
mStopped
private Thread
mPollingTask
private URL
mCirUrl
private long
mServerPollMin
Constructors Summary
public HttpCirChannel(ImpsConnection connection, DataChannel dataChannel)


         
        super(connection);
        this.mDataChannel = dataChannel;
    
Methods Summary
public voidconnect()

        ImpsSession session = mConnection.getSession();
        try {
            if (session.getCirHttpAddress() != null) {
                mCirUrl = new URL(session.getCirHttpAddress());
            }
        } catch (MalformedURLException e) {
            // Ignore
        }
        mServerPollMin = session.getServerPollMin() * 1000;

        mPollingTask = new Thread(this, "HTTPCIRChannel");
        mPollingTask.setDaemon(true);
        mPollingTask.start();
    
public voidrun()

        while (!mStopped) {
            long lastActive = mDataChannel.getLastActiveTime();

            if (mCirUrl != null) {
                if (SystemClock.elapsedRealtime() - lastActive >= HTTP_CIR_PING_INTERVAL) {
                    HttpURLConnection urlConnection;
                    try {
                        urlConnection = (HttpURLConnection) mCirUrl
                                .openConnection();

                        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                            mConnection.sendPollingRequest();
                        }
                    } catch (IOException e) {
                        mStopped = true;
                    }
                }

                try {
                    Thread.sleep(HTTP_CIR_PING_INTERVAL);
                } catch (InterruptedException e) {
                    // Ignore.
                }
            } else {
                // The server didn't provide a URL for CIR poll in the
                // capability negotiation, just send PollingRequest.
                if (SystemClock.elapsedRealtime() - lastActive >= mServerPollMin
                        && mDataChannel.isSendingQueueEmpty()) {
                    mConnection.sendPollingRequest();
                }

                try {
                    Thread.sleep(mServerPollMin);
                } catch (InterruptedException e) {
                    // Ignore.
                }
            }
        }
    
public voidshutdown()

        mStopped = true;