Java Code Examples for org.xnio.ChannelListeners#invokeChannelListener()

The following examples show how to use org.xnio.ChannelListeners#invokeChannelListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReadTimeoutStreamSourceConduit.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    handle = null;
    if (expireTime == -1) {
        return;
    }
    long current = System.currentTimeMillis();
    if (current  < expireTime) {
        //timeout has been bumped, re-schedule
        handle = WorkerUtils.executeAfter(connection.getIoThread(),timeoutCommand, (expireTime - current) + FUZZ_FACTOR, TimeUnit.MILLISECONDS);
        return;
    }
    UndertowLogger.REQUEST_LOGGER.tracef("Timing out channel %s due to inactivity", connection.getSourceChannel());
    IoUtils.safeClose(connection);
    if (connection.getSourceChannel().isReadResumed()) {
        ChannelListeners.invokeChannelListener(connection.getSourceChannel(), connection.getSourceChannel().getReadListener());
    }
    if (connection.getSinkChannel().isWriteResumed()) {
        ChannelListeners.invokeChannelListener(connection.getSinkChannel(), connection.getSinkChannel().getWriteListener());
    }
}
 
Example 2
Source File: Http2Channel.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void closeSubChannels() {

    for (Map.Entry<Integer, StreamHolder> e : currentStreams.entrySet()) {
        StreamHolder holder = e.getValue();
        AbstractHttp2StreamSourceChannel receiver = holder.sourceChannel;
        if(receiver != null) {
            receiver.markStreamBroken();
        }
        Http2StreamSinkChannel sink = holder.sinkChannel;
        if(sink != null) {
            if (sink.isWritesShutdown()) {
                ChannelListeners.invokeChannelListener(sink.getIoThread(), sink, ((ChannelListener.SimpleSetter) sink.getWriteSetter()).get());
            }
            IoUtils.safeClose(sink);
        }

    }
}
 
Example 3
Source File: WriteTimeoutStreamSinkConduit.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    handle = null;
    if (expireTime == -1) {
        return;
    }
    long current = System.currentTimeMillis();
    if (current  < expireTime) {
        //timeout has been bumped, re-schedule
        handle = WorkerUtils.executeAfter(getWriteThread(),timeoutCommand, (expireTime - current) + FUZZ_FACTOR, TimeUnit.MILLISECONDS);
        return;
    }
    UndertowLogger.REQUEST_LOGGER.tracef("Timing out channel %s due to inactivity", connection.getSinkChannel());
    IoUtils.safeClose(connection);
    if (connection.getSourceChannel().isReadResumed()) {
        ChannelListeners.invokeChannelListener(connection.getSourceChannel(), connection.getSourceChannel().getReadListener());
    }
    if (connection.getSinkChannel().isWriteResumed()) {
        ChannelListeners.invokeChannelListener(connection.getSinkChannel(), connection.getSinkChannel().getWriteListener());
    }
}
 
Example 4
Source File: UndertowXnioSsl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void handleEvent(final StreamConnection connection) {
    try {

        SSLEngine sslEngine = JsseSslUtils.createSSLEngine(sslContext, optionMap, destination);
        SSLParameters params = sslEngine.getSSLParameters();
        params.setServerNames(Collections.singletonList(new SNIHostName(destination.getHostString())));
        sslEngine.setSSLParameters(params);

        final SslConnection wrappedConnection = new UndertowSslConnection(connection, sslEngine, bufferPool);
        if (!futureResult.setResult(wrappedConnection)) {
            IoUtils.safeClose(connection);
        } else {
            ChannelListeners.invokeChannelListener(wrappedConnection, openListener);
        }
    } catch (Throwable e) {
        futureResult.setException(new IOException(e));
    }
}
 
Example 5
Source File: AbstractServerConnection.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handleEvent(StreamConnection channel) {
    try {
        for (CloseListener l : closeListeners) {
            try {
                l.closed(AbstractServerConnection.this);
            } catch (Throwable e) {
                UndertowLogger.REQUEST_LOGGER.exceptionInvokingCloseListener(l, e);
            }
        }
        if (current != null) {
            current.endExchange();
        }
        ChannelListeners.invokeChannelListener(AbstractServerConnection.this, listener);
    } finally {
        if(extraBytes != null) {
            extraBytes.close();
            extraBytes = null;
        }
    }
}
 
