Search in sources :

Example 1 with OutgoingFrames

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

the class ExtensionStack method getLastOutgoing.

private OutgoingFrames getLastOutgoing() {
    OutgoingFrames last = nextOutgoing;
    boolean done = false;
    while (!done) {
        if (last instanceof AbstractExtension) {
            last = ((AbstractExtension) last).getNextOutgoing();
        } else {
            done = true;
        }
    }
    return last;
}
Also used : OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames)

Example 2 with OutgoingFrames

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

the class MessageWriterTest method setupSession.

@Before
public void setupSession() throws Exception {
    policy = WebSocketPolicy.newServerPolicy();
    policy.setInputBufferSize(1024);
    policy.setMaxTextMessageBufferSize(1024);
    // Container
    WebSocketContainerScope containerScope = new SimpleContainerScope(policy, bufferPool);
    // Event Driver factory
    EventDriverFactory factory = new EventDriverFactory(containerScope);
    // local socket
    EventDriver driver = factory.wrap(new TrackingSocket("local"));
    // remote socket
    socket = new TrackingSocket("remote");
    OutgoingFrames socketPipe = FramePipes.to(factory.wrap(socket));
    session = new LocalWebSocketSession(containerScope, testname, driver);
    session.setPolicy(policy);
    // talk to our remote socket
    session.setOutgoingHandler(socketPipe);
    // start session
    session.start();
    // open connection
    session.open();
}
Also used : EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) LocalWebSocketSession(org.eclipse.jetty.websocket.common.io.LocalWebSocketSession) EventDriverFactory(org.eclipse.jetty.websocket.common.events.EventDriverFactory) WebSocketContainerScope(org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames) SimpleContainerScope(org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope) Before(org.junit.Before)

Example 3 with OutgoingFrames

use of org.eclipse.jetty.websocket.api.extensions.OutgoingFrames 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 4 with OutgoingFrames

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

the class ExtensionStackProcessingTest method testDeflateFrameExtension.

@Test
public void testDeflateFrameExtension() throws Exception {
    assumeDeflateFrameAvailable();
    ClientEndpointConfig config = ClientEndpointConfig.Builder.create().extensions(Arrays.<Extension>asList(new JsrExtension("deflate-frame"))).build();
    final String content = "deflate_me";
    final CountDownLatch messageLatch = new CountDownLatch(1);
    URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
    Session session = client.connectToServer(new EndpointAdapter() {

        @Override
        public void onMessage(String message) {
            Assert.assertEquals(content, message);
            messageLatch.countDown();
        }
    }, config, uri);
    // Make sure everything is wired properly.
    OutgoingFrames firstOut = ((JsrSession) session).getOutgoingHandler();
    Assert.assertTrue(firstOut instanceof ExtensionStack);
    ExtensionStack extensionStack = (ExtensionStack) firstOut;
    Assert.assertTrue(extensionStack.isRunning());
    OutgoingFrames secondOut = extensionStack.getNextOutgoing();
    Assert.assertTrue(secondOut instanceof DeflateFrameExtension);
    DeflateFrameExtension deflateExtension = (DeflateFrameExtension) secondOut;
    Assert.assertTrue(deflateExtension.isRunning());
    OutgoingFrames thirdOut = deflateExtension.getNextOutgoing();
    Assert.assertTrue(thirdOut instanceof WebSocketClientConnection);
    final CountDownLatch latch = new CountDownLatch(1);
    session.getAsyncRemote().sendText(content, new SendHandler() {

        @Override
        public void onResult(SendResult result) {
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(messageLatch.await(5, TimeUnit.SECONDS));
}
Also used : SendHandler(javax.websocket.SendHandler) WebSocketClientConnection(org.eclipse.jetty.websocket.client.io.WebSocketClientConnection) JsrSession(org.eclipse.jetty.websocket.jsr356.JsrSession) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ExtensionStack(org.eclipse.jetty.websocket.common.extensions.ExtensionStack) JsrExtension(org.eclipse.jetty.websocket.jsr356.JsrExtension) Extension(javax.websocket.Extension) DeflateFrameExtension(org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtension) JsrExtension(org.eclipse.jetty.websocket.jsr356.JsrExtension) SendResult(javax.websocket.SendResult) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) DeflateFrameExtension(org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtension) Session(javax.websocket.Session) JsrSession(org.eclipse.jetty.websocket.jsr356.JsrSession) Test(org.junit.Test)

Example 5 with OutgoingFrames

use of org.eclipse.jetty.websocket.api.extensions.OutgoingFrames 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

OutgoingFrames (org.eclipse.jetty.websocket.api.extensions.OutgoingFrames)6 IncomingFrames (org.eclipse.jetty.websocket.api.extensions.IncomingFrames)2 EventDriver (org.eclipse.jetty.websocket.common.events.EventDriver)2 EventDriverFactory (org.eclipse.jetty.websocket.common.events.EventDriverFactory)2 LocalWebSocketSession (org.eclipse.jetty.websocket.common.io.LocalWebSocketSession)2 SimpleContainerScope (org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope)2 WebSocketContainerScope (org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope)2 Before (org.junit.Before)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)1 Extension (javax.websocket.Extension)1 SendHandler (javax.websocket.SendHandler)1 SendResult (javax.websocket.SendResult)1 Session (javax.websocket.Session)1 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)1