Search in sources :

Example 16 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project ion by koush.

the class ProgressTests method testProgress.

public void testProgress() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Semaphore progressSemaphore = new Semaphore(0);
    final Md5 md5 = Md5.createInstance();
    Ion.with(getContext()).load("https://raw.githubusercontent.com/koush/AndroidAsync/master/AndroidAsync/test/assets/6691924d7d24237d3b3679310157d640").setHandler(null).setTimeout(600000).setLogging("testProgress", Log.VERBOSE).progress(new ProgressCallback() {

        @Override
        public void onProgress(long downloaded, long total) {
            // depending on gzip, etc. the total may vary... the actual length of the uncompressed data
            // is 100000
            assertTrue(total > 90000 && total < 110000);
            progressSemaphore.release();
        }
    }).write(new ByteArrayOutputStream()).setCallback(new FutureCallback<ByteArrayOutputStream>() {

        @Override
        public void onCompleted(Exception e, ByteArrayOutputStream result) {
            byte[] bytes = result.toByteArray();
            md5.update(new ByteBufferList(bytes));
            assertEquals(md5.digest(), dataNameAndHash);
            semaphore.release();
        }
    });
    assertTrue(semaphore.tryAcquire(600000, TimeUnit.MILLISECONDS));
    assertTrue(progressSemaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) ProgressCallback(com.koushikdutta.ion.ProgressCallback) Semaphore(java.util.concurrent.Semaphore) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 17 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project PushSms by koush.

the class GcmSocket method write.

@Override
public void write(ByteBuffer bb) {
    write(new ByteBufferList(bb.duplicate()));
    bb.position(bb.limit());
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList)

Example 18 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList 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) {
    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(new InetSocketAddress(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 19 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class AsyncSocketMiddleware method idleSocket.

private void idleSocket(final AsyncSocket socket) {
    // must listen for socket close, otherwise log will get spammed.
    socket.setEndCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            socket.setClosedCallback(null);
            socket.close();
        }
    });
    socket.setWriteableCallback(null);
    // should not get any data after this point...
    // if so, eat it and disconnect.
    socket.setDataCallback(new DataCallback.NullDataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            super.onDataAvailable(emitter, bb);
            bb.recycle();
            socket.setClosedCallback(null);
            socket.close();
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataEmitter(com.koushikdutta.async.DataEmitter) DataCallback(com.koushikdutta.async.callback.DataCallback)

Example 20 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project ion by koush.

the class GsonTests method testParserCastingSuccess.

public void testParserCastingSuccess() throws Exception {
    ByteBufferList b = new ByteBufferList(ByteBuffer.wrap("{}".getBytes()));
    FilteredDataEmitter emitter = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    GsonObjectParser g = new GsonObjectParser();
    Future<JsonObject> ret = g.parse(emitter);
    emitter.onDataAvailable(emitter, b);
    emitter.getEndCallback().onCompleted(null);
    JsonObject j = ret.get();
    assertNotNull(j);
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) GsonObjectParser(com.koushikdutta.ion.gson.GsonObjectParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) JsonObject(com.google.gson.JsonObject)

Aggregations

ByteBufferList (com.koushikdutta.async.ByteBufferList)39 DataEmitter (com.koushikdutta.async.DataEmitter)13 DataCallback (com.koushikdutta.async.callback.DataCallback)13 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)10 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)10 ByteBuffer (java.nio.ByteBuffer)10 IOException (java.io.IOException)6 Semaphore (java.util.concurrent.Semaphore)5 PushParser (com.koushikdutta.async.PushParser)4 ExecutionException (java.util.concurrent.ExecutionException)4 JsonObject (com.google.gson.JsonObject)3 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)3 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)3 GsonObjectParser (com.koushikdutta.ion.gson.GsonObjectParser)3 TimeoutException (java.util.concurrent.TimeoutException)3 BEncodedDictionary (org.cyanogenmod.pushsms.bencode.BEncodedDictionary)3 RemoteException (android.os.RemoteException)2 JsonParseException (com.google.gson.JsonParseException)2 LineEmitter (com.koushikdutta.async.LineEmitter)2 TapCallback (com.koushikdutta.async.TapCallback)2