use of com.koushikdutta.async.callback.ListenCallback in project AndroidAsync by koush.
the class AsyncServer method runLoop.
private static void runLoop(final AsyncServer server, final SelectorWrapper selector, final PriorityQueue<Scheduled> queue) throws AsyncSelectorException {
// Log.i(LOGTAG, "Keys: " + selector.keys().size());
boolean needsSelect = true;
// run the queue to populate the selector with keys
long wait = lockAndRunQueue(server, queue);
try {
synchronized (server) {
// select now to see if anything is ready immediately. this
// also clears the canceled key queue.
int readyNow = selector.selectNow();
if (readyNow == 0) {
// which means it would be time to turn this thread off.
if (selector.keys().size() == 0 && wait == QUEUE_EMPTY) {
// Log.i(LOGTAG, "Shutting down. keys: " + selector.keys().size() + " keepRunning: " + keepRunning);
return;
}
} else {
needsSelect = false;
}
}
if (needsSelect) {
if (wait == QUEUE_EMPTY) {
// wait until woken up
selector.select();
} else {
// nothing to select immediately but there's something pending so let's block that duration and wait.
selector.select(wait);
}
}
} catch (Exception e) {
throw new AsyncSelectorException(e);
}
// process whatever keys are ready
Set<SelectionKey> readyKeys = selector.selectedKeys();
for (SelectionKey key : readyKeys) {
try {
if (key.isAcceptable()) {
ServerSocketChannel nextReady = (ServerSocketChannel) key.channel();
SocketChannel sc = null;
SelectionKey ckey = null;
try {
sc = nextReady.accept();
if (sc == null)
continue;
sc.configureBlocking(false);
ckey = sc.register(selector.getSelector(), SelectionKey.OP_READ);
ListenCallback serverHandler = (ListenCallback) key.attachment();
AsyncNetworkSocket handler = new AsyncNetworkSocket();
handler.attach(sc, (InetSocketAddress) sc.socket().getRemoteSocketAddress());
handler.setup(server, ckey);
ckey.attach(handler);
serverHandler.onAccepted(handler);
} catch (IOException e) {
StreamUtility.closeQuietly(sc);
if (ckey != null)
ckey.cancel();
}
} else if (key.isReadable()) {
AsyncNetworkSocket handler = (AsyncNetworkSocket) key.attachment();
int transmitted = handler.onReadable();
server.onDataReceived(transmitted);
} else if (key.isWritable()) {
AsyncNetworkSocket handler = (AsyncNetworkSocket) key.attachment();
handler.onDataWritable();
} else if (key.isConnectable()) {
ConnectFuture cancel = (ConnectFuture) key.attachment();
SocketChannel sc = (SocketChannel) key.channel();
key.interestOps(SelectionKey.OP_READ);
AsyncNetworkSocket newHandler;
try {
sc.finishConnect();
newHandler = new AsyncNetworkSocket();
newHandler.setup(server, key);
newHandler.attach(sc, (InetSocketAddress) sc.socket().getRemoteSocketAddress());
key.attach(newHandler);
} catch (IOException ex) {
key.cancel();
StreamUtility.closeQuietly(sc);
if (cancel.setComplete(ex))
cancel.callback.onConnectCompleted(ex, null);
continue;
}
try {
if (cancel.setComplete(newHandler))
cancel.callback.onConnectCompleted(null, newHandler);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
Log.i(LOGTAG, "wtf");
throw new RuntimeException("Unknown key state.");
}
} catch (CancelledKeyException ex) {
}
}
readyKeys.clear();
}
use of com.koushikdutta.async.callback.ListenCallback 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();
}
Aggregations