nio4r 0.2.2-java → 0.3.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,136 +0,0 @@
1
- module NIO
2
- # Selectors monitor IO objects for events of interest
3
- class Selector
4
- java_import "java.nio.channels.Selector"
5
- java_import "java.nio.channels.SelectionKey"
6
-
7
- # Convert nio4r interest symbols to Java NIO interest ops
8
- def self.sym2iops(interest, channel)
9
- case interest
10
- when :r
11
- if channel.validOps & SelectionKey::OP_ACCEPT != 0
12
- SelectionKey::OP_ACCEPT
13
- else
14
- SelectionKey::OP_READ
15
- end
16
- when :w
17
- if channel.respond_to? :connected? and not channel.connected?
18
- SelectionKey::OP_CONNECT
19
- else
20
- SelectionKey::OP_WRITE
21
- end
22
- when :rw
23
- super(:r, channel) | super(:w, channel)
24
- else raise ArgumentError, "invalid interest type: #{interest}"
25
- end
26
- end
27
-
28
- # Convert Java NIO interest ops to the corresponding Ruby symbols
29
- def self.iops2sym(interest_ops)
30
- case interest_ops
31
- when SelectionKey::OP_READ, SelectionKey::OP_ACCEPT
32
- :r
33
- when SelectionKey::OP_WRITE, SelectionKey::OP_CONNECT
34
- :w
35
- when SelectionKey::OP_READ | SelectionKey::OP_WRITE
36
- :rw
37
- when 0 then nil
38
- else raise ArgumentError, "unknown interest op combination: 0x#{interest_ops.to_s(16)}"
39
- end
40
- end
41
-
42
- # Create a new NIO::Selector
43
- def initialize
44
- @java_selector = Selector.open
45
- @select_lock = Mutex.new
46
- end
47
-
48
- # Register interest in an IO object with the selector for the given types
49
- # of events. Valid event types for interest are:
50
- # * :r - is the IO readable?
51
- # * :w - is the IO writeable?
52
- # * :rw - is the IO either readable or writeable?
53
- def register(io, interest)
54
- java_channel = io.to_channel
55
- java_channel.configureBlocking(false)
56
- interest_ops = self.class.sym2iops(interest, java_channel)
57
-
58
- begin
59
- selector_key = java_channel.register @java_selector, interest_ops
60
- rescue NativeException => ex
61
- case ex.cause
62
- when java.lang.IllegalArgumentException
63
- raise ArgumentError, "invalid interest type for #{java_channel}: #{interest}"
64
- else raise
65
- end
66
- end
67
-
68
- NIO::Monitor.new(io, selector_key)
69
- end
70
-
71
- # Deregister the given IO object from the selector
72
- def deregister(io)
73
- key = io.to_channel.keyFor(@java_selector)
74
- return unless key
75
-
76
- monitor = key.attachment
77
- monitor.close
78
- monitor
79
- end
80
-
81
- # Is the given IO object registered with the selector?
82
- def registered?(io)
83
- key = io.to_channel.keyFor(@java_selector)
84
- return unless key
85
- !key.attachment.closed?
86
- end
87
-
88
- # Select which monitors are ready
89
- def select(timeout = nil)
90
- selected = []
91
- ready = select_each(timeout) { |monitor| selected << monitor }
92
- return unless ready
93
-
94
- selected
95
- end
96
-
97
- # Iterate across all selectable monitors
98
- def select_each(timeout = nil)
99
- @select_lock.synchronize do
100
- if timeout == 0
101
- # The Java NIO API thinks zero means you want to BLOCK FOREVER o_O
102
- # How about we don't block at all instead?
103
- ready = @java_selector.selectNow
104
- elsif timeout
105
- ready = @java_selector.select(timeout * 1000)
106
- else
107
- ready = @java_selector.select
108
- end
109
-
110
- return unless ready > 0 # timeout or wakeup
111
-
112
- @java_selector.selectedKeys.each { |key| yield key.attachment }
113
- @java_selector.selectedKeys.clear
114
-
115
- ready
116
- end
117
- end
118
-
119
- # Wake up the other thread that's currently blocking on this selector
120
- def wakeup
121
- raise IOError, "selector is closed" if closed?
122
- @java_selector.wakeup
123
- nil
124
- end
125
-
126
- # Close this selector
127
- def close
128
- @java_selector.close
129
- end
130
-
131
- # Is this selector closed?
132
- def closed?
133
- !@java_selector.isOpen
134
- end
135
- end
136
- end