All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.muserver.AsyncContext Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package io.muserver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @deprecated This interface is no longer used. Instead call {@link MuRequest#handleAsync()} from a standard Mu Handler.
 */
@Deprecated
public class AsyncContext {
    private static final Logger log = LoggerFactory.getLogger(AsyncContext.class);
    public final MuRequest request;
    public final MuResponse response;
    private final MuStatsImpl stats;
    public Object state;
    GrowableByteBufferInputStream requestBody;
    private AtomicBoolean completed = new AtomicBoolean(false);

    AsyncContext(MuRequest request, MuResponse response, MuStatsImpl stats) {
        this.request = request;
        this.response = response;
        this.stats = stats;
    }

    public Future complete() {
        boolean wasCompleted = this.completed.getAndSet(true);
        if (wasCompleted) {
            log.info("AsyncContext.complete called twice for " + request);
            return null;
        } else {
            stats.onRequestEnded(request);
            return ((NettyResponseAdaptor) response).complete();
        }
    }

    boolean isComplete() {
        return completed.get();
    }

    void onDisconnected() {
        boolean wasCompleted = isComplete();
        ((NettyRequestAdapter) request).onClientDisconnected(wasCompleted);
        if (!wasCompleted) {
            complete();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy