/** * Indicates whether this key's channel is interested in the write operation * and is ready to write. A call to this method is equal to executing * {@code (readyOps() & OP_WRITE) == OP_WRITE}. * * @return {@code true} if the channel is interested in the write operation * and is ready to write, {@code false} otherwise. * @throws CancelledKeyException * if the key has already been canceled. */ public final boolean isWritable() { return (readyOps() & OP_WRITE) == OP_WRITE; }
/** * Indicates whether this key's channel is interested in the read operation * and is ready to read. A call to this method is equal to executing * {@code (readyOps() & OP_READ) == OP_READ}. * * @return {@code true} if the channel is interested in the read operation * and is ready to read, {@code false} otherwise. * @throws CancelledKeyException * if the key has already been canceled. */ public final boolean isReadable() { return (readyOps() & OP_READ) == OP_READ; }
/** * Indicates whether this key's channel is interested in the accept * operation and is ready to accept new connections. A call to this method * is equal to executing {@code (readyOps() & OP_ACCEPT) == OP_ACCEPT}. * * @return {@code true} if the channel is interested in the accept operation * and is ready to accept new connections, {@code false} otherwise. * @throws CancelledKeyException * if the key has already been canceled. */ public final boolean isAcceptable() { return (readyOps() & OP_ACCEPT) == OP_ACCEPT; }
/** * Indicates whether this key's channel is interested in the connect * operation and is ready to connect. A call to this method is equal to * executing {@code (readyOps() & OP_CONNECT) == OP_CONNECT}. * * @return {@code true} if the channel is interested in the connect * operation and is ready to connect, {@code false} otherwise. * @throws CancelledKeyException * if the key has already been canceled. */ public final boolean isConnectable() { return (readyOps() & OP_CONNECT) == OP_CONNECT; }
private void select() { try { selector.select(); Set<SelectionKey> selected = selector.selectedKeys(); ArrayList<SelectionKey> selectedList = new ArrayList<SelectionKey>(selected); Collections.shuffle(selectedList); Iterator<SelectionKey> selectedKeys = selectedList.iterator(); while(!stopped && selectedKeys.hasNext()) { SelectionKey key = selectedKeys.next(); selected.remove(key); if (!key.isValid()) { cleanupSelectionKey(key); continue; } if (key.isReadable() || key.isWritable()) { handleIO(key); } else { LOG.warn("Unexpected ops in select " + key.readyOps()); } } } catch (IOException e) { LOG.warn("Ignoring IOException while selecting", e); } }
private void select() { try { selector.select(); Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator(); while (!stopped && selectedKeys.hasNext()) { SelectionKey key = selectedKeys.next(); selectedKeys.remove(); if (!key.isValid()) { continue; } if (key.isAcceptable()) { if (!doAccept()) { // If unable to pull a new connection off the accept // queue, pause accepting to give us time to free // up file descriptors and so the accept thread // doesn't spin in a tight loop. pauseAccept(10); } } else { LOG.warn("Unexpected ops in accept select " + key.readyOps()); } } } catch (IOException e) { LOG.warn("Ignoring IOException while selecting", e); } }
Object att = key.attachment(); if (att != null && key.isValid()) { int readyOps = key.readyOps(); if ((readyOps & SelectionKey.OP_READ) != 0) { read((NIOConnection) att);
Collections.shuffle(selectedList); for (SelectionKey k : selectedList) { if ((k.readyOps() & SelectionKey.OP_ACCEPT) != 0) { SocketChannel sc = ((ServerSocketChannel) k .channel()).accept(); addCnxn(cnxn); } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) { NIOServerCnxn c = (NIOServerCnxn) k.attachment(); c.doIO(k); if (LOG.isDebugEnabled()) { LOG.debug("Unexpected ops in select " + k.readyOps());
i.remove(); try { int readyOps = k.readyOps(); if ((readyOps & SelectionKey.OP_READ) != 0 || readyOps == 0) { if (!read(k)) {
for (SelectionKey k : selected) { SocketChannel sc = ((SocketChannel) k.channel()); if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) { if (sc.finishConnect()) { updateLastSendAndHeard(); sendThread.primeConnection(); } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) { doIO(pendingQueue, cnxn);
if (!key.isValid()) continue; int readyOps = key.readyOps(); if ((readyOps & SelectionKey.OP_READ) != 0) { read(key);
for (SelectionKey key : keys) { if (key.isValid()) { int readyOps = key.readyOps(); if ((readyOps & SelectionKey.OP_READ) != 0) { read(key);
for (SelectionKey k : selected) { SocketChannel sc = ((SocketChannel) k.channel()); if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) { if (sc.finishConnect()) { updateLastSendAndHeard(); sendThread.primeConnection(); } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) { doIO(pendingQueue, outgoingQueue, cnxn);
int readyOps = k.readyOps();
/** * Tests that a connect and disconnect in a single poll invocation results in the channel id being * in `disconnected`, but not `connected`. */ @Test public void testConnectDisconnectDuringInSinglePoll() throws Exception { // channel is connected, not ready and it throws an exception during prepare KafkaChannel kafkaChannel = mock(KafkaChannel.class); when(kafkaChannel.id()).thenReturn("1"); when(kafkaChannel.socketDescription()).thenReturn(""); when(kafkaChannel.state()).thenReturn(ChannelState.NOT_CONNECTED); when(kafkaChannel.finishConnect()).thenReturn(true); when(kafkaChannel.isConnected()).thenReturn(true); when(kafkaChannel.ready()).thenReturn(false); doThrow(new IOException()).when(kafkaChannel).prepare(); SelectionKey selectionKey = mock(SelectionKey.class); when(kafkaChannel.selectionKey()).thenReturn(selectionKey); when(selectionKey.channel()).thenReturn(SocketChannel.open()); when(selectionKey.readyOps()).thenReturn(SelectionKey.OP_CONNECT); selectionKey.attach(kafkaChannel); Set<SelectionKey> selectionKeys = Utils.mkSet(selectionKey); selector.pollSelectionKeys(selectionKeys, false, System.nanoTime()); assertFalse(selector.connected().contains(kafkaChannel.id())); assertTrue(selector.disconnected().containsKey(kafkaChannel.id())); assertNull(selectionKey.attachment()); verify(kafkaChannel, atLeastOnce()).ready(); verify(kafkaChannel).disconnect(); verify(kafkaChannel).close(); verify(selectionKey).cancel(); }
int readyOps = k.readyOps();
iter.remove(); try { int ops = selectionKey.readyOps(); if ((ops & SelectionKey.OP_READ) == SelectionKey.OP_READ) { if (selectionKey.attachment() == tcp) {
int readyOps = k.readyOps();
final boolean ready = (loadBalanceSession.getDesiredReadinessFlag() & selectionKey.readyOps()) != 0; if (!ready) { return false;