/*
 * Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package reactor.io.netty.util;

import java.util.concurrent.ThreadFactory;

import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.internal.PlatformDependent;

/**
 * @author Stephane Maldini
 */
public final class NettyNativeDetector {

	final static NettyNativeDetector DEFAULT;

	final boolean epoll;

	NettyNativeDetector(boolean epoll) {
		this.epoll = epoll;
	}

	public static NettyNativeDetector instance(){
		return DEFAULT;
	}

	public static NettyNativeDetector force(boolean _native){
		return new NettyNativeDetector(_native);
	}

	static {
		if (Boolean.parseBoolean(System.getProperty("reactor.io.epoll", "true"))) {
			if (!PlatformDependent.isWindows()) {
					DEFAULT = new NettyNativeDetector(EpollDetector.hasEpoll());
			}
			else {
				DEFAULT = new NettyNativeDetector(EpollDetector.hasEpoll());
			}
		} else {
			DEFAULT = new NettyNativeDetector(false);
		}
	}

	public EventLoopGroup newEventLoopGroup(int threads, ThreadFactory factory) {
		return epoll ? EpollDetector.newEventLoopGroup(threads, factory) : new NioEventLoopGroup(threads, factory);
	}

	public Class<? extends ServerChannel> getServerChannel(EventLoopGroup group) {
		return epoll ? EpollDetector.getServerChannel(group) :  NioServerSocketChannel.class;
	}

	public Class<? extends Channel> getChannel(EventLoopGroup group) {
		return epoll ? EpollDetector.getChannel(group) :  NioSocketChannel.class;
	}

	public Class<? extends Channel> getDatagramChannel(EventLoopGroup group) {
		return epoll ? EpollDetector.getDatagramChannel(group) :  NioDatagramChannel.class;
	}

}

@@ -1080,16 +1080,49 @@
 pendent;
+%0Aimport reactor.core.util.Logger;
 %0A%0A/**%0A *
@@ -1839,16 +1839,126 @@
 e);%0A%09%09%7D%0A
+%09%09Logger.getLogger(NettyNativeDetector.class).debug(%22Default Netty Epoll %22 +%0A%09%09%09%09%22support : %22+DEFAULT.epoll);%0A
 %09%7D%0A%0A%09pub

debug log for epoll