Search in sources :

Example 16 with CompletedCallback

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

the class HttpServerTests 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.get("/hello", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            assertNotNull(request.getHeaders().get("Host"));
            response.send("hello");
        }
    });
    httpServer.post("/echo", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            try {
                assertNotNull(request.getHeaders().get("Host"));
                JSONObject json = new JSONObject();
                if (request.getBody() instanceof UrlEncodedFormBody) {
                    UrlEncodedFormBody body = (UrlEncodedFormBody) request.getBody();
                    for (NameValuePair pair : body.get()) {
                        json.put(pair.getName(), pair.getValue());
                    }
                } else if (request.getBody() instanceof JSONObjectBody) {
                    json = ((JSONObjectBody) request.getBody()).get();
                } else if (request.getBody() instanceof StringBody) {
                    json.put("foo", ((StringBody) request.getBody()).get());
                } else if (request.getBody() instanceof MultipartFormDataBody) {
                    MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
                    for (NameValuePair pair : body.get()) {
                        json.put(pair.getName(), pair.getValue());
                    }
                }
                response.send(json);
            } catch (Exception e) {
            }
        }
    });
}
Also used : NameValuePair(com.koushikdutta.async.http.NameValuePair) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) JSONObjectBody(com.koushikdutta.async.http.body.JSONObjectBody) JSONObject(org.json.JSONObject) StringBody(com.koushikdutta.async.http.body.StringBody) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) UrlEncodedFormBody(com.koushikdutta.async.http.body.UrlEncodedFormBody) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 17 with CompletedCallback

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

the class FutureTests method testContinuationArray.

public void testContinuationArray() throws Exception {
    final ArrayList<Integer> results = new ArrayList<Integer>();
    final Semaphore semaphore = new Semaphore(0);
    final Continuation c = new Continuation(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            semaphore.release();
        }
    });
    for (int i = 0; i < 10; i++) {
        final int j = i;
        c.add(new ContinuationCallback() {

            @Override
            public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
                results.add(j);
                next.onCompleted(null);
            }
        });
    }
    new Thread() {

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

        ;
    }.start();
    assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
    assertEquals(10, results.size());
    for (int i = 0; i < 10; i++) {
        assertEquals((int) results.get(i), i);
    }
}
Also used : Continuation(com.koushikdutta.async.future.Continuation) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ArrayList(java.util.ArrayList) 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 18 with CompletedCallback

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

the class FutureTests method testFutureChain.

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

        @Override
        public void onCompleted(Exception ex) {
            semaphore.release();
        }
    });
    IntegerFuture i1;
    c.add(i1 = IntegerFuture.create(2, 200));
    IntegerFuture i2;
    c.add(i2 = IntegerFuture.create(3, 200));
    new Thread() {

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

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

Example 19 with CompletedCallback

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

the class FutureTests method testContinuationFail.

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

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

        @Override
        public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
            throw new Exception("fail");
        }
    });
    new Thread() {

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

        ;
    }.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 20 with CompletedCallback

use of com.koushikdutta.async.callback.CompletedCallback in project K6nele by Kaljurand.

the class WebSocketRecognitionService method startSocket.

/**
 * Opens the socket and starts recording/sending.
 *
 * @param url Webservice URL
 */
void startSocket(String url) {
    mIsEosSent = false;
    Log.i(url);
    AsyncHttpClient client = AsyncHttpClient.getDefaultInstance();
    if (false) {
        // http://stackoverflow.com/questions/37804816/androidasync-how-to-create-ssl-client-in-websocket-connection
        AsyncSSLSocketMiddleware sslSocketMiddleware = new AsyncSSLSocketMiddleware(client);
        SSLContext sslContext = null;
        try {
            sslContext = getSSLContext();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        sslSocketMiddleware.setSSLContext(sslContext);
        client.insertMiddleware(sslSocketMiddleware);
    }
    client.websocket(url, PROTOCOL, new AsyncHttpClient.WebSocketConnectCallback() {

        @Override
        public void onCompleted(Exception ex, final WebSocket webSocket) {
            mWebSocket = webSocket;
            if (ex != null) {
                handleException(ex);
                return;
            }
            webSocket.setStringCallback(new WebSocket.StringCallback() {

                public void onStringAvailable(String s) {
                    Log.i(s);
                    handleResult(s);
                }
            });
            webSocket.setClosedCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    if (ex == null) {
                        Log.e("ClosedCallback");
                        handleFinish(mIsEosSent);
                    } else {
                        Log.e("ClosedCallback: ", ex);
                        handleException(ex);
                    }
                }
            });
            webSocket.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    if (ex == null) {
                        Log.e("EndCallback");
                        handleFinish(mIsEosSent);
                    } else {
                        Log.e("EndCallback: ", ex);
                        handleException(ex);
                    }
                }
            });
            startSending(webSocket);
        }
    });
}
Also used : AsyncSSLSocketMiddleware(com.koushikdutta.async.http.AsyncSSLSocketMiddleware) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient) WebSocket(com.koushikdutta.async.http.WebSocket)

Aggregations

CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)46 IOException (java.io.IOException)12 TimeoutException (java.util.concurrent.TimeoutException)11 ByteBufferList (com.koushikdutta.async.ByteBufferList)10 DataEmitter (com.koushikdutta.async.DataEmitter)10 DataCallback (com.koushikdutta.async.callback.DataCallback)10 Semaphore (java.util.concurrent.Semaphore)9 ContinuationCallback (com.koushikdutta.async.callback.ContinuationCallback)8 Continuation (com.koushikdutta.async.future.Continuation)8 CancellationException (java.util.concurrent.CancellationException)8 ExecutionException (java.util.concurrent.ExecutionException)8 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)7 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)7 FileNotFoundException (java.io.FileNotFoundException)7 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)6 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)6 AsyncSocket (com.koushikdutta.async.AsyncSocket)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Uri (android.net.Uri)4 WritableCallback (com.koushikdutta.async.callback.WritableCallback)4