Class: Baykit::BayServer::Util::Selector
- Inherits:
-
Object
- Object
- Baykit::BayServer::Util::Selector
- Defined in:
- lib/baykit/bayserver/util/selector.rb
Overview
Like Selector class of Python
Constant Summary collapse
- OP_READ =
1- OP_WRITE =
2
Instance Attribute Summary collapse
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#lock ⇒ Object
readonly
Returns the value of attribute lock.
Instance Method Summary collapse
- #count ⇒ Object
- #get_op(ch) ⇒ Object
-
#initialize ⇒ Selector
constructor
A new instance of Selector.
- #modify(ch, op) ⇒ Object
- #register(ch, op) ⇒ Object
- #select(timeout = 0) ⇒ Object
- #unregister(ch) ⇒ Object
Constructor Details
#initialize ⇒ Selector
Returns a new instance of Selector.
15 16 17 18 |
# File 'lib/baykit/bayserver/util/selector.rb', line 15 def initialize @channels = {} @lock = Mutex.new() end |
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
12 13 14 |
# File 'lib/baykit/bayserver/util/selector.rb', line 12 def channels @channels end |
#lock ⇒ Object (readonly)
Returns the value of attribute lock.
13 14 15 |
# File 'lib/baykit/bayserver/util/selector.rb', line 13 def lock @lock end |
Instance Method Details
#count ⇒ Object
85 86 87 88 89 |
# File 'lib/baykit/bayserver/util/selector.rb', line 85 def count() @lock.synchronize do return @channels.length end end |
#get_op(ch) ⇒ Object
48 49 50 |
# File 'lib/baykit/bayserver/util/selector.rb', line 48 def get_op(ch) return @channels[ch] end |
#modify(ch, op) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/baykit/bayserver/util/selector.rb', line 34 def modify(ch, op) if op & OP_READ != 0 register_read(ch, @channels) else unregister_read(ch, @channels) end if op & OP_WRITE != 0 register_write(ch, @channels) else unregister_write(ch, @channels) end end |
#register(ch, op) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/baykit/bayserver/util/selector.rb', line 20 def register(ch, op) if op & OP_READ != 0 register_read(ch, @channels) end if op & OP_WRITE != 0 register_write(ch, @channels) end end |
#select(timeout = 0) ⇒ Object
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 |
# File 'lib/baykit/bayserver/util/selector.rb', line 52 def select(timeout = 0) except_list = [] read_list = [] write_list = [] @lock.synchronize do @channels.keys().each do |ch| if (@channels[ch] & OP_READ) != 0 read_list << ch end if (@channels[ch] & OP_WRITE) != 0 write_list << ch end end end selected_read_list, selected_write_list = Kernel.select(read_list, write_list, except_list, timeout) result = {} if selected_read_list selected_read_list.each do |ch| register_read(ch, result) end end if selected_write_list selected_write_list.each do |ch| register_write(ch, result) end end return result end |
#unregister(ch) ⇒ Object
29 30 31 32 |
# File 'lib/baykit/bayserver/util/selector.rb', line 29 def unregister(ch) unregister_read(ch, @channels) unregister_write(ch, @channels) end |