Search in sources :

Example 6 with Generator

use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.

the class DeflateFrameExtensionTest method assertOutgoing.

private void assertOutgoing(String text, String expectedHex) throws IOException {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    DeflateFrameExtension ext = new DeflateFrameExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(policy);
    ExtensionConfig config = ExtensionConfig.parse("deflate-frame");
    ext.setConfig(config);
    Generator generator = new Generator(policy, bufferPool, true);
    generator.configureFromExtensions(Collections.singletonList(ext));
    OutgoingNetworkBytesCapture capture = new OutgoingNetworkBytesCapture(generator);
    ext.setNextOutgoingFrames(capture);
    Frame frame = new TextFrame().setPayload(text);
    ext.outgoingFrame(frame, null, BatchMode.OFF);
    capture.assertBytes(0, expectedHex);
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) 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) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) OutgoingNetworkBytesCapture(org.eclipse.jetty.websocket.common.test.OutgoingNetworkBytesCapture) Generator(org.eclipse.jetty.websocket.common.Generator)

Example 7 with Generator

use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.

the class UnitGenerator method generate.

/**
     * Generate a single giant buffer of all provided frames Not appropriate for production code, but useful for testing.
     * @param frames the list of frames to generate from
     * @return the bytebuffer representing all of the generated frames
     */
public static ByteBuffer generate(List<WebSocketFrame> frames) {
    // Create non-symmetrical mask (helps show mask bytes order issues)
    byte[] MASK = { 0x11, 0x22, 0x33, 0x44 };
    // the generator
    Generator generator = new UnitGenerator();
    // Generate into single bytebuffer
    int buflen = 0;
    for (Frame f : frames) {
        buflen += f.getPayloadLength() + Generator.MAX_HEADER_LENGTH;
    }
    ByteBuffer completeBuf = ByteBuffer.allocate(buflen);
    BufferUtil.clearToFill(completeBuf);
    // Generate frames
    for (WebSocketFrame f : frames) {
        // make sure we have the test mask set
        f.setMask(MASK);
        BufferUtil.put(generator.generateHeaderBytes(f), completeBuf);
        ByteBuffer window = f.getPayload();
        if (BufferUtil.hasContent(window)) {
            BufferUtil.put(window, completeBuf);
        }
    }
    BufferUtil.flipToFlush(completeBuf, 0);
    if (LOG.isDebugEnabled()) {
        LOG.debug("generate({} frames) - {}", frames.size(), BufferUtil.toDetailString(completeBuf));
    }
    return completeBuf;
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Generator(org.eclipse.jetty.websocket.common.Generator)

Example 8 with Generator

use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.

the class UnitGenerator method generate.

/**
     * Generate All Frames into a single ByteBuffer.
     * <p>
     * This is highly inefficient and is not used in production! (This exists to make testing of the Generator easier)
     * 
     * @param frames
     *            the frames to generate from
     * @return the ByteBuffer representing all of the generated frames provided.
     */
public static ByteBuffer generate(Frame[] frames) {
    Generator generator = new UnitGenerator();
    // Generate into single bytebuffer
    int buflen = 0;
    for (Frame f : frames) {
        buflen += f.getPayloadLength() + Generator.MAX_HEADER_LENGTH;
    }
    ByteBuffer completeBuf = ByteBuffer.allocate(buflen);
    BufferUtil.clearToFill(completeBuf);
    // Generate frames
    for (Frame f : frames) {
        generator.generateWholeFrame(f, completeBuf);
    }
    BufferUtil.flipToFlush(completeBuf, 0);
    if (LOG.isDebugEnabled()) {
        LOG.debug("generate({} frames) - {}", frames.length, BufferUtil.toDetailString(completeBuf));
    }
    return completeBuf;
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Generator(org.eclipse.jetty.websocket.common.Generator)

Aggregations

Generator (org.eclipse.jetty.websocket.common.Generator)8 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)5 ByteBuffer (java.nio.ByteBuffer)4 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)4 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)3 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)3 Test (org.junit.Test)3 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)2 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)2 OutgoingNetworkBytesCapture (org.eclipse.jetty.websocket.common.test.OutgoingNetworkBytesCapture)2 Ignore (org.junit.Ignore)2 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 LeakTrackingByteBufferPool (org.eclipse.jetty.io.LeakTrackingByteBufferPool)1 AbstractExtensionTest (org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest)1 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)1 LeakTrackingBufferPoolRule (org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule)1 Before (org.junit.Before)1