ed-precompiled_nio4r 2.7.4-x86_64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,176 @@
1
+ package org.nio4r;
2
+
3
+ import java.nio.channels.Channel;
4
+ import java.nio.channels.SelectableChannel;
5
+ import java.nio.channels.SelectionKey;
6
+
7
+ import org.jruby.Ruby;
8
+ import org.jruby.RubyClass;
9
+ import org.jruby.RubyIO;
10
+ import org.jruby.RubyObject;
11
+ import org.jruby.anno.JRubyMethod;
12
+ import org.jruby.runtime.ThreadContext;
13
+ import org.jruby.runtime.builtin.IRubyObject;
14
+
15
+ public class Monitor extends RubyObject {
16
+ private static final long serialVersionUID = -3733782997115074794L;
17
+ private transient SelectionKey key;
18
+ private RubyIO io;
19
+ private transient IRubyObject interests, selector, value, closed;
20
+
21
+ public Monitor(final Ruby ruby, RubyClass rubyClass) {
22
+ super(ruby, rubyClass);
23
+ }
24
+
25
+ @JRubyMethod
26
+ public IRubyObject initialize(ThreadContext context, IRubyObject selectable, IRubyObject interests, IRubyObject selector) {
27
+ this.io = RubyIO.convertToIO(context, selectable);
28
+ this.interests = interests;
29
+ this.selector = selector;
30
+
31
+ this.value = context.nil;
32
+ this.closed = context.getRuntime().getFalse();
33
+
34
+ return context.nil;
35
+ }
36
+
37
+ public void setSelectionKey(SelectionKey key) {
38
+ this.key = key;
39
+ key.attach(this);
40
+ }
41
+
42
+ @JRubyMethod
43
+ public IRubyObject io(ThreadContext context) {
44
+ return io;
45
+ }
46
+
47
+ @JRubyMethod
48
+ public IRubyObject selector(ThreadContext context) {
49
+ return selector;
50
+ }
51
+
52
+ @JRubyMethod(name = "interests")
53
+ public IRubyObject getInterests(ThreadContext context) {
54
+ return interests;
55
+ }
56
+
57
+ @JRubyMethod(name = "interests=")
58
+ public IRubyObject setInterests(ThreadContext context, IRubyObject interests) {
59
+ if(this.closed == context.getRuntime().getTrue()) {
60
+ throw context.getRuntime().newEOFError("monitor is closed");
61
+ }
62
+
63
+ Ruby ruby = context.getRuntime();
64
+ SelectableChannel channel = (SelectableChannel)io.getChannel();
65
+
66
+ if(interests != context.nil) {
67
+ key.interestOps(Nio4r.symbolToInterestOps(ruby, channel, interests));
68
+ } else {
69
+ key.interestOps(0);
70
+ }
71
+
72
+ this.interests = interests;
73
+
74
+ return this.interests;
75
+ }
76
+
77
+ @JRubyMethod(name = "add_interest")
78
+ public IRubyObject addInterest(ThreadContext context, IRubyObject interest) {
79
+ if(this.closed == context.getRuntime().getTrue()) {
80
+ throw context.getRuntime().newEOFError("monitor is closed");
81
+ }
82
+
83
+ Ruby ruby = context.getRuntime();
84
+ SelectableChannel channel = (SelectableChannel)io.getChannel();
85
+ int newInterestOps = key.interestOps() | Nio4r.symbolToInterestOps(ruby, channel, interest);
86
+
87
+ key.interestOps(newInterestOps);
88
+ this.interests = Nio4r.interestOpsToSymbol(ruby, newInterestOps);
89
+
90
+ return this.interests;
91
+ }
92
+
93
+ @JRubyMethod(name = "remove_interest")
94
+ public IRubyObject removeInterest(ThreadContext context, IRubyObject interest) {
95
+ if(this.closed == context.getRuntime().getTrue()) {
96
+ throw context.getRuntime().newEOFError("monitor is closed");
97
+ }
98
+
99
+ Ruby ruby = context.getRuntime();
100
+ SelectableChannel channel = (SelectableChannel)io.getChannel();
101
+ int newInterestOps = key.interestOps() & ~Nio4r.symbolToInterestOps(ruby, channel, interest);
102
+
103
+ key.interestOps(newInterestOps);
104
+ this.interests = Nio4r.interestOpsToSymbol(ruby, newInterestOps);
105
+
106
+ return this.interests;
107
+ }
108
+
109
+ @JRubyMethod
110
+ public IRubyObject readiness(ThreadContext context) {
111
+ if(!key.isValid())
112
+ return this.interests;
113
+ return Nio4r.interestOpsToSymbol(context.getRuntime(), key.readyOps());
114
+ }
115
+
116
+ @JRubyMethod(name = "readable?")
117
+ public IRubyObject isReadable(ThreadContext context) {
118
+ Ruby runtime = context.getRuntime();
119
+ if (!this.key.isValid())
120
+ return runtime.getTrue();
121
+ int readyOps = this.key.readyOps();
122
+
123
+ if((readyOps & SelectionKey.OP_READ) != 0 || (readyOps & SelectionKey.OP_ACCEPT) != 0) {
124
+ return runtime.getTrue();
125
+ } else {
126
+ return runtime.getFalse();
127
+ }
128
+ }
129
+
130
+ @JRubyMethod(name = {"writable?", "writeable?"})
131
+ public IRubyObject writable(ThreadContext context) {
132
+ Ruby runtime = context.getRuntime();
133
+ if (!this.key.isValid())
134
+ return runtime.getTrue();
135
+ int readyOps = this.key.readyOps();
136
+
137
+ if((readyOps & SelectionKey.OP_WRITE) != 0 || (readyOps & SelectionKey.OP_CONNECT) != 0) {
138
+ return runtime.getTrue();
139
+ } else {
140
+ return runtime.getFalse();
141
+ }
142
+ }
143
+
144
+ @JRubyMethod(name = "value")
145
+ public IRubyObject getValue(ThreadContext context) {
146
+ return this.value;
147
+ }
148
+
149
+ @JRubyMethod(name = "value=")
150
+ public IRubyObject setValue(ThreadContext context, IRubyObject obj) {
151
+ this.value = obj;
152
+ return context.nil;
153
+ }
154
+
155
+ @JRubyMethod
156
+ public IRubyObject close(ThreadContext context) {
157
+ return close(context, context.getRuntime().getTrue());
158
+ }
159
+
160
+ @JRubyMethod
161
+ public IRubyObject close(ThreadContext context, IRubyObject deregister) {
162
+ Ruby runtime = context.getRuntime();
163
+ this.closed = runtime.getTrue();
164
+
165
+ if(deregister == runtime.getTrue()) {
166
+ selector.callMethod(context, "deregister", io);
167
+ }
168
+
169
+ return context.nil;
170
+ }
171
+
172
+ @JRubyMethod(name = "closed?")
173
+ public IRubyObject isClosed(ThreadContext context) {
174
+ return this.closed;
175
+ }
176
+ }
@@ -0,0 +1,104 @@
1
+ package org.nio4r;
2
+
3
+ import java.nio.channels.SelectableChannel;
4
+ import java.nio.channels.SelectionKey;
5
+ import java.nio.channels.SocketChannel;
6
+
7
+ import org.jruby.Ruby;
8
+ import org.jruby.RubyClass;
9
+ import org.jruby.RubyModule;
10
+ import org.jruby.runtime.ObjectAllocator;
11
+ import org.jruby.runtime.load.Library;
12
+ import org.jruby.runtime.builtin.IRubyObject;
13
+
14
+ import org.nio4r.ByteBuffer;
15
+ import org.nio4r.Monitor;
16
+ import org.nio4r.Selector;
17
+
18
+ public class Nio4r implements Library {
19
+ private Ruby ruby;
20
+
21
+ public void load(final Ruby ruby, boolean bln) {
22
+ this.ruby = ruby;
23
+
24
+ RubyModule nio = ruby.defineModule("NIO");
25
+
26
+ RubyClass selector = ruby.defineClassUnder("Selector", ruby.getObject(), new ObjectAllocator() {
27
+ public IRubyObject allocate(Ruby ruby, RubyClass rc) {
28
+ return new Selector(ruby, rc);
29
+ }
30
+ }, nio);
31
+
32
+ selector.defineAnnotatedMethods(Selector.class);
33
+
34
+ RubyClass monitor = ruby.defineClassUnder("Monitor", ruby.getObject(), new ObjectAllocator() {
35
+ public IRubyObject allocate(Ruby ruby, RubyClass rc) {
36
+ return new Monitor(ruby, rc);
37
+ }
38
+ }, nio);
39
+
40
+ monitor.defineAnnotatedMethods(Monitor.class);
41
+
42
+ RubyClass byteBuffer = ruby.defineClassUnder("ByteBuffer", ruby.getObject(), new ObjectAllocator() {
43
+ public IRubyObject allocate(Ruby ruby, RubyClass rc) {
44
+ return new ByteBuffer(ruby, rc);
45
+ }
46
+ }, nio);
47
+
48
+ byteBuffer.defineAnnotatedMethods(ByteBuffer.class);
49
+ byteBuffer.includeModule(ruby.getEnumerable());
50
+
51
+ ruby.defineClassUnder("OverflowError", ruby.getIOError(), ruby.getIOError().getAllocator(), byteBuffer);
52
+ ruby.defineClassUnder("UnderflowError", ruby.getIOError(), ruby.getIOError().getAllocator(), byteBuffer);
53
+ ruby.defineClassUnder("MarkUnsetError", ruby.getIOError(), ruby.getIOError().getAllocator(), byteBuffer);
54
+ }
55
+
56
+ public static int symbolToInterestOps(Ruby ruby, SelectableChannel channel, IRubyObject interest) {
57
+ if(interest == ruby.newSymbol("r")) {
58
+ if((channel.validOps() & SelectionKey.OP_ACCEPT) != 0) {
59
+ return SelectionKey.OP_ACCEPT;
60
+ } else {
61
+ return SelectionKey.OP_READ;
62
+ }
63
+ } else if(interest == ruby.newSymbol("w")) {
64
+ if(channel instanceof SocketChannel && !((SocketChannel)channel).isConnected()) {
65
+ return SelectionKey.OP_CONNECT;
66
+ } else {
67
+ return SelectionKey.OP_WRITE;
68
+ }
69
+ } else if(interest == ruby.newSymbol("rw")) {
70
+ int interestOps = 0;
71
+
72
+ /* nio4r emulates the POSIX behavior, which is sloppy about allowed modes */
73
+ if((channel.validOps() & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0) {
74
+ interestOps |= symbolToInterestOps(ruby, channel, ruby.newSymbol("r"));
75
+ }
76
+
77
+ if((channel.validOps() & (SelectionKey.OP_WRITE | SelectionKey.OP_CONNECT)) != 0) {
78
+ interestOps |= symbolToInterestOps(ruby, channel, ruby.newSymbol("w"));
79
+ }
80
+
81
+ return interestOps;
82
+ } else {
83
+ throw ruby.newArgumentError("invalid interest type: " + interest);
84
+ }
85
+ }
86
+
87
+ public static IRubyObject interestOpsToSymbol(Ruby ruby, int interestOps) {
88
+ switch(interestOps) {
89
+ case SelectionKey.OP_READ:
90
+ case SelectionKey.OP_ACCEPT:
91
+ return ruby.newSymbol("r");
92
+ case SelectionKey.OP_WRITE:
93
+ case SelectionKey.OP_CONNECT:
94
+ return ruby.newSymbol("w");
95
+ case SelectionKey.OP_READ | SelectionKey.OP_CONNECT:
96
+ case SelectionKey.OP_READ | SelectionKey.OP_WRITE:
97
+ return ruby.newSymbol("rw");
98
+ case 0:
99
+ return ruby.getNil();
100
+ default:
101
+ throw ruby.newArgumentError("unknown interest op combination");
102
+ }
103
+ }
104
+ }
@@ -0,0 +1,297 @@
1
+ package org.nio4r;
2
+
3
+ import java.util.Iterator;
4
+ import java.util.Map;
5
+ import java.util.HashMap;
6
+ import java.io.IOException;
7
+ import java.nio.channels.Channel;
8
+ import java.nio.channels.SelectableChannel;
9
+ import java.nio.channels.SelectionKey;
10
+
11
+ import org.jruby.Ruby;
12
+ import org.jruby.RubyArray;
13
+ import org.jruby.RubyClass;
14
+ import org.jruby.RubyIO;
15
+ import org.jruby.RubyNumeric;
16
+ import org.jruby.RubyObject;
17
+ import org.jruby.anno.JRubyMethod;
18
+ import org.jruby.runtime.Block;
19
+ import org.jruby.runtime.ThreadContext;
20
+ import org.jruby.runtime.builtin.IRubyObject;
21
+ import org.jruby.util.io.OpenFile;
22
+
23
+ public class Selector extends RubyObject {
24
+ private static final long serialVersionUID = -14562818539414873L;
25
+ private transient java.nio.channels.Selector selector;
26
+ private HashMap<SelectableChannel,SelectionKey> cancelledKeys;
27
+ private volatile boolean wakeupFired;
28
+
29
+ public Selector(final Ruby ruby, RubyClass rubyClass) {
30
+ super(ruby, rubyClass);
31
+ }
32
+
33
+ @JRubyMethod(meta = true)
34
+ public static IRubyObject backends(ThreadContext context, IRubyObject self) {
35
+ return context.runtime.newArray(context.runtime.newSymbol("java"));
36
+ }
37
+
38
+ @JRubyMethod
39
+ public IRubyObject initialize(ThreadContext context) {
40
+ initialize(context, context.runtime.newSymbol("java"));
41
+ return context.nil;
42
+ }
43
+
44
+ @JRubyMethod
45
+ public IRubyObject initialize(ThreadContext context, IRubyObject backend) {
46
+ if(backend != context.runtime.newSymbol("java") && !backend.isNil()) {
47
+ throw context.runtime.newArgumentError(":java is the only supported backend");
48
+ }
49
+
50
+ this.cancelledKeys = new HashMap<SelectableChannel,SelectionKey>();
51
+ this.wakeupFired = false;
52
+
53
+ try {
54
+ this.selector = java.nio.channels.Selector.open();
55
+ } catch(IOException ie) {
56
+ throw context.runtime.newIOError(ie.getLocalizedMessage());
57
+ }
58
+
59
+ return context.nil;
60
+ }
61
+
62
+ @JRubyMethod
63
+ public IRubyObject backend(ThreadContext context) {
64
+ return context.runtime.newSymbol("java");
65
+ }
66
+
67
+ @JRubyMethod
68
+ public IRubyObject close(ThreadContext context) {
69
+ try {
70
+ this.selector.close();
71
+ } catch(IOException ie) {
72
+ throw context.runtime.newIOError(ie.getLocalizedMessage());
73
+ }
74
+
75
+ return context.nil;
76
+ }
77
+
78
+ @JRubyMethod(name = "closed?")
79
+ public IRubyObject isClosed(ThreadContext context) {
80
+ Ruby runtime = context.getRuntime();
81
+ return this.selector.isOpen() ? runtime.getFalse() : runtime.getTrue();
82
+ }
83
+
84
+ @JRubyMethod(name = "empty?")
85
+ public IRubyObject isEmpty(ThreadContext context) {
86
+ Ruby runtime = context.getRuntime();
87
+ return this.selector.keys().isEmpty() ? runtime.getTrue() : runtime.getFalse();
88
+ }
89
+
90
+ @JRubyMethod
91
+ public IRubyObject register(ThreadContext context, IRubyObject io, IRubyObject interests) {
92
+ Ruby runtime = context.getRuntime();
93
+ Channel rawChannel = RubyIO.convertToIO(context, io).getChannel();
94
+
95
+ if(!this.selector.isOpen()) {
96
+ throw context.getRuntime().newIOError("selector is closed");
97
+ }
98
+
99
+ if(!(rawChannel instanceof SelectableChannel)) {
100
+ throw runtime.newArgumentError("not a selectable IO object");
101
+ }
102
+
103
+ SelectableChannel channel = (SelectableChannel)rawChannel;
104
+
105
+ try {
106
+ channel.configureBlocking(false);
107
+ } catch(IOException ie) {
108
+ throw runtime.newIOError(ie.getLocalizedMessage());
109
+ }
110
+
111
+ int interestOps = Nio4r.symbolToInterestOps(runtime, channel, interests);
112
+ SelectionKey key;
113
+
114
+ key = this.cancelledKeys.remove(channel);
115
+
116
+ if(key != null) {
117
+ key.interestOps(interestOps);
118
+ } else {
119
+ try {
120
+ key = channel.register(this.selector, interestOps);
121
+ } catch(java.lang.IllegalArgumentException ia) {
122
+ throw runtime.newArgumentError("mode not supported for this object: " + interests);
123
+ } catch(java.nio.channels.ClosedChannelException cce) {
124
+ throw context.runtime.newIOError(cce.getLocalizedMessage());
125
+ }
126
+ }
127
+
128
+ RubyClass monitorClass = runtime.getModule("NIO").getClass("Monitor");
129
+ Monitor monitor = (Monitor)monitorClass.newInstance(context, io, interests, this, null);
130
+ monitor.setSelectionKey(key);
131
+
132
+ return monitor;
133
+ }
134
+
135
+ @JRubyMethod
136
+ public IRubyObject deregister(ThreadContext context, IRubyObject io) {
137
+ Ruby runtime = context.getRuntime();
138
+ OpenFile file = RubyIO.convertToIO(context, io).getOpenFileInitialized();
139
+ if (file.fd() == null)
140
+ return context.nil;
141
+ Channel rawChannel = file.channel();
142
+
143
+ if(!(rawChannel instanceof SelectableChannel)) {
144
+ throw runtime.newArgumentError("not a selectable IO object");
145
+ }
146
+
147
+ SelectableChannel channel = (SelectableChannel)rawChannel;
148
+ SelectionKey key = channel.keyFor(this.selector);
149
+
150
+ if(key == null)
151
+ return context.nil;
152
+
153
+ Monitor monitor = (Monitor)key.attachment();
154
+ monitor.close(context, runtime.getFalse());
155
+ cancelledKeys.put(channel, key);
156
+
157
+ return monitor;
158
+ }
159
+
160
+ @JRubyMethod(name = "registered?")
161
+ public IRubyObject isRegistered(ThreadContext context, IRubyObject io) {
162
+ Ruby runtime = context.getRuntime();
163
+ Channel rawChannel = RubyIO.convertToIO(context, io).getChannel();
164
+
165
+ if(!(rawChannel instanceof SelectableChannel)) {
166
+ throw runtime.newArgumentError("not a selectable IO object");
167
+ }
168
+
169
+ SelectableChannel channel = (SelectableChannel)rawChannel;
170
+ SelectionKey key = channel.keyFor(this.selector);
171
+
172
+ if(key == null)
173
+ return context.nil;
174
+
175
+
176
+ if(((Monitor)key.attachment()).isClosed(context) == runtime.getTrue()) {
177
+ return runtime.getFalse();
178
+ } else {
179
+ return runtime.getTrue();
180
+ }
181
+ }
182
+
183
+ @JRubyMethod
184
+ public synchronized IRubyObject select(ThreadContext context, Block block) {
185
+ return select(context, context.nil, block);
186
+ }
187
+
188
+ @JRubyMethod
189
+ public synchronized IRubyObject select(ThreadContext context, IRubyObject timeout, Block block) {
190
+ Ruby runtime = context.getRuntime();
191
+
192
+ if(!this.selector.isOpen()) {
193
+ throw context.getRuntime().newIOError("selector is closed");
194
+ }
195
+
196
+ this.wakeupFired = false;
197
+ int ready = doSelect(runtime, context, timeout);
198
+
199
+ /* Timeout */
200
+ if(ready <= 0 && !this.wakeupFired) {
201
+ return context.nil;
202
+ }
203
+
204
+ RubyArray<?> array = null;
205
+
206
+ if(!block.isGiven()) {
207
+ array = runtime.newArray(this.selector.selectedKeys().size());
208
+ }
209
+
210
+ Iterator<SelectionKey> selectedKeys = this.selector.selectedKeys().iterator();
211
+ while(selectedKeys.hasNext()) {
212
+ SelectionKey key = selectedKeys.next();
213
+ processKey(key);
214
+
215
+ selectedKeys.remove();
216
+
217
+ if(block.isGiven()) {
218
+ block.call(context, (IRubyObject)key.attachment());
219
+ } else {
220
+ array.add(key.attachment());
221
+ }
222
+ }
223
+
224
+ if(block.isGiven()) {
225
+ return RubyNumeric.int2fix(runtime, ready);
226
+ } else {
227
+ return array;
228
+ }
229
+ }
230
+
231
+ /* Run the selector */
232
+ private int doSelect(Ruby runtime, ThreadContext context, IRubyObject timeout) {
233
+ int result;
234
+
235
+ cancelKeys();
236
+ try {
237
+ context.getThread().beforeBlockingCall(context);
238
+ if(timeout.isNil()) {
239
+ result = this.selector.select();
240
+ } else {
241
+ double t = RubyNumeric.num2dbl(timeout);
242
+ if(t == 0) {
243
+ result = this.selector.selectNow();
244
+ } else if(t < 0) {
245
+ throw runtime.newArgumentError("time interval must be positive");
246
+ } else {
247
+ long timeoutMilliSeconds = (long)(t * 1000);
248
+ if(timeoutMilliSeconds == 0) {
249
+ result = this.selector.selectNow();
250
+ } else {
251
+ result = this.selector.select(timeoutMilliSeconds);
252
+ }
253
+ }
254
+ }
255
+ context.getThread().afterBlockingCall();
256
+ return result;
257
+ } catch(IOException ie) {
258
+ throw runtime.newIOError(ie.getLocalizedMessage());
259
+ }
260
+ }
261
+
262
+ /* Flush our internal buffer of cancelled keys */
263
+ private void cancelKeys() {
264
+ Iterator<Map.Entry<SelectableChannel, SelectionKey>> cancelledKeys = this.cancelledKeys.entrySet().iterator();
265
+ while(cancelledKeys.hasNext()) {
266
+ Map.Entry<SelectableChannel, SelectionKey> entry = cancelledKeys.next();
267
+ SelectionKey key = entry.getValue();
268
+ key.cancel();
269
+ cancelledKeys.remove();
270
+ }
271
+ }
272
+
273
+ // Remove connect interest from connected sockets
274
+ // See: http://stackoverflow.com/questions/204186/java-nio-select-returns-without-selected-keys-why
275
+ private void processKey(SelectionKey key) {
276
+ if(key.isValid() && (key.readyOps() & SelectionKey.OP_CONNECT) != 0) {
277
+ int interestOps = key.interestOps();
278
+
279
+ interestOps &= ~SelectionKey.OP_CONNECT;
280
+ interestOps |= SelectionKey.OP_WRITE;
281
+
282
+ key.interestOps(interestOps);
283
+ }
284
+ }
285
+
286
+ @JRubyMethod
287
+ public IRubyObject wakeup(ThreadContext context) {
288
+ if(!this.selector.isOpen()) {
289
+ throw context.getRuntime().newIOError("selector is closed");
290
+ }
291
+
292
+ this.wakeupFired = true;
293
+ this.selector.wakeup();
294
+
295
+ return context.nil;
296
+ }
297
+ }