Search in sources :

Example 41 with CompletedCallback

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

the class MultipartFormDataBody method write.

@Override
public void write(AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
    if (mParts == null)
        return;
    Continuation c = new Continuation(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            completed.onCompleted(ex);
        //                if (ex == null)
        //                    sink.end();
        //                else
        //                    sink.close();
        }
    });
    for (final Part part : mParts) {
        c.add(new ContinuationCallback() {

            @Override
            public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
                byte[] bytes = part.getRawHeaders().toPrefixString(getBoundaryStart()).getBytes();
                com.koushikdutta.async.Util.writeAll(sink, bytes, next);
                written += bytes.length;
            }
        }).add(new ContinuationCallback() {

            @Override
            public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
                long partLength = part.length();
                if (partLength >= 0)
                    written += partLength;
                part.write(sink, next);
            }
        }).add(new ContinuationCallback() {

            @Override
            public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
                byte[] bytes = "\r\n".getBytes();
                com.koushikdutta.async.Util.writeAll(sink, bytes, next);
                written += bytes.length;
            }
        });
    }
    c.add(new ContinuationCallback() {

        @Override
        public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
            byte[] bytes = (getBoundaryEnd()).getBytes();
            com.koushikdutta.async.Util.writeAll(sink, bytes, next);
            written += bytes.length;
            assert written == totalToWrite;
        }
    });
    c.start();
}
Also used : Continuation(com.koushikdutta.async.future.Continuation) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ContinuationCallback(com.koushikdutta.async.callback.ContinuationCallback)

Example 42 with CompletedCallback

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

the class UrlEncodedFormBody method parse.

@Override
public void parse(DataEmitter emitter, final CompletedCallback completed) {
    final ByteBufferList data = new ByteBufferList();
    emitter.setDataCallback(new DataCallback() {

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

        @Override
        public void onCompleted(Exception ex) {
            if (ex != null) {
                completed.onCompleted(ex);
                return;
            }
            try {
                mParameters = Multimap.parseUrlEncoded(data.readString());
                completed.onCompleted(null);
            } catch (Exception e) {
                completed.onCompleted(e);
            }
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataEmitter(com.koushikdutta.async.DataEmitter) DataCallback(com.koushikdutta.async.callback.DataCallback) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 43 with CompletedCallback

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

Example 44 with CompletedCallback

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

the class SocketIOConnection method attach.

private void attach() {
    if (transport.heartbeats())
        setupHeartbeat();
    transport.setClosedCallback(new CompletedCallback() {

        @Override
        public void onCompleted(final Exception ex) {
            transport = null;
            reportDisconnect(ex);
        }
    });
    transport.setStringCallback(new SocketIOTransport.StringCallback() {

        @Override
        public void onStringAvailable(String message) {
            try {
                //                    Log.d(TAG, "Message: " + message);
                String[] parts = message.split(":", 4);
                int code = Integer.parseInt(parts[0]);
                switch(code) {
                    case 0:
                        // disconnect
                        transport.disconnect();
                        reportDisconnect(null);
                        break;
                    case 1:
                        // connect
                        reportConnect(parts[2]);
                        break;
                    case 2:
                        // heartbeat
                        transport.send("2::");
                        break;
                    case 3:
                        {
                            // message
                            reportString(parts[2], parts[3], acknowledge(parts[1], parts[2]));
                            break;
                        }
                    case 4:
                        {
                            //json message
                            final String dataString = parts[3];
                            final JSONObject jsonMessage = new JSONObject(dataString);
                            reportJson(parts[2], jsonMessage, acknowledge(parts[1], parts[2]));
                            break;
                        }
                    case 5:
                        {
                            final String dataString = parts[3];
                            final JSONObject data = new JSONObject(dataString);
                            final String event = data.getString("name");
                            final JSONArray args = data.optJSONArray("args");
                            reportEvent(parts[2], event, args, acknowledge(parts[1], parts[2]));
                            break;
                        }
                    case 6:
                        // ACK
                        final String[] ackParts = parts[3].split("\\+", 2);
                        Acknowledge ack = acknowledges.remove(ackParts[0]);
                        if (ack == null)
                            return;
                        JSONArray arguments = null;
                        if (ackParts.length == 2)
                            arguments = new JSONArray(ackParts[1]);
                        ack.acknowledge(arguments);
                        break;
                    case 7:
                        // error
                        reportError(parts[2], parts[3]);
                        break;
                    case 8:
                        // noop
                        break;
                    default:
                        throw new SocketIOException("unknown code");
                }
            } catch (Exception ex) {
                transport.setClosedCallback(null);
                transport.disconnect();
                transport = null;
                reportDisconnect(ex);
            }
        }
    });
    // now reconnect all the sockets that may have been previously connected
    select(null, new SelectCallback() {

        @Override
        public void onSelect(SocketIOClient client) {
            if (TextUtils.isEmpty(client.endpoint))
                return;
            connect(client);
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) JSONObject(org.json.JSONObject) SocketIOTransport(com.koushikdutta.async.http.socketio.transport.SocketIOTransport) JSONArray(org.json.JSONArray)

Example 45 with CompletedCallback

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

the class AsyncHttpServerResponseImpl method send.

@Override
public void send(String contentType, byte[] bytes) {
    assert mContentLength < 0;
    mContentLength = bytes.length;
    mRawHeaders.set("Content-Length", Integer.toString(bytes.length));
    mRawHeaders.set("Content-Type", contentType);
    Util.writeAll(this, bytes, new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            onEnd();
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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