Example usage for io.netty.channel ChannelOption SO_KEEPALIVE

List of usage examples for io.netty.channel ChannelOption SO_KEEPALIVE

Introduction

In this page you can find the example usage for io.netty.channel ChannelOption SO_KEEPALIVE.

Prototype

ChannelOption SO_KEEPALIVE

To view the source code for io.netty.channel ChannelOption SO_KEEPALIVE.

Click Source Link

Usage

From source file:org.onosproject.artemis.impl.moas.MoasServerController.java

License:Apache License

/**
 * Create netty server bootstrap.//from  www .jav  a2 s  .co  m
 *
 * @return bootstrap
 */
private ServerBootstrap createServerBootStrap() {
    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup();

    return new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.SO_KEEPALIVE, true)
            .childOption(ChannelOption.TCP_NODELAY, true);
}

From source file:org.onosproject.openflow.controller.impl.Controller.java

License:Apache License

/**
 * Tell controller that we're ready to accept switches loop.
 */// w w w .  ja  v a2 s . c om
public void run() {

    try {
        final ServerBootstrap bootstrap = createServerBootStrap();
        bootstrap.option(ChannelOption.SO_REUSEADDR, true);
        bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
        bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
        bootstrap.childOption(ChannelOption.SO_SNDBUF, Controller.SEND_BUFFER_SIZE);
        //            bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK,
        //                                  new WriteBufferWaterMark(8 * 1024, 32 * 1024));

        bootstrap.childHandler(new OFChannelInitializer(this, null, sslContext));

        openFlowPorts.forEach(port -> {
            // TODO revisit if this is best way to listen to multiple ports
            cg.add(bootstrap.bind(port).syncUninterruptibly().channel());
            log.info("Listening for switch connections on {}", port);
        });

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:org.onosproject.ovsdb.controller.impl.Controller.java

License:Apache License

/**
 * Accepts incoming connections./* ww w .  j  ava 2 s  .  com*/
 */
private void startAcceptingConnections() throws InterruptedException {
    ServerBootstrap b = new ServerBootstrap();

    b.group(bossGroup, workerGroup).channel(serverChannelClass)
            .childHandler(new OnosCommunicationChannelInitializer());
    b.option(ChannelOption.SO_BACKLOG, 128);
    b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
    b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
    b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.childOption(ChannelOption.SO_KEEPALIVE, true);
    b.bind(ovsdbPort).sync();
}

From source file:org.onosproject.store.cluster.messaging.impl.NettyMessagingManager.java

License:Apache License

private void startAcceptingConnections() throws InterruptedException {
    ServerBootstrap b = new ServerBootstrap();
    b.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
    b.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
    b.option(ChannelOption.SO_RCVBUF, 1048576);
    b.option(ChannelOption.TCP_NODELAY, true);
    b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.group(serverGroup, clientGroup);/*ww  w.  j  av a  2s . c  o  m*/
    b.channel(serverChannelClass);
    if (enableNettyTls) {
        b.childHandler(new SslServerCommunicationChannelInitializer());
    } else {
        b.childHandler(new OnosCommunicationChannelInitializer());
    }
    b.option(ChannelOption.SO_BACKLOG, 128);
    b.childOption(ChannelOption.SO_KEEPALIVE, true);

    // Bind and start to accept incoming connections.
    b.bind(localEp.port()).sync().addListener(future -> {
        if (future.isSuccess()) {
            log.info("{} accepting incoming connections on port {}", localEp.host(), localEp.port());
        } else {
            log.warn("{} failed to bind to port {}", localEp.host(), localEp.port(), future.cause());
        }
    });
}

From source file:org.onosproject.tl1.impl.DefaultTl1Controller.java

License:Apache License

@Override
public void connectDevice(DeviceId deviceId) {
    Tl1Device device = deviceMap.get(deviceId);
    if (device == null || device.isConnected()) {
        return;//from   www .j  a va 2  s . c om
    }

    Bootstrap b = new Bootstrap();
    b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(8192, DELIMITER));
                    socketChannel.pipeline().addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
                    // TODO
                    //socketChannel.pipeline().addLast(new Tl1Decoder());
                    socketChannel.pipeline().addLast(new Tl1InboundHandler());
                }
            }).remoteAddress(device.ip().toInetAddress(), device.port()).connect()
            .addListener((ChannelFuture channelFuture) -> {
                if (channelFuture.isSuccess()) {
                    msgMap.put(channelFuture.channel(), new ConcurrentHashMap<>());
                    device.connect(channelFuture.channel());
                    tl1Listeners.forEach(l -> executor.execute(() -> l.deviceConnected(deviceId)));
                }
            });
}

From source file:org.opencloudb.config.model.SystemConfig.java

License:Open Source License

