use of com.koushikdutta.async.callback.DataCallback in project AndroidAsync by koush.
the class DataEmitterReader method handlePendingData.
private boolean handlePendingData(DataEmitter emitter) {
if (mPendingReadLength > mPendingData.remaining())
return false;
DataCallback pendingRead = mPendingRead;
mPendingRead = null;
pendingRead.onDataAvailable(emitter, mPendingData);
assert !mPendingData.hasRemaining();
return true;
}
use of com.koushikdutta.async.callback.DataCallback in project PushSms by koush.
the class MiddlewareService method findOrCreateGcmSocket.
// given a registration, find/create the gcm socket that manages
// the secure connection between the two devices.
private GcmSocket findOrCreateGcmSocket(Registration registration) {
GcmSocket ret = gcmConnectionManager.findGcmSocket(registration);
if (ret == null) {
final GcmSocket gcmSocket = ret = gcmConnectionManager.createGcmSocket(registration, getNumber());
// parse data from the gcm connection as we get it
ret.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
parseGcmMessage(gcmSocket, bb);
// save the registration info (sequence numbers changed, etc)
registry.register(gcmSocket.registration.endpoint, gcmSocket.registration);
}
});
// on error, fail over any pending messages for this number
ret.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
for (String pending : new ArrayList<String>(messagesAwaitingAck.keySet())) {
String numberPart = pending.split(":")[0];
if (!PhoneNumberUtils.compare(MiddlewareService.this, numberPart, gcmSocket.getNumber()))
continue;
GcmText gcmText = messagesAwaitingAck.get(pending);
if (gcmText == null)
continue;
gcmText.manageFailure(handler, smsManager);
}
}
});
}
return ret;
}
use of com.koushikdutta.async.callback.DataCallback in project AndroidAsync by koush.
the class MultipartTests method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
httpServer = new AsyncHttpServer();
httpServer.setErrorCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
fail();
}
});
httpServer.listen(AsyncServer.getDefault(), 5000);
httpServer.post("/", new HttpServerRequestCallback() {
int gotten = 0;
@Override
public void onRequest(final AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
final MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
body.setMultipartCallback(new MultipartCallback() {
@Override
public void onPart(Part part) {
if (part.isFile()) {
body.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
gotten += bb.remaining();
bb.recycle();
}
});
}
}
});
request.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
response.send(body.getField("baz") + gotten + body.getField("foo"));
}
});
}
});
}
use of com.koushikdutta.async.callback.DataCallback in project AndroidAsync by koush.
the class HttpClientTests method testGithubRandomData.
public void testGithubRandomData() throws Exception {
final Semaphore semaphore = new Semaphore(0);
final Md5 md5 = Md5.createInstance();
AsyncHttpGet get = new AsyncHttpGet(github);
get.setLogging("AsyncTest", Log.VERBOSE);
client.execute(get, new HttpConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
assertNull(ex);
// make sure gzip decoding works, as that is generally what github sends.
// this broke sometime in 03/2014
// Assert.assertEquals("gzip", response.getHeaders().getContentEncoding());
response.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
md5.update(bb);
}
});
response.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
semaphore.release();
}
});
}
});
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
assertEquals(md5.digest(), dataNameAndHash);
}
use of com.koushikdutta.async.callback.DataCallback in project AndroidAsync by koush.
the class HttpClientTests method testClockworkMod.
public void testClockworkMod() throws Exception {
final Semaphore semaphore = new Semaphore(0);
final Md5 md5 = Md5.createInstance();
client.execute("http://www.clockworkmod.com", new HttpConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
// make sure gzip decoding works, as that is generally what github sends.
Assert.assertEquals("gzip", response.headers().get("Content-Encoding"));
response.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
md5.update(bb);
}
});
response.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
semaphore.release();
}
});
}
});
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
Aggregations