Search in sources :

Example 61 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.

the class MulticastServer method start.

public void start() throws InterruptedException, SocketException, UnknownHostException {
    NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName("127.0.0.1"));
    InetSocketAddress groupAddress = new InetSocketAddress("239.255.27.1", 1234);
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(group).channelFactory(new ChannelFactory<NioDatagramChannel>() {

        @Override
        public NioDatagramChannel newChannel() {
            return new NioDatagramChannel(InternetProtocolFamily.IPv4);
        }
    }).handler(new ChannelInitializer<NioDatagramChannel>() {

        @Override
        protected void initChannel(NioDatagramChannel ch) throws Exception {
            ch.pipeline().addLast(new ServerHandler());
        }
    }).option(ChannelOption.SO_REUSEADDR, true);
    NioDatagramChannel ch = (NioDatagramChannel) b.bind(groupAddress.getPort()).sync().channel();
    ch.joinGroup(groupAddress, ni).sync();
    ch.closeFuture().await();
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 62 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.

the class MulticastClient method start.

public void start() throws InterruptedException, SocketException, UnknownHostException {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(group).channelFactory(new ChannelFactory<NioDatagramChannel>() {

        @Override
        public NioDatagramChannel newChannel() {
            return new NioDatagramChannel(InternetProtocolFamily.IPv4);
        }
    }).handler(new ChannelInitializer<NioDatagramChannel>() {

        @Override
        protected void initChannel(NioDatagramChannel ch) throws Exception {
            ch.pipeline().addLast(myHandler);
        }
    }).option(ChannelOption.SO_REUSEADDR, true);
    NioDatagramChannel ch = (NioDatagramChannel) b.bind(1234).sync().channel();
    new Thread() {

        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                    myHandler.sendMessage("Hola desde netty");
                    System.out.println("Envio Hola");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
    ch.closeFuture().await();
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 63 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project ambry by linkedin.

the class MultiplexedChannelRecordTest method setup.

@Before
public void setup() throws Exception {
    loopGroup = new NioEventLoopGroup(4);
    channel = new MockChannel();
    idleTimeoutMillis = 500L;
    streamChannelInitializer = new ChannelInitializer() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
        // do nothing
        }
    };
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Before(org.junit.Before)

Example 64 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project ambry by linkedin.

the class FrontendNettyFactoryTest method doGetNettyServerTest.

/**
 * Test that a {@link NettyServer} can be constructed by the factory.
 * @param properties the {@link Properties} to use.
 * @param defaultSslFactory the default {@link SSLFactory} to pass into the constructor.
 */
private void doGetNettyServerTest(Properties properties, SSLFactory defaultSslFactory) throws Exception {
    VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
    NettyConfig nettyConfig = new NettyConfig(verifiableProperties);
    FrontendNettyFactory nettyServerFactory = new FrontendNettyFactory(verifiableProperties, new MetricRegistry(), REST_REQUEST_HANDLER, PUBLIC_ACCESS_LOGGER, REST_SERVER_STATE, defaultSslFactory, null);
    NioServer nioServer = nettyServerFactory.getNioServer();
    assertNotNull("No NioServer returned", nioServer);
    assertEquals("Did not receive a NettyServer instance", NettyServer.class.getCanonicalName(), nioServer.getClass().getCanonicalName());
    Map<Integer, ChannelInitializer<SocketChannel>> channelInitializers = nettyServerFactory.channelInitializers;
    if (nettyConfig.nettyServerEnableSSL && defaultSslFactory != null) {
        assertEquals("Expected two ChannelInitializers when SSLFactory is not null", 2, channelInitializers.size());
        assertNotNull("No ChannelInitializer for SSL port", channelInitializers.get(nettyConfig.nettyServerSSLPort));
    } else {
        assertEquals("Expected one ChannelInitializer when SSLFactory is null", 1, channelInitializers.size());
    }
    assertNotNull("No ChannelInitializer for plaintext port", channelInitializers.get(nettyConfig.nettyServerPort));
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) MetricRegistry(com.codahale.metrics.MetricRegistry) ChannelInitializer(io.netty.channel.ChannelInitializer) NettyConfig(com.github.ambry.config.NettyConfig)

Example 65 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project ambry by linkedin.

the class NettyServer method start.

@Override
public void start() throws InstantiationException {
    long startupBeginTime = System.currentTimeMillis();
    try {
        logger.trace("Starting NettyServer deployment");
        if (Epoll.isAvailable()) {
            logger.trace("Using EpollEventLoopGroup in NettyServer.");
            bossGroup = new EpollEventLoopGroup(nettyConfig.nettyServerBossThreadCount);
            workerGroup = new EpollEventLoopGroup(nettyConfig.nettyServerWorkerThreadCount);
        } else {
            bossGroup = new NioEventLoopGroup(nettyConfig.nettyServerBossThreadCount);
            workerGroup = new NioEventLoopGroup(nettyConfig.nettyServerWorkerThreadCount);
        }
        for (Map.Entry<Integer, ChannelInitializer<SocketChannel>> entry : channelInitializers.entrySet()) {
            bindServer(entry.getKey(), entry.getValue(), bossGroup, workerGroup);
        }
        nettyMetrics.registerNettyPendingTasksGauge(bossGroup, "Boss");
        nettyMetrics.registerNettyPendingTasksGauge(workerGroup, "Worker");
    } catch (InterruptedException e) {
        logger.error("NettyServer start await was interrupted", e);
        nettyMetrics.nettyServerStartError.inc();
        throw new InstantiationException("Netty server bind to port [" + nettyConfig.nettyServerPort + "] was interrupted");
    } finally {
        long startupTime = System.currentTimeMillis() - startupBeginTime;
        logger.info("NettyServer start took {} ms", startupTime);
        nettyMetrics.nettyServerStartTimeInMs.update(startupTime);
    }
}
Also used : EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ChannelInitializer(io.netty.channel.ChannelInitializer) Map(java.util.Map) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

ChannelInitializer (io.netty.channel.ChannelInitializer)86 Channel (io.netty.channel.Channel)59 Bootstrap (io.netty.bootstrap.Bootstrap)42 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)39 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)36 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)32 InetSocketAddress (java.net.InetSocketAddress)32 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)31 ChannelFuture (io.netty.channel.ChannelFuture)30 ChannelPipeline (io.netty.channel.ChannelPipeline)26 EventLoopGroup (io.netty.channel.EventLoopGroup)26 LocalServerChannel (io.netty.channel.local.LocalServerChannel)21 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)21 LocalChannel (io.netty.channel.local.LocalChannel)20 SocketChannel (io.netty.channel.socket.SocketChannel)18 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)17 SslHandler (io.netty.handler.ssl.SslHandler)17 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)14 Map (java.util.Map)12 CountDownLatch (java.util.concurrent.CountDownLatch)12