use of java.nio.channels.SelectionKey in project jeromq by zeromq.
the class Poller method run.
@Override
public void run() {
int returnsImmediately = 0;
while (!stopping) {
// Execute any due timers.
long timeout = executeTimers();
while (retired.compareAndSet(true, false)) {
Iterator<Map.Entry<SelectableChannel, PollSet>> it = fdTable.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<SelectableChannel, PollSet> entry = it.next();
SelectableChannel ch = entry.getKey();
PollSet pollset = entry.getValue();
if (pollset.key == null) {
try {
pollset.key = ch.register(selector, pollset.ops, pollset.handler);
} catch (ClosedChannelException e) {
}
}
if (pollset.cancelled || !ch.isOpen()) {
if (pollset.key != null) {
pollset.key.cancel();
}
it.remove();
}
}
}
// Wait for events.
int rc;
long start = System.currentTimeMillis();
try {
rc = selector.select(timeout);
} catch (IOException e) {
throw new ZError.IOException(e);
}
if (rc == 0) {
// Guess JDK epoll bug
if (timeout == 0 || System.currentTimeMillis() - start < timeout / 2) {
returnsImmediately++;
} else {
returnsImmediately = 0;
}
if (returnsImmediately > 10) {
rebuildSelector();
returnsImmediately = 0;
}
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
IPollEvents evt = (IPollEvents) key.attachment();
it.remove();
try {
if (key.isReadable()) {
evt.inEvent();
} else if (key.isAcceptable()) {
evt.acceptEvent();
} else if (key.isConnectable()) {
evt.connectEvent();
}
if (key.isWritable()) {
evt.outEvent();
}
} catch (CancelledKeyException e) {
// channel might have been closed
}
}
}
stopped = true;
}
use of java.nio.channels.SelectionKey in project wifikeyboard by IvanVolosyuk.
the class HttpServer method run.
@Override
public void run() {
// Debug.d("HttpServer started listening");
try {
ch.configureBlocking(false);
SelectionKey serverkey = ch.register(selector, SelectionKey.OP_ACCEPT);
final ArrayList<Update> newUpdates = new ArrayList<Update>();
Action checkUpdates = new Action() {
@Override
public Object run() {
for (Update u : pendingUpdates) {
newUpdates.add(u);
}
pendingUpdates.clear();
return null;
}
};
while (true) {
newUpdates.clear();
runAction(checkUpdates);
for (Update u : newUpdates) {
u.run();
}
// Debug.d("waiting for event");
selector.select();
// Debug.d("got an event");
Set<SelectionKey> keys = selector.selectedKeys();
for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext(); ) {
SelectionKey key = i.next();
i.remove();
if (key == serverkey) {
if (key.isAcceptable()) {
SocketChannel client = ch.accept();
// Debug.d("NEW CONNECTION from " + client.socket().getRemoteSocketAddress());
client.configureBlocking(false);
SelectionKey clientkey = client.register(selector, SelectionKey.OP_READ);
HttpConnection con = newConnection(client);
clientkey.attach(con);
con.setKey(clientkey);
}
} else {
HttpConnection conn = (HttpConnection) key.attachment();
try {
HttpConnection.ConnectionState prevState, newState;
if (key.isReadable()) {
prevState = HttpConnection.ConnectionState.SELECTOR_WAIT_FOR_NEW_INPUT;
// Debug.d("processing read event");
// long start = System.currentTimeMillis();
newState = conn.newInput();
// long end = System.currentTimeMillis();
// Debug.d("delay = " + (end - start));
// int size = android.os.Debug.getThreadAllocSize();
// android.os.Debug.stopAllocCounting();
// Log.d("wifikeyboard", "finished read event, allocs = " + size);
// android.os.Debug.startAllocCounting();
} else if (key.isWritable()) {
prevState = HttpConnection.ConnectionState.SELECTOR_WAIT_FOR_OUTPUT_BUFFER;
// Debug.d("processing write event");
newState = conn.newOutputBuffer();
// Log.d("wifikeyboard", "finished write event");
} else {
continue;
}
if (newState == prevState)
continue;
key.interestOps((newState == HttpConnection.ConnectionState.SELECTOR_WAIT_FOR_NEW_INPUT ? SelectionKey.OP_READ : 0) | (newState == HttpConnection.ConnectionState.SELECTOR_WAIT_FOR_OUTPUT_BUFFER ? SelectionKey.OP_WRITE : 0));
} catch (IOException io) {
// conn.getClient().socket().getRemoteSocketAddress());
if (key != null)
key.cancel();
conn.getClient().close();
} catch (ConnectionFailureException e) {
if (key != null)
key.cancel();
conn.getClient().close();
} catch (NumberFormatException e) {
// FIXME: move to ConnectionFailureException
if (key != null)
key.cancel();
conn.getClient().close();
}
}
}
}
} catch (IOException e) {
Debug.e("network loop terminated", e);
} catch (NetworkThreadStopException e) {
Debug.e("network thread stop requested", e);
}
try {
selector.close();
} catch (Throwable t) {
}
try {
ch.close();
} catch (Throwable t) {
}
onExit();
}
use of java.nio.channels.SelectionKey in project zm-mailbox by Zimbra.
the class ZimbraSocketAcceptor method accept.
@Override
protected NioSession accept(IoProcessor<NioSession> processor, ServerSocketChannel handle) throws Exception {
SelectionKey key = handle.keyFor(selector);
if ((key == null) || (!key.isValid()) || (!key.isAcceptable())) {
return null;
}
// accept the connection from the client
SocketChannel ch = handle.accept();
if (ch == null) {
return null;
}
return new NioSocketSession(this, processor, ch);
}
use of java.nio.channels.SelectionKey in project zm-mailbox by Zimbra.
the class ZimbraSocketAcceptor method close.
@Override
protected void close(ServerSocketChannel handle) throws Exception {
SelectionKey key = handle.keyFor(selector);
if (key != null) {
key.cancel();
}
handle.close();
}
use of java.nio.channels.SelectionKey in project aerospike-client-java by aerospike.
the class NioEventLoop method runCommands.
private void runCommands() throws Exception {
registerCommands();
runScheduled();
awakened.set(false);
selector.select(selectorTimeout);
if (awakened.get()) {
selector.wakeup();
}
final Set<SelectionKey> keys = selector.selectedKeys();
if (keys.isEmpty()) {
return;
}
try {
final Iterator<SelectionKey> iter = keys.iterator();
while (iter.hasNext()) {
final SelectionKey key = iter.next();
if (!key.isValid()) {
continue;
}
processKey(key);
}
} finally {
keys.clear();
}
}
Aggregations