Skip to content

Commit 6f09ee8

Browse files
committed
change zone query to async
1 parent 1d75820 commit 6f09ee8

File tree

10 files changed

+223
-77
lines changed

10 files changed

+223
-77
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#Changelog
22

3+
## 7.3.0 (2016-09-30)
4+
5+
### 增加
6+
* 自动判断上传存储区
7+
38
## 7.2.3 (2016-09-07)
49

510
### 增加

library/src/androidTest/java/com/qiniu/android/HttpTest.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,28 +156,28 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
156156
Assert.assertEquals(ResponseInfo.UnknownHost, info.statusCode);
157157
}
158158

159-
@SmallTest
160-
public void testPostNoPort() throws Throwable {
161-
162-
httpManager.asyncPost("http://up.qiniu.com:12345", "hello".getBytes(),
163-
null, null, new CompletionHandler() {
164-
@Override
165-
public void complete(ResponseInfo rinfo, JSONObject response) {
166-
Log.d("qiniutest", rinfo.toString());
167-
info = rinfo;
168-
signal.countDown();
169-
}
170-
}, null);
171-
172-
try {
173-
signal.await(60, TimeUnit.SECONDS); // wait for callback
174-
} catch (InterruptedException e) {
175-
e.printStackTrace();
176-
}
177-
Assert.assertEquals("", info.reqId);
178-
Assert.assertTrue(ResponseInfo.CannotConnectToHost == info.statusCode ||
179-
ResponseInfo.TimedOut == info.statusCode);
180-
}
159+
// @SmallTest
160+
// public void testPostNoPort() throws Throwable {
161+
//
162+
// httpManager.asyncPost("http://up.qiniu.com:12345", "hello".getBytes(),
163+
// null, null, new CompletionHandler() {
164+
// @Override
165+
// public void complete(ResponseInfo rinfo, JSONObject response) {
166+
// Log.d("qiniutest", rinfo.toString());
167+
// info = rinfo;
168+
// signal.countDown();
169+
// }
170+
// }, null);
171+
//
172+
// try {
173+
// signal.await(60, TimeUnit.SECONDS); // wait for callback
174+
// } catch (InterruptedException e) {
175+
// e.printStackTrace();
176+
// }
177+
// Assert.assertEquals("", info.reqId);
178+
// Assert.assertTrue(ResponseInfo.CannotConnectToHost == info.statusCode ||
179+
// ResponseInfo.TimedOut == info.statusCode);
180+
// }
181181

