Skip to content

Commit 37fa91b

Browse files
committed
Merge pull request #116 from simon-liubin/add/port
Add/port
2 parents bccec38 + 0beb8a8 commit 37fa91b

File tree

5 files changed

+152
-16
lines changed

5 files changed

+152
-16
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.qiniu.android;
2+
3+
import android.test.InstrumentationTestCase;
4+
import android.test.suitebuilder.annotation.MediumTest;
5+
import android.test.suitebuilder.annotation.SmallTest;
6+
import android.util.Log;
7+
8+
import com.qiniu.android.http.CompletionHandler;
9+
import com.qiniu.android.http.HttpManager;
10+
import com.qiniu.android.http.ResponseInfo;
11+
import com.qiniu.android.storage.Configuration;
12+
import com.qiniu.android.storage.UpCompletionHandler;
13+
import com.qiniu.android.storage.UploadManager;
14+
import com.qiniu.android.storage.UploadOptions;
15+
16+
import junit.framework.Assert;
17+
18+
import org.apache.http.Header;
19+
import org.apache.http.message.BasicHeader;
20+
import org.json.JSONObject;
21+
22+
import java.io.File;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
import java.util.concurrent.CountDownLatch;
26+
import java.util.concurrent.TimeUnit;
27+
28+
/**
29+
* Created by bailong on 14/10/12.
30+
*/
31+
public class PortTest extends InstrumentationTestCase {
32+
final CountDownLatch signal = new CountDownLatch(1);
33+
private UploadManager uploadManager;
34+
private volatile String key;
35+
private volatile ResponseInfo info;
36+
private volatile JSONObject resp;
37+
38+
@Override
39+
protected void setUp() throws Exception {
40+
Configuration config = new Configuration.Builder().upPort(8888).build();
41+
uploadManager = new UploadManager(config);
42+
}
43+
44+
@SmallTest
45+
public void testData() throws Throwable {
46+
final String expectKey = "你好;\"\r\n\r\n\r\n_port";
47+
Map<String, String> params = new HashMap<String, String>();
48+
params.put("x:foo", "fooval");
49+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
50+
51+
uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
52+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
53+
Log.i("qiniutest", k + rinfo);
54+
key = k;
55+
info = rinfo;
56+
resp = response;
57+
signal.countDown();
58+
}
59+
}, opt);
60+
61+
check(expectKey);
62+
}
63+
64+
@MediumTest
65+
public void test123K() throws Throwable {
66+
fileTemplate(123);
67+
}
68+
69+
@MediumTest
70+
public void test323K() throws Throwable {
71+
fileTemplate(323);
72+
}
73+
74+
@MediumTest
75+
public void test4223K() throws Throwable {
76+
fileTemplate(4223);
77+
}
78+
79+
public void fileTemplate(int size) throws Throwable {
80+
final String expectKey = "r=" + size + "k_port";
81+
final File f = TempFile.createFile(size);
82+
Map<String, String> params = new HashMap<String, String>();
83+
params.put("x:foo", "fooval");
84+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
85+
86+
uploadManager.put(f, expectKey, TestConfig.token, new UpCompletionHandler() {
87+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
88+
Log.i("qiniutest", k + rinfo);
89+
key = k;
90+
info = rinfo;
91+
resp = response;
92+
signal.countDown();
93+
}
94+
}, opt);
95+
96+
check(expectKey);
97+
}
98+
99+
private void check( final String expectKey){
100+
try {
101+
signal.await(120, TimeUnit.SECONDS); // wait for callback
102+
} catch (InterruptedException e) {
103+
e.printStackTrace();
104+
}
105+
// 尝试获取info信息。
106+
// key == null : 没进入 complete ? 什么导致的?
107+
if (!expectKey.equals(key)) {
108+
//此处通不过, travis 会打印信息
109+
Assert.assertEquals("", info);
110+
}
111+
if (info == null || !info.isOK()) {
112+
//此处通不过, travis 会打印信息
113+
Assert.assertEquals("", info);
114+
}
115+
Assert.assertEquals(expectKey, key);
116+
Assert.assertTrue(info.isOK());
117+
Assert.assertNotNull(info.reqId);
118+
Assert.assertNotNull(resp);
119+
}
120+
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@ public final class ResponseHandler extends AsyncHttpResponseHandler {
4848
*/
4949
private String ip = null;
5050

51+
/**
52+
* 服务器端口
53+
*/
54+
private int port = -1;
55+
5156
public ResponseHandler(String url, CompletionHandler completionHandler, ProgressHandler progressHandler) {
5257
super(Looper.getMainLooper());
5358
URI uri = null;
5459
try {
5560
uri = new URI(url);
5661
this.host = uri.getHost();
62+
this.port = uri.getPort();
5763
} catch (URISyntaxException e) {
5864
this.host = "N/A";
5965
e.printStackTrace();
@@ -63,9 +69,9 @@ public ResponseHandler(String url, CompletionHandler completionHandler, Progress
6369
}
6470

6571
private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers, byte[] responseBody,
66-
String host, String ip, double duration, Throwable error) {
72+
String host, String ip, int port, double duration, Throwable error) {
6773

68-
if(error != null && error instanceof CancellationHandler.CancellationException) {
74+
if (error != null && error instanceof CancellationHandler.CancellationException) {
6975
return ResponseInfo.cancelled();
7076
}
7177

@@ -132,7 +138,7 @@ private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers,
132138
}
133139
}
134140

135-
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, duration, err);
141+
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, port, duration, err);
136142
}
137143

