Search in sources :

Example 1 with ExecutableCommand

use of org.apache.hc.core5.http.nio.command.ExecutableCommand in project httpcomponents-core by apache.

the class AbstractH2StreamMultiplexer method processPendingCommands.

private void processPendingCommands() throws IOException, HttpException {
    while (streamMap.size() < remoteConfig.getMaxConcurrentStreams()) {
        final Command command = ioSession.poll();
        if (command == null) {
            break;
        }
        if (command instanceof ShutdownCommand) {
            final ShutdownCommand shutdownCommand = (ShutdownCommand) command;
            if (shutdownCommand.getType() == CloseMode.IMMEDIATE) {
                for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
                    final Map.Entry<Integer, H2Stream> entry = it.next();
                    final H2Stream stream = entry.getValue();
                    stream.cancel();
                }
                streamMap.clear();
                connState = ConnectionHandshake.SHUTDOWN;
            } else {
                if (connState.compareTo(ConnectionHandshake.ACTIVE) <= 0) {
                    final RawFrame goAway = frameFactory.createGoAway(processedRemoteStreamId, H2Error.NO_ERROR, "Graceful shutdown");
                    commitFrame(goAway);
                    connState = streamMap.isEmpty() ? ConnectionHandshake.SHUTDOWN : ConnectionHandshake.GRACEFUL_SHUTDOWN;
                }
            }
            break;
        } else if (command instanceof PingCommand) {
            final PingCommand pingCommand = (PingCommand) command;
            final AsyncPingHandler handler = pingCommand.getHandler();
            pingHandlers.add(handler);
            final RawFrame ping = frameFactory.createPing(handler.getData());
            commitFrame(ping);
        } else if (command instanceof ExecutableCommand) {
            final int streamId = generateStreamId();
            final H2StreamChannelImpl channel = new H2StreamChannelImpl(streamId, true, initInputWinSize, initOutputWinSize);
            final ExecutableCommand executableCommand = (ExecutableCommand) command;
            final H2StreamHandler streamHandler = createLocallyInitiatedStream(executableCommand, channel, httpProcessor, connMetrics);
            final H2Stream stream = new H2Stream(channel, streamHandler, false);
            streamMap.put(streamId, stream);
            if (streamListener != null) {
                final int initInputWindow = stream.getInputWindow().get();
                streamListener.onInputFlowControl(this, streamId, initInputWindow, initInputWindow);
                final int initOutputWindow = stream.getOutputWindow().get();
                streamListener.onOutputFlowControl(this, streamId, initOutputWindow, initOutputWindow);
            }
            if (stream.isOutputReady()) {
                stream.produceOutput();
            }
            final CancellableDependency cancellableDependency = executableCommand.getCancellableDependency();
            if (cancellableDependency != null) {
                cancellableDependency.setDependency(stream::abort);
            }
            if (!outputQueue.isEmpty()) {
                return;
            }
        }
    }
}
Also used : ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) CancellableDependency(org.apache.hc.core5.concurrent.CancellableDependency) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) Command(org.apache.hc.core5.reactor.Command) ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) AsyncPingHandler(org.apache.hc.core5.http2.nio.AsyncPingHandler) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ExecutableCommand

use of org.apache.hc.core5.http.nio.command.ExecutableCommand in project httpcomponents-core by apache.

the class ClientH2StreamMultiplexer method createLocallyInitiatedStream.

@Override
H2StreamHandler createLocallyInitiatedStream(final ExecutableCommand command, final H2StreamChannel channel, final HttpProcessor httpProcessor, final BasicHttpConnectionMetrics connMetrics) throws IOException {
    if (command instanceof RequestExecutionCommand) {
        final RequestExecutionCommand executionCommand = (RequestExecutionCommand) command;
        final AsyncClientExchangeHandler exchangeHandler = executionCommand.getExchangeHandler();
        final HandlerFactory<AsyncPushConsumer> pushHandlerFactory = executionCommand.getPushHandlerFactory();
        final HttpCoreContext context = HttpCoreContext.adapt(executionCommand.getContext());
        context.setAttribute(HttpCoreContext.SSL_SESSION, getSSLSession());
        context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, getEndpointDetails());
        return new ClientH2StreamHandler(channel, httpProcessor, connMetrics, exchangeHandler, pushHandlerFactory != null ? pushHandlerFactory : this.pushHandlerFactory, context);
    }
    throw new H2ConnectionException(H2Error.INTERNAL_ERROR, "Unexpected executable command");
}
Also used : AsyncPushConsumer(org.apache.hc.core5.http.nio.AsyncPushConsumer) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) H2ConnectionException(org.apache.hc.core5.http2.H2ConnectionException) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand)

