Class: Distributor::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/distributor/connector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnector

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

#connectionsObject (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

#listenObject



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