Search in sources :

Example 26 with CompletedCallback

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

the class FilteredDataEmitter method setDataEmitter.

@Override
public void setDataEmitter(DataEmitter emitter) {
    if (mEmitter != null) {
        mEmitter.setDataCallback(null);
    }
    mEmitter = emitter;
    mEmitter.setDataCallback(this);
    mEmitter.setEndCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            report(ex);
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback)

Example 27 with CompletedCallback

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

the class Issues method testIssue312.

public void testIssue312() throws Exception {
    String b64 = "SFRUUC8xLjAgMzAyIEZvdW5kDQpTZXQtQ29va2ll\n" + "OlNFU1NJT049NUJBRDlERTEwQjY0NjgwNDsKTG9j\n" + "YXRpb246IGhvbWUuY2dpCkNvbnRlbnQtdHlwZTog\n" + "dGV4dC9odG1sCgo8aHRtbD48aGVhZD48bWV0YSBo\n" + "dHRwLWVxdWl2PSdyZWZyZXNoJyBjb250ZW50PScw\n" + "OyB1cmw9aG9tZS5jZ2knPjwvbWV0YT48L2hlYWQ+\n" + "PGJvZHk+PC9ib2R5PjwvaHRtbD4K";
    /*
        HTTP/1.0 302 Found
        Set-Cookie:SESSION=5BAD9DE10B646804;
        Location: home.cgi
        Content-type: text/html

        <html><head><meta http-equiv='refresh' content='0; url=home.cgi'></meta></head><body></body></html>
         */
    // the above is using newlines, and not CRLF.
    final byte[] responseData = Base64.decode(b64, 0);
    server = Ion.getDefault(getContext()).getServer().listen(null, 0, new ListenCallback() {

        @Override
        public void onAccepted(final AsyncSocket socket) {
            Util.writeAll(socket, responseData, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    socket.end();
                    server.stop();
                }
            });
        }

        @Override
        public void onListening(AsyncServerSocket socket) {
        }

        @Override
        public void onCompleted(Exception ex) {
        }
    });
    Ion.with(getContext()).load("http://localhost:" + server.getLocalPort()).followRedirect(false).asString().get();
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncSocket(com.koushikdutta.async.AsyncSocket) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ListenCallback(com.koushikdutta.async.callback.ListenCallback)

Example 28 with CompletedCallback

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

the class StreamTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    AsyncHttpServer server = new AsyncHttpServer();
    port = server.listen(0).getLocalPort();
    server.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.code(200);
            ByteBuffer b = ByteBufferList.obtain(random.length);
            b.put(random);
            b.flip();
            ByteBufferList list = new ByteBufferList(b);
            Util.writeAll(response, list, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    response.end();
                }
            });
        }
    });
}
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) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) ByteBuffer(java.nio.ByteBuffer)

Example 29 with CompletedCallback

use of com.koushikdutta.async.callback.CompletedCallback 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 30 with CompletedCallback

use of com.koushikdutta.async.callback.CompletedCallback 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)

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