Search in sources :

Example 1 with AsyncBinary

use of org.apache.tomcat.websocket.TesterMessageCountClient.AsyncBinary in project tomcat70 by apache.

the class TestWsRemoteEndpoint method doTestWriter.

private void doTestWriter(Class<?> clazz, boolean useWriter) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Session wsSession;
    URI uri = new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC);
    if (Endpoint.class.isAssignableFrom(clazz)) {
        @SuppressWarnings("unchecked") Class<? extends Endpoint> endpointClazz = (Class<? extends Endpoint>) clazz;
        wsSession = wsContainer.connectToServer(endpointClazz, Builder.create().build(), uri);
    } else {
        wsSession = wsContainer.connectToServer(clazz, uri);
    }
    CountDownLatch latch = new CountDownLatch(1);
    TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint");
    tep.setLatch(latch);
    AsyncHandler<?> handler;
    if (useWriter) {
        handler = new AsyncText(latch);
    } else {
        handler = new AsyncBinary(latch);
    }
    wsSession.addMessageHandler(handler);
    if (useWriter) {
        Writer w = wsSession.getBasicRemote().getSendWriter();
        for (int i = 0; i < 8; i++) {
            w.write(TEST_MESSAGE_5K);
        }
        w.close();
    } else {
        OutputStream s = wsSession.getBasicRemote().getSendStream();
        for (int i = 0; i < 8; i++) {
            s.write(TEST_MESSAGE_5K.getBytes(B2CConverter.UTF_8));
        }
        s.close();
    }
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    List<String> results = new ArrayList<String>();
    if (useWriter) {
        @SuppressWarnings("unchecked") List<String> messages = (List<String>) handler.getMessages();
        results.addAll(messages);
    } else {
        // Take advantage of the fact that the message uses characters that
        // are represented as a single UTF-8 byte so won't be split across
        // binary messages
        @SuppressWarnings("unchecked") List<ByteBuffer> messages = (List<ByteBuffer>) handler.getMessages();
        for (ByteBuffer message : messages) {
            byte[] bytes = new byte[message.limit()];
            message.get(bytes);
            results.add(new String(bytes, B2CConverter.UTF_8));
        }
    }
    int offset = 0;
    int i = 0;
    for (String result : results) {
        // First may be a fragment
        Assert.assertEquals(SEQUENCE.substring(offset, S_LEN), result.substring(0, S_LEN - offset));
        i = S_LEN - offset;
        while (i + S_LEN < result.length()) {
            if (!SEQUENCE.equals(result.substring(i, i + S_LEN))) {
                Assert.fail();
            }
            i += S_LEN;
        }
        offset = result.length() - i;
        if (!SEQUENCE.substring(0, offset).equals(result.substring(i))) {
            Assert.fail();
        }
    }
}
Also used : AsyncText(org.apache.tomcat.websocket.TesterMessageCountClient.AsyncText) Tomcat(org.apache.catalina.startup.Tomcat) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) TesterAnnotatedEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterAnnotatedEndpoint) Endpoint(javax.websocket.Endpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) ArrayList(java.util.ArrayList) List(java.util.List) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Context(org.apache.catalina.Context) WebSocketContainer(javax.websocket.WebSocketContainer) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) TesterAnnotatedEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterAnnotatedEndpoint) Endpoint(javax.websocket.Endpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) AsyncBinary(org.apache.tomcat.websocket.TesterMessageCountClient.AsyncBinary) Writer(java.io.Writer) Session(javax.websocket.Session)

Example 2 with AsyncBinary

use of org.apache.tomcat.websocket.TesterMessageCountClient.AsyncBinary in project tomcat by apache.

the class TestWsRemoteEndpoint method doTestWriter.

