Search in sources :

Example 1 with StreamMsg

use of com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg in project webpieces by deanhiller.

the class TestStreamingRaw method testAsyncJsonGet.

@Test
public void testAsyncJsonGet() throws InterruptedException, ExecutionException {
    MockChannel channel = new MockChannel();
    XFuture<DataListener> future = mgr.simulateHttpConnect(channel);
    DataListener dataListener = future.get();
    HttpFullRequest fullRequest = Requests.createJsonRequest(KnownHttpMethod.GET, "/json/streaming");
    ByteBuffer buffer = parser.marshalToByteBuffer(fullRequest.getRequest());
    DataWrapper data = fullRequest.getData();
    byte[] headers = new byte[buffer.remaining()];
    buffer.get(headers);
    data.getReadableSize();
    byte[] part1 = data.readBytesAt(0, 10);
    String str = new String(part1);
    byte[] part2 = data.readBytesAt(10, data.getReadableSize() - 10);
    String str2 = new String(part2);
    ByteBuffer firstPacket = ByteBuffer.allocate(headers.length + part1.length);
    firstPacket.put(headers);
    firstPacket.put(part1);
    firstPacket.flip();
    XFuture<Boolean> authFuture = new XFuture<Boolean>();
    mockAuth.addValueToReturn(authFuture);
    Assert.assertEquals(0, Context.getContext().size());
    // Feed in request with content-length AND part of the body as well...
    XFuture<Void> fut1 = dataListener.incomingData(channel, firstPacket);
    Assert.assertEquals(0, Context.getContext().size());
    // Feed in more BEFORE authFuture is complete(this was the bug, ie. race condition)
    ByteBuffer buf2 = ByteBuffer.allocate(part2.length);
    buf2.put(part2);
    buf2.flip();
    XFuture<Void> fut2 = dataListener.incomingData(channel, buf2);
    Assert.assertEquals(0, Context.getContext().size());
    XFuture<StreamWriter> streamWriterFuture = new XFuture<StreamWriter>();
    mockStreamClient.addStreamWriter(streamWriterFuture);
    // complete it
    authFuture.complete(true);
    Assert.assertFalse(fut1.isDone());
    Assert.assertFalse(fut2.isDone());
    MockStreamWriter2 mockStreamWriter = new MockStreamWriter2();
    streamWriterFuture.complete(mockStreamWriter);
    Assert.assertEquals(0, Context.getContext().size());
    Assert.assertTrue(fut1.isDone());
    Assert.assertTrue(fut2.isDone());
    List<StreamMsg> frames = mockStreamWriter.getFrames();
    Assert.assertEquals(2, frames.size());
    DataFrame f1 = (DataFrame) frames.get(0);
    DataFrame f2 = (DataFrame) frames.get(1);
    String s1 = f1.getData().createStringFromUtf8(0, f1.getData().getReadableSize());
    Assert.assertEquals(str, s1);
    String s2 = f2.getData().createStringFromUtf8(0, f2.getData().getReadableSize());
    Assert.assertEquals(str2, s2);
    Assert.assertEquals(0, Context.getContext().size());
}
Also used : StreamMsg(com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg) MockChannel(org.webpieces.webserver.test.sockets.MockChannel) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) ByteBuffer(java.nio.ByteBuffer) DataWrapper(org.webpieces.data.api.DataWrapper) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) DataListener(org.webpieces.nio.api.handlers.DataListener) AbstractWebpiecesTest(org.webpieces.webserver.test.sockets.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 2 with StreamMsg

use of com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg in project webpieces by deanhiller.

the class Level8NotifyClntListeners method sendPieceToApp.

@Override
public XFuture<Void> sendPieceToApp(Stream stream, StreamMsg payload) {
    ClientStream str = (ClientStream) stream;
    StreamWriter writer = str.getResponseWriter();
    return writer.processPiece(payload).thenApply(s -> null);
}
Also used : StreamWriter(com.webpieces.http2.api.streaming.StreamWriter)

Example 3 with StreamMsg

use of com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg in project webpieces by deanhiller.

the class Level6LocalFlowControl method fireDataToClient.

public XFuture<Void> fireDataToClient(Stream stream, StreamMsg payload) {
    if (!(payload instanceof DataFrame))
        return notifyListener.sendPieceToApp(stream, payload);
    DataFrame f = (DataFrame) payload;
    long frameLength = f.getTransmitFrameLength();
    if (frameLength > connectionLocalWindowSize) {
        throw new ConnectionException(CancelReasonCode.FLOW_CONTROL_ERROR, logId, f.getStreamId(), "connectionLocalWindowSize too small=" + connectionLocalWindowSize + " frame len=" + frameLength + " for frame=" + f);
    } else if (frameLength > stream.getLocalWindowSize()) {
        throw new StreamException(CancelReasonCode.FLOW_CONTROL_ERROR, logId, f.getStreamId(), "connectionLocalWindowSize too small=" + connectionLocalWindowSize + " frame len=" + frameLength + " for frame=" + f);
    }
    totalSent += frameLength;
    connectionLocalWindowSize -= frameLength;
    stream.incrementLocalWindow(-frameLength);
    log.info("received framelen=" + frameLength + " newConnectionWindowSize=" + connectionLocalWindowSize + " streamSize=" + stream.getLocalWindowSize() + " totalSent=" + totalSent);
    return notifyListener.sendPieceToApp(stream, payload).thenApply(c -> updateFlowControl(frameLength, stream));
}
Also used : DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) ConnectionException(com.webpieces.http2.api.dto.error.ConnectionException) StreamException(com.webpieces.http2.api.dto.error.StreamException)

