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();
}
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);
}
}
});
}
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;
}
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);
}
});
}
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();
}
});
}
Aggregations