Search in sources :

Example 1 with BoundChannel

use of org.xnio.channels.BoundChannel 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 2 with BoundChannel

use of org.xnio.channels.BoundChannel in project undertow by undertow-io.

the class Http2ClearClientProvider method connect.

@Override
public void connect(final ClientCallback<ClientConnection> listener, final InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    final URI upgradeUri;
    try {
        upgradeUri = new URI("http", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
    } catch (URISyntaxException e) {
        listener.failed(new IOException(e));
        return;
    }
    if (bindAddress != null) {
        ioThread.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort()), new ChannelListener<StreamConnection>() {

            @Override
            public void handleEvent(StreamConnection channel) {
                Map<String, String> headers = createHeaders(options, bufferPool, uri);
                HttpUpgrade.performUpgrade(channel, upgradeUri, headers, new Http2ClearOpenListener(bufferPool, options, listener, uri.getHost()), null).addNotifier(new FailedNotifier(listener), null);
            }
        }, new ChannelListener<BoundChannel>() {

            @Override
            public void handleEvent(BoundChannel channel) {
            }
        }, options).addNotifier(new FailedNotifier(listener), null);
    } else {
        ioThread.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort()), new ChannelListener<StreamConnection>() {

            @Override
            public void handleEvent(StreamConnection channel) {
                Map<String, String> headers = createHeaders(options, bufferPool, uri);
                HttpUpgrade.performUpgrade(channel, upgradeUri, headers, new Http2ClearOpenListener(bufferPool, options, listener, uri.getHost()), null).addNotifier(new FailedNotifier(listener), null);
            }
        }, new ChannelListener<BoundChannel>() {

            @Override
            public void handleEvent(BoundChannel channel) {
            }
        }, options).addNotifier(new FailedNotifier(listener), null);
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) InetSocketAddress(java.net.InetSocketAddress) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StreamConnection(org.xnio.StreamConnection) URI(java.net.URI) BoundChannel(org.xnio.channels.BoundChannel)

Aggregations

IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 ChannelListener (org.xnio.ChannelListener)2 BoundChannel (org.xnio.channels.BoundChannel)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 FutureResult (org.xnio.FutureResult)1 StreamConnection (org.xnio.StreamConnection)1 Xnio (org.xnio.Xnio)1 XnioWorker (org.xnio.XnioWorker)1 ConnectedStreamChannel (org.xnio.channels.ConnectedStreamChannel)1