Search in sources :

Example 51 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project netty by netty.

the class DnsNameResolverClientSubnetTest method testSubnetQuery.

// See https://www.gsic.uva.es/~jnisigl/dig-edns-client-subnet.html
// Ignore as this needs to query real DNS servers.
@Ignore
@Test
public void testSubnetQuery() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup(1);
    DnsNameResolver resolver = newResolver(group).build();
    try {
        // Same as:
        // # /.bind-9.9.3-edns/bin/dig @ns1.google.com www.google.es +client=157.88.0.0/24
        Future<List<InetAddress>> future = resolver.resolveAll("www.google.es", Collections.<DnsRecord>singleton(// 157.88.0.0 / 24
        new DefaultDnsOptEcsRecord(1024, 24, SocketUtils.addressByName("157.88.0.0").getAddress())));
        for (InetAddress address : future.syncUninterruptibly().getNow()) {
            System.err.println(address);
        }
    } finally {
        resolver.close();
        group.shutdownGracefully(0, 0, TimeUnit.SECONDS);
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultDnsOptEcsRecord(io.netty.handler.codec.dns.DefaultDnsOptEcsRecord) List(java.util.List) InetAddress(java.net.InetAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 52 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project netty by netty.

the class DnsNameResolverTest method testRecursiveResolveCache.

private static void testRecursiveResolveCache(boolean cache) throws Exception {
    final String hostname = "some.record.netty.io";
    final String hostname2 = "some2.record.netty.io";
    final TestDnsServer dnsServerAuthority = new TestDnsServer(new HashSet<String>(Arrays.asList(hostname, hostname2)));
    dnsServerAuthority.start();
    TestDnsServer dnsServer = new RedirectingTestDnsServer(hostname, dnsServerAuthority.localAddress().getAddress().getHostAddress());
    dnsServer.start();
    TestDnsCache nsCache = new TestDnsCache(cache ? new DefaultDnsCache() : NoopDnsCache.INSTANCE);
    EventLoopGroup group = new NioEventLoopGroup(1);
    DnsNameResolver resolver = new DnsNameResolver(group.next(), new ReflectiveChannelFactory<DatagramChannel>(NioDatagramChannel.class), DnsServerAddresses.singleton(dnsServer.localAddress()), NoopDnsCache.INSTANCE, nsCache, 3000, ResolvedAddressTypes.IPV4_ONLY, true, 10, true, 4096, false, HostsFileEntriesResolver.DEFAULT, NoopDnsServerAddressStreamProvider.INSTANCE, DnsNameResolver.DEFAULT_SEARCH_DOMAINS, 0, true) {

        @Override
        int dnsRedirectPort(InetAddress server) {
            return server.equals(dnsServerAuthority.localAddress().getAddress()) ? dnsServerAuthority.localAddress().getPort() : DnsServerAddresses.DNS_PORT;
        }
    };
    try {
        resolver.resolveAll(hostname).syncUninterruptibly();
        if (cache) {
            assertNull(nsCache.cache.get("io.", null));
            assertNull(nsCache.cache.get("netty.io.", null));
            List<DnsCacheEntry> entries = nsCache.cache.get("record.netty.io.", null);
            assertEquals(1, entries.size());
            assertNull(nsCache.cache.get(hostname, null));
            // Test again via cache.
            resolver.resolveAll(hostname).syncUninterruptibly();
            resolver.resolveAll(hostname2).syncUninterruptibly();
            // Check that it only queried the cache for record.netty.io.
            assertNull(nsCache.cacheHits.get("io."));
            assertNull(nsCache.cacheHits.get("netty.io."));
            assertNotNull(nsCache.cacheHits.get("record.netty.io."));
            assertNull(nsCache.cacheHits.get("some.record.netty.io."));
        }
    } finally {
        resolver.close();
        group.shutdownGracefully(0, 0, TimeUnit.SECONDS);
        dnsServer.stop();
        dnsServerAuthority.stop();
    }
}
Also used : NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) InetAddress(java.net.InetAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 53 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project neo4j by neo4j.

the class NettyServer method start.

@Override
public void start() throws Throwable {
    // The boss thread accepts new incoming connections and chooses a worker thread to be responsible for the
    // IO of the new connection. We expect new connections to be (comparatively) rare, so we allocate a single
    // thread for this.
    // TODO: In fact, dedicating a whole thread to sit and spin in #select for new connections may be a waste of
    // time, we could have the same event loop groups for both handling new connections and for handling events
    // on existing connections
    bossGroup = new NioEventLoopGroup(1, tf);
    // These threads handle live channels. Each thread has a set of channels it is responsible for, and it will
    // continuously run a #select() loop to react to new events on these channels.
    selectorGroup = new NioEventLoopGroup(NUM_SELECTOR_THREADS, tf);
    for (ProtocolInitializer initializer : bootstrappers) {
        try {
            new ServerBootstrap().option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).group(bossGroup, selectorGroup).channel(NioServerSocketChannel.class).childHandler(initializer.channelInitializer()).bind(initializer.address().socketAddress()).sync();
        } catch (Throwable e) {
            // In any case, we do all this just in order to throw a more helpful bind exception, oh, and here's that part coming right now!
            if (e instanceof BindException) {
                throw new PortBindException(initializer.address(), (BindException) e);
            }
            throw e;
        }
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PortBindException(org.neo4j.helpers.PortBindException) BindException(java.net.BindException) PortBindException(org.neo4j.helpers.PortBindException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 54 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project netty by netty.

the class MemcacheClient method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                }
                p.addLast(new BinaryMemcacheClientCodec());
                p.addLast(new BinaryMemcacheObjectAggregator(Integer.MAX_VALUE));
                p.addLast(new MemcacheClientHandler());
            }
        });
        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();
        // Read commands from the stdin.
        System.out.println("Enter commands (quit to end)");
        System.out.println("get <key>");
        System.out.println("set <key> <value>");
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (; ; ) {
            String line = in.readLine();
            if (line == null) {
                break;
            }
            if ("quit".equals(line.toLowerCase())) {
                ch.close().sync();
                break;
            }
            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line);
        }
        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InputStreamReader(java.io.InputStreamReader) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) BinaryMemcacheClientCodec(io.netty.handler.codec.memcache.binary.BinaryMemcacheClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BufferedReader(java.io.BufferedReader) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) BinaryMemcacheObjectAggregator(io.netty.handler.codec.memcache.binary.BinaryMemcacheObjectAggregator)

