Class: Pwrake::NBIO::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/nbio.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_class = IO) ⇒ Selector

Returns a new instance of Selector.



13
14
15
16
17
18
# File 'lib/pwrake/nbio.rb', line 13

def initialize(io_class=IO)
  @reader = {}
  @writer = {}
  @running = false
  @io_class = io_class
end

Instance Attribute Details

#readerObject (readonly)

Returns the value of attribute reader.



20
21
22
# File 'lib/pwrake/nbio.rb', line 20

def reader
  @reader
end

#writerObject (readonly)

Returns the value of attribute writer.



20
21
22
# File 'lib/pwrake/nbio.rb', line 20

def writer
  @writer
end

Instance Method Details

#add_reader(hdl) ⇒ Object



22
23
24
# File 'lib/pwrake/nbio.rb', line 22

def add_reader(hdl)
  @reader[hdl.io] = hdl
end

#add_writer(hdl) ⇒ Object



30
31
32
# File 'lib/pwrake/nbio.rb', line 30

def add_writer(hdl)
  @writer[hdl.io] = hdl
end

#clearObject



42
43
44
45
# File 'lib/pwrake/nbio.rb', line 42

def clear
  @reader.clear
  @writer.clear
end

#delete_reader(hdl) ⇒ Object



26
27
28
# File 'lib/pwrake/nbio.rb', line 26

def delete_reader(hdl)
  @reader.delete(hdl.io)
end

#delete_writer(hdl) ⇒ Object



34
35
36
# File 'lib/pwrake/nbio.rb', line 34

def delete_writer(hdl)
  @writer.delete(hdl.io)
end

#empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/pwrake/nbio.rb', line 38

def empty?
  @reader.empty? && @writer.empty?
end

#get_host(io) ⇒ Object

used to print an error message



54
55
56
57
# File 'lib/pwrake/nbio.rb', line 54

def get_host(io)
  hdl = @reader[io] || @writer[io]
  hdl.respond_to?(:host) ? hdl.host : nil
end

#haltObject



47
48
49
50
51
# File 'lib/pwrake/nbio.rb', line 47

def halt
  @running = false
  @writer.each_value{|w| w.halt}
  @reader.each_value{|r| r.halt}
end

#run(timeout = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pwrake/nbio.rb', line 59

def run(timeout=nil)
  @running = true
  init_heartbeat if timeout
  while @running && !empty?
    if $debug && defined? Log
      rd_insp = @reader.map{|k,v|
        "%s=>%s,%s" % [k.inspect,v.class.inspect,v.waiter.inspect]
      }.join(",")
      Log.debug "Selector#run:\n "+caller[0..1].join("\n ")+
        "\n @reader={#{rd_insp}}\n @writer.size=#{@writer.size}"
      $stderr.puts "Selector#run: "+caller[0]
    end
    run_select(timeout)
  end
ensure
  @running = false
  @hb_time = nil
end