Search in sources :

Example 1 with IncomingFrames

use of org.eclipse.jetty.websocket.api.extensions.IncomingFrames in project jetty.project by eclipse.

the class DeflateFrameExtensionTest method testCompressAndDecompressBigPayload.

@Test
public void testCompressAndDecompressBigPayload() throws Exception {
    byte[] input = new byte[1024 * 1024];
    // Make them not compressible.
    new Random().nextBytes(input);
    int maxMessageSize = (1024 * 1024) + 8192;
    DeflateFrameExtension clientExtension = new DeflateFrameExtension();
    clientExtension.setBufferPool(bufferPool);
    clientExtension.setPolicy(WebSocketPolicy.newClientPolicy());
    clientExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    clientExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    clientExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    final DeflateFrameExtension serverExtension = new DeflateFrameExtension();
    serverExtension.setBufferPool(bufferPool);
    serverExtension.setPolicy(WebSocketPolicy.newServerPolicy());
    serverExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    serverExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    serverExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    // Chain the next element to decompress.
    clientExtension.setNextOutgoingFrames(new OutgoingFrames() {

        @Override
        public void outgoingFrame(Frame frame, WriteCallback callback, BatchMode batchMode) {
            LOG.debug("outgoingFrame({})", frame);
            serverExtension.incomingFrame(frame);
            callback.writeSuccess();
        }
    });
    final ByteArrayOutputStream result = new ByteArrayOutputStream(input.length);
    serverExtension.setNextIncomingFrames(new IncomingFrames() {

        @Override
        public void incomingFrame(Frame frame) {
            LOG.debug("incomingFrame({})", frame);
            try {
                result.write(BufferUtil.toArray(frame.getPayload()));
            } catch (IOException x) {
                throw new RuntimeIOException(x);
            }
        }

        @Override
        public void incomingError(Throwable t) {
        }
    });
    BinaryFrame frame = new BinaryFrame();
    frame.setPayload(input);
    frame.setFin(true);
    clientExtension.outgoingFrame(frame, null, BatchMode.OFF);
    Assert.assertArrayEquals(input, result.toByteArray());
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) IncomingFrames(org.eclipse.jetty.websocket.api.extensions.IncomingFrames) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) BatchMode(org.eclipse.jetty.websocket.api.BatchMode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) Random(java.util.Random) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames) AbstractExtensionTest(org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest) Test(org.junit.Test)

Example 2 with IncomingFrames

use of org.eclipse.jetty.websocket.api.extensions.IncomingFrames in project jetty.project by eclipse.

the class ExtensionStack method getLastIncoming.

private IncomingFrames getLastIncoming() {
    IncomingFrames last = nextIncoming;
    boolean done = false;
    while (!done) {
        if (last instanceof AbstractExtension) {
            last = ((AbstractExtension) last).getNextIncoming();
        } else {
            done = true;
        }
    }
    return last;
}
Also used : IncomingFrames(org.eclipse.jetty.websocket.api.extensions.IncomingFrames)

Example 3 with IncomingFrames

use of org.eclipse.jetty.websocket.api.extensions.IncomingFrames in project jetty.project by eclipse.

the class ExtensionStack method dump.

@Override
public void dump(Appendable out, String indent) throws IOException {
    super.dump(out, indent);
    IncomingFrames websocket = getLastIncoming();
    OutgoingFrames network = getLastOutgoing();
    out.append(indent).append(" +- Stack").append(System.lineSeparator());
    out.append(indent).append("     +- Network  : ").append(network.toString()).append(System.lineSeparator());
    for (Extension ext : extensions) {
        out.append(indent).append("     +- Extension: ").append(ext.toString()).append(System.lineSeparator());
    }
    out.append(indent).append("     +- Websocket: ").append(websocket.toString()).append(System.lineSeparator());
}
Also used : Extension(org.eclipse.jetty.websocket.api.extensions.Extension) IncomingFrames(org.eclipse.jetty.websocket.api.extensions.IncomingFrames) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames)

Aggregations

IncomingFrames (org.eclipse.jetty.websocket.api.extensions.IncomingFrames)3 OutgoingFrames (org.eclipse.jetty.websocket.api.extensions.OutgoingFrames)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Random (java.util.Random)1 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)1 BatchMode (org.eclipse.jetty.websocket.api.BatchMode)1 WriteCallback (org.eclipse.jetty.websocket.api.WriteCallback)1 Extension (org.eclipse.jetty.websocket.api.extensions.Extension)1 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)1 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)1 AbstractExtensionTest (org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest)1 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)1 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)1 Test (org.junit.Test)1