• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.net.http;
18 
19 import android.content.Context;
20 import android.os.SystemClock;
21 
22 import org.apache.http.HttpHost;
23 
24 import java.lang.Thread;
25 
26 /**
27  * {@hide}
28  */
29 class ConnectionThread extends Thread {
30 
31     static final int WAIT_TIMEOUT = 5000;
32     static final int WAIT_TICK = 1000;
33 
34     // Performance probe
35     long mCurrentThreadTime;
36     long mTotalThreadTime;
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 
ConnectionThread(Context context, int id, RequestQueue.ConnectionManager connectionManager, RequestFeeder requestFeeder)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 
requestStop()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      */
run()70     public void run() {
71         android.os.Process.setThreadPriority(
72                 android.os.Process.THREAD_PRIORITY_DEFAULT +
73                 android.os.Process.THREAD_PRIORITY_LESS_FAVORABLE);
74 
75         // these are used to get performance data. When it is not in the timing,
76         // mCurrentThreadTime is 0. When it starts timing, mCurrentThreadTime is
77         // first set to -1, it will be set to the current thread time when the
78         // next request starts.
79         mCurrentThreadTime = 0;
80         mTotalThreadTime = 0;
81 
82         while (mRunning) {
83             if (mCurrentThreadTime == -1) {
84                 mCurrentThreadTime = SystemClock.currentThreadTimeMillis();
85             }
86 
87             Request request;
88 
89             /* Get a request to process */
90             request = mRequestFeeder.getRequest();
91 
92             /* wait for work */
93             if (request == null) {
94                 synchronized(mRequestFeeder) {
95                     if (HttpLog.LOGV) HttpLog.v("ConnectionThread: Waiting for work");
96                     mWaiting = true;
97                     try {
98                         mRequestFeeder.wait();
99                     } catch (InterruptedException e) {
100                     }
101                     mWaiting = false;
102                     if (mCurrentThreadTime != 0) {
103                         mCurrentThreadTime = SystemClock
104                                 .currentThreadTimeMillis();
105                     }
106                 }
107             } else {
108                 if (HttpLog.LOGV) HttpLog.v("ConnectionThread: new request " +
109                                             request.mHost + " " + request );
110 
111                 HttpHost proxy = mConnectionManager.getProxyHost();
112 
113                 HttpHost host;
114                 if (false) {
115                     // Allow https proxy
116                     host = proxy == null ? request.mHost : proxy;
117                 } else {
118                     // Disallow https proxy -- tmob proxy server
119                     // serves a request loop for https reqs
120                     host = (proxy == null ||
121                             request.mHost.getSchemeName().equals("https")) ?
122                             request.mHost : proxy;
123                 }
124                 mConnection = mConnectionManager.getConnection(mContext, host);
125                 mConnection.processRequests(request);
126                 if (mConnection.getCanPersist()) {
127                     if (!mConnectionManager.recycleConnection(host,
128                                 mConnection)) {
129                         mConnection.closeConnection();
130                     }
131                 } else {
132                     mConnection.closeConnection();
133                 }
134                 mConnection = null;
135 
136                 if (mCurrentThreadTime > 0) {
137                     long start = mCurrentThreadTime;
138                     mCurrentThreadTime = SystemClock.currentThreadTimeMillis();
139                     mTotalThreadTime += mCurrentThreadTime - start;
140                 }
141             }
142 
143         }
144     }
145 
toString()146     public synchronized String toString() {
147         String con = mConnection == null ? "" : mConnection.toString();
148         String active = mWaiting ? "w" : "a";
149         return "cid " + mId + " " + active + " "  + con;
150     }
151 
152 }
153