Search in sources :

Example 26 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class AnnotatedEndpointTest method testImplicitIntegerConversion.

@Test
public void testImplicitIntegerConversion() throws Exception {
    final byte[] payload = "12".getBytes();
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(WebSocketVersion.V13, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/increment/2"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "14".getBytes(), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) URI(java.net.URI) Test(org.junit.Test)

Example 27 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class AnnotatedEndpointTest method testWebSocketInRootContext.

@Test
public void testWebSocketInRootContext() throws Exception {
    final byte[] payload = "hello".getBytes();
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(WebSocketVersion.V13, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "hello".getBytes(), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) URI(java.net.URI) Test(org.junit.Test)

Example 28 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testCloseFrame.

@org.junit.Test
public void testCloseFrame() throws Exception {
    final int code = 1000;
    final String reasonText = "TEST";
    final AtomicReference<CloseReason> reason = new AtomicReference<>();
    ByteBuffer payload = ByteBuffer.allocate(reasonText.length() + 2);
    payload.putShort((short) code);
    payload.put(reasonText.getBytes("UTF-8"));
    payload.flip();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    final CountDownLatch clientLatch = new CountDownLatch(1);
    final AtomicInteger closeCount = new AtomicInteger();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
        }

        @Override
        public void onClose(Session session, CloseReason closeReason) {
            closeCount.incrementAndGet();
            reason.set(closeReason);
            clientLatch.countDown();
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, 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://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new CloseWebSocketFrame(code, reasonText), new FrameChecker(CloseWebSocketFrame.class, payload.array(), latch));
    latch.getIoFuture().get();
    clientLatch.await();
    Assert.assertEquals(code, reason.get().getCloseCode().getCode());
    Assert.assertEquals(reasonText, reason.get().getReasonPhrase());
    Assert.assertEquals(1, closeCount.get());
    client.destroy();
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) 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) CloseReason(javax.websocket.CloseReason) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) Test(org.junit.Test)

Example 29 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testTextByCompletion.

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

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

                @Override
                public void onMessage(String message) {
                    session.getAsyncRemote().sendText(message, new SendHandler() {

                        @Override
                        public void onResult(SendResult result) {
                            sendResult.set(result);
                            if (result.getException() != null) {
                                latch2.setException(new IOException(result.getException()));
                            } else {
                                latch2.setResult(null);
                            }
                        }
                    });
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, 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://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    latch2.getIoFuture().get();
    SendResult result = sendResult.get();
    Assert.assertNotNull(result);
    Assert.assertNull(result.getException());
    client.destroy();
}
Also used : SendHandler(javax.websocket.SendHandler) MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) 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) SendResult(javax.websocket.SendResult) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) 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) Test(org.junit.Test)

Example 30 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class UndertowXnioSsl method connectSsl.

@SuppressWarnings("deprecation")
public IoFuture<ConnectedSslStreamChannel> connectSsl(final XnioWorker worker, final InetSocketAddress bindAddress, final InetSocketAddress destination, final ChannelListener<? super ConnectedSslStreamChannel> openListener, final ChannelListener<? super BoundChannel> bindListener, final OptionMap optionMap) {
    final FutureResult<ConnectedSslStreamChannel> futureResult = new FutureResult<>(IoUtils.directExecutor());
    final IoFuture<SslConnection> futureSslConnection = openSslConnection(worker, bindAddress, destination, new ChannelListener<SslConnection>() {

        public void handleEvent(final SslConnection sslConnection) {
            final ConnectedSslStreamChannel assembledChannel = new AssembledConnectedSslStreamChannel(sslConnection, sslConnection.getSourceChannel(), sslConnection.getSinkChannel());
            if (!futureResult.setResult(assembledChannel)) {
                safeClose(assembledChannel);
            } else {
                ChannelListeners.invokeChannelListener(assembledChannel, openListener);
            }
        }
    }, bindListener, optionMap).addNotifier(new IoFuture.HandlingNotifier<SslConnection, FutureResult<ConnectedSslStreamChannel>>() {

        public void handleCancelled(final FutureResult<ConnectedSslStreamChannel> result) {
            result.setCancelled();
        }

        public void handleFailed(final IOException exception, final FutureResult<ConnectedSslStreamChannel> result) {
            result.setException(exception);
        }
    }, futureResult);
    futureResult.getIoFuture().addNotifier(new IoFuture.HandlingNotifier<ConnectedStreamChannel, IoFuture<SslConnection>>() {

        public void handleCancelled(final IoFuture<SslConnection> result) {
            result.cancel();
        }
    }, futureSslConnection);
    futureResult.addCancelHandler(futureSslConnection);
    return futureResult.getIoFuture();
}
Also used : SslConnection(org.xnio.ssl.SslConnection) ConnectedStreamChannel(org.xnio.channels.ConnectedStreamChannel) ChannelListener(org.xnio.ChannelListener) FutureResult(org.xnio.FutureResult) IoFuture(org.xnio.IoFuture) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel) IOException(java.io.IOException) ConnectedSslStreamChannel(org.xnio.channels.ConnectedSslStreamChannel) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel)

Aggregations

FutureResult (org.xnio.FutureResult)34 URI (java.net.URI)28 Test (org.junit.Test)28 FrameChecker (io.undertow.websockets.utils.FrameChecker)27 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)15 IOException (java.io.IOException)15 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)14 UndertowSession (io.undertow.websockets.jsr.UndertowSession)14 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 Endpoint (javax.websocket.Endpoint)14 EndpointConfig (javax.websocket.EndpointConfig)14 Session (javax.websocket.Session)14 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)14 MessageHandler (javax.websocket.MessageHandler)11 ByteBuffer (java.nio.ByteBuffer)10 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)7 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)5