Class: Distributor::Connector
- Inherits:
-
Object
- Object
- Distributor::Connector
- Defined in:
- lib/distributor/connector.rb
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
Instance Method Summary collapse
- #close(io) ⇒ Object
- #handle(from, &handler) ⇒ Object
-
#initialize ⇒ Connector
constructor
A new instance of Connector.
- #listen ⇒ Object
- #on_close(from, &handler) ⇒ Object
Constructor Details
#initialize ⇒ Connector
Returns a new instance of Connector.
7 8 9 10 |
# File 'lib/distributor/connector.rb', line 7 def initialize @connections = {} @on_close = Hash.new { |hash,key| hash[key] = Array.new } end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
5 6 7 |
# File 'lib/distributor/connector.rb', line 5 def connections @connections end |
Instance Method Details
#close(io) ⇒ Object
32 33 34 35 |
# File 'lib/distributor/connector.rb', line 32 def close(io) @connections.delete(io) @on_close[io].each { |c| c.call(io) } end |
#handle(from, &handler) ⇒ Object
12 13 14 15 |
# File 'lib/distributor/connector.rb', line 12 def handle(from, &handler) return unless from @connections[from] = handler end |
#listen ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/distributor/connector.rb', line 21 def listen rs, ws = IO.select(@connections.keys, [], [], 1) (rs || []).each do |from| begin @on_close[from].each { |c| c.call(from) } if from.eof? @connections[from].call(from) rescue Errno::EIO end end end |
#on_close(from, &handler) ⇒ Object
17 18 19 |
# File 'lib/distributor/connector.rb', line 17 def on_close(from, &handler) @on_close[from] << handler end |