182182
@SmallTest
183183
public void testPostIP() throws Throwable {

library/src/androidTest/java/com/qiniu/android/common/AutoZoneTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import junit.framework.Assert;
88

9+
import java.util.concurrent.CountDownLatch;
10+
911
/**
1012
* Created by long on 2016/9/30.
1113
*/
@@ -16,6 +18,24 @@ public class AutoZoneTest extends AndroidTestCase {
1618

1719
public void testHttp() {
1820
AutoZone zone = new AutoZone(false, null);
21+
final CountDownLatch countDownLatch = new CountDownLatch(1);
22+
zone.preQueryIndex(new AutoZone.ZoneIndex(ak, bkt), new Zone.QueryHandler() {
23+
@Override
24+
public void onSuccess() {
25+
countDownLatch.countDown();
26+
}
27+
28+
@Override
29+
public void onFailure(int reason) {
30+
countDownLatch.countDown();
31+
fail();
32+
}
33+
});
34+
try {
35+
countDownLatch.await();
36+
} catch (InterruptedException e) {
37+
e.printStackTrace();
38+
}
1939
AutoZone.ZoneInfo zoneInfo = zone.zoneInfo(ak, bkt);
2040
assertEquals(zoneInfo.upHost, "http://upload.qiniu.com");
2141
assertEquals(zoneInfo.upBackup, "http://up.qiniu.com");
@@ -36,6 +56,24 @@ public void testSplitE() {
3656

3757
public void testC1() {
3858
AutoZone autoZone = new AutoZone(false, null);
59+
final CountDownLatch countDownLatch = new CountDownLatch(1);
60+
autoZone.preQueryIndex(new AutoZone.ZoneIndex(ak, bkt), new Zone.QueryHandler() {
61+
@Override
62+
public void onSuccess() {
63+
countDownLatch.countDown();
64+
}
65+
66+
@Override
67+
public void onFailure(int reason) {
68+
countDownLatch.countDown();
69+
fail();
70+
}
71+
});
72+
try {
73+
countDownLatch.await();
74+
} catch (InterruptedException e) {
75+
e.printStackTrace();
76+
}
3977
AutoZone.ZoneInfo info = autoZone.zoneInfo(ak, bkt);
4078
System.out.println("zone0: " + info.toString());
4179

library/src/main/java/com/qiniu/android/common/AutoZone.java

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.qiniu.android.common;
22

33
import com.qiniu.android.dns.DnsManager;
4-
import com.qiniu.android.dns.Domain;
54
import com.qiniu.android.http.Client;
5+
import com.qiniu.android.http.CompletionHandler;
66
import com.qiniu.android.http.ResponseInfo;
77
import com.qiniu.android.utils.UrlSafeBase64;
88

@@ -12,7 +12,6 @@
1212

1313
import java.net.URI;
1414
import java.net.URISyntaxException;
15-
import java.util.List;
1615
import java.util.Map;
1716
import java.util.concurrent.ConcurrentHashMap;
1817

@@ -21,9 +20,9 @@
2120
*/
2221

2322
public final class AutoZone extends Zone {
24-
private final String ucServer;
2523
private static Map<ZoneIndex, ZoneInfo> zones = new ConcurrentHashMap<>();
2624
private static Client client = new Client();
25+
private final String ucServer;
2726
private final DnsManager dns;
2827
private final boolean https;
2928

@@ -37,23 +36,28 @@ public AutoZone(boolean https, DnsManager dns) {
3736
this.dns = dns;
3837
}
3938

40-
private ZoneInfo getZoneJson(ZoneIndex index) {
39+
// private ZoneInfo getZoneJson(ZoneIndex index) {
40+
// String address = ucServer + "/v1/query?ak=" + index.accessKey + "&bucket=" + index.bucket;
41+
//
42+
// ResponseInfo r = client.syncGet(address, null);
43+
// if (r.isOK()){
44+
// try {
45+
// return ZoneInfo.buildFromJson(r.response);
46+
// } catch (JSONException e) {
47+
// e.printStackTrace();
48+
// return null;
49+
// }
50+
// }
51+
// return null;
52+
// }
53+
54+
private void getZoneJsonAsync(ZoneIndex index, CompletionHandler handler) {
4155
String address = ucServer + "/v1/query?ak=" + index.accessKey + "&bucket=" + index.bucket;
42-
43-
ResponseInfo r = client.syncGet(address, null);
44-
if (r.isOK()){
45-
try {
46-
return ZoneInfo.buildFromJson(r.response);
47-
} catch (JSONException e) {
48-
e.printStackTrace();
49-
return null;
50-
}
51-
}
52-
return null;
56+
client.asyncGet(address, null, handler);
5357
}
5458

55-
private void putHosts(ZoneInfo info){
56-
if (dns != null){
59+
private void putHosts(ZoneInfo info) {
60+
if (dns != null) {
5761
try {
5862
String httpDomain = new URI(info.upHost).getHost();
5963
String httpsDomain = new URI(info.upHttps).getHost();
@@ -71,13 +75,13 @@ private void putHosts(ZoneInfo info){
7175
ZoneInfo zoneInfo(String ak, String bucket) {
7276
ZoneIndex index = new ZoneIndex(ak, bucket);
7377
ZoneInfo info = zones.get(index);
74-
if (info == null) {
75-
info = getZoneJson(index);
76-
if (info != null) {
77-
zones.put(index, info);
78-
putHosts(info);
79-
}
80-
}
78+
// if (info == null) {
79+
// info = getZoneJson(index);
80+
// if (info != null) {
81+
// zones.put(index, info);
82+
// putHosts(info);
83+
// }
84+
// }
8185
return info;
8286
}
8387

@@ -104,7 +108,7 @@ public ServiceAddress upHost(String token) {
104108
if (info == null) {
105109
return null;
106110
}
107-
if (https){
111+
if (https) {
108112
return new ServiceAddress(info.upHttps);
109113
}
110114
return new ServiceAddress(info.upHost, new String[]{info.upIp});
@@ -115,12 +119,46 @@ public ServiceAddress upHostBackup(String token) {
115119
if (info == null) {
116120
return null;
117121
}
118-
if (https){
122+
if (https) {
119123
return null;
120124
}
121125
return new ServiceAddress(info.upBackup, new String[]{info.upIp});
122126
}
123127

128+
void preQueryIndex(final ZoneIndex index, final QueryHandler complete){
129+
if (index == null) {
130+
complete.onFailure(ResponseInfo.InvalidToken);
131+
return;
132+
}
133+
ZoneInfo info = zones.get(index);
134+
if (info != null) {
135+
complete.onSuccess();
136+
return;
137+
}
138+
139+
getZoneJsonAsync(index, new CompletionHandler() {
140+
@Override
141+
public void complete(ResponseInfo info, JSONObject response) {
142+
if (info.isOK() && response != null) {
143+
try {
144+
ZoneInfo info2 = ZoneInfo.buildFromJson(response);
145+
zones.put(index, info2);
146+
putHosts(info2);
147+
complete.onSuccess();
148+
} catch (JSONException e) {
149+
e.printStackTrace();
150+
complete.onFailure(ResponseInfo.NetworkError);
151+
}
152+
}
153+
}
154+
});
155+
}
156+
@Override
157+
public void preQuery(String token, QueryHandler complete) {
158+
ZoneIndex index = ZoneIndex.getFromToken(token);
159+
preQueryIndex(index, complete);
160+
}
161+
124162
static class ZoneInfo {
125163
final String upHost;
126164
final String upIp;
@@ -146,7 +184,7 @@ static ZoneInfo buildFromJson(JSONObject obj) throws JSONException {
146184
}
147185
}
148186

149-
private static class ZoneIndex {
187+
static class ZoneIndex {
150188
private final String accessKey;
151189
private final String bucket;
152190

@@ -155,6 +193,24 @@ private static class ZoneIndex {
155193
this.bucket = bucket;
156194
}
157195

196+
public static ZoneIndex getFromToken(String token) {
197+
// http://developer.qiniu.com/article/developer/security/upload-token.html
198+
// http://developer.qiniu.com/article/developer/security/put-policy.html
199+
String[] strings = token.split(":");
200+
String ak = strings[0];
201+
String policy = null;
202+
try {
203+
policy = new String(UrlSafeBase64.decode(strings[2]), Constants.UTF_8);
204+
JSONObject obj = new JSONObject(policy);
205+
String scope = obj.getString("scope");
206+
String bkt = scope.split(":")[0];
207+
return new ZoneIndex(ak, bkt);
208+
} catch (Exception e) {
209+
e.printStackTrace();
210+
}
211+
return null;
212+
}
213+
158214
public int hashCode() {
159215
return accessKey.hashCode() * 37 + bucket.hashCode();
160216
}

library/src/main/java/com/qiniu/android/common/FixedZone.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public ServiceAddress upHost(String token){
1919
public ServiceAddress upHostBackup(String token){
2020
return upBackup;
2121
}
22+
23+
@Override
24+
public void preQuery(String token, QueryHandler complete) {
25+
complete.onSuccess();
26+
}
2227
}

library/src/main/java/com/qiniu/android/common/Zone.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.qiniu.android.common;
22

33
import com.qiniu.android.dns.DnsManager;
4+
import com.qiniu.android.http.CompletionHandler;
45

56
/**
67
* Created by bailong on 15/10/10.
@@ -17,16 +18,12 @@ public abstract class Zone {
1718
/**
1819
* 默认上传服务器
1920
*/
20-
public ServiceAddress upHost(String token){
21-
throw new UnsupportedOperationException();
22-
}
21+
public abstract ServiceAddress upHost(String token);
2322

2423
/**
2524
* 备用上传服务器,当默认服务器网络连接失败时使用
2625
*/
27-
public ServiceAddress upHostBackup(String token){
28-
throw new UnsupportedOperationException();
29-
}
26+
public abstract ServiceAddress upHostBackup(String token);
3027

3128
private static Zone createZone(String upHost, String upHostBackup, String upIp, String upIp2) {
3229
String[] upIps = {upIp, upIp2};
@@ -45,4 +42,11 @@ public static void addDnsIp(DnsManager dns){
4542
zone2.upHost("").addIpToDns(dns);
4643
zone2.upHostBackup("").addIpToDns(dns);
4744
}
45+
46+
public interface QueryHandler{
47+
void onSuccess();
48+
void onFailure(int reason);
49+
}
50+
51+
public abstract void preQuery(String token, QueryHandler complete);
4852
}

library/src/main/java/com/qiniu/android/http/Client.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,19 +289,24 @@ private static void onRet(okhttp3.Response response, String ip, long duration,
289289
final CompletionHandler complete) {
290290
final ResponseInfo info = buildResponseInfo(response, ip, duration);
291291

292-
AsyncRun.run(new Runnable() {
292+
AsyncRun.runInMain(new Runnable() {
293293
@Override
294294
public void run() {
295295
complete.complete(info, info.response);
296296
}
297297
});
298298
}
299299

300-
public ResponseInfo syncGet(String url, StringMap headers){
300+
public void asyncGet(String url, StringMap headers, CompletionHandler completionHandler){
301301
Request.Builder requestBuilder = new Request.Builder().get().url(url);
302-
return send(requestBuilder, headers);
302+
asyncSend(requestBuilder, headers, completionHandler);
303303
}
304304

305+
// public ResponseInfo syncGet(String url, StringMap headers){
306+
// Request.Builder requestBuilder = new Request.Builder().get().url(url);
307+
// return send(requestBuilder, headers);
308+
// }
309+
305310
public ResponseInfo send(final Request.Builder requestBuilder, StringMap headers) {
306311
if (headers != null) {
307312
headers.forEach(new StringMap.Consumer() {

library/src/main/java/com/qiniu/android/http/CountingRequestBody.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void write(Buffer source, long byteCount) throws IOException {
7272
super.write(source, byteCount);
7373
bytesWritten += byteCount;
7474
if (progress != null) {
75-
AsyncRun.run(new Runnable() {
75+
AsyncRun.runInMain(new Runnable() {
7676
@Override
7777
public void run() {
7878
try {

0 commit comments

Comments
 (0)