-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathIOSelector.java
More file actions
152 lines (140 loc) · 6.7 KB
/
Copy pathIOSelector.java
File metadata and controls
152 lines (140 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* The MIT License
* Copyright © 2018 Phillip Schichtel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package tel.schich.javacan.select;
import java.io.IOException;
import java.nio.channels.Channel;
import java.nio.channels.ClosedChannelException;
import java.time.Duration;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
/**
* <p>
* This interface is an improved version of {@link java.nio.channels.Selector} that is able to support SocketCAN sockets together with EPoll.
* It also tries to model differences between platforms more closely by parameterizing the type of the resource handle.
* </p>
* <p>
* The JDK's {@link java.nio.channels.Selector} interface as a bunch of assumptions about the channel types it supports that make it impossible
* to correctly use epoll. JavaCAN 2.x attempted to extend the existing JDK interfaces to cover the SocketCAN channel types, however certain
* issues were not possible to fix as the JDK interfaces are too restrictive in their extension points, especially around registration and
* cancellation.
* </p>
*
* @param <HandleType> The type of the resource handle
*
* @see java.nio.channels.Selector
*/
public interface IOSelector<HandleType> extends AutoCloseable {
boolean isOpen();
/**
* Registers a channel to this selector and returns a registration.
*
* @param ch the channel to register
* @param ops the operations to register for
* @param <ChannelType> the type of the channel
* @return the registration
* @throws ClosedChannelException if the channel is already closed
* @throws IOException if any low level IO operation failed
*/
default <ChannelType extends Channel> SelectorRegistration<HandleType, ChannelType> register(ChannelType ch, SelectorRegistration.Operation... ops) throws IOException {
return register(ch, EnumSet.copyOf(Arrays.asList(ops)));
}
/**
* Registers a channel to this selector and returns a registration.
*
* @param ch the channel to register
* @param ops the operations to register for
* @param <ChannelType> the type of the channel
* @return the registration
* @throws ClosedChannelException if the channel is already closed
* @throws IOException if any low level IO operation failed
*/
<ChannelType extends Channel> SelectorRegistration<HandleType, ChannelType> register(ChannelType ch, Set<SelectorRegistration.Operation> ops) throws IOException;
/**
* Cancels a given registration.
*
* @param registration the registration to cancel
* @param <ChannelType> the type of the channel
* @return true if the cancellation was successful
* @throws ClosedChannelException if the channel is already closed
* @throws IOException if any low level IO operation failed
*/
<ChannelType extends Channel> boolean cancel(SelectorRegistration<HandleType, ChannelType> registration) throws IOException;
/**
* <p>
* Updates a registration's interested operations.
* </p>
* <p>
* The returned registration is a copy of the given registration with the interested ops updated, however the old
* registration instance remains valid and can equally be used to perform selector operations.
* </p>
*
* @param registration the registration to update
* @param ops the new operations
* @param <ChannelType> the type of the channel
* @return the updated registration
* @throws ClosedChannelException if the channel is already closed
* @throws IOException if any low level IO operation failed
*/
<ChannelType extends Channel> SelectorRegistration<HandleType, ChannelType> updateRegistration(SelectorRegistration<HandleType, ChannelType> registration, Set<SelectorRegistration.Operation> ops) throws IOException;
/**
* This operation selects IO events on this selector possibly blocking indefinitely until events happen.
* Depending on the implementation this operation may still return without any events, especially when
* {@link #wakeup()} is used.
*
* @return the events that occurred on channels registered to this selector since the last selection.
* @throws IOException if any low level IO operation failed
*/
List<IOEvent<HandleType>> select() throws IOException;
/**
* This operation selects IO events on this selector possibly blocking for the given {@link Duration} until events happen.
* Depending on the implementation this operation may still return earlier without any events, especially when
* {@link #wakeup()} is used.
*
* @param timeout the maximum time to wait for events
* @return the events that occurred on channels registered to this selector since the last selection.
* @throws IOException if any low level IO operation failed
*/
List<IOEvent<HandleType>> select(Duration timeout) throws IOException;
/**
* This operation selects IO events on this selector without blocking when no events exist.
*
* @return the events that occurred on channels registered to this selector since the last selection.
* @throws IOException if any low level IO operation failed
*/
List<IOEvent<HandleType>> selectNow() throws IOException;
/**
* This operation wakes up any blocking {@link #select()} or {@link #select(Duration)} calls, without actually having
* any IO events.
*
* @throws IOException if any low level IO operation failed
*/
void wakeup() throws IOException;
/**
* This operation closes the selector and frees all resources related to it.
*
* @throws IOException if any low level IO operation failed
*/
void close() throws IOException;
}