SelectionKey selector - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for SelectionKey selector method.

This code example shows how to use the following methods:cancel, interestOps, isValid, selector, SelectionKey.interestOps
     *  
     * @return The updated selection key or a new one if it was registered 
     *         again. 
     */ 
    public SelectionKey update() {
        if (this.selectionKey.isValid()) {
            if (isCanceling()) { 
                Context.getCurrentLogger().log(Level.FINER,
                        "Cancelling of the selection key requested"); 
                this.selectionKey.cancel();
            } else { 
                try { 
                    if (Context.getCurrentLogger().isLoggable(Level.FINEST)) {
                        Context.getCurrentLogger() 
                                .log(Level.FINEST,
                                        "Update key (old | new) : " 
                                                + SelectionRegistration 
                                                        .getName(this.selectionKey
                                                                .interestOps())
                                                + " | " 

6
CodeRank
This code example shows how to use the following methods:channel, selector
		System.err.println(hexdump.dump(prefix, data));
} 
 
void 
cleanup() throws IOException {
	key.selector().close();
	key.channel().close();
} 
 
} 
6
CodeRank
This code example shows how to use the following methods:interestOps, isValid, selector
 
    /** 
     * Notify Dispatcher Selector that we want write some data here. 
     */ 
    protected final void enableWriteInterest() { 
        if (key.isValid()) {
            key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
            key.selector().wakeup();
        } 
    } 
 
    /** 
     * @return Dispatcher to witch this connection is registered. 
     */ 
    final Dispatcher getDispatcher() { 
        return dispatcher;
    } 
 
    /** 
     * @return SocketChannel representing this connection. 
5
CodeRank
This code example shows how to use the following methods:channel, selector
    else 
      handle(buf);
  } 
 
  private static void accept(SelectionKey key) throws IOException {
    ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
    SocketChannel sc = ssc.accept();
    sc.configureBlocking(false);
    Selector selector = key.selector();
    sc.register(selector, SelectionKey.OP_READ);
  } 
 
  private static void handle(ByteBuffer buf) {
  } 
 
 
} 
5
CodeRank
This code example shows how to use the following methods:channel, selector
        srv.close();
      } 
    } 
 
    public static void handleAccept(SelectionKey key) throws IOException {
      ServerSocketChannel srv = (ServerSocketChannel) key.channel();
      SocketChannel client = srv.accept();
      client.configureBlocking(false);
      ServerState state = new ServerState();
      client.register(key.selector(), SelectionKey.OP_READ, state);
      log.debug("{}: accepted socket", state.workerName);
    } 
 
    public static boolean handleRead(SelectionKey key, ServerState state) throws IOException {
      NumberMessage msg = readFromChannel(key);
      if (msg == null) {
        log.debug("{}: trying to read from a channel but there are too few bytes", state.workerName);
        return false; 
      } else { 
        log.debug("{}: read message {}", state.workerName, msg);
4
CodeRank
This code example shows how to use the following methods:interestOps, selector
    public void enableRead() { 
        final Lock lock = this.keyLock;
        lock.lock();
        try { 
            SelectionKey key = this.processKey;
            key.interestOps(key.interestOps() | SelectionKey.OP_READ);
        } finally { 
            lock.unlock();
        } 
        processKey.selector().wakeup();
    } 
 
    /** 
     * 关闭读事件 
     */ 
    public void disableRead() { 
        final Lock lock = this.keyLock;
        lock.lock();
        try { 
            SelectionKey key = this.processKey;
3
CodeRank
This code example shows how to use the following methods:selector
    public void setConnectTimeout(int timeout) {
        if (this.connectTimeout != timeout) {
            this.connectTimeout = timeout;
            SelectionKey key = this.key;
            if (key != null) {
                key.selector().wakeup();
            } 
        } 
    } 
 
    public void cancel() { 
        if (this.completed) {
            return; 
        } 
        this.completed = true;
        SelectionKey key = this.key;
        if (key != null) {
            key.cancel();
            Channel channel = key.channel();
            if (channel.isOpen()) {
4
CodeRank
This code example shows how to use the following methods:channel, selector
        this.buffer = buffer;
    } 
 
    public void react(SelectionKey key) throws IOException {
        try { 
            SocketChannel sc = (SocketChannel) key.channel();
            buffer.flip();
            int count = sc.write(buffer);
 
            if (!buffer.hasRemaining())
                // Nothing left to write: OK to start reading again 
                sc.register(key.selector(), SelectionKey.OP_READ, new Reader(buffer));
        } 
        finally { 
            buffer.compact();
        } 
    } 
} 
5
CodeRank
This code example shows how to use the following methods:interestOps, selector
    } 
 
    @GuardedBy("lock") 
    private void setWriteOps() { 
        // Make sure we are registered to get updated when writing is available again 
        key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
        // Refresh the selector to make sure it gets the new interestOps 
        key.selector().wakeup();
    } 
 
    // Tries to write any outstanding write bytes, runs in any thread (possibly unlocked) 
    private void tryWriteBytes() throws IOException {
        lock.lock();
        try { 
            // Iterate through the outbound ByteBuff queue, pushing as much as possible into the 
            // OS' network buffer. 
            Iterator<ByteBuffer> bytesIterator = bytesToWrite.iterator();
            while (bytesIterator.hasNext()) {
                ByteBuffer buff = bytesIterator.next();
                bytesToWriteRemaining -= channel.write(buff);
4
CodeRank
This code example shows how to use the following methods:interestOps, selector
    public void enableRead() { 
        final Lock lock = this.keyLock;
        lock.lock();
        try { 
            SelectionKey key = this.processKey;
            key.interestOps(key.interestOps() | SelectionKey.OP_READ);
        } finally { 
            lock.unlock();
        } 
        processKey.selector().wakeup();
    } 
 
    /** 
     * 关闭读事件 
     */ 
    public void disableRead() { 
        final Lock lock = this.keyLock;
        lock.lock();
        try { 
            SelectionKey key = this.processKey;
3
CodeRank
selector Method Overview
Returns the selector for which this key was created.
See Code Examples for other Java 8 SelectionKey Methods: