Java 8 SelectionKey - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 SelectionKey class.

This code example shows how to use the following methods:attachment, interestOps, isValid, isWritable
    } 
 
    @Override 
    @edu.umd.cs.findbugs.annotations.SuppressWarnings({"VO_VOLATILE_INCREMENT" }) 
    protected void handleSelectionKey(SelectionKey sk) {
        if (sk.isValid() && sk.isWritable()) {
            writeEvents++;
            sk.interestOps(sk.interestOps() & ~SelectionKey.OP_WRITE);
            SelectionHandler handler = (SelectionHandler) sk.attachment();
            handler.handle();
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:interestOps, isReadable, isValid, isWritable, readyOps
        // Make the client non blocking and register it with selector for reads 
        client.configureBlocking(false);
        SelectionKey key = client.register(selector,SelectionKey.OP_READ);
 
        // assert it is not selected 
        assertTrue(key.isValid());
        assertFalse(key.isReadable());
        assertEquals(0,key.readyOps());
 
        // try selecting and assert nothing selected 
        int selected = selector.selectNow();
        assertEquals(0,selected);
        assertEquals(0,selector.selectedKeys().size());
        assertTrue(key.isValid());
        assertFalse(key.isReadable());
        assertEquals(0,key.readyOps());
 
        // Write a byte from server to client 
        server.getOutputStream().write(42);
        server.getOutputStream().flush();

7
CodeRank
This code example shows how to use the following methods:attach, attachment, cancel, channel, interestOps
        closeSelector(); 
        shutdownComplete(); 
    } 
 
    private SocketChannel channelFor(SelectionKey key) {
        return (SocketChannel) key.channel();
    } 
 
    private void close(SelectionKey key) {
        SocketChannel channel = (SocketChannel) key.channel();
        if (logger.isDebugEnabled()) { 
            logger.debug("Closing connection from " + channel.socket().getRemoteSocketAddress());
        } 
        Closer.closeQuietly(channel.socket());
        Closer.closeQuietly(channel);
        key.attach(null);
        key.cancel();
    } 
 
    private void write(SelectionKey key) throws IOException {

7
CodeRank
This code example shows how to use the following methods:attachment, isReadable, isValid
    } 
 
    @Override 
    @edu.umd.cs.findbugs.annotations.SuppressWarnings({"VO_VOLATILE_INCREMENT" }) 
    protected void handleSelectionKey(SelectionKey sk) {
        if (sk.isValid() && sk.isReadable()) {
            readEvents++;
            SelectionHandler handler = (SelectionHandler) sk.attachment();
            handler.handle();
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:interestOps, isConnectable, isReadable, isValid, isWritable
     * @param key SelectionKey 
     * @return boolean 
     * @throws IOException 
     */ 
    public boolean process(SelectionKey key, boolean waitForAck) throws IOException {
        int ops = key.readyOps();
        key.interestOps(key.interestOps() & ~ops);
        //in case disconnect has been called 
        if ((!isConnected()) && (!connecting)) throw new IOException("Sender has been disconnected, can't selection key.");
        if ( !key.isValid() ) throw new IOException("Key is not valid, it must have been cancelled.");
        if ( key.isConnectable() ) {
            if ( socketChannel.finishConnect() ) {
                completeConnect(); 
                if ( current != null ) key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
                return false; 
            } else  { 
                //wait for the connection to finish 
                key.interestOps(key.interestOps() | SelectionKey.OP_CONNECT);
                return false; 
            }//end if 

7
CodeRank
This code example shows how to use the following methods:attach, cancel, interestOps, isConnectable, isValid
    key.attach(this);
  } 
 
  void registerForFirstWrite(SelectionKey key) throws IOException {
    state = State.WRITING_REQUEST_SIZE;
    key.interestOps(SelectionKey.OP_WRITE);
  } 
 
  protected ByteBuffer getFrameBuffer() {
    return frameBuffer;
  } 
 
  /** 
   * Transition to next state, doing whatever work is required. Since this 
   * method is only called by the selector thread, we can make changes to our 
   * select interests without worrying about concurrency. 
   * @param key 
   */ 
  protected void transition(SelectionKey key) {
    // Ensure key is valid 

6
CodeRank
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:cancel, interestOps, isConnectable, isReadable, isValid
	 * @param key 
	 *            The key to be tested. 
	 */ 
	public synchronized void testKey(SelectionKey key) {
//		System.out.println("test key "+(connectQueue.peek()!=null)+" "+(readQueue.peek()!=null)+" "+(writeQueue.peek()!=null)); 
		if (!key.isValid()) {
			logger.error("invalid key; closeing "+asynchronousChannel);
			key.cancel();
			this.close();
			return; 
		} 
		 
		boolean connectable = key.isConnectable();
		boolean readable = key.isReadable();
		boolean writable = key.isWritable();
		 
		if (connectable) {
			handleOperation(connectQueue.poll());
			key.cancel();
			return; 

6
CodeRank
This code example shows how to use the following methods:cancel, channel, isAcceptable, isConnectable, isReadable
        } 
    } 
 
    private void processTcpSelectionKey(SelectionKey selKey) {
        //Incomming connection established 
        if (selKey.isValid() && selKey.isAcceptable()) {
            try { 
                ServerSocketChannel ssChannel = (ServerSocketChannel) selKey.channel();
                SocketChannel sChannel = ssChannel.accept();
                if (sChannel != null) {
                    sChannel.configureBlocking(false);
                    try { 
                        onAccept(sChannel);
                    } catch (Throwable t) {
                        Log.e(TAG, "Can't establish connection", t);
                        onError(sChannel);
                        return; 
                    } 
                } 
            } catch (Throwable t) {

6
CodeRank
This code example shows how to use the following methods:cancel, channel
		} 
	} 
 
	private void readOperation(SelectionKey key) throws Exception {
		session.setSelecotr(selector);
		session.setKey(key);
		session.setIsServerChannel(false);
		 
		SocketChannel channel = (SocketChannel) key.channel();
		ByteBuffer dst = ByteBuffer.allocate(1024);
		int n = -1;
		try { 
			n = channel.read(dst);
		} catch (Exception e) {
			System.err.println("cannot read io");
		} 
		if (n == -1) {
			key.cancel();
			channel.close();
			return; 

6
CodeRank

Related Java 8 SelectionKey Questions & Answers:

See Code Examples for Java 8 SelectionKey Methods: