Search in sources :

Example 6 with ContinuationCallback

use of com.koushikdutta.async.callback.ContinuationCallback in project AndroidAsync by koush.

the class FutureTests method testContinuation.

public void testContinuation() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    someValue = 0;
    final Continuation c = new Continuation(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            assertNull(ex);
            semaphore.release();
        }
    });
    c.add(new ContinuationCallback() {

        @Override
        public void onContinue(Continuation continuation, final CompletedCallback next) throws Exception {
            new Thread() {

                public void run() {
                    someValue++;
                    next.onCompleted(null);
                }

                ;
            }.start();
        }
    });
    c.add(new ContinuationCallback() {

        @Override
        public void onContinue(Continuation continuation, final CompletedCallback next) throws Exception {
            new Thread() {

                public void run() {
                    someValue++;
                    next.onCompleted(null);
                }

                ;
            }.start();
        }
    });
    c.add(new ContinuationCallback() {

        @Override
        public void onContinue(Continuation continuation, final CompletedCallback next) throws Exception {
            someValue++;
            next.onCompleted(null);
        }
    });
    new Thread() {

        public void run() {
            c.start();
        }

        ;
    }.start();
    assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
    assertEquals(someValue, 3);
}
Also used : Continuation(com.koushikdutta.async.future.Continuation) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ContinuationCallback(com.koushikdutta.async.callback.ContinuationCallback) Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with ContinuationCallback

use of com.koushikdutta.async.callback.ContinuationCallback in project AndroidAsync by koush.

the class FutureTests method testContinuationCancel.

public void testContinuationCancel() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Continuation c = new Continuation(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            fail();
            semaphore.release();
        }
    });
    c.setCancelCallback(new Runnable() {

        @Override
        public void run() {
            semaphore.release();
        }
    });
    c.add(new ContinuationCallback() {

        @Override
        public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
            Thread.sleep(10000);
        }
    });
    new Thread() {

        public void run() {
            c.start();
        }

        ;
    }.start();
    new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
            c.cancel();
        }

        ;
    }.start();
    assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
}
Also used : Continuation(com.koushikdutta.async.future.Continuation) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ContinuationCallback(com.koushikdutta.async.callback.ContinuationCallback) Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with ContinuationCallback

use of com.koushikdutta.async.callback.ContinuationCallback in project AndroidAsync by koush.

the class FutureTests method testChildContinuationCancel.

public void testChildContinuationCancel() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Continuation c = new Continuation(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            fail();
            semaphore.release();
        }
    });
    c.setCancelCallback(new Runnable() {

        @Override
        public void run() {
            semaphore.release();
        }
    });
    c.add(new ContinuationCallback() {

        @Override
        public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
            Thread.sleep(10000);
        }
    });
    final Continuation child = new Continuation();
    child.add(new ContinuationCallback() {

        @Override
        public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
            Thread.sleep(10000);
        }
    });
    c.add(child);
    new Thread() {

        public void run() {
            c.start();
        }

        ;
    }.start();
    new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
            child.cancel();
        }

        ;
    }.start();
    assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
}
Also used : Continuation(com.koushikdutta.async.future.Continuation) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ContinuationCallback(com.koushikdutta.async.callback.ContinuationCallback) Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with ContinuationCallback

use of com.koushikdutta.async.callback.ContinuationCallback in project AndroidAsync by koush.

the class MultipartFormDataBody method write.

@Override
public void write(AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
    if (mParts == null)
        return;
    Continuation c = new Continuation(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            completed.onCompleted(ex);
        //                if (ex == null)
        //                    sink.end();
        //                else
        //                    sink.close();
        }
    });
    for (final Part part : mParts) {
        c.add(new ContinuationCallback() {

            @Override
            public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
                byte[] bytes = part.getRawHeaders().toPrefixString(getBoundaryStart()).getBytes();
                com.koushikdutta.async.Util.writeAll(sink, bytes, next);
                written += bytes.length;
            }
        }).add(new ContinuationCallback() {

            @Override
            public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
                long partLength = part.length();
                if (partLength >= 0)
                    written += partLength;
                part.write(sink, next);
            }
        }).add(new ContinuationCallback() {

            @Override
            public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
                byte[] bytes = "\r\n".getBytes();
                com.koushikdutta.async.Util.writeAll(sink, bytes, next);
                written += bytes.length;
            }
        });
    }
    c.add(new ContinuationCallback() {

        @Override
        public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
            byte[] bytes = (getBoundaryEnd()).getBytes();
            com.koushikdutta.async.Util.writeAll(sink, bytes, next);
            written += bytes.length;
            assert written == totalToWrite;
        }
    });
    c.start();
}
Also used : Continuation(com.koushikdutta.async.future.Continuation) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ContinuationCallback(com.koushikdutta.async.callback.ContinuationCallback)

Aggregations

ContinuationCallback (com.koushikdutta.async.callback.ContinuationCallback)9 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)8 Continuation (com.koushikdutta.async.future.Continuation)7 CancellationException (java.util.concurrent.CancellationException)5 ExecutionException (java.util.concurrent.ExecutionException)5 Semaphore (java.util.concurrent.Semaphore)5 TimeoutException (java.util.concurrent.TimeoutException)5 Uri (android.net.Uri)1 AsyncSocket (com.koushikdutta.async.AsyncSocket)1 ConnectCallback (com.koushikdutta.async.callback.ConnectCallback)1 SimpleCancellable (com.koushikdutta.async.future.SimpleCancellable)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1