Example 6
Source File: UndertowSslConnection.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void run() {
    final ChannelListener<? super SslConnection> listener = handshakeSetter.get();
    if (listener == null) {
        return;
    }
    ChannelListeners.<SslConnection>invokeChannelListener(UndertowSslConnection.this, listener);
}
 
Example 7
Source File: AbstractFramedStreamSourceChannel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void close() {
    if(anyAreSet(state, STATE_CLOSED)) {
        return;
    }
    synchronized (lock) {
        state |= STATE_CLOSED;
        if (allAreClear(state, STATE_DONE | STATE_LAST_FRAME)) {
            state |= STATE_STREAM_BROKEN;
            channelForciblyClosed();
        }
        if (data != null) {
            data.close();
            data = null;
        }
        while (!pendingFrameData.isEmpty()) {
            pendingFrameData.poll().frameData.close();
        }

        ChannelListeners.invokeChannelListener(this, (ChannelListener<? super AbstractFramedStreamSourceChannel<C, R, S>>) closeSetter.get());
        if (closeListeners != null) {
            for (int i = 0; i < closeListeners.length; ++i) {
                closeListeners[i].handleEvent(this);
            }
        }
    }
}
 
Example 8
Source File: AbstractFramedChannel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void handleEvent(final StreamSourceChannel channel) {
    //clear the task queue before reading
    while (!taskRunQueue.isEmpty()) {
        taskRunQueue.poll().run();
    }

    final R receiver = AbstractFramedChannel.this.receiver;
    if ((readChannelDone || receivesSuspended) && receiver == null) {
        channel.suspendReads();
        return;
    } else {
        ChannelListener listener = receiveSetter.get();
        if(listener == null) {
            listener = DRAIN_LISTENER;
        }
        UndertowLogger.REQUEST_IO_LOGGER.tracef("Invoking receive listener", receiver);
        ChannelListeners.invokeChannelListener(AbstractFramedChannel.this, listener);
    }
    if (readData != null  && !readData.isFreed() && channel.isOpen()) {
        try {
            runInIoThread(new Runnable() {
                @Override
                public void run() {
                    ChannelListeners.invokeChannelListener(channel, FrameReadListener.this);
                }
            });
        } catch (RejectedExecutionException e) {
            IoUtils.safeClose(AbstractFramedChannel.this);
        }
    }
}
 
Example 9
Source File: AbstractFramedStreamSinkChannel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void markBroken() {
    this.broken = true;
    try {
        wakeupWrites();
        wakeupWaiters();
        if (isWriteResumed()) {
            ChannelListener<? super S> writeListener = this.writeSetter.get();
            if (writeListener != null) {
                ChannelListeners.invokeChannelListener(getIoThread(), (S) this, writeListener);
            }
        }
        ChannelListener<? super S> closeListener = this.closeSetter.get();
        if (closeListener != null) {
            ChannelListeners.invokeChannelListener(getIoThread(), (S) this, closeListener);
        }
    } finally {
        if(header != null) {
            if( header.getByteBuffer() != null) {
                header.getByteBuffer().close();
                header = null;
            }
        }
        if(body != null) {
            body.close();
            body = null;
        }
        if(writeBuffer != null) {
            writeBuffer.close();
            writeBuffer = null;
        }
    }
}
 
Example 10
Source File: DetachableStreamSourceChannel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void handleEvent(final StreamSourceChannel channel) {
    ChannelListener<? super StreamSourceChannel> channelListener = setter.get();
    if(channelListener != null) {
        ChannelListeners.invokeChannelListener(this.channel, channelListener);
    } else {
        UndertowLogger.REQUEST_LOGGER.debugf("suspending reads on %s to prevent listener runaway", channel);
        channel.suspendReads();
    }
}
 
Example 11
Source File: Http2ClientConnection.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handleEvent(Http2Channel channel) {
    ChannelListeners.invokeChannelListener(Http2ClientConnection.this, closeSetter.get());
    for (ChannelListener<ClientConnection> listener : closeListeners) {
        listener.handleEvent(Http2ClientConnection.this);
    }
    for (Map.Entry<Integer, Http2ClientExchange> entry : currentExchanges.entrySet()) {
        entry.getValue().failed(new ClosedChannelException());
    }
    currentExchanges.clear();
}
 
