aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKoushik Dutta <koushd@gmail.com>2013-03-20 16:36:12 -0700
committerKoushik Dutta <koushd@gmail.com>2013-03-20 16:36:12 -0700
commitf8e2cdf4bdad484cb1966cc6ce795a9706ae0390 (patch)
tree30e694642350b3de03f26dc97859b02d9dd30667
parentde1f928555f3342692841d0d7250b5d27a27f67c (diff)
downloadAndroidAsync-f8e2cdf4bdad484cb1966cc6ce795a9706ae0390.tar.gz
AndroidAsync-f8e2cdf4bdad484cb1966cc6ce795a9706ae0390.tar.bz2
AndroidAsync-f8e2cdf4bdad484cb1966cc6ce795a9706ae0390.zip
wip
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/WrapperSocket.java50
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java16
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClientMiddleware.java3
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/AsyncSocketMiddleware.java17
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/filter/ContentLengthFilter.java25
5 files changed, 61 insertions, 50 deletions
diff --git a/AndroidAsync/src/com/koushikdutta/async/WrapperSocket.java b/AndroidAsync/src/com/koushikdutta/async/WrapperSocket.java
index 41afe36..694cb15 100644
--- a/AndroidAsync/src/com/koushikdutta/async/WrapperSocket.java
+++ b/AndroidAsync/src/com/koushikdutta/async/WrapperSocket.java
@@ -3,58 +3,18 @@ package com.koushikdutta.async;
import java.nio.ByteBuffer;
import com.koushikdutta.async.callback.CompletedCallback;
-import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.callback.WritableCallback;
-public class WrapperSocket implements AsyncSocket {
+public class WrapperSocket extends FilteredDataEmitter implements AsyncSocket {
private AsyncSocket mSocket;
public void setSocket(AsyncSocket socket) {
mSocket = socket;
+ setDataEmitter(mSocket);
}
public AsyncSocket getSocket() {
return mSocket;
}
-
- @Override
- public void setDataCallback(DataCallback callback) {
- mSocket.setDataCallback(callback);
- }
-
- @Override
- public DataCallback getDataCallback() {
- return mSocket.getDataCallback();
- }
-
- @Override
- public boolean isChunked() {
- return mSocket.isChunked();
- }
-
- @Override
- public void pause() {
- mSocket.pause();
- }
-
- @Override
- public void resume() {
- mSocket.resume();
- }
-
- @Override
- public boolean isPaused() {
- return mSocket.isPaused();
- }
-
- @Override
- public void setEndCallback(CompletedCallback callback) {
- mSocket.setEndCallback(callback);
- }
-
- @Override
- public CompletedCallback getEndCallback() {
- return mSocket.getEndCallback();
- }
@Override
public void write(ByteBuffer bb) {
@@ -95,10 +55,4 @@ public class WrapperSocket implements AsyncSocket {
public CompletedCallback getClosedCallback() {
return mSocket.getClosedCallback();
}
-
- @Override
- public AsyncServer getServer() {
- return mSocket.getServer();
- }
-
}
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java b/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java
index 5a9b6a7..c77d446 100644
--- a/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java
+++ b/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java
@@ -7,6 +7,7 @@ import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.ByteBuffer;
+import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.concurrent.CancellationException;
@@ -17,7 +18,6 @@ import org.json.JSONObject;
import android.os.Handler;
import android.os.Looper;
-import android.util.Log;
import com.koushikdutta.async.AsyncSSLException;
import com.koushikdutta.async.AsyncSSLSocket;
@@ -44,6 +44,13 @@ public class AsyncHttpClient {
return mDefaultInstance;
}
+ ArrayList<AsyncHttpClientMiddleware> mMiddleware = new ArrayList<AsyncHttpClientMiddleware>();
+ public void addMiddleware(AsyncHttpClientMiddleware middleware) {
+ synchronized (mMiddleware) {
+ mMiddleware.add(middleware);
+ }
+ }
+
private Hashtable<String, HashSet<AsyncSocket>> mSockets = new Hashtable<String, HashSet<AsyncSocket>>();
AsyncServer mServer;
public AsyncHttpClient(AsyncServer server) {
@@ -249,6 +256,13 @@ public class AsyncHttpClient {
ret.setSocket(socket);
}
};
+
+ synchronized (mMiddleware) {
+ for (AsyncHttpClientMiddleware middleware: mMiddleware) {
+ if (middleware.getSocket(request, socketConnected))
+ return;
+ }
+ }
HashSet<AsyncSocket> sockets = mSockets.get(lookup);
if (sockets != null) {
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClientMiddleware.java b/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClientMiddleware.java
index 59756f8..2198fb7 100644
--- a/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClientMiddleware.java
+++ b/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClientMiddleware.java
@@ -1,8 +1,9 @@
package com.koushikdutta.async.http;
import com.koushikdutta.async.AsyncSocket;
+import com.koushikdutta.async.callback.ConnectCallback;
public interface AsyncHttpClientMiddleware {
- public AsyncSocket getSocket(final AsyncHttpRequest request, final HttpConnectCallback callback);
+ public boolean getSocket(final AsyncHttpRequest request, final ConnectCallback callback);
public AsyncSocket onSocket(AsyncSocket socket);
}
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/AsyncSocketMiddleware.java b/AndroidAsync/src/com/koushikdutta/async/http/AsyncSocketMiddleware.java
new file mode 100644
index 0000000..eb4159f
--- /dev/null
+++ b/AndroidAsync/src/com/koushikdutta/async/http/AsyncSocketMiddleware.java
@@ -0,0 +1,17 @@
+package com.koushikdutta.async.http;
+
+import com.koushikdutta.async.AsyncSocket;
+import com.koushikdutta.async.callback.ConnectCallback;
+
+public class AsyncSocketMiddleware implements AsyncHttpClientMiddleware {
+ @Override
+ public boolean getSocket(AsyncHttpRequest request, ConnectCallback callback) {
+
+ return true;
+ }
+
+ @Override
+ public AsyncSocket onSocket(AsyncSocket socket) {
+ return null;
+ }
+}
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/filter/ContentLengthFilter.java b/AndroidAsync/src/com/koushikdutta/async/http/filter/ContentLengthFilter.java
new file mode 100644
index 0000000..2dc8346
--- /dev/null
+++ b/AndroidAsync/src/com/koushikdutta/async/http/filter/ContentLengthFilter.java
@@ -0,0 +1,25 @@
+package com.koushikdutta.async.http.filter;
+
+import junit.framework.Assert;
+
+import com.koushikdutta.async.ByteBufferList;
+import com.koushikdutta.async.DataEmitter;
+import com.koushikdutta.async.FilteredDataEmitter;
+
+public class ContentLengthFilter extends FilteredDataEmitter {
+ public ContentLengthFilter(int contentLength) {
+ this.contentLength = contentLength;
+ }
+
+ int contentLength;
+ int totalRead;
+ @Override
+ public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
+ Assert.assertTrue(totalRead < contentLength);
+ ByteBufferList list = bb.get(Math.min(contentLength - totalRead, bb.remaining()));
+ totalRead += list.remaining();
+ super.onDataAvailable(emitter, list);
+ if (totalRead == contentLength)
+ report(null);
+ }
+}