Example usage for java.nio.channels SelectionKey interestOps

List of usage examples for java.nio.channels SelectionKey interestOps

Introduction

In this page you can find the example usage for java.nio.channels SelectionKey interestOps.

Prototype

public abstract SelectionKey interestOps(int ops);

Source Link

Document

Sets this key's interest set to the given value.

Usage

From source file:com.facebook.infrastructure.net.UdpConnection.java

public void read(SelectionKey key) {
    key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
    ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
    try {//  w  w w .  j  a  va2s  . c  o  m
        SocketAddress sa = socketChannel_.receive(buffer);
        if (sa == null) {
            logger_.debug("*** No datagram packet was available to be read ***");
            return;
        }
        buffer.flip();

        byte[] bytes = gobbleHeaderAndExtractBody(buffer);
        if (bytes.length > 0) {
            DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
            Message message = Message.serializer().deserialize(dis);
            if (message != null) {
                MessagingService.receive(message);
            }
        }
    } catch (IOException ioe) {
        logger_.warn(LogUtil.throwableToString(ioe));
    } finally {
        key.interestOps(key_.interestOps() | SelectionKey.OP_READ);
    }
}

From source file:com.byteatebit.nbserver.task.ReadDelimitedMessageTask.java

protected void invokeExceptionHandler(SelectionKey selectionKey, Consumer<Exception> exceptionHandler,
        Exception exception) {//from   ww w. j  a  v  a  2 s  .c  o  m
    selectionKey.interestOps(selectionKey.interestOps() & ~SelectionKey.OP_READ);
    try {
        exceptionHandler.accept(exception);
    } catch (Exception e) {
        LOG.error("Read exception handler failed", e);
    }
}

From source file:com.openteach.diamond.network.waverider.network.DefaultNetWorkServer.java

/**
 * ??, SessiononWrite, Session??// w  w  w .j  ava 2 s  . c o m
 * @param key
 * @throws IOException
 */
private void onWrite(SelectionKey key) throws IOException {
    key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
    Session session = (Session) key.attachment();
    session.onWrite();
    //key.interestOps(SelectionKey.OP_READ);
}

From source file:com.facebook.infrastructure.net.TcpConnection.java

public void modifyKeyForRead(SelectionKey key) {
    key.interestOps(key_.interestOps() | SelectionKey.OP_READ);
}

From source file:com.facebook.infrastructure.net.TcpConnection.java

public void modifyKeyForWrite(SelectionKey key) {
    key.interestOps(key_.interestOps() | SelectionKey.OP_WRITE);
}

From source file:com.facebook.infrastructure.net.TcpConnection.java

public void read(SelectionKey key) {
    key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
    // publish this event onto to the TCPReadEvent Queue.
    MessagingService.getReadExecutor().execute(readWork_);
}

From source file:com.facebook.infrastructure.net.TcpConnection.java

public void write(SelectionKey key) {
    key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE));
    doPendingWrites();//from  w  w w .  ja  v a 2 s.  com
    /*
     * This is executed only if we are in streaming mode.
     * Idea is that we read a chunk of data from a source
     * and wait to read the next from the source until we 
     * are siganlled to do so from here. 
    */
    resumeStreaming();
}

From source file:com.facebook.infrastructure.net.TcpConnection.java

public void connect(SelectionKey key) {
    key.interestOps(key.interestOps() & (~SelectionKey.OP_CONNECT));
    try {/*from  w w  w . java2  s  .  c  om*/
        if (!socketChannel_.finishConnect()) {
            throw new IOException("Unable to finishConnect to " + socketChannel_);
        }
        SelectorManager.getSelectorManager().modifyKeyForRead(key);
        connected_.set(true);

        // this will flush the pending
        if (!pendingWrites_.isEmpty()) {
            SelectorManager.getSelectorManager().modifyKeyForWrite(key_);
        }
        resumeStreaming();
    } catch (IOException e) {
        logger_.error("Encountered IOException on connection: " + socketChannel_, e);
        errorClose();
    }
}

From source file:com.openteach.diamond.network.waverider.network.DefaultNetWorkClient.java

/**
 * ?, outputBuffer/*from w w  w  . ja va2 s.c o m*/
 * ?
 * @param key
 * @throws IOException
 */
private void onWrite(SelectionKey key) throws IOException {
    logger.debug("onWrite");
    key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
    Command command = null;
    Packet packet = null;
    ByteBuffer data = null;
    //int size = 0;
    while ((command = outputBuffer.poll()) != null) {
        //size = 0;
        // ???
        packet = Packet.newDataPacket(command);
        // ???ByteBuffer
        data = packet.marshall();
        //size = data.remaining();
        while (data.hasRemaining()) {
            socketChannel.write(data);
        }
        socketChannel.socket().getOutputStream().flush();
        //logger.info("Slave write one packet, " + size + " bytes");
    }
    //key.interestOps(SelectionKey.OP_READ);
}

From source file:oz.hadoop.yarn.api.net.ApplicationContainerServerImpl.java

/**
 * //from w  ww .j a  va  2 s.c om
 */
void doWrite(SelectionKey selectionKey, ByteBuffer buffer) {
    ByteBuffer message = ByteBufferUtils.merge(ByteBuffer.allocate(4).putInt(buffer.limit() + 4), buffer);
    message.flip();

    selectionKey.attach(message);
    selectionKey.interestOps(SelectionKey.OP_WRITE);
}