Search in sources :

Example 36 with Endpoint

use of javax.websocket.Endpoint in project undertow by undertow-io.

the class WebsocketStressTestCase method webSocketStringStressTestCase.

@Test
public void webSocketStringStressTestCase() throws Exception {
    List<CountDownLatch> latches = new ArrayList<>();
    for (int i = 0; i < NUM_THREADS; ++i) {
        final CountDownLatch latch = new CountDownLatch(1);
        latches.add(latch);
        final Session session = deployment.connectToServer(new Endpoint() {

            @Override
            public void onOpen(Session session, EndpointConfig config) {
            }

            @Override
            public void onClose(Session session, CloseReason closeReason) {
                latch.countDown();
            }

            @Override
            public void onError(Session session, Throwable thr) {
                latch.countDown();
            }
        }, null, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/stress"));
        final int thread = i;
        executor.submit(() -> {
            try {
                executor.submit(new SendRunnable(session, thread, executor));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
    for (CountDownLatch future : latches) {
        assertTrue(future.await(40, TimeUnit.SECONDS));
    }
    for (int t = 0; t < NUM_THREADS; ++t) {
        for (int i = 0; i < NUM_REQUESTS; ++i) {
            String msg = "t-" + t + "-m-" + i;
            assertTrue(msg, StressEndpoint.MESSAGES.remove(msg));
        }
    }
    assertEquals(0, StressEndpoint.MESSAGES.size());
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Endpoint(javax.websocket.Endpoint) CloseReason(javax.websocket.CloseReason) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 37 with Endpoint

use of javax.websocket.Endpoint in project undertow by undertow-io.

the class WebsocketStressTestCase method websocketFragmentationStressTestCase.

@Test
public void websocketFragmentationStressTestCase() throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final CountDownLatch done = new CountDownLatch(1);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10000; ++i) {
        sb.append("message ");
        sb.append(i);
    }
    String toSend = sb.toString();
    final Session session = defaultContainer.connectToServer(new Endpoint() {

        @Override
        public void onOpen(Session session, EndpointConfig config) {
            session.addMessageHandler(new MessageHandler.Partial<byte[]>() {

                @Override
                public void onMessage(byte[] bytes, boolean b) {
                    try {
                        out.write(bytes);
                    } catch (IOException e) {
                        e.printStackTrace();
                        done.countDown();
                    }
                    if (b) {
                        done.countDown();
                    }
                }
            });
        }

        @Override
        public void onClose(Session session, CloseReason closeReason) {
            done.countDown();
        }

        @Override
        public void onError(Session session, Throwable thr) {
            thr.printStackTrace();
            done.countDown();
        }
    }, null, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/stress"));
    OutputStream stream = session.getBasicRemote().getSendStream();
    for (int i = 0; i < toSend.length(); ++i) {
        stream.write(toSend.charAt(i));
        stream.flush();
    }
    stream.close();
    assertTrue(done.await(40, TimeUnit.SECONDS));
    assertEquals(toSend, new String(out.toByteArray()));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) Endpoint(javax.websocket.Endpoint) CloseReason(javax.websocket.CloseReason) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 38 with Endpoint

use of javax.websocket.Endpoint in project undertow by undertow-io.

the class ServerWebSocketContainer method connectToServer.

public Session connectToServer(final Object annotatedEndpointInstance, WebSocketClient.ConnectionBuilder connectionBuilder) throws DeploymentException, IOException {
    if (closed) {
        throw new ClosedChannelException();
    }
    ConfiguredClientEndpoint config = getClientEndpoint(annotatedEndpointInstance.getClass(), false);
    if (config == null) {
        throw JsrWebSocketMessages.MESSAGES.notAValidClientEndpointType(annotatedEndpointInstance.getClass());
    }
    Endpoint instance = config.getFactory().createInstance(new ImmediateInstanceHandle<>(annotatedEndpointInstance));
    return connectToServerInternal(instance, config, connectionBuilder);
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint)

Example 39 with Endpoint

use of javax.websocket.Endpoint in project undertow by undertow-io.

the class ServerWebSocketContainer method addEndpoint.

@Override
public void addEndpoint(final ServerEndpointConfig endpoint) throws DeploymentException {
    if (deploymentComplete) {
        throw JsrWebSocketMessages.MESSAGES.cannotAddEndpointAfterDeployment();
    }
    JsrWebSocketLogger.ROOT_LOGGER.addingProgramaticEndpoint(endpoint.getEndpointClass(), endpoint.getPath());
    final PathTemplate template = PathTemplate.create(endpoint.getPath());
    if (seenPaths.contains(template)) {
        PathTemplate existing = null;
        for (PathTemplate p : seenPaths) {
            if (p.compareTo(template) == 0) {
                existing = p;
                break;
            }
        }
        throw JsrWebSocketMessages.MESSAGES.multipleEndpointsWithOverlappingPaths(template, existing);
    }
    seenPaths.add(template);
    EncodingFactory encodingFactory = EncodingFactory.createFactory(classIntrospecter, endpoint.getDecoders(), endpoint.getEncoders());
    AnnotatedEndpointFactory annotatedEndpointFactory = null;
    if (!Endpoint.class.isAssignableFrom(endpoint.getEndpointClass())) {
        // We may want to check that the path in @ServerEndpoint matches the specified path, and throw if they are not equivalent
        annotatedEndpointFactory = AnnotatedEndpointFactory.create(endpoint.getEndpointClass(), encodingFactory, template.getParameterNames());
    }
    ConfiguredServerEndpoint confguredServerEndpoint = new ConfiguredServerEndpoint(endpoint, null, template, encodingFactory, annotatedEndpointFactory, endpoint.getExtensions());
    configuredServerEndpoints.add(confguredServerEndpoint);
    handleAddingFilterMapping();
}
Also used : AnnotatedEndpointFactory(io.undertow.websockets.jsr.annotated.AnnotatedEndpointFactory) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint) PathTemplate(io.undertow.util.PathTemplate)

Example 40 with Endpoint

use of javax.websocket.Endpoint in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testBinaryWithByteBuffer.

@org.junit.Test
public void testBinaryWithByteBuffer() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Throwable> cause = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult<?> latch = new FutureResult<>();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {

                @Override
                public void onMessage(ByteBuffer message) {
                    ByteBuffer buf = ByteBuffer.allocate(message.remaining());
                    buf.put(message);
                    buf.flip();
                    session.getAsyncRemote().sendBinary(buf);
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
    // FIXME UNDERTOW-1862 assertEquals(DONE, latch.getIoFuture().await());
    latch.getIoFuture().await();
    assertNull(cause.get());
    client.destroy();
}
Also used : MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession)

Aggregations

Endpoint (javax.websocket.Endpoint)46 Session (javax.websocket.Session)29 URI (java.net.URI)24 EndpointConfig (javax.websocket.EndpointConfig)24 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)20 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)14 UndertowSession (io.undertow.websockets.jsr.UndertowSession)14 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)14 FrameChecker (io.undertow.websockets.utils.FrameChecker)14 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 FutureResult (org.xnio.FutureResult)14 IOException (java.io.IOException)13 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)12 MessageHandler (javax.websocket.MessageHandler)12 ByteBuffer (java.nio.ByteBuffer)11 ServerEndpoint (javax.websocket.server.ServerEndpoint)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 ClientEndpoint (javax.websocket.ClientEndpoint)10