use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class FutureTests method testFutureChain.
public void testFutureChain() throws Exception {
final Semaphore semaphore = new Semaphore(0);
final Continuation c = new Continuation(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
semaphore.release();
}
});
IntegerFuture i1;
c.add(i1 = IntegerFuture.create(2, 200));
IntegerFuture i2;
c.add(i2 = IntegerFuture.create(3, 200));
new Thread() {
public void run() {
c.start();
}
;
}.start();
assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
assertEquals((int) i1.get(), 2);
assertEquals((int) i2.get(), 3);
}
use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class FutureTests method testContinuationFail.
public void testContinuationFail() throws Exception {
final Semaphore semaphore = new Semaphore(0);
final Continuation c = new Continuation(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
assertNotNull(ex);
semaphore.release();
}
});
c.add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
throw new Exception("fail");
}
});
new Thread() {
public void run() {
c.start();
}
;
}.start();
assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
}
use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class AsyncSSLSocketWrapper method report.
private void report(Exception e) {
final HandshakeCallback hs = handshakeCallback;
if (hs != null) {
handshakeCallback = null;
mSocket.setDataCallback(new DataCallback.NullDataCallback());
mSocket.end();
// handshake sets this callback. unset it.
mSocket.setClosedCallback(null);
mSocket.close();
hs.onHandshakeCompleted(e, null);
return;
}
CompletedCallback cb = getEndCallback();
if (cb != null)
cb.onCompleted(e);
}
use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class Continuation method add.
public Continuation add(final DependentFuture future) {
future.setParent(this);
add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
future.get();
next.onCompleted(null);
}
});
return this;
}
use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class AsyncHttpClient method executeFile.
public Future<File> executeFile(AsyncHttpRequest req, final String filename, final FileCallback callback) {
final File file = new File(filename);
file.getParentFile().mkdirs();
final OutputStream fout;
try {
fout = new BufferedOutputStream(new FileOutputStream(file), 8192);
} catch (FileNotFoundException e) {
SimpleFuture<File> ret = new SimpleFuture<File>();
ret.setComplete(e);
return ret;
}
final FutureAsyncHttpResponse cancel = new FutureAsyncHttpResponse();
final SimpleFuture<File> ret = new SimpleFuture<File>() {
@Override
public void cancelCleanup() {
try {
cancel.get().setDataCallback(new DataCallback.NullDataCallback());
cancel.get().close();
} catch (Exception e) {
}
try {
fout.close();
} catch (Exception e) {
}
file.delete();
}
};
ret.setParent(cancel);
execute(req, 0, cancel, new HttpConnectCallback() {
long mDownloaded = 0;
@Override
public void onConnectCompleted(Exception ex, final AsyncHttpResponse response) {
if (ex != null) {
try {
fout.close();
} catch (IOException e) {
}
file.delete();
invoke(callback, ret, response, ex, null);
return;
}
invokeConnect(callback, response);
final long contentLength = HttpUtil.contentLength(response.headers());
response.setDataCallback(new OutputStreamDataCallback(fout) {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
mDownloaded += bb.remaining();
super.onDataAvailable(emitter, bb);
invokeProgress(callback, response, mDownloaded, contentLength);
}
});
response.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
try {
fout.close();
} catch (IOException e) {
ex = e;
}
if (ex != null) {
file.delete();
invoke(callback, ret, response, ex, null);
} else {
invoke(callback, ret, response, null, file);
}
}
});
}
});
return ret;
}
Aggregations