Example 12
Source File: Http2StreamSourceChannel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void complete() throws IOException {
    super.complete();
    if (completionListener != null) {
        ChannelListeners.invokeChannelListener(this, completionListener);
    }
}
 
Example 13
Source File: SslConduit.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void writeReady() {
    if(anyAreSet(state, FLAG_READ_REQUIRES_WRITE)) {
        if(anyAreSet(state, FLAG_READS_RESUMED)) {
            readReadyHandler.readReady();
        } else {
            try {
                doHandshake();
            } catch (IOException e) {
                UndertowLogger.REQUEST_LOGGER.ioException(e);
                IoUtils.safeClose(delegate);
            } catch (Throwable t) {
                UndertowLogger.REQUEST_LOGGER.handleUnexpectedFailure(t);
                IoUtils.safeClose(delegate);
            }
        }
    }
    if (anyAreSet(state, FLAG_WRITES_RESUMED)) {
        if(delegateHandler == null) {
                final ChannelListener<? super ConduitStreamSinkChannel> writeListener = connection.getSinkChannel().getWriteListener();
                if (writeListener == null) {
                    suspendWrites();
                } else {
                    ChannelListeners.invokeChannelListener(connection.getSinkChannel(), writeListener);
                }
        } else {
            delegateHandler.writeReady();
        }
    }
    if(!anyAreSet(state, FLAG_WRITES_RESUMED | FLAG_READ_REQUIRES_WRITE)) {
        delegate.getSinkChannel().suspendWrites();
    }
}
 
Example 14
Source File: DetachableStreamSinkChannel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void handleEvent(final StreamSinkChannel channel) {
    ChannelListener<? super StreamSinkChannel> channelListener = setter.get();
    if(channelListener != null) {
        ChannelListeners.invokeChannelListener(this.channel, channelListener);
    } else {
        UndertowLogger.REQUEST_LOGGER.debugf("suspending writes on %s to prevent listener runaway", channel);
        channel.suspendWrites();
    }
}
 
Example 15
Source File: Http2DataStreamSinkChannel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void channelForciblyClosed() throws IOException {
    super.channelForciblyClosed();
    if (completionListener != null) {
        ChannelListeners.invokeChannelListener(this, completionListener);
        completionListener = null;
    }
}
 
Example 16
Source File: Http2DataStreamSinkChannel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean flush() throws IOException {
    if(completionListenerReady && completionListener != null) {
        ChannelListeners.invokeChannelListener(this, completionListener);
        completionListener = null;
    }
    return super.flush();
}
 
Example 17
Source File: AbstractFramedStreamSinkChannel.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method that is invoked when a frame has been fully flushed. This method is only invoked by the IO thread
 */
final void flushComplete() throws IOException {
    synchronized (lock) {
        try {
            bufferFull = false;
            int remaining = header.getRemainingInBuffer();
            boolean finalFrame = finalFrameQueued;
            boolean channelClosed = finalFrame && remaining == 0 && !header.isAnotherFrameRequired();
            if (remaining > 0) {
                body.getBuffer().limit(body.getBuffer().limit() + remaining);
                if (finalFrame) {
                    //we clear the final frame flag, as it could not actually be written out
                    //note that we don't attempt to requeue, as whatever stopped it from being written will likely still
                    //be an issue
                    this.finalFrameQueued = false;
                }
            } else if (header.isAnotherFrameRequired()) {
                this.finalFrameQueued = false;
                if (body != null) {
                    body.close();
                    body = null;
                    state &= ~STATE_PRE_WRITE_CALLED;
                }
            } else if (body != null) {
                body.close();
                body = null;
                state &= ~STATE_PRE_WRITE_CALLED;
            }
            if (channelClosed) {
                fullyFlushed = true;
                if (body != null) {
                    body.close();
                    body = null;
                    state &= ~STATE_PRE_WRITE_CALLED;
                }
            } else if (body != null) {
                // We still have a body, but since we just flushed, we transfer it to the write buffer.
                // This works as long as you call write() again

                //TODO: this code may not work if the channel has frame level compression and flow control
                //we don't have an implementation that needs this yet so it is ok for now

                body.getBuffer().compact();
                writeBuffer = body;
                body = null;
                state &= ~STATE_PRE_WRITE_CALLED;
            }

            if (header.getByteBuffer() != null) {
                header.getByteBuffer().close();
            }
            header = null;

            readyForFlush = false;
            if (isWriteResumed() && !channelClosed) {
                wakeupWrites();
            } else if (isWriteResumed()) {
                //we need to execute the write listener one last time
                //we need to dispatch it back to the IO thread, so we don't invoke it recursivly
                ChannelListeners.invokeChannelListener(getIoThread(), (S) this, getWriteListener());
            }

            final ChannelListener<? super S> closeListener = this.closeSetter.get();
            if (channelClosed && closeListener != null) {
                ChannelListeners.invokeChannelListener(getIoThread(), (S) AbstractFramedStreamSinkChannel.this, closeListener);
            }
            handleFlushComplete(channelClosed);
        } finally {
            wakeupWaiters();
        }
    }
}
 
