Example usage for java.nio.channels SelectionKey OP_READ

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

Introduction

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

Prototype

int OP_READ

To view the source code for java.nio.channels SelectionKey OP_READ.

Click Source Link

Document

Operation-set bit for read operations.

Usage

From source file:org.cloudata.core.commitlog.pipe.RunState.java

public void init(Pipe.Context ctx) throws IOException {
    ctx.registerToSelect(ctx.prevChannel, SelectionKey.OP_READ);
    ctx.prevChannel.socket().setTcpNoDelay(true);
    ctx.prevChannel.configureBlocking(false);

    if (ctx.nextChannel != null) {
        ctx.registerToSelect(ctx.nextChannel, SelectionKey.OP_READ);
    }/* ww  w.j  a v  a2  s. c  o  m*/

    ctx.bulk = new Bulk();
}

From source file:org.cloudata.core.commitlog.pipe.InitState.java

public void init(Pipe.Context ctx) throws IOException {
    // LOG.debug("INIT STATE!!!");
    ctx.msg.clear();/*from  w  w w . jav a 2 s. c  o m*/
    ctx.prevChannel.configureBlocking(false);
    ctx.registerToSelect(ctx.prevChannel, SelectionKey.OP_READ);
}

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

/**
 * //  w w w . j av  a2  s. com
 */
@Override
void init() throws IOException {
    SocketChannel channel = (SocketChannel) this.rootChannel;

    boolean connected = channel.connect(this.address);
    if (connected) {
        channel.configureBlocking(false);

        channel.register(this.selector, SelectionKey.OP_READ);

        if (logger.isInfoEnabled()) {
            logger.info("Connected to " + this.address);
        }
    } else {
        throw new IllegalStateException("Failed to connect to ClientServer at: " + this.address);
    }
}

From source file:org.sipfoundry.sipxrelay.DataShuffler.java

private static synchronized void initializeSelector() {

    try {//  w  w  w .java 2  s.c o  m
        checkWorkQueue();

        if (selector != null) {
            selector.close();
        }
        selector = Selector.open();

        for (Bridge bridge : ConcurrentSet.getBridges()) {

            for (Sym session : bridge.sessions) {
                try {
                    if (session.getReceiver() != null && bridge.getState() == BridgeState.RUNNING
                            && session.getReceiver().getDatagramChannel().isOpen()) {

                        session.getReceiver().getDatagramChannel().configureBlocking(false);
                        session.getReceiver().getDatagramChannel().register(selector, SelectionKey.OP_READ);
                    }
                } catch (ClosedChannelException ex) {
                    // Avoid loading any closed channels in our select set.
                    continue;
                }
            }
            initializeSelectors.set(false);
        }

    } catch (IOException ex) {
        logger.error("Unepxected exception", ex);
        return;
    }

}

From source file:de.kapsi.net.daap.nio.DaapConnectionNIO.java

/**
 * What do you do next?//from   w ww .  j a va 2s  .c o  m
 *
 * @return
 */
int interrestOps() {

    if (isUndef()) {
        return SelectionKey.OP_READ;

    } else if (isDaapConnection()) {

        int op = SelectionKey.OP_READ;

        if (!writer.isEmpty())
            op |= SelectionKey.OP_WRITE;

        return op;

    } else {
        // isAudioStream
        return SelectionKey.OP_WRITE;
    }
}

From source file:com.byteatebit.nbserver.simple.udp.UdpDatagramChannelHandler.java

@Override
public SelectionKey accept(INbContext nbContext, DatagramChannel datagramChannel) {
    IDatagramMessageHandler messageHandler = datagramMessageHandlerFactory.create(nbContext, datagramChannel);
    ReadDatagramTask readDatagramTask = ReadDatagramTask.Builder.builder().withNbContext(nbContext)
            .withDatagramChannel(datagramChannel).withBufferSize(maxDatagramReceiveSize).build();
    Consumer<Exception> exceptionHandler = (e) -> {
        LOG.error("Message read failed.  Closing datagramChannel");
        IOUtils.closeQuietly(datagramChannel);
    };//from w  w w .  j av a 2  s  . co m
    return nbContext.register(datagramChannel, SelectionKey.OP_READ,
            selectionKey -> readDatagramTask.readMessage(selectionKey, messageHandler::accept,
                    exceptionHandler),
            (selectionKey, ops) -> exceptionHandler.accept(new TimeoutException("write timed out")), -1);
}

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

public void readMessages(IChannelSelectorRegistrar channelSelectorRegistrar, Consumer<List<String>> callback,
        Consumer<Exception> exceptionHandler) {
    try {/*w  ww .  java 2 s.com*/
        channelSelectorRegistrar.register(SelectionKey.OP_READ,
                (selectionKey -> read(selectionKey, callback, exceptionHandler)),
                (selectionKey, ops) -> exceptionHandler.accept(new TimeoutException("read timed out")));
    } catch (Exception e) {
        exceptionHandler.accept(e);
    }
}

From source file:com.ok2c.lightmtp.impl.protocol.SimpleSendHeloCodec.java

@Override
public void reset(final IOSession iosession, final ClientState sessionState)
        throws IOException, SMTPProtocolException {
    this.parser.reset();
    this.writer.reset();
    this.codecState = CodecState.SERVICE_READY_EXPECTED;

    iosession.setEvent(SelectionKey.OP_READ);
}

From source file:org.pvalsecc.comm.ServerConnection.java

/**
 * Called when the socket is ready to send.
 *///from w w  w.j av a2s .co  m
public synchronized int send(SocketChannel socket) throws IOException {
    ByteBuffer buffer = toSend.peek();
    int bytesSent = socket.write(buffer);
    if (!buffer.hasRemaining()) {
        toSend.remove();

        if (!hasSomeMoreDataToSend()) { //no more data to send, set the selector back to read-only
            key.interestOps(SelectionKey.OP_READ);
        }
    }

    return bytesSent;
}

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

public void init(int port) throws IOException {
    // TODO: get TCP port from config and add one.
    localEndPoint_ = new EndPoint(port);
    socketChannel_ = DatagramChannel.open();
    socketChannel_.socket().bind(localEndPoint_.getInetAddress());
    socketChannel_.socket().setReuseAddress(true);
    socketChannel_.configureBlocking(false);
    key_ = SelectorManager.getUdpSelectorManager().register(socketChannel_, this, SelectionKey.OP_READ);
}