From 76cd3a37e5ea33c7d79d6aa6dace5167bd60f8a0 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 26 Jun 2014 12:20:47 +0200 Subject: [PATCH] [#2598] Add Epoll.isAvailable() which allows to check if epoll can be used. Motivation: At the moment there is no simple way for a user to check if the native epoll transport can be used on the running platform. Thus the user can only try to instance it and catch any exception and fallback to nio transport. Modification: Add Epoll.isAvailable() which allows to check if epoll can be used. Result: User can easily check if epoll transport can be used or not --- .../java/io/netty/channel/epoll/Epoll.java | 64 +++++++++++++++++++ .../io/netty/channel/epoll/EpollTest.java | 27 ++++++++ 2 files changed, 91 insertions(+) create mode 100644 transport-native-epoll/src/main/java/io/netty/channel/epoll/Epoll.java create mode 100644 transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollTest.java diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/Epoll.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Epoll.java new file mode 100644 index 0000000000..dd10b74b91 --- /dev/null +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Epoll.java @@ -0,0 +1,64 @@ +/* + * Copyright 2014 The Netty Project + * + * The Netty Project licenses this file to you 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 io.netty.channel.epoll; + +/** + * Tells if {@code netty-transport-native-epoll} is supported. + */ +public final class Epoll { + + private static final boolean IS_AVAILABLE; + + static { + boolean available; + int epollFd = -1; + int eventFd = -1; + try { + epollFd = Native.epollCreate(); + eventFd = Native.eventFd(); + available = true; + } catch (Throwable cause) { + // ignore + available = false; + } finally { + if (epollFd != -1) { + try { + Native.close(epollFd); + } catch (Exception ignore) { + // ignore + } + } + if (eventFd != -1) { + try { + Native.close(eventFd); + } catch (Exception ignore) { + // ignore + } + } + } + IS_AVAILABLE = available; + } + + /** + * Returns {@code true} if and only if the + * {@code netty-transport-native-epoll} can be used. + */ + public static boolean isAvailable() { + return IS_AVAILABLE; + } + + private Epoll() { } +} diff --git a/transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollTest.java b/transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollTest.java new file mode 100644 index 0000000000..a2d4fb2d40 --- /dev/null +++ b/transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollTest.java @@ -0,0 +1,27 @@ +/* + * Copyright 2014 The Netty Project + * + * The Netty Project licenses this file to you 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 io.netty.channel.epoll; + +import org.junit.Assert; +import org.junit.Test; + +public class EpollTest { + + @Test + public void testIsAvailable() { + Assert.assertTrue(Epoll.isAvailable()); + } +}