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
Direct Known Subclasses
Constant Summary collapse
- OP_READ =
1- OP_WRITE =
2
Instance Attribute Summary collapse
-
#io_op_map ⇒ Object
readonly
Returns the value of attribute io_op_map.
-
#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_sec = nil) ⇒ Object
- #unregister(ch) ⇒ Object
- #validate_channel(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 @io_op_map = {} @lock = Mutex.new() end |
Instance Attribute Details
#io_op_map ⇒ Object (readonly)
Returns the value of attribute io_op_map.
12 13 14 |
# File 'lib/baykit/bayserver/util/selector.rb', line 12 def io_op_map @io_op_map 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
56 57 58 59 60 |
# File 'lib/baykit/bayserver/util/selector.rb', line 56 def count @lock.synchronize do return @io_op_map.length end end |
#get_op(ch) ⇒ Object
51 52 53 54 |
# File 'lib/baykit/bayserver/util/selector.rb', line 51 def get_op(ch) validate_channel(ch) return @io_op_map[ch] end |
#modify(ch, op) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/baykit/bayserver/util/selector.rb', line 36 def modify(ch, op) validate_channel(ch) if op & OP_READ != 0 register_read(ch, @io_op_map) else unregister_read(ch, @io_op_map) end if op & OP_WRITE != 0 register_write(ch, @io_op_map) else unregister_write(ch, @io_op_map) end end |
#register(ch, op) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/baykit/bayserver/util/selector.rb', line 20 def register(ch, op) validate_channel(ch) if op & OP_READ != 0 register_read(ch, @io_op_map) end if op & OP_WRITE != 0 register_write(ch, @io_op_map) end end |
#select(timeout_sec = nil) ⇒ Object
62 63 64 |
# File 'lib/baykit/bayserver/util/selector.rb', line 62 def select(timeout_sec = nil) raise NotImplementedError end |
#unregister(ch) ⇒ Object
30 31 32 33 34 |
# File 'lib/baykit/bayserver/util/selector.rb', line 30 def unregister(ch) validate_channel(ch) unregister_read(ch, @io_op_map) unregister_write(ch, @io_op_map) end |
#validate_channel(ch) ⇒ Object
66 67 68 69 70 |
# File 'lib/baykit/bayserver/util/selector.rb', line 66 def validate_channel(ch) unless ch.is_a?(IO) || (defined?(OpenSSL::SSL::SSLSocket) && ch.is_a?(OpenSSL::SSL::SSLSocket)) raise ArgumentError end end |