Search in sources :

Example 1 with DataCallback

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);
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) WritableCallback(com.koushikdutta.async.callback.WritableCallback) IOException(java.io.IOException) DataCallback(com.koushikdutta.async.callback.DataCallback) IOException(java.io.IOException)

Example 2 with DataCallback

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())));
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataEmitter(com.koushikdutta.async.DataEmitter) FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback)

Example 3 with DataCallback

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);
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) LineEmitter(com.koushikdutta.async.LineEmitter) Headers(com.koushikdutta.async.http.Headers) DataEmitter(com.koushikdutta.async.DataEmitter) StringCallback(com.koushikdutta.async.LineEmitter.StringCallback) DataCallback(com.koushikdutta.async.callback.DataCallback)

Example 4 with DataCallback

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;
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) GcmSocket(org.cyanogenmod.pushsms.socket.GcmSocket) DataEmitter(com.koushikdutta.async.DataEmitter) DataCallback(com.koushikdutta.async.callback.DataCallback) RemoteException(android.os.RemoteException) IOException(java.io.IOException)

Example 5 with DataCallback

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");
    }
}
Also used : DataCallback(com.koushikdutta.async.callback.DataCallback)

Aggregations

DataCallback (com.koushikdutta.async.callback.DataCallback)14 ByteBufferList (com.koushikdutta.async.ByteBufferList)11 DataEmitter (com.koushikdutta.async.DataEmitter)11 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)8 IOException (java.io.IOException)3 Semaphore (java.util.concurrent.Semaphore)3 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)2 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)2 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)2 ByteBuffer (java.nio.ByteBuffer)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Test (org.junit.Test)2 RemoteException (android.os.RemoteException)1 AsyncDatagramSocket (com.koushikdutta.async.AsyncDatagramSocket)1 AsyncServerSocket (com.koushikdutta.async.AsyncServerSocket)1 AsyncSocket (com.koushikdutta.async.AsyncSocket)1 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)1 LineEmitter (com.koushikdutta.async.LineEmitter)1