From 688ee7c1ef8ddb48755216e7848a5b2b014033b4 Mon Sep 17 00:00:00 2001 From: Matt Gill Date: Tue, 5 Feb 2019 13:28:56 +0000 Subject: [PATCH] Moved Http2 OutputQueueRecord into separate class. --- .../grizzly/http2/Http2OutputQueueRecord.java | 96 +++++++++++++++++ .../grizzly/http2/Http2SessionOutputSink.java | 100 +----------------- 2 files changed, 99 insertions(+), 97 deletions(-) create mode 100644 modules/http2/src/main/java/org/glassfish/grizzly/http2/Http2OutputQueueRecord.java diff --git a/modules/http2/src/main/java/org/glassfish/grizzly/http2/Http2OutputQueueRecord.java b/modules/http2/src/main/java/org/glassfish/grizzly/http2/Http2OutputQueueRecord.java new file mode 100644 index 0000000000..b12caf8ed2 --- /dev/null +++ b/modules/http2/src/main/java/org/glassfish/grizzly/http2/Http2OutputQueueRecord.java @@ -0,0 +1,96 @@ +package org.glassfish.grizzly.http2; + +import java.util.List; + +import org.glassfish.grizzly.Buffer; +import org.glassfish.grizzly.CompletionHandler; +import org.glassfish.grizzly.WriteResult; +import org.glassfish.grizzly.asyncqueue.AsyncQueueRecord; +import org.glassfish.grizzly.http2.frames.DataFrame; +import org.glassfish.grizzly.http2.frames.Http2Frame; +import org.glassfish.grizzly.http2.utils.ChunkedCompletionHandler; + +public class Http2OutputQueueRecord extends AsyncQueueRecord { + + private final int streamId; + + private ChunkedCompletionHandler chunkedCompletionHandler; + private final CompletionHandler originalCompletionHandler; + private Buffer buffer; + private final boolean isLast; + + private final boolean isZeroSizeData; + + Http2OutputQueueRecord(final int streamId, final Buffer buffer, + final CompletionHandler completionHandler, final boolean isLast) { + super(null, null, null); + + this.streamId = streamId; + this.buffer = buffer; + this.isZeroSizeData = !buffer.hasRemaining(); + this.originalCompletionHandler = completionHandler; + this.isLast = isLast; + } + + @Override + public void notifyFailure(final Throwable e) { + final CompletionHandler chLocal = getCompletionHandler(); + if (chLocal != null) { + chLocal.failed(e); + } + } + + @Override + public void recycle() { + } + + @Override + public WriteResult getCurrentResult() { + return null; + } + + CompletionHandler getCompletionHandler() { + return chunkedCompletionHandler != null ? chunkedCompletionHandler : originalCompletionHandler; + } + + boolean isZeroSizeData() { + return isZeroSizeData; + } + + boolean isFinished() { + return buffer == null; + } + + int serializeTo(final List frames, final int maxDataSize) { + + final int recordSize = buffer.remaining(); + + if (recordSize <= maxDataSize) { + final DataFrame dataFrame = DataFrame.builder().streamId(streamId).data(buffer).endStream(isLast).build(); + + frames.add(dataFrame); + + buffer = null; + + return recordSize; + } else { + if (originalCompletionHandler != null && chunkedCompletionHandler == null) { + chunkedCompletionHandler = new ChunkedCompletionHandler(originalCompletionHandler); + } + + if (chunkedCompletionHandler != null) { + chunkedCompletionHandler.incChunks(); + } + + final Buffer remainder = buffer.split(buffer.position() + maxDataSize); + + final DataFrame dataFrame = DataFrame.builder().streamId(streamId).data(buffer).endStream(false).build(); + + frames.add(dataFrame); + + buffer = remainder; + + return maxDataSize; + } + } +} \ No newline at end of file diff --git a/modules/http2/src/main/java/org/glassfish/grizzly/http2/Http2SessionOutputSink.java b/modules/http2/src/main/java/org/glassfish/grizzly/http2/Http2SessionOutputSink.java index ebefe28652..4190870717 100644 --- a/modules/http2/src/main/java/org/glassfish/grizzly/http2/Http2SessionOutputSink.java +++ b/modules/http2/src/main/java/org/glassfish/grizzly/http2/Http2SessionOutputSink.java @@ -52,7 +52,7 @@ public class Http2SessionOutputSink { private static final int MAX_OUTPUT_QUEUE_SIZE = 65536; // async output queue - private final TaskQueue outputQueue = + private final TaskQueue outputQueue = TaskQueue.createTaskQueue(new TaskQueue.MutableMaxQueueSize() { @Override @@ -182,7 +182,7 @@ protected void writeDataDownStream(final Http2Stream stream, data = messageCloner.clone(http2Session.getConnection(), data); } - final Http2SessionOutputSink.OutputQueueRecord record = new Http2SessionOutputSink.OutputQueueRecord( + final Http2OutputQueueRecord record = new Http2OutputQueueRecord( stream.getId(), data, completionHandler, isLast); @@ -227,7 +227,7 @@ private void flushOutputQueue() { while (availWindowSize > bytesToTransfer && queueSize > queueSizeToFree) { - final Http2SessionOutputSink.OutputQueueRecord record = outputQueue.poll(); + final Http2OutputQueueRecord record = outputQueue.poll(); if (record == null) { // keep this warning for now @@ -315,98 +315,4 @@ public void close() { outputQueue.onClose(); } - private static class OutputQueueRecord extends AsyncQueueRecord { - private final int streamId; - - private ChunkedCompletionHandler chunkedCompletionHandler; - private final CompletionHandler originalCompletionHandler; - private Buffer buffer; - private final boolean isLast; - - private final boolean isZeroSizeData; - - public OutputQueueRecord(final int streamId, - final Buffer buffer, - final CompletionHandler completionHandler, - final boolean isLast) { - super(null, null, null); - - this.streamId = streamId; - this.buffer = buffer; - this.isZeroSizeData = !buffer.hasRemaining(); - this.originalCompletionHandler = completionHandler; - this.isLast = isLast; - } - - public CompletionHandler getCompletionHandler() { - return chunkedCompletionHandler != null ? - chunkedCompletionHandler : - originalCompletionHandler; - } - - @Override - public void notifyFailure(final Throwable e) { - final CompletionHandler chLocal = getCompletionHandler(); - if (chLocal != null) { - chLocal.failed(e); - } - } - - @Override - public void recycle() { - } - - @Override - public WriteResult getCurrentResult() { - return null; - } - - private boolean isZeroSizeData() { - return isZeroSizeData; - } - - private boolean isFinished() { - return buffer == null; - } - - private int serializeTo(final List frames, - final int maxDataSize) { - - final int recordSize = buffer.remaining(); - - if (recordSize <= maxDataSize) { - final DataFrame dataFrame = DataFrame.builder() - .streamId(streamId) - .data(buffer).endStream(isLast) - .build(); - - frames.add(dataFrame); - - buffer = null; - - return recordSize; - } else { - if (originalCompletionHandler != null && chunkedCompletionHandler == null) { - chunkedCompletionHandler = new ChunkedCompletionHandler(originalCompletionHandler); - } - - if (chunkedCompletionHandler != null) { - chunkedCompletionHandler.incChunks(); - } - - final Buffer remainder = buffer.split(buffer.position() + maxDataSize); - - final DataFrame dataFrame = DataFrame.builder() - .streamId(streamId) - .data(buffer).endStream(false) - .build(); - - frames.add(dataFrame); - - buffer = remainder; - - return maxDataSize; - } - } - } }