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());
}
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);
}
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));
}
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);
}
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);
}
Aggregations