Search in sources :

Example 46 with XFuture

use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.

the class BasChannelImpl method unqueueAndFailWritesThenClose.

private void unqueueAndFailWritesThenClose(CloseRunnable action) {
    List<XFuture<Void>> promises;
    synchronized (this) {
        // put here for emphasis that we are synchronizing here but not below
        promises = failAllWritesInQueue();
    }
    // TODO: This should really be inlined now.  It's a remnant of an old design since close didn't
    // work well outside the selector thread previously
    action.runDelayedAction();
    // notify clients outside the synchronization block!!!
    for (XFuture<Void> promise : promises) {
        log.info("WRITES outstanding while close was called, notifying client through his failure method of the exception");
        // we only incur the cost of Throwable.fillInStackTrace() if we will use this exception
        // (it's called in the Throwable constructor) so we don't do this on every close channel
        NioClosedChannelException closeExc = new NioClosedChannelException(this + "There are " + promises.size() + " writes that are not complete yet(you called write but " + "they did not call success back to the client).");
        promise.completeExceptionally(closeExc);
    }
}
Also used : XFuture(org.webpieces.util.futures.XFuture) NioClosedChannelException(org.webpieces.util.exceptions.NioClosedChannelException)

Example 47 with XFuture

use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.

the class BasChannelImpl method writeAll.

/**
 * This method is reading from the queue and writing out to the socket buffers that
 * did not get written out when client called write.
 */
void writeAll() {
    List<XFuture<Void>> finishedPromises = new ArrayList<>();
    synchronized (writeLock) {
        if (dataToBeWritten.isEmpty())
            throw new IllegalStateException(this + "bug, I am not sure this is possible..it shouldn't be...look into");
        while (!dataToBeWritten.isEmpty()) {
            WriteInfo writer = dataToBeWritten.peek();
            ByteBuffer buffer = writer.getBuffer();
            int initialSize = buffer.remaining();
            int wroteOut = this.writeImpl(buffer);
            if (buffer.hasRemaining()) {
                if (buffer.remaining() + wroteOut != initialSize)
                    throw new IllegalStateException(this + "Something went wrong.  b.remaining()=" + buffer.remaining() + " written=" + wroteOut + " total=" + initialSize);
                if (log.isTraceEnabled())
                    log.trace(this + "Did not write all data out");
                int leftOverSize = buffer.remaining();
                int writtenOut = initialSize - leftOverSize;
                waitingBytesCounter -= writtenOut;
                break;
            }
            // if it finished, remove the item from the queue.  It
            // does not need to be run again.
            dataToBeWritten.poll();
            waitingBytesCounter -= initialSize;
            finishedPromises.add(writer.getPromise());
        }
        // we are registered for writes with ANY size queue
        if (dataToBeWritten.isEmpty() && inDelayedWriteMode) {
            inDelayedWriteMode = false;
            if (log.isTraceEnabled())
                log.trace(this + "unregister writes");
            router.unregisterSelectableChannel(this, SelectionKey.OP_WRITE);
        }
    }
    // MAKE SURE to notify clients outside of synchronization block so no deadlocks with their locks
    for (XFuture<Void> promise : finishedPromises) {
        promise.complete(null);
    }
}
Also used : XFuture(org.webpieces.util.futures.XFuture) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 48 with XFuture

use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.

the class BasChannelImpl method failAllWritesInQueue.

// synchronized with writeAll as both try to go through every element in the queue
// while most of the time there will be no contention(only on the close do we hit this)
private synchronized List<XFuture<Void>> failAllWritesInQueue() {
    List<XFuture<Void>> copy = new ArrayList<>();
    while (!dataToBeWritten.isEmpty()) {
        WriteInfo runnable = dataToBeWritten.remove();
        ByteBuffer buffer = runnable.getBuffer();
        // mark buffer read before releasing it
        buffer.position(buffer.limit());
        pool.releaseBuffer(buffer);
        copy.add(runnable.getPromise());
    }
    waitingBytesCounter = 0;
    return copy;
}
Also used : XFuture(org.webpieces.util.futures.XFuture) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 49 with XFuture

use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.

the class Level2ParsingAndRemoteSettings method parseImpl.

