Search in sources :

Example 16 with SimpleFuture

use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.

the class Dns method lookup.

public static Future<DnsResponse> lookup(AsyncServer server, String host, final boolean multicast, final FutureCallback<DnsResponse> callback) {
    if (!server.isAffinityThread()) {
        SimpleFuture<DnsResponse> ret = new SimpleFuture<>();
        server.post(() -> ret.setComplete(lookup(server, host, multicast, callback)));
        return ret;
    }
    ByteBuffer packet = ByteBufferList.obtain(1024).order(ByteOrder.BIG_ENDIAN);
    short id = (short) new Random().nextInt();
    short flags = (short) setQuery(0);
    if (!multicast)
        flags = (short) setRecursion(flags);
    packet.putShort(id);
    packet.putShort(flags);
    // number questions
    packet.putShort(multicast ? (short) 1 : (short) 2);
    // number answer rr
    packet.putShort((short) 0);
    // number authority rr
    packet.putShort((short) 0);
    // number additional rr
    packet.putShort((short) 0);
    addName(packet, host);
    // query
    packet.putShort(multicast ? (short) 12 : (short) 1);
    // request internet address
    packet.putShort((short) 1);
    if (!multicast) {
        addName(packet, host);
        // AAAA query
        packet.putShort((short) 28);
        // request internet address
        packet.putShort((short) 1);
    }
    packet.flip();
    try {
        final AsyncDatagramSocket dgram;
        // todo, use the dns server...
        if (!multicast) {
            dgram = server.connectDatagram(new InetSocketAddress("8.8.8.8", 53));
        } else {
            // System.out.println("multicast dns...");
            dgram = AsyncServer.getDefault().openDatagram(null, 0, true);
            Field field = DatagramSocket.class.getDeclaredField("impl");
            field.setAccessible(true);
            Object impl = field.get(dgram.getSocket());
            Method method = impl.getClass().getDeclaredMethod("join", InetAddress.class);
            method.setAccessible(true);
            method.invoke(impl, InetAddress.getByName("224.0.0.251"));
            ((DatagramSocket) dgram.getSocket()).setBroadcast(true);
        }
        final SimpleFuture<DnsResponse> ret = new SimpleFuture<DnsResponse>() {

            @Override
            protected void cleanup() {
                super.cleanup();
                // System.out.println("multicast dns cleanup...");
                dgram.close();
            }
        };
        dgram.setDataCallback(new DataCallback() {

            @Override
            public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                try {
                    // System.out.println(dgram.getRemoteAddress());
                    DnsResponse response = DnsResponse.parse(bb);
                    // System.out.println(response);
                    response.source = dgram.getRemoteAddress();
                    if (!multicast) {
                        dgram.close();
                        ret.setComplete(response);
                    } else {
                        callback.onCompleted(null, response);
                    }
                } catch (Exception e) {
                }
                bb.recycle();
            }
        });
        if (!multicast)
            dgram.write(new ByteBufferList(packet));
        else
            dgram.send(new InetSocketAddress("224.0.0.251", 5353), packet);
        return ret;
    } catch (Exception e) {
        SimpleFuture<DnsResponse> ret = new SimpleFuture<DnsResponse>();
        ret.setComplete(e);
        if (multicast)
            callback.onCompleted(e, null);
        return ret;
    }
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) InetSocketAddress(java.net.InetSocketAddress) Method(java.lang.reflect.Method) DataCallback(com.koushikdutta.async.callback.DataCallback) ByteBuffer(java.nio.ByteBuffer) AsyncDatagramSocket(com.koushikdutta.async.AsyncDatagramSocket) Field(java.lang.reflect.Field) Random(java.util.Random) AsyncDatagramSocket(com.koushikdutta.async.AsyncDatagramSocket) DatagramSocket(java.net.DatagramSocket) DataEmitter(com.koushikdutta.async.DataEmitter) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 17 with SimpleFuture

use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.

the class FutureTests method testPostCancelCallback.

@Test
public void testPostCancelCallback() throws Exception {
    SimpleFuture<String> future = new SimpleFuture<String>();
    final Semaphore semaphore = new Semaphore(0);
    future.cancel();
    future.setCallback(new FutureCallback<String>() {

        @Override
        public void onCompleted(Exception e, String result) {
            assertTrue(e instanceof CancellationException);
            semaphore.release();
        }
    });
    assertTrue(semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS));
    assertNull(future.getCallback());
}
Also used : CancellationException(java.util.concurrent.CancellationException) Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) SimpleFuture(com.koushikdutta.async.future.SimpleFuture) Test(org.junit.Test)

Example 18 with SimpleFuture

use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.

the class FutureTests method testPreCancelCallback.

@Test
public void testPreCancelCallback() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    SimpleFuture<String> future = new SimpleFuture<String>();
    future.setCallback(new FutureCallback<String>() {

        @Override
        public void onCompleted(Exception e, String result) {
            assertTrue(e instanceof CancellationException);
            semaphore.release();
        }
    });
    assertNotNull(future.getCallback());
    future.cancel();
    assertTrue(semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS));
    assertNull(future.getCallback());
}
Also used : CancellationException(java.util.concurrent.CancellationException) Semaphore(java.util.concurrent.Semaphore) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) SimpleFuture(com.koushikdutta.async.future.SimpleFuture) Test(org.junit.Test)

Example 19 with SimpleFuture

use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.

the class ByteBufferListParser method parse.

@Override
public Future<ByteBufferList> parse(final DataEmitter emitter) {
    final ByteBufferList bb = new ByteBufferList();
    final SimpleFuture<ByteBufferList> ret = new SimpleFuture<ByteBufferList>() {

        @Override
        protected void cancelCleanup() {
            emitter.close();
        }
    };
    emitter.setDataCallback(new DataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList data) {
            data.get(bb);
        }
    });
    emitter.setEndCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            if (ex != null) {
                ret.setComplete(ex);
                return;
            }
            try {
                ret.setComplete(bb);
            } catch (Exception e) {
                ret.setComplete(e);
            }
        }
    });
    return ret;
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataEmitter(com.koushikdutta.async.DataEmitter) DataCallback(com.koushikdutta.async.callback.DataCallback) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Aggregations

SimpleFuture (com.koushikdutta.async.future.SimpleFuture)19 IOException (java.io.IOException)6 Bitmap (android.graphics.Bitmap)5 Point (android.graphics.Point)5 BitmapInfo (com.koushikdutta.ion.bitmap.BitmapInfo)5 ByteBufferList (com.koushikdutta.async.ByteBufferList)4 DataEmitter (com.koushikdutta.async.DataEmitter)4 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)4 DataCallback (com.koushikdutta.async.callback.DataCallback)4 File (java.io.File)4 InetSocketAddress (java.net.InetSocketAddress)4 TimeoutException (java.util.concurrent.TimeoutException)4 AsyncSSLException (com.koushikdutta.async.AsyncSSLException)3 Cancellable (com.koushikdutta.async.future.Cancellable)3 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)3 FileNotFoundException (java.io.FileNotFoundException)3 Future (com.koushikdutta.async.future.Future)2 FutureCallback (com.koushikdutta.async.future.FutureCallback)2 InputStream (java.io.InputStream)2 CancelledKeyException (java.nio.channels.CancelledKeyException)2