Search in sources :

Example 1 with SocketChannelAttachment

use of org.apache.nifi.processor.util.listen.dispatcher.SocketChannelAttachment in project nifi by apache.

the class SSLSocketChannelHandler method run.

@Override
public void run() {
    boolean eof = false;
    SSLSocketChannel sslSocketChannel = null;
    try {
        int bytesRead;
        final SocketChannel socketChannel = (SocketChannel) key.channel();
        final SocketChannelAttachment attachment = (SocketChannelAttachment) key.attachment();
        // get the SSLSocketChannel from the attachment
        sslSocketChannel = attachment.getSslSocketChannel();
        // SSLSocketChannel deals with byte[] so ByteBuffer isn't used here, but we'll use the size to create a new byte[]
        final ByteBuffer socketBuffer = attachment.getByteBuffer();
        byte[] socketBufferArray = new byte[socketBuffer.limit()];
        // read until no more data
        try {
            while ((bytesRead = sslSocketChannel.read(socketBufferArray)) > 0) {
                processBuffer(sslSocketChannel, socketChannel, bytesRead, socketBufferArray);
                logger.debug("bytes read from sslSocketChannel {}", new Object[] { bytesRead });
            }
        } catch (SocketTimeoutException ste) {
            // SSLSocketChannel will throw this exception when 0 bytes are read and the timeout threshold
            // is exceeded, we don't want to close the connection in this case
            bytesRead = 0;
        }
        // Check for closed socket
        if (bytesRead < 0) {
            eof = true;
            logger.debug("Reached EOF, closing connection");
        } else {
            logger.debug("No more data available, returning for selection");
        }
    } catch (ClosedByInterruptException | InterruptedException e) {
        logger.debug("read loop interrupted, closing connection");
        // Treat same as closed socket
        eof = true;
    } catch (ClosedChannelException e) {
        // ClosedChannelException doesn't have a message so handle it separately from IOException
        logger.error("Error reading from channel due to channel being closed", e);
        // Treat same as closed socket
        eof = true;
    } catch (IOException e) {
        logger.error("Error reading from channel due to {}", new Object[] { e.getMessage() }, e);
        // Treat same as closed socket
        eof = true;
    } finally {
        if (eof == true) {
            IOUtils.closeQuietly(sslSocketChannel);
            dispatcher.completeConnection(key);
        } else {
            dispatcher.addBackForSelection(key);
        }
    }
}
Also used : SSLSocketChannel(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SocketChannelAttachment(org.apache.nifi.processor.util.listen.dispatcher.SocketChannelAttachment) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SSLSocketChannel(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel) SocketTimeoutException(java.net.SocketTimeoutException)

Example 2 with SocketChannelAttachment

use of org.apache.nifi.processor.util.listen.dispatcher.SocketChannelAttachment in project nifi by apache.

the class StandardSocketChannelHandler method run.

@Override
public void run() {
    boolean eof = false;
    SocketChannel socketChannel = null;
    try {
        int bytesRead;
        socketChannel = (SocketChannel) key.channel();
        final SocketChannelAttachment attachment = (SocketChannelAttachment) key.attachment();
        final ByteBuffer socketBuffer = attachment.getByteBuffer();
        // read until the buffer is full
        while ((bytesRead = socketChannel.read(socketBuffer)) > 0) {
            // prepare byte buffer for reading
            socketBuffer.flip();
            // mark the current position as start, in case of partial message read
            socketBuffer.mark();
            // process the contents that have been read into the buffer
            processBuffer(socketChannel, socketBuffer);
            // Preserve bytes in buffer for next call to run
            // NOTE: This code could benefit from the  two ByteBuffer read calls to avoid
            // this compact for higher throughput
            socketBuffer.reset();
            socketBuffer.compact();
            logger.debug("bytes read {}", new Object[] { bytesRead });
        }
        // Check for closed socket
        if (bytesRead < 0) {
            eof = true;
            logger.debug("Reached EOF, closing connection");
        } else {
            logger.debug("No more data available, returning for selection");
        }
    } catch (ClosedByInterruptException | InterruptedException e) {
        logger.debug("read loop interrupted, closing connection");
        // Treat same as closed socket
        eof = true;
    } catch (ClosedChannelException e) {
        // ClosedChannelException doesn't have a message so handle it separately from IOException
        logger.error("Error reading from channel due to channel being closed", e);
        // Treat same as closed socket
        eof = true;
    } catch (IOException e) {
        logger.error("Error reading from channel due to {}", new Object[] { e.getMessage() }, e);
        // Treat same as closed socket
        eof = true;
    } finally {
        if (eof == true) {
            IOUtils.closeQuietly(socketChannel);
            dispatcher.completeConnection(key);
        } else {
            dispatcher.addBackForSelection(key);
        }
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SocketChannelAttachment(org.apache.nifi.processor.util.listen.dispatcher.SocketChannelAttachment)

Aggregations

IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 SocketChannel (java.nio.channels.SocketChannel)2 SocketChannelAttachment (org.apache.nifi.processor.util.listen.dispatcher.SocketChannelAttachment)2 SocketTimeoutException (java.net.SocketTimeoutException)1 SSLSocketChannel (org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel)1