public XFuture<Void> parseImpl(DataWrapper newData) {
    parsingState = lowLevelParser.unmarshal(parsingState, newData);
    List<Http2Msg> parsedMessages = parsingState.getParsedFrames();
    // All the below futures must be chained with previous ones in case previous ones are not
    // done which will serialize it all to be in sequence
    XFuture<Void> future = parsingState.getProcessFuture();
    for (Http2Msg lowLevelFrame : parsedMessages) {
        // VERY IMPORTANT: Writing the code like this would slam through calling process N times
        // BUT it doesn't give the clients a chance to seet a flag between packets
        // Mainly done for exceptions and streaming so you can log exc, set a boolean so you
        // don't get 100 exceptions while something is happening like socket disconnect
        // In these 2 lines of code, processCorrectly is CALLED N times RIGHT NOW
        // The code below this only calls them right now IF AND ONLY IF the client returns
        // a completed future each time!!!
        // XFuture<Void> messageFuture = process(lowLevelFrame);
        // allFutures = allFutures.thenCompose( f -> messageFuture);
        future = future.thenCompose(f -> process(lowLevelFrame));
    }
    parsingState.setProcessFuturee(future);
    return future;
}
Also used : WindowUpdateFrame(com.webpieces.http2.api.dto.lowlevel.WindowUpdateFrame) PriorityFrame(com.webpieces.http2.api.dto.lowlevel.PriorityFrame) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) Http2Config(com.webpieces.http2engine.api.client.Http2Config) LoggerFactory(org.slf4j.LoggerFactory) UnknownFrame(com.webpieces.http2.api.dto.lowlevel.UnknownFrame) ByteAckTracker(org.webpieces.util.acking.ByteAckTracker) HeaderSettings(com.webpieces.http2engine.impl.shared.data.HeaderSettings) PingFrame(com.webpieces.http2.api.dto.lowlevel.PingFrame) HpackParser(com.webpieces.hpack.api.HpackParser) ConnectionException(com.webpieces.http2.api.dto.error.ConnectionException) UnmarshalState(com.webpieces.hpack.api.UnmarshalState) DataWrapper(org.webpieces.data.api.DataWrapper) SettingsFrame(com.webpieces.http2.api.dto.lowlevel.SettingsFrame) ConnectionClosedException(com.webpieces.http2engine.api.error.ConnectionClosedException) Logger(org.slf4j.Logger) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame) ConnectionFailure(com.webpieces.http2engine.api.error.ConnectionFailure) CancelReasonCode(com.webpieces.http2.api.dto.error.CancelReasonCode) List(java.util.List) StreamException(com.webpieces.http2.api.dto.error.StreamException) ReceivedGoAway(com.webpieces.http2engine.api.error.ReceivedGoAway) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)

Example 50 with XFuture

use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.

the class Level5AStates method translateException.

private XFuture<Void> translateException(Stream stream, Throwable t) {
    XFuture<Void> fut = new XFuture<>();
    if (t instanceof NoTransitionConnectionError)
        fut.completeExceptionally(new ConnectionException(CancelReasonCode.BAD_FRAME_RECEIVED_FOR_THIS_STATE, logId, stream.getStreamId(), t.getMessage(), t));
    else if (t instanceof NoTransitionStreamError)
        fut.completeExceptionally(new StreamException(CancelReasonCode.CLOSED_STREAM, logId, stream.getStreamId(), t.getMessage(), t));
    else
        fut.completeExceptionally(t);
    return fut;
}
Also used : NoTransitionStreamError(com.webpieces.http2engine.impl.shared.data.NoTransitionStreamError) XFuture(org.webpieces.util.futures.XFuture) NoTransitionConnectionError(com.webpieces.http2engine.impl.shared.data.NoTransitionConnectionError) ConnectionException(com.webpieces.http2.api.dto.error.ConnectionException) StreamException(com.webpieces.http2.api.dto.error.StreamException)

Aggregations

XFuture (org.webpieces.util.futures.XFuture)71 Test (org.junit.Test)21 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)20 ByteBuffer (java.nio.ByteBuffer)16 Logger (org.slf4j.Logger)15 LoggerFactory (org.slf4j.LoggerFactory)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Map (java.util.Map)12 DataWrapper (org.webpieces.data.api.DataWrapper)12 HttpFullRequest (org.webpieces.httpclient11.api.HttpFullRequest)12 HttpFullResponse (org.webpieces.httpclient11.api.HttpFullResponse)12 NotFoundException (org.webpieces.http.exception.NotFoundException)11 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)11 ResponseWrapper (org.webpieces.webserver.test.ResponseWrapper)11 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)10 PrivateWebserverForTest (org.webpieces.webserver.PrivateWebserverForTest)10 StreamRef (com.webpieces.http2.api.streaming.StreamRef)9 RequestContext (org.webpieces.ctx.api.RequestContext)9 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)8