use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.
the class SocketIOConnection method reconnect.
void reconnect(final DependentCancellable child) {
if (isConnected()) {
return;
}
// if a connection is in progress, just wait.
if (connecting != null && !connecting.isDone() && !connecting.isCancelled()) {
if (child != null)
child.setParent(connecting);
return;
}
request.logi("Reconnecting socket.io");
connecting = httpClient.executeString(request, null).then(new TransformFuture<SocketIOTransport, String>() {
@Override
protected void transform(String result) throws Exception {
String[] parts = result.split(":");
final String sessionId = parts[0];
if (!"".equals(parts[1]))
heartbeat = Integer.parseInt(parts[1]) / 2 * 1000;
else
heartbeat = 0;
String transportsLine = parts[3];
String[] transports = transportsLine.split(",");
HashSet<String> set = new HashSet<String>(Arrays.asList(transports));
final SimpleFuture<SocketIOTransport> transport = new SimpleFuture<SocketIOTransport>();
if (set.contains("websocket")) {
final String sessionUrl = Uri.parse(request.getUri().toString()).buildUpon().appendPath("websocket").appendPath(sessionId).build().toString();
httpClient.websocket(sessionUrl, null, null).setCallback(new FutureCallback<WebSocket>() {
@Override
public void onCompleted(Exception e, WebSocket result) {
if (e != null) {
transport.setComplete(e);
return;
}
transport.setComplete(new WebSocketTransport(result, sessionId));
}
});
} else if (set.contains("xhr-polling")) {
final String sessionUrl = Uri.parse(request.getUri().toString()).buildUpon().appendPath("xhr-polling").appendPath(sessionId).build().toString();
XHRPollingTransport xhrPolling = new XHRPollingTransport(httpClient, sessionUrl, sessionId);
transport.setComplete(xhrPolling);
} else {
throw new SocketIOException("transport not supported");
}
setComplete(transport);
}
}).setCallback(new FutureCallback<SocketIOTransport>() {
@Override
public void onCompleted(Exception e, SocketIOTransport result) {
if (e != null) {
reportDisconnect(e);
return;
}
reconnectDelay = request.config.reconnectDelay;
SocketIOConnection.this.transport = result;
attach();
}
});
if (child != null)
child.setParent(connecting);
}
use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.
the class AsyncServer method connectSocket.
public Cancellable connectSocket(final InetSocketAddress remote, final ConnectCallback callback) {
if (!remote.isUnresolved())
return connectResolvedInetSocketAddress(remote, callback);
final SimpleFuture<AsyncNetworkSocket> ret = new SimpleFuture<AsyncNetworkSocket>();
Future<InetAddress> lookup = getByName(remote.getHostName());
ret.setParent(lookup);
lookup.setCallback(new FutureCallback<InetAddress>() {
@Override
public void onCompleted(Exception e, InetAddress result) {
if (e != null) {
callback.onConnectCompleted(e, null);
ret.setComplete(e);
return;
}
ret.setComplete(connectResolvedInetSocketAddress(new InetSocketAddress(result, remote.getPort()), callback));
}
});
return ret;
}
use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.
the class AsyncServer method createDatagram.
private Cancellable createDatagram(ValueFunction<InetAddress> inetAddressValueFunction, final int port, final boolean reuseAddress, FutureCallback<AsyncDatagramSocket> callback) {
SimpleFuture<AsyncDatagramSocket> ret = new SimpleFuture<>();
ret.setCallback(callback);
post(() -> {
DatagramChannel socket = null;
try {
socket = DatagramChannel.open();
final AsyncDatagramSocket handler = new AsyncDatagramSocket();
handler.attach(socket);
InetSocketAddress address;
if (inetAddressValueFunction == null)
address = new InetSocketAddress(port);
else
address = new InetSocketAddress(inetAddressValueFunction.getValue(), port);
if (reuseAddress)
socket.socket().setReuseAddress(reuseAddress);
socket.socket().bind(address);
handleSocket(handler);
if (!ret.setComplete(handler))
socket.close();
} catch (Exception e) {
StreamUtility.closeQuietly(socket);
ret.setComplete(e);
}
});
return ret;
}
use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.
the class AsyncHttpClient method executeFile.
public Future<File> executeFile(AsyncHttpRequest req, final String filename, final FileCallback callback) {
final File file = new File(filename);
file.getParentFile().mkdirs();
final OutputStream fout;
try {
fout = new BufferedOutputStream(new FileOutputStream(file), 8192);
} catch (FileNotFoundException e) {
SimpleFuture<File> ret = new SimpleFuture<File>();
ret.setComplete(e);
return ret;
}
final FutureAsyncHttpResponse cancel = new FutureAsyncHttpResponse();
final SimpleFuture<File> ret = new SimpleFuture<File>() {
@Override
public void cancelCleanup() {
try {
cancel.get().setDataCallback(new DataCallback.NullDataCallback());
cancel.get().close();
} catch (Exception e) {
}
try {
fout.close();
} catch (Exception e) {
}
file.delete();
}
};
ret.setParent(cancel);
execute(req, 0, cancel, new HttpConnectCallback() {
long mDownloaded = 0;
@Override
public void onConnectCompleted(Exception ex, final AsyncHttpResponse response) {
if (ex != null) {
try {
fout.close();
} catch (IOException e) {
}
file.delete();
invoke(callback, ret, response, ex, null);
return;
}
invokeConnect(callback, response);
final long contentLength = HttpUtil.contentLength(response.headers());
response.setDataCallback(new OutputStreamDataCallback(fout) {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
mDownloaded += bb.remaining();
super.onDataAvailable(emitter, bb);
invokeProgress(callback, response, mDownloaded, contentLength);
}
});
response.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
try {
fout.close();
} catch (IOException e) {
ex = e;
}
if (ex != null) {
file.delete();
invoke(callback, ret, response, ex, null);
} else {
invoke(callback, ret, response, null, file);
}
}
});
}
});
return ret;
}
use of com.koushikdutta.async.future.SimpleFuture in project AndroidAsync by koush.
the class AsyncHttpServerRouter method ensureManifest.
static synchronized Manifest ensureManifest(Context context) {
Future<Manifest> future = AppManifests.get(context.getPackageName());
if (future != null)
return future.tryGet();
ZipFile zip = null;
SimpleFuture<Manifest> result = new SimpleFuture<>();
try {
zip = new ZipFile(context.getPackageResourcePath());
ZipEntry entry = zip.getEntry("META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(zip.getInputStream(entry));
result.setComplete(manifest);
return manifest;
} catch (Exception e) {
result.setComplete(e);
return null;
} finally {
StreamUtility.closeQuietly(zip);
AppManifests.put(context.getPackageName(), result);
}
}
Aggregations