import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.EpollDatagramChannel;
+import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.util.concurrent.GenericFutureListener;
private final SettableFuture<Boolean> isOnlineFuture;
private UdpChannelInitializer channelInitializer;
private ThreadConfiguration threadConfig;
+ private Class<? extends DatagramChannel> datagramChannelClass;
/**
* Constructor of UdpHandler that listens on selected port.
@Override
public void run() {
- if (threadConfig != null) {
- group = new NioEventLoopGroup(threadConfig.getWorkerThreadCount());
- } else {
- group = new NioEventLoopGroup();
- }
final ChannelFuture f;
try {
Bootstrap b = new Bootstrap();
b.group(group)
- .channel(NioDatagramChannel.class)
+ .channel(datagramChannelClass)
.option(ChannelOption.SO_BROADCAST, false)
.handler(channelInitializer);
public void setThreadConfig(ThreadConfiguration threadConfig) {
this.threadConfig = threadConfig;
}
+
+ /**
+ * Initiate event loop groups
+ * @param threadConfiguration number of threads to be created, if not specified in threadConfig
+ */
+ public void initiateEventLoopGroups(ThreadConfiguration threadConfiguration, boolean isEpollEnabled) {
+
+ if(isEpollEnabled) {
+ initiateEpollEventLoopGroups(threadConfiguration);
+ } else {
+ initiateNioEventLoopGroups(threadConfiguration);
+ }
+ }
+
+ /**
+ * Initiate Nio event loop groups
+ * @param threadConfiguration number of threads to be created, if not specified in threadConfig
+ */
+ public void initiateNioEventLoopGroups(ThreadConfiguration threadConfiguration) {
+ datagramChannelClass = NioDatagramChannel.class;
+ if (threadConfiguration != null) {
+ group = new NioEventLoopGroup(threadConfiguration.getWorkerThreadCount());
+ } else {
+ group = new NioEventLoopGroup();
+ }
+ }
+
+ /**
+ * Initiate Epoll event loop groups with Nio as fall back
+ * @param threadConfiguration
+ */
+ protected void initiateEpollEventLoopGroups(ThreadConfiguration threadConfiguration) {
+ try {
+ datagramChannelClass = EpollDatagramChannel.class;
+ if (threadConfiguration != null) {
+ group = new EpollEventLoopGroup(threadConfiguration.getWorkerThreadCount());
+ } else {
+ group = new EpollEventLoopGroup();
+ }
+ return;
+ } catch (Throwable ex) {
+ LOGGER.debug("Epoll initiation failed");
+ }
+
+ //Fallback mechanism
+ initiateNioEventLoopGroups(threadConfiguration);
+ }
}
\ No newline at end of file