Search in sources :

Example 21 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class Launcher method main.

public static void main(String[] args) {
    EventLoopGroup group = new NioEventLoopGroup();
    Http2Server http2 = new Http2Server(group);
    HttpServer http = new HttpServer(group);
    try {
        http2.start();
        System.err.println("Open your web browser and navigate to " + "http://" + Html.IP + ":" + HttpServer.PORT);
        http.start().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 22 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class HttpUploadClient method main.

public static void main(String[] args) throws Exception {
    String postSimple, postFile, get;
    if (BASE_URL.endsWith("/")) {
        postSimple = BASE_URL + "formpost";
        postFile = BASE_URL + "formpostmultipart";
        get = BASE_URL + "formget";
    } else {
        postSimple = BASE_URL + "/formpost";
        postFile = BASE_URL + "/formpostmultipart";
        get = BASE_URL + "/formget";
    }
    URI uriSimple = new URI(postSimple);
    String scheme = uriSimple.getScheme() == null ? "http" : uriSimple.getScheme();
    String host = uriSimple.getHost() == null ? "127.0.0.1" : uriSimple.getHost();
    int port = uriSimple.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }
    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;
    }
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }
    URI uriFile = new URI(postFile);
    File file = new File(FILE);
    if (!file.canRead()) {
        throw new FileNotFoundException(FILE);
    }
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    // setup the factory: here using a mixed memory/disk based on size threshold
    // Disk if MINSIZE exceed
    HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
    // should delete file on exit (in normal exit)
    DiskFileUpload.deleteOnExitTemporaryFile = true;
    // system temp directory
    DiskFileUpload.baseDirectory = null;
    // should delete file on exit (in normal exit)
    DiskAttribute.deleteOnExitTemporaryFile = true;
    // system temp directory
    DiskAttribute.baseDirectory = null;
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new HttpUploadClientIntializer(sslCtx));
        // Simple Get form: no factory used (not usable)
        List<Entry<String, String>> headers = formget(b, host, port, get, uriSimple);
        if (headers == null) {
            factory.cleanAllHttpData();
            return;
        }
        // Simple Post form: factory used for big attributes
        List<InterfaceHttpData> bodylist = formpost(b, host, port, uriSimple, file, factory, headers);
        if (bodylist == null) {
            factory.cleanAllHttpData();
            return;
        }
        // Multipart Post form: factory used
        formpostmultipart(b, host, port, uriFile, factory, headers, bodylist);
    } finally {
        // Shut down executor threads to exit.
        group.shutdownGracefully();
        // Really clean all temporary files if they still exist
        factory.cleanAllHttpData();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) URI(java.net.URI) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Entry(java.util.Map.Entry) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) Bootstrap(io.netty.bootstrap.Bootstrap) File(java.io.File) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 23 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class SctpMultiHomingEchoClient method main.