private void doTestWriter(Class<?> clazz, boolean useWriter, String testMessage) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Session wsSession;
    URI uri = new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC);
    if (Endpoint.class.isAssignableFrom(clazz)) {
        @SuppressWarnings("unchecked") Class<? extends Endpoint> endpointClazz = (Class<? extends Endpoint>) clazz;
        wsSession = wsContainer.connectToServer(endpointClazz, Builder.create().build(), uri);
    } else {
        wsSession = wsContainer.connectToServer(clazz, uri);
    }
    CountDownLatch latch = new CountDownLatch(1);
    TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint");
    tep.setLatch(latch);
    AsyncHandler<?> handler;
    if (useWriter) {
        handler = new AsyncText(latch);
    } else {
        handler = new AsyncBinary(latch);
    }
    wsSession.addMessageHandler(handler);
    if (useWriter) {
        Writer w = wsSession.getBasicRemote().getSendWriter();
        for (int i = 0; i < 8; i++) {
            w.write(testMessage);
        }
        w.close();
    } else {
        OutputStream s = wsSession.getBasicRemote().getSendStream();
        for (int i = 0; i < 8; i++) {
            s.write(testMessage.getBytes(StandardCharsets.UTF_8));
        }
        s.close();
    }
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    List<String> results = new ArrayList<>();
    if (useWriter) {
        @SuppressWarnings("unchecked") List<String> messages = (List<String>) handler.getMessages();
        results.addAll(messages);
    } else {
        // Take advantage of the fact that the message uses characters that
        // are represented as a single UTF-8 byte so won't be split across
        // binary messages
        @SuppressWarnings("unchecked") List<ByteBuffer> messages = (List<ByteBuffer>) handler.getMessages();
        for (ByteBuffer message : messages) {
            byte[] bytes = new byte[message.limit()];
            message.get(bytes);
            results.add(new String(bytes, StandardCharsets.UTF_8));
        }
    }
    int offset = 0;
    int i = 0;
    for (String result : results) {
        if (testMessage.length() == 0) {
            Assert.assertEquals(0, result.length());
        } else {
            // First may be a fragment
            Assert.assertEquals(SEQUENCE.substring(offset, S_LEN), result.substring(0, S_LEN - offset));
            i = S_LEN - offset;
            while (i + S_LEN < result.length()) {
                if (!SEQUENCE.equals(result.substring(i, i + S_LEN))) {
                    Assert.fail();
                }
                i += S_LEN;
            }
            offset = result.length() - i;
            if (!SEQUENCE.substring(0, offset).equals(result.substring(i))) {
                Assert.fail();
            }
        }
    }
}
Also used : AsyncText(org.apache.tomcat.websocket.TesterMessageCountClient.AsyncText) Tomcat(org.apache.catalina.startup.Tomcat) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) TesterAnnotatedEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterAnnotatedEndpoint) Endpoint(jakarta.websocket.Endpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) ArrayList(java.util.ArrayList) List(java.util.List) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Context(org.apache.catalina.Context) WebSocketContainer(jakarta.websocket.WebSocketContainer) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) TesterAnnotatedEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterAnnotatedEndpoint) Endpoint(jakarta.websocket.Endpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) AsyncBinary(org.apache.tomcat.websocket.TesterMessageCountClient.AsyncBinary) Writer(java.io.Writer) Session(jakarta.websocket.Session)

Aggregations

OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 URI (java.net.URI)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Context (org.apache.catalina.Context)2 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)2 Tomcat (org.apache.catalina.startup.Tomcat)2 AsyncBinary (org.apache.tomcat.websocket.TesterMessageCountClient.AsyncBinary)2 AsyncText (org.apache.tomcat.websocket.TesterMessageCountClient.AsyncText)2 TesterAnnotatedEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterAnnotatedEndpoint)2 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)2 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)2 Endpoint (jakarta.websocket.Endpoint)1 Session (jakarta.websocket.Session)1 WebSocketContainer (jakarta.websocket.WebSocketContainer)1 Endpoint (javax.websocket.Endpoint)1 Session (javax.websocket.Session)1