Search in sources :

Example 31 with XFuture

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

the class Level6RemoteFlowControl method sendDataToSocket.

public XFuture<Void> sendDataToSocket(Stream stream, DataFrame dataFrame) {
    log.info("sending payload to socket=" + dataFrame);
    XFuture<Void> future = new XFuture<>();
    DataTry data = new DataTry(stream, dataFrame, future, false);
    trySendPayload(data);
    return future;
}
Also used : XFuture(org.webpieces.util.futures.XFuture) DataTry(com.webpieces.http2engine.impl.DataTry)

Example 32 with XFuture

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

the class MockHttp2Channel method write.

@SuppressWarnings("unchecked")
@Override
public XFuture<Void> write(ByteBuffer b) {
    if (!prefaceReceived) {
        // copy and store preface
        ByteBuffer prefaceBuffer = ByteBuffer.allocate(b.remaining());
        prefaceBuffer.put(b);
        prefaceReceived = true;
        Preface preface = new Preface(prefaceBuffer);
        List<Http2Msg> msgs = new ArrayList<>();
        msgs.add(preface);
        return (XFuture<Void>) super.calledMethod(Method.INCOMING_FRAME, msgs);
    }
    return processData(b);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) ArrayList(java.util.ArrayList) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) ByteBuffer(java.nio.ByteBuffer)

Example 33 with XFuture

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

the class MockHttp2Channel method processData.

@SuppressWarnings("unchecked")
private XFuture<Void> processData(ByteBuffer b) {
    DataWrapper data = dataGen.wrapByteBuffer(b);
    parser.unmarshal(unmarshalState, data);
    List<Http2Msg> parsedFrames = unmarshalState.getParsedFrames();
    return (XFuture<Void>) super.calledMethod(Method.INCOMING_FRAME, parsedFrames);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) XFuture(org.webpieces.util.futures.XFuture) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)

Example 34 with XFuture

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

the class EScopedRouter method invokeRouter.

private RouterStreamRef invokeRouter(AbstractRouter router, RequestContext ctx, ProxyStreamHandle handler, boolean isCorsRequest) {
    RouterStreamRef streamRef = invokeWithProtection(router, ctx, handler, isCorsRequest);
    XFuture<StreamWriter> writer = streamRef.getWriter().handle((r, t) -> {
        if (t == null)
            return XFuture.completedFuture(r);
        XFuture<StreamWriter> fut = new XFuture<>();
        Throwable exc = convert(router.getMatchInfo(), t);
        fut.completeExceptionally(exc);
        return fut;
    }).thenCompose(Function.identity());
    return new RouterStreamRef("eScoped2", writer, streamRef);
}
Also used : ProcessCors(org.webpieces.router.api.routebldr.ProcessCors) SneakyThrow(org.webpieces.util.exceptions.SneakyThrow) LoggerFactory(org.slf4j.LoggerFactory) RouterHeader(org.webpieces.ctx.api.RouterHeader) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NotFoundException(org.webpieces.http.exception.NotFoundException) Strings(com.google.common.base.Strings) RouterRequest(org.webpieces.ctx.api.RouterRequest) RequestContext(org.webpieces.ctx.api.RequestContext) Map(java.util.Map) RouterInfo(org.webpieces.router.impl.model.RouterInfo) WebpiecesException(org.webpieces.util.exceptions.WebpiecesException) Logger(org.slf4j.Logger) RouterFutureUtil(org.webpieces.router.impl.RouterFutureUtil) AccessResult(org.webpieces.router.api.routebldr.AccessResult) SpecificRouterInvokeException(org.webpieces.router.api.exceptions.SpecificRouterInvokeException) TimeUnit(java.util.concurrent.TimeUnit) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) List(java.util.List) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) MatchResult2(org.webpieces.router.impl.model.MatchResult2) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 35 with XFuture

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

the class TestHttp1Backpressure method testBasicBackpressureChunked.

@Test
public void testBasicBackpressureChunked() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    Http2Response response1 = Requests.createChunkedResponse(1);
    HttpChunk response2 = Requests.createHttpChunk(250);
    HttpLastChunk lastChunk = new HttpLastChunk();
    List<ByteBuffer> buffers = create4BuffersWith3Messags(response1, response2, lastChunk);
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // resolved since it never made it to the client.
    Assert.assertTrue(fut1.isDone());
    // this keeps bytes in-memory that is less than bufPool.getSuggestedSize. ie. < 5k
    XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
    listener.addProcessResponse(requestFuture);
    XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
    // not resolved yet since client only has part of the data
    Assert.assertFalse(fut2.isDone());
    XFuture<Void> streamWriterFuture = new XFuture<Void>();
    MockStreamWriter mockWriter = new MockStreamWriter();
    mockWriter.addProcessResponse(streamWriterFuture);
    // This releases the response BUT 'some' data was with response so can't ack yet
    requestFuture.complete(mockWriter);
    // not resolved yet since requestFuture AND streamWriterFutuer BOTH need to be resolved
    Assert.assertFalse(fut2.isDone());
    // This is BECAUSE 68 bytes resulted in TWO messages and both futures can ack the 68 bytes.
    streamWriterFuture.complete(null);
    // NOW it's resolved
    fut2.get(2, TimeUnit.SECONDS);
    // feed the rest of first chunk in and feed part of last chunk
    XFuture<Void> firstChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(firstChunkAck);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    // ack the http chunk packet
    firstChunkAck.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
    XFuture<Void> lastChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(lastChunkAck);
    XFuture<Void> fut4 = dataListener.incomingData(mockChannel, buffers.get(3));
    Assert.assertFalse(fut4.isDone());
    lastChunkAck.complete(null);
    fut4.get(2, TimeUnit.SECONDS);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) ByteBuffer(java.nio.ByteBuffer) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

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