Example 4 with StreamMsg

use of com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg in project webpieces by deanhiller.

the class NullWriter method processPiece.

@Override
public XFuture<Void> processPiece(StreamMsg data) {
    DataFrame f = (DataFrame) data;
    DataWrapper body = f.getData();
    String str = body.createStringFromUtf8(0, body.getReadableSize());
    log.error("Should not be receiving data(len=" + body.getReadableSize() + ").  data Received=" + str, new RuntimeException("Received data here and should not").fillInStackTrace());
    return XFuture.completedFuture(null);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame)

Example 5 with StreamMsg

use of com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg in project webpieces by deanhiller.

the class RequestStreamWriter2 method processPiece.

@Override
public XFuture<Void> processPiece(StreamMsg frame) {
    if (cancelled) {
        return XFuture.completedFuture(null);
    } else if (frame instanceof CancelReason) {
        cancelled = true;
        responseFuture.cancel(true);
    } else if (frame instanceof DataFrame) {
        DataFrame dataFrame = (DataFrame) frame;
        data = dataGen.chainDataWrappers(data, dataFrame.getData());
    } else if (frame instanceof Http2Headers) {
        if (!frame.isEndOfStream())
            throw new IllegalArgumentException("Trailing headers from client must have end of stream set");
        trailingHeaders = (Http2Headers) frame;
    } else {
        throw new IllegalStateException("frame not expected=" + frame);
    }
    if (frame.isEndOfStream()) {
        return handleCompleteRequestImpl();
    }
    // return immediately resolved as we need more data to form request
    return XFuture.completedFuture(null);
}
Also used : CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Http2Headers(com.webpieces.http2.api.dto.highlevel.Http2Headers) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame)

Aggregations

DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)5 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)3 Http2Headers (com.webpieces.http2.api.dto.highlevel.Http2Headers)2 StreamMsg (com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg)2 Test (org.junit.Test)2 DataWrapper (org.webpieces.data.api.DataWrapper)2 ConnectionException (com.webpieces.http2.api.dto.error.ConnectionException)1 StreamException (com.webpieces.http2.api.dto.error.StreamException)1 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)1 ByteBuffer (java.nio.ByteBuffer)1 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)1 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)1 RequestHolder (org.webpieces.http2client.util.RequestHolder)1 RequestsSent (org.webpieces.http2client.util.RequestsSent)1 HttpFullRequest (org.webpieces.httpclient11.api.HttpFullRequest)1 DataListener (org.webpieces.nio.api.handlers.DataListener)1 XFuture (org.webpieces.util.futures.XFuture)1 PrivateWebserverForTest (org.webpieces.webserver.PrivateWebserverForTest)1 AbstractWebpiecesTest (org.webpieces.webserver.test.sockets.AbstractWebpiecesTest)1 MockChannel (org.webpieces.webserver.test.sockets.MockChannel)1