Search in sources :

Example 6 with DataCallback

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

the class DataEmitterReader method handlePendingData.

private boolean handlePendingData(DataEmitter emitter) {
    if (mPendingReadLength > mPendingData.remaining())
        return false;
    DataCallback pendingRead = mPendingRead;
    mPendingRead = null;
    pendingRead.onDataAvailable(emitter, mPendingData);
    assert !mPendingData.hasRemaining();
    return true;
}
Also used : DataCallback(com.koushikdutta.async.callback.DataCallback)

Example 7 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 8 with DataCallback

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

the class MultipartTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    httpServer = new AsyncHttpServer();
    httpServer.setErrorCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            fail();
        }
    });
    httpServer.listen(AsyncServer.getDefault(), 5000);
    httpServer.post("/", new HttpServerRequestCallback() {

        int gotten = 0;

        @Override
        public void onRequest(final AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            final MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
            body.setMultipartCallback(new MultipartCallback() {

                @Override
                public void onPart(Part part) {
                    if (part.isFile()) {
                        body.setDataCallback(new DataCallback() {

                            @Override
                            public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                                gotten += bb.remaining();
                                bb.recycle();
                            }
                        });
                    }
                }
            });
            request.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    response.send(body.getField("baz") + gotten + body.getField("foo"));
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) MultipartCallback(com.koushikdutta.async.http.body.MultipartFormDataBody.MultipartCallback) DataCallback(com.koushikdutta.async.callback.DataCallback) Part(com.koushikdutta.async.http.body.Part) DataEmitter(com.koushikdutta.async.DataEmitter) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 9 with DataCallback

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

the class HttpClientTests method testGithubRandomData.

public void testGithubRandomData() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Md5 md5 = Md5.createInstance();
    AsyncHttpGet get = new AsyncHttpGet(github);
    get.setLogging("AsyncTest", Log.VERBOSE);
    client.execute(get, new HttpConnectCallback() {

        @Override
        public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
            assertNull(ex);
            // make sure gzip decoding works, as that is generally what github sends.
            // this broke sometime in 03/2014
            //                Assert.assertEquals("gzip", response.getHeaders().getContentEncoding());
            response.setDataCallback(new DataCallback() {

                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    md5.update(bb);
                }
            });
            response.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    semaphore.release();
                }
            });
        }
    });
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals(md5.digest(), dataNameAndHash);
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) DataEmitter(com.koushikdutta.async.DataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with DataCallback

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

the class HttpClientTests method testClockworkMod.

public void testClockworkMod() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Md5 md5 = Md5.createInstance();
    client.execute("http://www.clockworkmod.com", new HttpConnectCallback() {

        @Override
        public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
            // make sure gzip decoding works, as that is generally what github sends.
            Assert.assertEquals("gzip", response.headers().get("Content-Encoding"));
            response.setDataCallback(new DataCallback() {

                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    md5.update(bb);
                }
            });
            response.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    semaphore.release();
                }
            });
        }
    });
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) DataEmitter(com.koushikdutta.async.DataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

DataCallback (com.koushikdutta.async.callback.DataCallback)13 ByteBufferList (com.koushikdutta.async.ByteBufferList)10 DataEmitter (com.koushikdutta.async.DataEmitter)10 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)7 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 RemoteException (android.os.RemoteException)1 AsyncDatagramSocket (com.koushikdutta.async.AsyncDatagramSocket)1 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)1 LineEmitter (com.koushikdutta.async.LineEmitter)1 StringCallback (com.koushikdutta.async.LineEmitter.StringCallback)1 PushParser (com.koushikdutta.async.PushParser)1 ParseCallback (com.koushikdutta.async.PushParser.ParseCallback)1