use of com.koushikdutta.async.callback.DataCallback in project AndroidAsync by koush.
the class Util method pump.
public static void pump(final DataEmitter emitter, final DataSink sink, final CompletedCallback callback) {
final DataCallback dataCallback = new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
sink.write(bb);
if (bb.remaining() > 0)
emitter.pause();
}
};
emitter.setDataCallback(dataCallback);
sink.setWriteableCallback(new WritableCallback() {
@Override
public void onWriteable() {
emitter.resume();
}
});
final CompletedCallback wrapper = new CompletedCallback() {
boolean reported;
@Override
public void onCompleted(Exception ex) {
if (reported)
return;
reported = true;
emitter.setDataCallback(null);
emitter.setEndCallback(null);
sink.setClosedCallback(null);
sink.setWriteableCallback(null);
callback.onCompleted(ex);
}
};
emitter.setEndCallback(wrapper);
sink.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex == null)
ex = new IOException("sink was closed before emitter ended");
wrapper.onCompleted(ex);
}
});
}
use of com.koushikdutta.async.callback.DataCallback in project AndroidAsync by koush.
the class CacheTests method testFilteredDataEmitter.
public void testFilteredDataEmitter() throws Exception {
final Semaphore semaphore = new Semaphore(0);
FilteredDataEmitter f = new FilteredDataEmitter() {
@Override
public boolean isPaused() {
return false;
}
};
f.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
assertEquals(bb.readString(), "hello");
bb.recycle();
semaphore.release();
}
});
f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
f.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
fail();
}
});
f.close();
f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
}
use of com.koushikdutta.async.callback.DataCallback in project AndroidAsync by koush.
the class MultipartFormDataBody method onBoundaryStart.
@Override
protected void onBoundaryStart() {
final Headers headers = new Headers();
liner = new LineEmitter();
liner.setLineCallback(new StringCallback() {
@Override
public void onStringAvailable(String s) {
if (!"\r".equals(s)) {
headers.addLine(s);
} else {
handleLast();
liner = null;
setDataCallback(null);
Part part = new Part(headers);
if (mCallback != null)
mCallback.onPart(part);
if (getDataCallback() == null) {
// if (part.isFile()) {
// setDataCallback(new NullDataCallback());
// return;
// }
lastPart = part;
lastData = new ByteBufferList();
setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
bb.get(lastData);
}
});
}
}
}
});
setDataCallback(liner);
}
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 Util method emitAllData.
public static void emitAllData(DataEmitter emitter, ByteBufferList list) {
int remaining;
DataCallback handler = null;
while (!emitter.isPaused() && (handler = emitter.getDataCallback()) != null && (remaining = list.remaining()) > 0) {
handler.onDataAvailable(emitter, list);
if (remaining == list.remaining() && handler == emitter.getDataCallback() && !emitter.isPaused()) {
// this is generally indicative of failure...
// 1) The data callback has not changed
// 2) no data was consumed
// 3) the data emitter was not paused
// call byteBufferList.recycle() or read all the data to prevent this assertion.
// this is nice to have, as it identifies protocol or parsing errors.
// System.out.println("Data: " + list.peekString());
System.out.println("handler: " + handler);
list.recycle();
if (SUPRESS_DEBUG_EXCEPTIONS)
return;
throw new RuntimeException("mDataHandler failed to consume data, yet remains the mDataHandler.");
}
}
if (list.remaining() != 0 && !emitter.isPaused()) {
// not all the data was consumed...
// call byteBufferList.recycle() or read all the data to prevent this assertion.
// this is nice to have, as it identifies protocol or parsing errors.
// System.out.println("Data: " + list.peekString());
System.out.println("handler: " + handler);
System.out.println("emitter: " + emitter);
list.recycle();
if (SUPRESS_DEBUG_EXCEPTIONS)
return;
// throw new AssertionError("Not all data was consumed by Util.emitAllData");
}
}
Aggregations