Example usage for java.nio.channels SelectionKey channel

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

Introduction

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

Prototype

public abstract SelectableChannel channel();

Source Link

Document

Returns the channel for which this key was created.

Usage

From source file:idgs.client.TcpClient.java

private void processWrite(SelectionKey key) throws IOException {
    log.debug("-------------process write-----------");
    SocketChannel channel = (SocketChannel) key.channel();
    handler.onWrite(channel);//from  w w  w .ja  va  2s.  c  o m
    log.debug("-------------end to process write-----------");
}

From source file:eu.stratosphere.nephele.taskmanager.bytebuffered.OutgoingConnectionThread.java

public void unsubscribeFromWriteEvent(SelectionKey selectionKey) throws IOException {

    final SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
    final OutgoingConnection outgoingConnection = (OutgoingConnection) selectionKey.attachment();

    final SelectionKey newSelectionKey = socketChannel.register(this.selector, SelectionKey.OP_READ);
    newSelectionKey.attach(outgoingConnection);
    outgoingConnection.setSelectionKey(newSelectionKey);

    synchronized (this.connectionsToClose) {
        this.connectionsToClose.put(outgoingConnection, Long.valueOf(System.currentTimeMillis()));
    }/*w  ww  . j  a  v a2 s .  c  o m*/
}

From source file:org.apache.nifi.processor.util.listen.dispatcher.SocketChannelDispatcher.java

@Override
public void close() {
    stopped = true;/*from w ww . j  a  v a2 s . co m*/
    if (selector != null) {
        selector.wakeup();
    }

    if (executor != null) {
        executor.shutdown();
        try {
            // Wait a while for existing tasks to terminate
            if (!executor.awaitTermination(1000L, TimeUnit.MILLISECONDS)) {
                executor.shutdownNow();
            }
        } catch (InterruptedException ie) {
            // (Re-)Cancel if current thread also interrupted
            executor.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
    }

    if (selector != null) {
        synchronized (selector.keys()) {
            for (SelectionKey key : selector.keys()) {
                IOUtils.closeQuietly(key.channel());
            }
        }
    }
    IOUtils.closeQuietly(selector);
}

From source file:idgs.client.TcpClient.java

private void processRead(SelectionKey key) throws IOException {
    log.debug("-------------begin to process read-----------");
    SocketChannel channel = (SocketChannel) key.channel();
    channel.configureBlocking(false);//from  ww  w  .j  a v  a  2 s  .co  m
    handler.onRead(channel);
    log.debug("-------------end to process read-----------");
}

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

private void readyToSend(SelectionKey key) {
    SocketChannel socket = (SocketChannel) key.channel();
    ServerConnection connection = (ServerConnection) key.attachment();

    try {/*from   ww w  . ja v a2s.c  o  m*/
        nbBytesSent += connection.send(socket);
        nbSent++;
    } catch (IOException e) {
        connection.error("Cannot send data", e);
        key.cancel();
        SystemUtilities.safeClose(socket);
    }
}

From source file:org.apache.nifi.processor.util.listen.dispatcher.SocketChannelDispatcher.java

@Override
public int getPort() {
    // Return the port for the key listening for accepts
    for (SelectionKey key : selector.keys()) {
        if (key.isValid()) {
            final Channel channel = key.channel();
            if (channel instanceof ServerSocketChannel) {
                return ((ServerSocketChannel) channel).socket().getLocalPort();
            }//from w  ww. j a  v  a2s .  c  om
        }
    }
    return 0;
}

From source file:idgs.client.TcpClient.java

private void processConnect(SelectionKey key) throws IOException {
    if (!isConnected.get()) {
        if (key.isConnectable()) {
            channel = (SocketChannel) key.channel();
            if (channel.isConnectionPending()) {
                channel.finishConnect();
                channel.configureBlocking(false);
                isConnected.set(true);/*from ww w.  j  ava  2s  . c o  m*/
                log.debug("connected to server: " + host);
                handler.onConnected(channel);
            }
        }
    } else {
        log.debug("already connected...");
    }
}

From source file:org.openhab.io.transport.cul.internal.network.CULNetworkHandlerImpl.java

private void processConnect(SelectionKey key) throws Exception {
    SocketChannel ch = (SocketChannel) key.channel();
    if (ch.finishConnect()) {
        key.interestOps(key.interestOps() ^ SelectionKey.OP_CONNECT);
        key.interestOps(key.interestOps() | SelectionKey.OP_READ);
        reconnectInterval = INITIAL_RECONNECT_INTERVAL;
        connected.set(true);//from w ww .  ja  v a2s. c  o m
        onConnected();
    }
}

From source file:net.ymate.platform.serv.nio.support.NioEventProcessor.java

protected void __doExceptionEvent(final SelectionKey key, final Throwable e) {
    final INioSession _session = (INioSession) key.attachment();
    if (_session == null) {
        try {/*ww  w . ja  va 2 s .c om*/
            key.channel().close();
            key.cancel();
        } catch (Throwable ex) {
            _LOG.error(e.getMessage(), RuntimeUtils.unwrapThrow(ex));
        }
    } else {
        _session.status(ISession.Status.ERROR);
    }
    __eventGroup.executorService().submit(new Runnable() {
        public void run() {
            try {
                __eventGroup.listener().onExceptionCaught(e, _session);
            } catch (Throwable ex) {
                _LOG.error(e.getMessage(), RuntimeUtils.unwrapThrow(ex));
            }
        }
    });
    if (_session != null) {
        try {
            _session.close();
        } catch (Throwable ex) {
            _LOG.error(e.getMessage(), RuntimeUtils.unwrapThrow(ex));
        }
    }
}

From source file:org.openhab.io.transport.cul.internal.network.CULNetworkHandlerImpl.java

private void processRead(SelectionKey key) throws Exception {
    ReadableByteChannel ch = (ReadableByteChannel) key.channel();

    int bytesOp = 0, bytesTotal = 0;
    while (readBuf.hasRemaining() && (bytesOp = ch.read(readBuf)) > 0) {
        bytesTotal += bytesOp;//  ww  w  . ja v  a2 s .  co  m
    }
    logger.debug("Read {} bytes from network", bytesTotal);

    if (bytesTotal > 0) {
        readBuf.flip();
        onRead(readBuf);
        readBuf.compact();
    } else if (bytesOp == -1) {
        logger.info("peer closed read channel");
        ch.close();
    }
}