ConnectionThread.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.net.http; 18 19import android.content.Context; 20import android.os.SystemClock; 21 22import org.apache.http.HttpHost; 23 24import java.lang.Thread; 25 26/** 27 * {@hide} 28 */ 29class ConnectionThread extends Thread { 30 31 static final int WAIT_TIMEOUT = 5000; 32 static final int WAIT_TICK = 1000; 33 34 // Performance probe 35 long mStartThreadTime; 36 long mCurrentThreadTime; 37 38 private boolean mWaiting; 39 private volatile boolean mRunning = true; 40 private Context mContext; 41 private RequestQueue.ConnectionManager mConnectionManager; 42 private RequestFeeder mRequestFeeder; 43 44 private int mId; 45 Connection mConnection; 46 47 ConnectionThread(Context context, 48 int id, 49 RequestQueue.ConnectionManager connectionManager, 50 RequestFeeder requestFeeder) { 51 super(); 52 mContext = context; 53 setName("http" + id); 54 mId = id; 55 mConnectionManager = connectionManager; 56 mRequestFeeder = requestFeeder; 57 } 58 59 void requestStop() { 60 synchronized (mRequestFeeder) { 61 mRunning = false; 62 mRequestFeeder.notify(); 63 } 64 } 65 66 /** 67 * Loop until app shutdown. Runs connections in priority 68 * order. 69 */ 70 public void run() { 71 android.os.Process.setThreadPriority( 72 android.os.Process.THREAD_PRIORITY_LESS_FAVORABLE); 73 74 mStartThreadTime = -1; 75 mCurrentThreadTime = SystemClock.currentThreadTimeMillis(); 76 77 while (mRunning) { 78 Request request; 79 80 /* Get a request to process */ 81 request = mRequestFeeder.getRequest(); 82 83 /* wait for work */ 84 if (request == null) { 85 synchronized(mRequestFeeder) { 86 if (HttpLog.LOGV) HttpLog.v("ConnectionThread: Waiting for work"); 87 mWaiting = true; 88 try { 89 if (mStartThreadTime != -1) { 90 mCurrentThreadTime = SystemClock 91 .currentThreadTimeMillis(); 92 } 93 mRequestFeeder.wait(); 94 } catch (InterruptedException e) { 95 } 96 mWaiting = false; 97 } 98 } else { 99 if (HttpLog.LOGV) HttpLog.v("ConnectionThread: new request " + 100 request.mHost + " " + request ); 101 102 HttpHost proxy = mConnectionManager.getProxyHost(); 103 104 HttpHost host; 105 if (false) { 106 // Allow https proxy 107 host = proxy == null ? request.mHost : proxy; 108 } else { 109 // Disallow https proxy -- tmob proxy server 110 // serves a request loop for https reqs 111 host = (proxy == null || 112 request.mHost.getSchemeName().equals("https")) ? 113 request.mHost : proxy; 114 } 115 mConnection = mConnectionManager.getConnection(mContext, host); 116 mConnection.processRequests(request); 117 if (mConnection.getCanPersist()) { 118 if (!mConnectionManager.recycleConnection(host, 119 mConnection)) { 120 mConnection.closeConnection(); 121 } 122 } else { 123 mConnection.closeConnection(); 124 } 125 mConnection = null; 126 } 127 128 } 129 } 130 131 public synchronized String toString() { 132 String con = mConnection == null ? "" : mConnection.toString(); 133 String active = mWaiting ? "w" : "a"; 134 return "cid " + mId + " " + active + " " + con; 135 } 136 137} 138