Example 3 with ExecutableCommand

use of org.apache.hc.core5.http.nio.command.ExecutableCommand in project httpcomponents-core by apache.

the class AbstractH2StreamMultiplexer method onDisconnect.

public final void onDisconnect() {
    for (; ; ) {
        final AsyncPingHandler pingHandler = pingHandlers.poll();
        if (pingHandler != null) {
            pingHandler.cancel();
        } else {
            break;
        }
    }
    for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
        final Map.Entry<Integer, H2Stream> entry = it.next();
        final H2Stream stream = entry.getValue();
        stream.cancel();
    }
    for (; ; ) {
        final Command command = ioSession.poll();
        if (command != null) {
            if (command instanceof ExecutableCommand) {
                ((ExecutableCommand) command).failed(new ConnectionClosedException());
            } else {
                command.cancel();
            }
        } else {
            break;
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) Command(org.apache.hc.core5.reactor.Command) ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) AsyncPingHandler(org.apache.hc.core5.http2.nio.AsyncPingHandler) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with ExecutableCommand

use of org.apache.hc.core5.http.nio.command.ExecutableCommand in project httpcomponents-core by apache.

the class CommandSupport method failCommands.

/**
 * Fails all pending session {@link Command}s.
 */
public static void failCommands(final IOSession ioSession, final Exception ex) {
    Args.notNull(ioSession, "I/O session");
    Command command;
    while ((command = ioSession.poll()) != null) {
        if (command instanceof ExecutableCommand) {
            ((ExecutableCommand) command).failed(ex);
        } else {
            command.cancel();
        }
    }
}
Also used : Command(org.apache.hc.core5.reactor.Command)

Example 5 with ExecutableCommand

use of org.apache.hc.core5.http.nio.command.ExecutableCommand in project httpcomponents-core by apache.

the class CommandSupport method cancelCommands.

/**
 * Cancels all pending session {@link Command}s.
 */
public static void cancelCommands(final IOSession ioSession) {
    Args.notNull(ioSession, "I/O session");
    Command command;
    while ((command = ioSession.poll()) != null) {
        if (command instanceof ExecutableCommand) {
            if (!ioSession.isOpen()) {
                ((ExecutableCommand) command).failed(new RequestNotExecutedException());
            } else {
                command.cancel();
            }
        } else {
            command.cancel();
        }
    }
}
Also used : Command(org.apache.hc.core5.reactor.Command) RequestNotExecutedException(org.apache.hc.core5.http.RequestNotExecutedException)

Aggregations

Command (org.apache.hc.core5.reactor.Command)5 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ExecutableCommand (org.apache.hc.core5.http.nio.command.ExecutableCommand)3 ShutdownCommand (org.apache.hc.core5.http.nio.command.ShutdownCommand)3 AsyncPingHandler (org.apache.hc.core5.http2.nio.AsyncPingHandler)3 PingCommand (org.apache.hc.core5.http2.nio.command.PingCommand)3 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)2 H2ConnectionException (org.apache.hc.core5.http2.H2ConnectionException)2 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)2 IOException (java.io.IOException)1 CancellableDependency (org.apache.hc.core5.concurrent.CancellableDependency)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1 RequestNotExecutedException (org.apache.hc.core5.http.RequestNotExecutedException)1 AsyncClientExchangeHandler (org.apache.hc.core5.http.nio.AsyncClientExchangeHandler)1 AsyncPushConsumer (org.apache.hc.core5.http.nio.AsyncPushConsumer)1 RequestExecutionCommand (org.apache.hc.core5.http.nio.command.RequestExecutionCommand)1 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)1 H2Error (org.apache.hc.core5.http2.H2Error)1