2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.transport.tcp;
10 import io.netty.bootstrap.Bootstrap;
11 import io.netty.bootstrap.ServerBootstrap;
12 import io.netty.channel.ChannelOption;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.channel.socket.nio.NioChannelOption;
16 import io.netty.channel.socket.nio.NioServerSocketChannel;
17 import io.netty.channel.socket.nio.NioSocketChannel;
19 import java.util.concurrent.ThreadFactory;
20 import jdk.net.ExtendedSocketOptions;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.common.rev220524.tcp.common.grouping.Keepalives;
25 final class NioNettyImpl extends AbstractNettyImpl {
26 private static final ChannelOption<Integer> TCP_KEEPIDLE = NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPIDLE);
27 private static final ChannelOption<Integer> TCP_KEEPCNT = NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPCOUNT);
28 private static final ChannelOption<Integer> TCP_KEEPINTVL =
29 NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPINTERVAL);
31 private final boolean supportsKeepalives;
34 final var ch = new NioSocketChannel();
36 supportsKeepalives = ch.config().setOptions(Map.of(
37 ChannelOption.SO_KEEPALIVE, Boolean.TRUE, TCP_KEEPIDLE, 7200, TCP_KEEPCNT, 3, TCP_KEEPINTVL, 5));
44 Class<NioSocketChannel> channelClass() {
45 return NioSocketChannel.class;
49 Class<NioServerSocketChannel> serverChannelClass() {
50 return NioServerSocketChannel.class;
54 EventLoopGroup newEventLoopGroup(final int numThreads, final ThreadFactory threadFactory) {
55 return new NioEventLoopGroup(numThreads, threadFactory);
59 boolean supportsKeepalives() {
60 return supportsKeepalives;
64 void configureKeepalives(final Bootstrap bootstrap, final Keepalives keepalives) {
66 .option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)
67 .option(TCP_KEEPIDLE, keepalives.requireIdleTime().toJava())
68 .option(TCP_KEEPCNT, keepalives.requireMaxProbes().toJava())
69 .option(TCP_KEEPINTVL, keepalives.requireProbeInterval().toJava());
73 void configureKeepalives(final ServerBootstrap bootstrap, final Keepalives keepalives) {
75 .childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)
76 .childOption(TCP_KEEPIDLE, keepalives.requireIdleTime().toJava())
77 .childOption(TCP_KEEPCNT, keepalives.requireMaxProbes().toJava())
78 .childOption(TCP_KEEPINTVL, keepalives.requireProbeInterval().toJava());
82 public String toString() {