Skip to content

Commit c4edc98

Browse files
committed
Moved some things around
1 parent 25b89c9 commit c4edc98

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

src/main/java/tel/schich/javacan/CanChannels.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
package tel.schich.javacan;
2424

2525
import java.io.IOException;
26-
import java.nio.channels.spi.SelectorProvider;
2726

28-
import tel.schich.javacan.select.ExtensibleSelectorProvider;
27+
import static tel.schich.javacan.linux.epoll.EPollSelector.PROVIDER;
2928

3029
/**
3130
* This utility class provides helper methods to easily create new channels similar to those in
@@ -38,12 +37,6 @@
3837
*/
3938
public class CanChannels {
4039

41-
/**
42-
* A {@link java.nio.channels.spi.SelectorProvider} implementation that supports custom
43-
* {@link java.nio.channels.Channel} implementations just like this one.
44-
*/
45-
public static final SelectorProvider PROVIDER = new ExtensibleSelectorProvider();
46-
4740
private CanChannels() {
4841
}
4942

src/main/java/tel/schich/javacan/linux/epoll/EPollSelector.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import tel.schich.javacan.linux.LinuxNativeOperationException;
2626
import tel.schich.javacan.linux.UnixFileDescriptor;
27+
import tel.schich.javacan.select.ExtensibleSelectorProvider;
2728
import tel.schich.javacan.select.NativeChannel;
2829
import tel.schich.javacan.select.NativeHandle;
2930
import tel.schich.javacan.util.UngrowableSet;
@@ -54,6 +55,12 @@
5455
*/
5556
public class EPollSelector extends AbstractSelector {
5657

58+
/**
59+
* A {@link java.nio.channels.spi.SelectorProvider} implementation that supports custom
60+
* {@link java.nio.channels.Channel} implementations just like this one.
61+
*/
62+
public static final SelectorProvider PROVIDER = new ExtensibleSelectorProvider();
63+
5764
private static final long SELECT_NO_BLOCKING = 0;
5865
private static final long SELECT_BLOCK_INDEFINITELY = -1;
5966

@@ -292,4 +299,12 @@ public Selector wakeup() {
292299
}
293300
return this;
294301
}
302+
303+
public static EPollSelector open() throws IOException {
304+
return new EPollSelector(PROVIDER);
305+
}
306+
307+
public static EPollSelector open(int maxEvents) throws IOException {
308+
return new EPollSelector(PROVIDER, maxEvents);
309+
}
295310
}

src/main/java/tel/schich/javacan/util/CanBroker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
import tel.schich.javacan.RawCanChannel;
4444

4545
import static java.time.Duration.ofMinutes;
46-
import static tel.schich.javacan.CanChannels.PROVIDER;
4746
import static tel.schich.javacan.CanSocketOptions.FILTER;
4847
import static tel.schich.javacan.CanSocketOptions.LOOPBACK;
48+
import static tel.schich.javacan.linux.epoll.EPollSelector.PROVIDER;
4949

5050
/**
5151
* This class implements an event driven interface over several CAN interface to send and receive

src/test/java/tel/schich/javacan/test/select/EPollSelectorTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@
2828
import tel.schich.javacan.CanFrame;
2929
import tel.schich.javacan.CanSocketOptions;
3030
import tel.schich.javacan.RawCanChannel;
31+
import tel.schich.javacan.linux.epoll.EPollSelector;
3132
import tel.schich.javacan.select.ExtensibleSelectorProvider;
3233
import tel.schich.javacan.test.CanTestHelper;
3334

3435
import java.io.IOException;
3536
import java.nio.channels.SelectionKey;
37+
import java.nio.channels.Selector;
3638
import java.nio.channels.spi.AbstractSelector;
3739
import java.util.Set;
3840

3941
import static java.time.Duration.ofMillis;
4042
import static org.junit.jupiter.api.Assertions.*;
41-
import static tel.schich.javacan.CanChannels.PROVIDER;
4243
import static tel.schich.javacan.CanFrame.FD_NO_FLAGS;
4344
import static tel.schich.javacan.CanSocketOptions.RECV_OWN_MSGS;
4445
import static tel.schich.javacan.test.CanTestHelper.CAN_INTERFACE;
@@ -55,7 +56,7 @@ public void testOpenClose() throws IOException {
5556
@Test
5657
public void testWriteRead() throws IOException {
5758
try (RawCanChannel ch = CanChannels.newRawChannel()) {
58-
try (AbstractSelector selector = ch.provider().openSelector()) {
59+
try (Selector selector = EPollSelector.open()) {
5960
ch.setOption(RECV_OWN_MSGS, true);
6061
ch.configureBlocking(false);
6162
ch.bind(CAN_INTERFACE);
@@ -79,8 +80,7 @@ public void testWriteRead() throws IOException {
7980

8081
@Test
8182
public void testWakeup() throws IOException {
82-
ExtensibleSelectorProvider provider = new ExtensibleSelectorProvider();
83-
try (AbstractSelector selector = provider.openSelector()) {
83+
try (AbstractSelector selector = EPollSelector.open()) {
8484
runDelayed(ofMillis(100), selector::wakeup);
8585
assertTimeoutPreemptively(ofMillis(200), (Executable) selector::select);
8686
}
@@ -89,7 +89,7 @@ public void testWakeup() throws IOException {
8989
@Test
9090
public void testPollWithClosedChannel() throws IOException {
9191

92-
try (final AbstractSelector selector = PROVIDER.openSelector()) {
92+
try (final Selector selector = EPollSelector.open()) {
9393

9494
RawCanChannel firstChannel = configureAndRegisterChannel(selector);
9595
firstChannel.close();
@@ -102,7 +102,7 @@ public void testPollWithClosedChannel() throws IOException {
102102
@Test
103103
public void testEPollFdReuse() throws IOException, InterruptedException {
104104

105-
try (final AbstractSelector selector = PROVIDER.openSelector()) {
105+
try (final AbstractSelector selector = EPollSelector.open()) {
106106

107107
try (RawCanChannel firstChannel = configureAndRegisterChannel(selector)) {
108108
CanTestHelper.sendFrameViaUtils(CAN_INTERFACE, CanFrame.create(0x3, CanFrame.FD_NO_FLAGS, new byte[] {1}));
@@ -122,7 +122,7 @@ public void testEPollFdReuse() throws IOException, InterruptedException {
122122
}
123123
}
124124

125-
private static RawCanChannel configureAndRegisterChannel(AbstractSelector selector) throws IOException {
125+
private static RawCanChannel configureAndRegisterChannel(Selector selector) throws IOException {
126126
final RawCanChannel ch = CanChannels.newRawChannel(CAN_INTERFACE);
127127
System.out.println("Created channel: " + ch);
128128

0 commit comments

Comments
 (0)