138144
private static JSONObject buildJsonResp(byte[] body) throws Exception {
@@ -150,15 +156,15 @@ public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
150156
} catch (Exception e) {
151157
exception = e;
152158
}
153-
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, ip, duration, exception);
159+
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, ip, port, duration, exception);
154160
Log.i("upload----success", info.toString());
155161
completionHandler.complete(info, obj);
156162
}
157163

158164
@Override
159165
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
160166
double duration = (System.currentTimeMillis() - reqStartTime) / 1000.0;
161-
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, ip, duration, error);
167+
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, ip, port, duration, error);
162168
Log.i("upload----failed", info.toString());
163169
completionHandler.complete(info, null);
164170
}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public final class ResponseInfo {
5252
*/
5353
public final String ip;
5454

55-
public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, String host, String ip, double duration, String error) {
55+
/**
56+
* 服务器端口
57+
*/
58+
public final int port;
59+
60+
public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, String host, String ip, int port, double duration, String error) {
5661
this.statusCode = statusCode;
5762
this.reqId = reqId;
5863
this.xlog = xlog;
@@ -61,20 +66,21 @@ public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, Stri
6166
this.duration = duration;
6267
this.error = error;
6368
this.ip = ip;
69+
this.port = port;
6470
}
6571

6672
public static ResponseInfo cancelled() {
67-
return new ResponseInfo(Cancelled, "", "", "", "", "", 0, "cancelled by user");
73+
return new ResponseInfo(Cancelled, "", "", "", "", "", -1, 0, "cancelled by user");
6874
}
6975

7076
public static ResponseInfo invalidArgument(String message) {
71-
return new ResponseInfo(InvalidArgument, "", "", "", "", "", 0,
77+
return new ResponseInfo(InvalidArgument, "", "", "", "", "", -1, 0,
7278
message);
7379
}
7480

7581

7682
public static ResponseInfo fileError(Exception e) {
77-
return new ResponseInfo(InvalidFile, "", "", "", "", "",
83+
return new ResponseInfo(InvalidFile, "", "", "", "", "", -1,
7884
0, e.getMessage());
7985
}
8086

@@ -103,11 +109,11 @@ public boolean needSwitchServer() {
103109

104110
public boolean needRetry() {
105111
return !isCancelled() && (isNetworkBroken() || isServerError() || statusCode == 406
106-
|| (statusCode == 200 && error != null) );
112+
|| (statusCode == 200 && error != null));
107113
}
108114

109115
public String toString() {
110-
return String.format(Locale.ENGLISH, "{ResponseInfo:%s,status:%d, reqId:%s, xlog:%s, xvia:%s, host:%s, ip:%s, duration:%f s, error:%s}",
111-
super.toString(), statusCode, reqId, xlog, xvia, host, ip, duration, error);
116+
return String.format(Locale.ENGLISH, "{ResponseInfo:%s,status:%d, reqId:%s, xlog:%s, xvia:%s, host:%s, ip:%s, port:%d, duration:%f s, error:%s}",
117+
super.toString(), statusCode, reqId, xlog, xvia, host, ip, port, duration, error);
112118
}
113119
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void complete(ResponseInfo info, JSONObject response) {
107107
if (info.isOK()) {
108108
options.progressHandler.progress(key, 1.0);
109109
completionHandler.complete(key, info, response);
110-
} else if(info.needRetry()) {
110+
} else if (info.needRetry()) {
111111
CompletionHandler retried = new CompletionHandler() {
112112
@Override
113113
public void complete(ResponseInfo info, JSONObject response) {
@@ -121,13 +121,17 @@ public void complete(ResponseInfo info, JSONObject response) {
121121
if (info.needSwitchServer()) {
122122
host = config.upHostBackup;
123123
}
124-
httpManager.multipartPost("http://" + host, args, progress, retried, options.cancellationSignal);
124+
httpManager.multipartPost(genUploadAddress(host, config.upPort), args, progress, retried, options.cancellationSignal);
125125
} else {
126126
completionHandler.complete(key, info, response);
127127
}
128128
}
129129
};
130130

131-
httpManager.multipartPost("http://" + config.upHost, args, progress, completion, options.cancellationSignal);
131+
httpManager.multipartPost(genUploadAddress(config.upHost, config.upPort), args, progress, completion, options.cancellationSignal);
132+
}
133+
134+
private static String genUploadAddress(String host, int port) {
135+
return "http://" + host + ":" + port;
132136
}
133137
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void makeBlock(String host, int offset, int blockSize, int chunkSize, Pr
110110
private void putChunk(String host, int offset, int chunkSize, String context, ProgressHandler progress,
111111
CompletionHandler _completionHandler, UpCancellationSignal c) {
112112
int chunkOffset = offset % Configuration.BLOCK_SIZE;
113-
String url = format(Locale.ENGLISH, "http://%s/bput/%s/%d", host, context, chunkOffset);
113+
String url = format(Locale.ENGLISH, "http://%s:%d/bput/%s/%d", host, config.upPort, context, chunkOffset);
114114
try {
115115
file.seek(offset);
116116
file.read(chunkBuffer, 0, chunkSize);

0 commit comments

Comments
 (0)