Example 55 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project netty by netty.

the class ObjectEchoServer method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc()));
                }
                p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new ObjectEchoServerHandler());
            }
        });
        // Bind and start to accept incoming connections.
        b.bind(PORT).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) ObjectDecoder(io.netty.handler.codec.serialization.ObjectDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ObjectEncoder(io.netty.handler.codec.serialization.ObjectEncoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)201 EventLoopGroup (io.netty.channel.EventLoopGroup)100 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)80 Bootstrap (io.netty.bootstrap.Bootstrap)72 ChannelFuture (io.netty.channel.ChannelFuture)58 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)56 Channel (io.netty.channel.Channel)55 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)54 SocketChannel (io.netty.channel.socket.SocketChannel)48 SslContext (io.netty.handler.ssl.SslContext)41 LoggingHandler (io.netty.handler.logging.LoggingHandler)38 InetSocketAddress (java.net.InetSocketAddress)36 ChannelPipeline (io.netty.channel.ChannelPipeline)35 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)26 Test (org.testng.annotations.Test)23 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)20 ByteBuf (io.netty.buffer.ByteBuf)18 NettyClientMetrics (com.linkedin.pinot.transport.metrics.NettyClientMetrics)16 ChannelInitializer (io.netty.channel.ChannelInitializer)16 HashedWheelTimer (io.netty.util.HashedWheelTimer)16