public static void main(String[] args) throws Exception {
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSctpChannel.class).option(SctpChannelOption.SCTP_NODELAY, true).handler(new ChannelInitializer<SctpChannel>() {

            @Override
            public void initChannel(SctpChannel ch) throws Exception {
                ch.pipeline().addLast(//                             new LoggingHandler(LogLevel.INFO),
                new SctpEchoClientHandler());
            }
        });
        InetSocketAddress localAddress = SocketUtils.socketAddress(CLIENT_PRIMARY_HOST, CLIENT_PORT);
        InetAddress localSecondaryAddress = SocketUtils.addressByName(CLIENT_SECONDARY_HOST);
        InetSocketAddress remoteAddress = SocketUtils.socketAddress(SERVER_REMOTE_HOST, SERVER_REMOTE_PORT);
        // Bind the client channel.
        ChannelFuture bindFuture = b.bind(localAddress).sync();
        // Get the underlying sctp channel
        SctpChannel channel = (SctpChannel) bindFuture.channel();
        // Bind the secondary address.
        // Please note that, bindAddress in the client channel should be done before connecting if you have not
        // enable Dynamic Address Configuration. See net.sctp.addip_enable kernel param
        channel.bindAddress(localSecondaryAddress).sync();
        // Finish connect
        ChannelFuture connectFuture = channel.connect(remoteAddress).sync();
        // Wait until the connection is closed.
        connectFuture.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSctpChannel(io.netty.channel.sctp.nio.NioSctpChannel) SctpChannel(io.netty.channel.sctp.SctpChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SctpEchoClientHandler(io.netty.example.sctp.SctpEchoClientHandler) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) InetAddress(java.net.InetAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 24 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class SecureChatClient method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new SecureChatClientInitializer(sslCtx));
        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();
        // Read commands from the stdin.
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (; ; ) {
            String line = in.readLine();
            if (line == null) {
                break;
            }
            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line + "\r\n");
            // the connection.
            if ("bye".equals(line.toLowerCase())) {
                ch.closeFuture().sync();
                break;
            }
        }
        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        // The connection is closed automatically on shutdown.
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) InputStreamReader(java.io.InputStreamReader) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) BufferedReader(java.io.BufferedReader) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 25 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project netty by netty.

the class LocalChannelTest method localChannelRaceCondition.

@Test
public void localChannelRaceCondition() throws Exception {
    final CountDownLatch closeLatch = new CountDownLatch(1);
    final EventLoopGroup clientGroup = new DefaultEventLoopGroup(1) {

        @Override
        protected EventLoop newChild(Executor threadFactory, Object... args) throws Exception {
            return new SingleThreadEventLoop(this, threadFactory, true) {

                @Override
                protected void run() {
                    for (; ; ) {
                        Runnable task = takeTask();
                        if (task != null) {
                            /* Only slow down the anonymous class in LocalChannel#doRegister() */
                            if (task.getClass().getEnclosingClass() == LocalChannel.class) {
                                try {
                                    closeLatch.await();
                                } catch (InterruptedException e) {
                                    throw new Error(e);
                                }
                            }
                            task.run();
                            updateLastExecutionTime();
                        }
                        if (confirmShutdown()) {
                            break;
                        }
                    }
                }
            };
        }
    };
    Channel sc = null;
    Channel cc = null;
    try {
        ServerBootstrap sb = new ServerBootstrap();
        sc = sb.group(group2).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.close();
                closeLatch.countDown();
            }
        }).bind(TEST_ADDRESS).sync().channel();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(clientGroup).channel(LocalChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
            /* Do nothing */
            }
        });
        ChannelFuture future = bootstrap.connect(sc.localAddress());
        assertTrue("Connection should finish, not time out", future.await(200));
        cc = future.channel();
    } finally {
        closeChannel(cc);
        closeChannel(sc);
        clientGroup.shutdownGracefully(0, 0, SECONDS).await();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) SingleThreadEventLoop(io.netty.channel.SingleThreadEventLoop) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Executor(java.util.concurrent.Executor) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Aggregations

EventLoopGroup (io.netty.channel.EventLoopGroup)134 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)92 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)64 Channel (io.netty.channel.Channel)64 Bootstrap (io.netty.bootstrap.Bootstrap)61 Test (org.junit.Test)36 ChannelFuture (io.netty.channel.ChannelFuture)35 SslContext (io.netty.handler.ssl.SslContext)35 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)31 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)28 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)27 LoggingHandler (io.netty.handler.logging.LoggingHandler)26 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)25 SocketChannel (io.netty.channel.socket.SocketChannel)22 ChannelPipeline (io.netty.channel.ChannelPipeline)20 LocalAddress (io.netty.channel.local.LocalAddress)18 LocalChannel (io.netty.channel.local.LocalChannel)18 LocalServerChannel (io.netty.channel.local.LocalServerChannel)18 InetSocketAddress (java.net.InetSocketAddress)18 ByteBuf (io.netty.buffer.ByteBuf)17