public void setSocketParams(AbstractBootstrap<?, ?> bootstrap, boolean isFrontChannel) throws IOException {
    int sorcvbuf = 0;
    int sosndbuf = 0;
    int soNoDelay = 0;
    if (isFrontChannel) {
        sorcvbuf = getFrontsocketsorcvbuf();
        sosndbuf = getFrontsocketsosndbuf();
        soNoDelay = getFrontSocketNoDelay();
    } else {//  w w  w. j  a  v a2 s  . c o  m
        sorcvbuf = getBacksocketsorcvbuf();
        sosndbuf = getBacksocketsosndbuf();
        soNoDelay = getBackSocketNoDelay();
    }

    bootstrap.option(ChannelOption.SO_RCVBUF, sorcvbuf);
    bootstrap.option(ChannelOption.SO_SNDBUF, sosndbuf);
    bootstrap.option(ChannelOption.TCP_NODELAY, soNoDelay == 1);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 1024 * 1024);
}

From source file:org.opendaylight.ocpjava.protocol.impl.core.TcpHandler.java

License:Open Source License

/**
 * Starts server on selected port.//w ww  . j  a v a2  s . co  m
 */
@Override
public void run() {
    /*
     * We generally do not perform IO-unrelated tasks, so we want to have
     * all outstanding tasks completed before the executing thread goes
     * back into select.
     *
     * Any other setting means netty will measure the time it spent selecting
     * and spend roughly proportional time executing tasks.
     */
    workerGroup.setIoRatio(100);

    final ChannelFuture f;
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(channelInitializer)
                .option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_REUSEADDR, true)
                //modify to "false" for OCP health-check
                .childOption(ChannelOption.SO_KEEPALIVE, false).childOption(ChannelOption.TCP_NODELAY, true)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, DEFAULT_WRITE_HIGH_WATERMARK * 1024)
                .childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, DEFAULT_WRITE_LOW_WATERMARK * 1024)
                .childOption(ChannelOption.WRITE_SPIN_COUNT, DEFAULT_WRITE_SPIN_COUNT);

        if (startupAddress != null) {
            f = b.bind(startupAddress.getHostAddress(), port).sync();
        } else {
            f = b.bind(port).sync();
        }
    } catch (InterruptedException e) {
        LOG.error("Interrupted while binding port {}", port, e);
        return;
    }

    try {
        InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
        address = isa.getHostString();

        // Update port, as it may have been specified as 0
        this.port = isa.getPort();

        LOG.debug("address from tcphandler: {}", address);
        isOnlineFuture.set(true);
        LOG.info("RadioHead listener started and ready to accept incoming tcp/tls connections on port: {}",
                port);
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        LOG.error("Interrupted while waiting for port {} shutdown", port, e);
    } finally {
        shutdown();
    }
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.TcpHandler.java

License:Open Source License

/**
 * Starts server on selected port./* w  w w . j av a  2 s . c om*/
 */
@Override
public void run() {
    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(channelInitializer)
                .option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_REUSEADDR, true)
                .childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture f;
        if (startupAddress != null) {
            f = b.bind(startupAddress.getHostAddress(), port).sync();
        } else {
            f = b.bind(port).sync();
        }

        InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
        address = isa.getHostString();
        LOGGER.debug("address from tcphandler: " + address);
        port = isa.getPort();
        isOnlineFuture.set(true);
        LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
        f.channel().closeFuture().sync();
    } catch (InterruptedException ex) {
        LOGGER.error(ex.getMessage(), ex);
    } finally {
        shutdown();
    }
}

From source file:org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl.java

License:Open Source License

protected Bootstrap createClientBootStrap(final Optional<KeyMapping> keys, final EventLoopGroup workerGroup) {
    final Bootstrap bootstrap = new Bootstrap();
    if (Epoll.isAvailable()) {
        bootstrap.channel(EpollSocketChannel.class);
        bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    } else {// www . j av a 2s  . co  m
        bootstrap.channel(NioSocketChannel.class);
    }
    if (keys.isPresent()) {
        if (Epoll.isAvailable()) {
            bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys.get());
        } else {
            throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
        }
    }

    // Make sure we are doing round-robin processing
    bootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
    bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATER_MARK);
    bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATER_MARK);

    if (bootstrap.group() == null) {
        bootstrap.group(workerGroup);
    }

    return bootstrap;
}

From source file:org.opendaylight.protocol.bgp.rib.impl.TestClientDispatcher.java

License:Open Source License

protected TestClientDispatcher(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup,
        final MessageRegistry messageRegistry, final InetSocketAddress localAddress) {
    this.disp = new BGPDispatcherImpl(messageRegistry, bossGroup, workerGroup) {
        @Override/*from  ww w. j a  va2 s  .  c  o  m*/
        protected Bootstrap createClientBootStrap(final Optional<KeyMapping> keys,
                final EventLoopGroup workerGroup) {
            final Bootstrap bootstrap = new Bootstrap();
            if (Epoll.isAvailable()) {
                bootstrap.channel(EpollSocketChannel.class);
            } else {
                bootstrap.channel(NioSocketChannel.class);
            }
            // Make sure we are doing round-robin processing
            bootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
            bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);

            if (bootstrap.group() == null) {
                bootstrap.group(workerGroup);
            }
            bootstrap.localAddress(localAddress);
            bootstrap.option(ChannelOption.SO_REUSEADDR, true);
            return bootstrap;
        }
    };
    this.hf = new BGPHandlerFactory(messageRegistry);
    this.localAddress = localAddress;
    this.defaultAddress = localAddress;
}