Skip to content

Commit 9ce5473

Browse files
authored
Merge pull request #225 from longbai/multizone-auto
Multizone auto
2 parents e02dc3b + 35c4acc commit 9ce5473

File tree

15 files changed

+100
-106
lines changed

15 files changed

+100
-106
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## 7.3.0 (2016-09-30)
44

5+
### 增加
6+
* 北美上传地址
7+
8+
## 7.3.0 (2016-09-30)
9+
510
### 增加
611
* 自动判断上传存储区
712

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Sun Jul 12 16:36:40 CST 2015
1+
#Mon Oct 24 19:53:45 CST 2016
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ String version = versionName()
2020
int code = versionNameToCode(version)
2121

2222
android {
23-
compileSdkVersion 23
24-
buildToolsVersion '23.0.1'
23+
compileSdkVersion 24
24+
buildToolsVersion '24.0.2'
2525
defaultConfig {
2626
//applicationId "com.qiniu.android"
2727
minSdkVersion 9

library/library.iml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@
6565
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
6666
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
6767
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
68-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
69-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
70-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
71-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
72-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
73-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
74-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
75-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
7668
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
7769
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
7870
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
@@ -81,6 +73,14 @@
8173
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
8274
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
8375
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
76+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
77+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
78+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
79+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
80+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
81+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
82+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
83+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
8484
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
8585
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
8686
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
@@ -106,7 +106,7 @@
106106
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
107107
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
108108
</content>
109-
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
109+
<orderEntry type="jdk" jdkName="Android API 24 Platform" jdkType="Android SDK" />
110110
<orderEntry type="sourceFolder" forTests="false" />
111111
<orderEntry type="library" exported="" name="okio-1.6.0" level="project" />
112112
<orderEntry type="library" exported="" name="okhttp-3.2.0" level="project" />

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,6 @@ public AutoZone(boolean https, DnsManager dns) {
3636
this.dns = dns;
3737
}
3838

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-
5439
private void getZoneJsonAsync(ZoneIndex index, CompletionHandler handler) {
5540
String address = ucServer + "/v1/query?ak=" + index.accessKey + "&bucket=" + index.bucket;
5641
client.asyncGet(address, null, handler);
@@ -75,13 +60,6 @@ private void putHosts(ZoneInfo info) {
7560
ZoneInfo zoneInfo(String ak, String bucket) {
7661
ZoneIndex index = new ZoneIndex(ak, bucket);
7762
ZoneInfo info = zones.get(index);
78-
// if (info == null) {
79-
// info = getZoneJson(index);
80-
// if (info != null) {
81-
// zones.put(index, info);
82-
// putHosts(info);
83-
// }
84-
// }
8563
return info;
8664
}
8765

@@ -125,7 +103,7 @@ public ServiceAddress upHostBackup(String token) {
125103
return new ServiceAddress(info.upBackup, new String[]{info.upIp});
126104
}
127105

128-
void preQueryIndex(final ZoneIndex index, final QueryHandler complete){
106+
void preQueryIndex(final ZoneIndex index, final QueryHandler complete) {
129107
if (index == null) {
130108
complete.onFailure(ResponseInfo.InvalidToken);
131109
return;
@@ -153,6 +131,7 @@ public void complete(ResponseInfo info, JSONObject response) {
153131
}
154132
});
155133
}
134+
156135
@Override
157136
public void preQuery(String token, QueryHandler complete) {
158137
ZoneIndex index = ZoneIndex.getFromToken(token);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public final class Constants {
5-
public static final String VERSION = "7.3.0";
5+
public static final String VERSION = "7.3.1";
66

77
public static final String UTF_8 = "utf-8";
88
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ public FixedZone(ServiceAddress up, ServiceAddress upBackup) {
1313
this.upBackup = upBackup;
1414
}
1515

16-
public ServiceAddress upHost(String token){
16+
public ServiceAddress upHost(String token) {
1717
return up;
1818
}
19-
public ServiceAddress upHostBackup(String token){
19+
20+
public ServiceAddress upHostBackup(String token) {
2021
return upBackup;
2122
}
2223

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

33
import com.qiniu.android.dns.DnsManager;
4-
import com.qiniu.android.http.CompletionHandler;
54

65
/**
76
* Created by bailong on 15/10/10.
87
*/
98
public abstract class Zone {
109
public static final Zone zone0 =
1110
createZone("upload.qiniu.com", "up.qiniu.com", "183.136.139.10", "115.231.182.136");
11+
1212
public static final Zone zone1 =
1313
createZone("upload-z1.qiniu.com", "up-z1.qiniu.com", "106.38.227.27", "106.38.227.28");
1414

1515
public static final Zone zone2 =
1616
createZone("upload-z2.qiniu.com", "up-z2.qiniu.com", "183.60.214.197", "14.152.37.7");
1717

18-
/**
19-
* 默认上传服务器
20-
*/
21-
public abstract ServiceAddress upHost(String token);
22-
23-
/**
24-
* 备用上传服务器,当默认服务器网络连接失败时使用
25-
*/
26-
public abstract ServiceAddress upHostBackup(String token);
18+
public static final Zone zoneNa0 =
19+
createZone("upload-na0.qiniu.com", "up-na0.qiniu.com", "23.236.102.3", "23.236.102.2");
2720

2821
private static Zone createZone(String upHost, String upHostBackup, String upIp, String upIp2) {
2922
String[] upIps = {upIp, upIp2};
@@ -32,7 +25,7 @@ private static Zone createZone(String upHost, String upHostBackup, String upIp,
3225
return new FixedZone(up, upBackup);
3326
}
3427

35-
public static void addDnsIp(DnsManager dns){
28+
public static void addDnsIp(DnsManager dns) {
3629
zone0.upHost("").addIpToDns(dns);
3730
zone0.upHostBackup("").addIpToDns(dns);
3831

@@ -43,10 +36,21 @@ public static void addDnsIp(DnsManager dns){
4336
zone2.upHostBackup("").addIpToDns(dns);
4437
}
4538

46-
public interface QueryHandler{
39+
/**
40+
* 默认上传服务器
41+
*/
42+
public abstract ServiceAddress upHost(String token);
43+
44+
/**
45+
* 备用上传服务器,当默认服务器网络连接失败时使用
46+
*/
47+
public abstract ServiceAddress upHostBackup(String token);
48+
49+
public abstract void preQuery(String token, QueryHandler complete);
50+
51+
public interface QueryHandler {
4752
void onSuccess();
53+
4854
void onFailure(int reason);
4955
}
50-
51-
public abstract void preQuery(String token, QueryHandler complete);
5256
}

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

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,51 @@ private static JSONObject buildJsonResp(byte[] body) throws Exception {
138138
return new JSONObject(str);
139139
}
140140

141+
private static ResponseInfo buildResponseInfo(okhttp3.Response response, String ip, long duration) {
142+
int code = response.code();
143+
String reqId = response.header("X-Reqid");
144+
reqId = (reqId == null) ? null : reqId.trim();
145+
byte[] body = null;
146+
String error = null;
147+
try {
148+
body = response.body().bytes();
149+
} catch (IOException e) {
150+
error = e.getMessage();
151+
}
152+
JSONObject json = null;
153+
if (ctype(response).equals(Client.JsonMime) && body != null) {
154+
try {
155+
json = buildJsonResp(body);
156+
if (response.code() != 200) {
157+
String err = new String(body, Constants.UTF_8);
158+
error = json.optString("error", err);
159+
}
160+
} catch (Exception e) {
161+
if (response.code() < 300) {
162+
error = e.getMessage();
163+
}
164+
}
165+
} else {
166+
error = body == null ? "null body" : new String(body);
167+
}
168+
169+
HttpUrl u = response.request().url();
170+
return new ResponseInfo(json, code, reqId, response.header("X-Log"),
171+
via(response), u.host(), u.encodedPath(), ip, u.port(), duration, 0, error);
172+
}
173+
174+
private static void onRet(okhttp3.Response response, String ip, long duration,
175+
final CompletionHandler complete) {
176+
final ResponseInfo info = buildResponseInfo(response, ip, duration);
177+
178+
AsyncRun.runInMain(new Runnable() {
179+
@Override
180+
public void run() {
181+
complete.complete(info, info.response);
182+
}
183+
});
184+
}
185+
141186
public void asyncSend(final Request.Builder requestBuilder, StringMap headers, final CompletionHandler complete) {
142187
if (headers != null) {
143188
headers.forEach(new StringMap.Consumer() {
@@ -252,52 +297,7 @@ public void accept(String key, Object value) {
252297
asyncSend(requestBuilder, null, completionHandler);
253298
}
254299

255-
private static ResponseInfo buildResponseInfo(okhttp3.Response response, String ip, long duration){
256-
int code = response.code();
257-
String reqId = response.header("X-Reqid");
258-
reqId = (reqId == null) ? null : reqId.trim();
259-
byte[] body = null;
260-
String error = null;
261-
try {
262-
body = response.body().bytes();
263-
} catch (IOException e) {
264-
error = e.getMessage();
265-
}
266-
JSONObject json = null;
267-
if (ctype(response).equals(Client.JsonMime) && body != null) {
268-
try {
269-
json = buildJsonResp(body);
270-
if (response.code() != 200) {
271-
String err = new String(body, Constants.UTF_8);
272-
error = json.optString("error", err);
273-
}
274-
} catch (Exception e) {
275-
if (response.code() < 300) {
276-
error = e.getMessage();
277-
}
278-
}
279-
} else {
280-
error = body == null ? "null body" : new String(body);
281-
}
282-
283-
HttpUrl u = response.request().url();
284-
return new ResponseInfo(json, code, reqId, response.header("X-Log"),
285-
via(response), u.host(), u.encodedPath(), ip, u.port(), duration, 0, error);
286-
}
287-
288-
private static void onRet(okhttp3.Response response, String ip, long duration,
289-
final CompletionHandler complete) {
290-
final ResponseInfo info = buildResponseInfo(response, ip, duration);
291-
292-
AsyncRun.runInMain(new Runnable() {
293-
@Override
294-
public void run() {
295-
complete.complete(info, info.response);
296-
}
297-
});
298-
}
299-
300-
public void asyncGet(String url, StringMap headers, CompletionHandler completionHandler){
300+
public void asyncGet(String url, StringMap headers, CompletionHandler completionHandler) {
301301
Request.Builder requestBuilder = new Request.Builder().get().url(url);
302302
asyncSend(requestBuilder, headers, completionHandler);
303303
}

library/src/main/java/com/qiniu/android/storage/Configuration.java

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

33

4-
import com.qiniu.android.common.ServiceAddress;
54
import com.qiniu.android.common.Zone;
65
import com.qiniu.android.dns.DnsManager;
76
import com.qiniu.android.dns.IResolver;
@@ -83,13 +82,13 @@ private Configuration(Builder builder) {
8382

8483
urlConverter = builder.urlConverter;
8584

86-
zone = builder.zone == null? Zone.zone0 : builder.zone;
85+
zone = builder.zone == null ? Zone.zone0 : builder.zone;
8786
dns = initDns(builder);
8887
}
8988

9089
private static DnsManager initDns(Builder builder) {
9190
DnsManager d = builder.dns;
92-
if (d != null){
91+
if (d != null) {
9392
Zone.addDnsIp(d);
9493
}
9594

library/src/main/java/com/qiniu/android/storage/FormUploader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void complete(ResponseInfo info, JSONObject response) {
135135
};
136136
URI u = config.zone.upHost(token.token).address;
137137
if (config.zone.upHostBackup(token.token) != null
138-
&&(info.needSwitchServer() || info.isNotQiniu())) {
138+
&& (info.needSwitchServer() || info.isNotQiniu())) {
139139
u = config.zone.upHostBackup(token.token).address;
140140
}
141141

library/src/main/java/com/qiniu/android/storage/ResumeUploader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public void complete(ResponseInfo info, JSONObject response) {
280280
}
281281

282282
if (config.zone.upHostBackup(token.token) != null
283-
&&((isNotChunkToQiniu(info, response) || info.needRetry())
283+
&& ((isNotChunkToQiniu(info, response) || info.needRetry())
284284
&& retried < config.retryMax)) {
285285
nextTask(offset, retried + 1, config.zone.upHostBackup(token.token).address);
286286
return;

library/src/main/java/com/qiniu/android/utils/AsyncRun.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static void runInMain(Runnable r) {
1212
h.post(r);
1313
}
1414

15-
public static void runInBack(Runnable r){
15+
public static void runInBack(Runnable r) {
1616

1717
}
1818
}

0 commit comments

Comments
 (0)