Search in sources :

Example 16 with FutureResult

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

the class AnnotatedEndpointTest method testStringOnMessage.

@Test
public void testStringOnMessage() 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/chat/Stuart"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "hello Stuart".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 17 with FutureResult

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

the class AnnotatedEndpointTest method testEncodingWithGenericSuperclass.

@Test
public void testEncodingWithGenericSuperclass() 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/encodingGenerics/Stuart"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "hello Stuart".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 18 with FutureResult

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

the class AnnotatedEndpointTest method testRequestUri.

@Test
public void testRequestUri() 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/request?a=b"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "/ws/request?a=b".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 19 with FutureResult

use of org.xnio.FutureResult in project baseio by generallycloud.

the class NioTcpChannelTestCase method main.

public static void main(String[] args) throws Exception {
    log.info("Test: acceptor");
    final CountDownLatch ioLatch = new CountDownLatch(4);
    final CountDownLatch closeLatch = new CountDownLatch(2);
    final AtomicBoolean clientOpened = new AtomicBoolean();
    final AtomicBoolean clientReadOnceOK = new AtomicBoolean();
    final AtomicBoolean clientReadDoneOK = new AtomicBoolean();
    final AtomicBoolean clientReadTooMuch = new AtomicBoolean();
    final AtomicBoolean clientWriteOK = new AtomicBoolean();
    final AtomicBoolean serverOpened = new AtomicBoolean();
    final AtomicBoolean serverReadOnceOK = new AtomicBoolean();
    final AtomicBoolean serverReadDoneOK = new AtomicBoolean();
    final AtomicBoolean serverReadTooMuch = new AtomicBoolean();
    final AtomicBoolean serverWriteOK = new AtomicBoolean();
    final byte[] bytes = "Ummagumma!".getBytes("UTF-8");
    final Xnio xnio = Xnio.getInstance("nio");
    final XnioWorker worker = xnio.createWorker(OptionMap.create(Options.WORKER_WRITE_THREADS, 2, Options.WORKER_READ_THREADS, 2));
    try {
        final FutureResult<InetSocketAddress> futureAddressResult = new FutureResult<InetSocketAddress>();
        final IoFuture<InetSocketAddress> futureAddress = futureAddressResult.getIoFuture();
        worker.acceptStream(new InetSocketAddress(Inet4Address.getByAddress(new byte[] { 127, 0, 0, 1 }), 0), new ChannelListener<ConnectedStreamChannel>() {

            private final ByteBuffer inboundBuf = ByteBuffer.allocate(512);

            private int readCnt = 0;

            private final ByteBuffer outboundBuf = ByteBuffer.wrap(bytes);

            public void handleEvent(final ConnectedStreamChannel channel) {
                channel.getCloseSetter().set(new ChannelListener<ConnectedStreamChannel>() {

                    public void handleEvent(final ConnectedStreamChannel channel) {
                        closeLatch.countDown();
                    }
                });
                channel.getReadSetter().set(new ChannelListener<ConnectedStreamChannel>() {

                    public void handleEvent(final ConnectedStreamChannel channel) {
                        try {
                            final int res = channel.read(inboundBuf);
                            if (res == -1) {
                                serverReadDoneOK.set(true);
                                ioLatch.countDown();
                                channel.shutdownReads();
                            } else if (res > 0) {
                                final int ttl = readCnt += res;
                                if (ttl == bytes.length) {
                                    serverReadOnceOK.set(true);
                                } else if (ttl > bytes.length) {
                                    serverReadTooMuch.set(true);
                                    IoUtils.safeClose(channel);
                                    return;
                                }
                            }
                        } catch (IOException e) {
                            log.errorf(e, "Server read failed");
                            IoUtils.safeClose(channel);
                        }
                    }
                });
                channel.getWriteSetter().set(new ChannelListener<ConnectedStreamChannel>() {

                    public void handleEvent(final ConnectedStreamChannel channel) {
                        try {
                            channel.write(outboundBuf);
                            if (!outboundBuf.hasRemaining()) {
                                serverWriteOK.set(true);
                                Channels.shutdownWritesBlocking(channel);
                                ioLatch.countDown();
                            }
                        } catch (IOException e) {
                            log.errorf(e, "Server write failed");
                            IoUtils.safeClose(channel);
                        }
                    }
                });
                channel.resumeReads();
                channel.resumeWrites();
                serverOpened.set(true);
            }
        }, new ChannelListener<BoundChannel>() {

            public void handleEvent(final BoundChannel channel) {
                futureAddressResult.setResult(channel.getLocalAddress(InetSocketAddress.class));
            }
        }, OptionMap.create(Options.REUSE_ADDRESSES, Boolean.TRUE));
        final InetSocketAddress localAddress = futureAddress.get();
        worker.connectStream(localAddress, new ChannelListener<ConnectedStreamChannel>() {

            private final ByteBuffer inboundBuf = ByteBuffer.allocate(512);

            private int readCnt = 0;

            private final ByteBuffer outboundBuf = ByteBuffer.wrap(bytes);

            public void handleEvent(final ConnectedStreamChannel channel) {
                channel.getCloseSetter().set(new ChannelListener<ConnectedStreamChannel>() {

                    public void handleEvent(final ConnectedStreamChannel channel) {
                        closeLatch.countDown();
                    }
                });
                channel.getReadSetter().set(new ChannelListener<ConnectedStreamChannel>() {

                    public void handleEvent(final ConnectedStreamChannel channel) {
                        try {
                            final int res = channel.read(inboundBuf);
                            if (res == -1) {
                                channel.shutdownReads();
                                clientReadDoneOK.set(true);
                                ioLatch.countDown();
                            } else if (res > 0) {
                                final int ttl = readCnt += res;
                                if (ttl == bytes.length) {
                                    clientReadOnceOK.set(true);
                                } else if (ttl > bytes.length) {
                                    clientReadTooMuch.set(true);
                                    IoUtils.safeClose(channel);
                                    return;
                                }
                            }
                        } catch (IOException e) {
                            log.errorf(e, "Client read failed");
                            IoUtils.safeClose(channel);
                        }
                    }
                });
                channel.getWriteSetter().set(new ChannelListener<ConnectedStreamChannel>() {

                    public void handleEvent(final ConnectedStreamChannel channel) {
                        try {
                            channel.write(outboundBuf);
                            if (!outboundBuf.hasRemaining()) {
                                clientWriteOK.set(true);
                                Channels.shutdownWritesBlocking(channel);
                                ioLatch.countDown();
                            }
                        } catch (IOException e) {
                            log.errorf(e, "Client write failed");
                            IoUtils.safeClose(channel);
                        }
                    }
                });
                channel.resumeReads();
                channel.resumeWrites();
                clientOpened.set(true);
            }
        }, null, OptionMap.EMPTY);
    // assertTrue("Read timed out", ioLatch.await(500L, TimeUnit.MILLISECONDS));
    // assertTrue("Close timed out", closeLatch.await(500L, TimeUnit.MILLISECONDS));
    // assertFalse("Client read too much", clientReadTooMuch.get());
    // assertTrue("Client read OK", clientReadOnceOK.get());
    // assertTrue("Client read done", clientReadDoneOK.get());
    // assertTrue("Client write OK", clientWriteOK.get());
    // assertFalse("Server read too much", serverReadTooMuch.get());
    // assertTrue("Server read OK", serverReadOnceOK.get());
    // assertTrue("Server read done", serverReadDoneOK.get());
    // assertTrue("Server write OK", serverWriteOK.get());
    } finally {
        worker.shutdown();
    }
}
Also used : ConnectedStreamChannel(org.xnio.channels.ConnectedStreamChannel) ChannelListener(org.xnio.ChannelListener) XnioWorker(org.xnio.XnioWorker) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) BoundChannel(org.xnio.channels.BoundChannel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FutureResult(org.xnio.FutureResult) Xnio(org.xnio.Xnio)

Example 20 with FutureResult

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

the class WebSocketServletTest method testText.

@Test
public void testText() throws Exception {
    final AtomicBoolean connected = new AtomicBoolean(false);
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentUtils.setupServlet(new ServletInfo("websocket", WebSocketServlet.class, new ImmediateInstanceFactory<Servlet>(new WebSocketServlet(new WebSocketConnectionCallback() {

        @Override
        public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
            connected.set(true);
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
                    final String string = message.getData();
                    if (string.equals("hello")) {
                        WebSockets.sendText("world", channel, null);
                    } else {
                        WebSockets.sendText(string, channel, null);
                    }
                }
            });
            channel.resumeReceives();
        }
    }))).addMapping("/*"));
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(io.netty.handler.codec.http.websocketx.WebSocketVersion.V13, new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/servletContext/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.copiedBuffer("hello", US_ASCII)), new FrameChecker(TextWebSocketFrame.class, "world".getBytes(US_ASCII), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) WebSocketServlet(io.undertow.servlet.websockets.WebSocketServlet) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) ServletInfo(io.undertow.servlet.api.ServletInfo) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) ServletContainer(io.undertow.servlet.api.ServletContainer) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) WebSocketServlet(io.undertow.servlet.websockets.WebSocketServlet) Servlet(javax.servlet.Servlet) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Test(org.junit.Test)

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