blob: d82553046f450828bb81427a47d159f6a3ed1480 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import java.lang.Thread;
23
24/**
25 * {@hide}
26 */
27class ConnectionThread extends Thread {
28
29 static final int WAIT_TIMEOUT = 5000;
30 static final int WAIT_TICK = 1000;
31
32 // Performance probe
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 long mCurrentThreadTime;
Grace Kloba340a1b22009-07-02 15:30:34 -070034 long mTotalThreadTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36 private boolean mWaiting;
37 private volatile boolean mRunning = true;
38 private Context mContext;
39 private RequestQueue.ConnectionManager mConnectionManager;
40 private RequestFeeder mRequestFeeder;
41
42 private int mId;
43 Connection mConnection;
44
45 ConnectionThread(Context context,
46 int id,
47 RequestQueue.ConnectionManager connectionManager,
48 RequestFeeder requestFeeder) {
49 super();
50 mContext = context;
51 setName("http" + id);
52 mId = id;
53 mConnectionManager = connectionManager;
54 mRequestFeeder = requestFeeder;
55 }
56
57 void requestStop() {
58 synchronized (mRequestFeeder) {
59 mRunning = false;
60 mRequestFeeder.notify();
61 }
62 }
63
64 /**
65 * Loop until app shutdown. Runs connections in priority
66 * order.
67 */
68 public void run() {
69 android.os.Process.setThreadPriority(
Patrick Scott4a06b682009-07-20 08:55:06 -040070 android.os.Process.THREAD_PRIORITY_DEFAULT +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 android.os.Process.THREAD_PRIORITY_LESS_FAVORABLE);
72
Grace Kloba340a1b22009-07-02 15:30:34 -070073 // these are used to get performance data. When it is not in the timing,
74 // mCurrentThreadTime is 0. When it starts timing, mCurrentThreadTime is
75 // first set to -1, it will be set to the current thread time when the
76 // next request starts.
77 mCurrentThreadTime = 0;
78 mTotalThreadTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80 while (mRunning) {
Grace Kloba340a1b22009-07-02 15:30:34 -070081 if (mCurrentThreadTime == -1) {
82 mCurrentThreadTime = SystemClock.currentThreadTimeMillis();
83 }
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 Request request;
86
87 /* Get a request to process */
88 request = mRequestFeeder.getRequest();
89
90 /* wait for work */
91 if (request == null) {
92 synchronized(mRequestFeeder) {
93 if (HttpLog.LOGV) HttpLog.v("ConnectionThread: Waiting for work");
94 mWaiting = true;
95 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 mRequestFeeder.wait();
97 } catch (InterruptedException e) {
98 }
99 mWaiting = false;
Grace Kloba340a1b22009-07-02 15:30:34 -0700100 if (mCurrentThreadTime != 0) {
101 mCurrentThreadTime = SystemClock
102 .currentThreadTimeMillis();
103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 }
105 } else {
106 if (HttpLog.LOGV) HttpLog.v("ConnectionThread: new request " +
107 request.mHost + " " + request );
108
Patrick Scott86806ce12009-10-01 15:54:46 -0400109 mConnection = mConnectionManager.getConnection(mContext,
110 request.mHost);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 mConnection.processRequests(request);
112 if (mConnection.getCanPersist()) {
Patrick Scott86806ce12009-10-01 15:54:46 -0400113 if (!mConnectionManager.recycleConnection(mConnection)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 mConnection.closeConnection();
115 }
116 } else {
117 mConnection.closeConnection();
118 }
119 mConnection = null;
Grace Kloba340a1b22009-07-02 15:30:34 -0700120
121 if (mCurrentThreadTime > 0) {
122 long start = mCurrentThreadTime;
123 mCurrentThreadTime = SystemClock.currentThreadTimeMillis();
124 mTotalThreadTime += mCurrentThreadTime - start;
125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 }
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}