Example 18
Source File: SslConduit.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void terminated() {
    ChannelListeners.invokeChannelListener(connection.getSourceChannel(), connection.getSourceChannel().getCloseListener());
}
 
Example 19
Source File: SslConduit.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void readReady() {
    if(anyAreSet(state, FLAG_WRITE_REQUIRES_READ) && anyAreSet(state, FLAG_WRITES_RESUMED | FLAG_READS_RESUMED) && !anyAreSet(state, FLAG_ENGINE_INBOUND_SHUTDOWN)) {
        try {
            invokingReadListenerHandshake = true;
            doHandshake();
        } catch (IOException e) {
            UndertowLogger.REQUEST_LOGGER.ioException(e);
            IoUtils.safeClose(delegate);
        } catch (Throwable t) {
            UndertowLogger.REQUEST_IO_LOGGER.handleUnexpectedFailure(t);
            IoUtils.safeClose(delegate);
        } finally {
            invokingReadListenerHandshake = false;
        }

        if(!anyAreSet(state, FLAG_READS_RESUMED) && !allAreSet(state, FLAG_WRITE_REQUIRES_READ | FLAG_WRITES_RESUMED)) {
            delegate.getSourceChannel().suspendReads();
        }
    }

    boolean noProgress = false;
    int initialDataToUnwrap = -1;
    int initialUnwrapped = -1;
    if (anyAreSet(state, FLAG_READS_RESUMED)) {
        if (delegateHandler == null) {
            final ChannelListener<? super ConduitStreamSourceChannel> readListener = connection.getSourceChannel().getReadListener();
            if (readListener == null) {
                suspendReads();
            } else {
                if(anyAreSet(state, FLAG_DATA_TO_UNWRAP)) {
                    initialDataToUnwrap = dataToUnwrap.getBuffer().remaining();
                }
                if(unwrappedData != null) {
                    initialUnwrapped = unwrappedData.getBuffer().remaining();
                }
                ChannelListeners.invokeChannelListener(connection.getSourceChannel(), readListener);
                if(anyAreSet(state, FLAG_DATA_TO_UNWRAP) && initialDataToUnwrap == dataToUnwrap.getBuffer().remaining()) {
                    noProgress = true;
                } else if(unwrappedData != null && unwrappedData.getBuffer().remaining() == initialUnwrapped) {
                    noProgress = true;
                }
            }
        } else {
            delegateHandler.readReady();
        }
    }
    if(anyAreSet(state, FLAG_READS_RESUMED) && (unwrappedData != null || anyAreSet(state, FLAG_DATA_TO_UNWRAP))) {
        if(anyAreSet(state, FLAG_READ_CLOSED)) {
            if(unwrappedData != null) {
                unwrappedData.close();
            }
            if(dataToUnwrap != null) {
                dataToUnwrap.close();
            }
            unwrappedData = null;
            dataToUnwrap = null;
        } else {
            //there is data in the buffers so we do a wakeup
            //as we may not get an actual read notification
            //if we need to write for the SSL engine to progress we don't invoke the read listener
            //otherwise it will run in a busy loop till the channel becomes writable
            //we also don't re-run if we have outstanding tasks
            if(!(anyAreSet(state, FLAG_READ_REQUIRES_WRITE) && wrappedData != null) && outstandingTasks == 0 && !noProgress) {
                runReadListener(false);
            }
        }
    }
}
 
Example 20
Source File: Http2StreamSourceChannel.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void setCompletionListener(ChannelListener<Http2StreamSourceChannel> completionListener) {
    this.completionListener = completionListener;
    if(isComplete()) {
        ChannelListeners.invokeChannelListener(this, completionListener);
    }
}