Class: Pwrake::Handler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner, *io, &blk) ⇒ Handler

Returns a new instance of Handler.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pwrake/iomux/handler.rb', line 5

def initialize(runner,*io,&blk)
  if !runner.kind_of?(Runner)
    raise TypeError, "Argument must be Runner but #{runner.class}"
  end
  @runner = runner
  @channel = {}
  if !io.empty?
    if !block_given?
      @ior,@iow,@ioe = *io
    end
  else
    if block_given?
      @ior,w0 = IO.pipe
      @ioe,w1 = IO.pipe
      r2,@iow = IO.pipe
      yield(w0,w1,r2)
    end
  end
  if @ior.nil? && @iow.nil?
    raise "fail to initialize IO"
  end
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



29
30
31
# File 'lib/pwrake/iomux/handler.rb', line 29

def host
  @host
end

#ioeObject (readonly)

Returns the value of attribute ioe.



28
29
30
# File 'lib/pwrake/iomux/handler.rb', line 28

def ioe
  @ioe
end

#iorObject (readonly)

Returns the value of attribute ior.



28
29
30
# File 'lib/pwrake/iomux/handler.rb', line 28

def ior
  @ior
end

#iowObject (readonly)

Returns the value of attribute iow.



28
29
30
# File 'lib/pwrake/iomux/handler.rb', line 28

def iow
  @iow
end

#runnerObject (readonly)

Returns the value of attribute runner.



28
29
30
# File 'lib/pwrake/iomux/handler.rb', line 28

def runner
  @runner
end

Instance Method Details

#add_channel(chan) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/pwrake/iomux/handler.rb', line 31

def add_channel(chan)
  if !chan.kind_of?(Channel)
    raise TypeError, "Argument must be Channel but #{chan.class}"
  end
  @channel[chan.id] = chan
  @runner.add_handler(self)
end

#delete_channel(chan) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/pwrake/iomux/handler.rb', line 39

def delete_channel(chan)
  if !chan.kind_of?(Channel)
    raise TypeError, "Argument must be Channel but #{chan.class}"
  end
  @channel.delete(chan.id)
  if @channel.empty?
    @runner.delete_handler(self)
  end
end

#finishObject



77
78
79
80
81
# File 'lib/pwrake/iomux/handler.rb', line 77

def finish
  @channel.values.each do |chan|
    chan.finish
  end
end

#getsObject



105
106
107
# File 'lib/pwrake/iomux/handler.rb', line 105

def gets
  @ior.gets
end

#process_lineObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pwrake/iomux/handler.rb', line 49

def process_line
  if s = (@ior.eof?) ? nil : @ior.gets
    default_channel = @channel[nil]
    if @channel.size > ((default_channel) ? 1 : 0)
      if /^(\d+):(.*)$/ =~ s
        ch,line = $1,$2
        if chan = @channel[ch.to_i]
          return chan.run_fiber(line)
        else
          raise "No channel##{ch}"
        end
      end
    end
    if default_channel
      return default_channel.run_fiber(s.chomp)
    else
      raise "No default_channel, received: #{s.chomp}"
    end
  else
    # End of IO
    @channel.values.each do |chan|
      if chan.fiber
        chan.run_fiber(nil)
      end
    end
  end
end

#put_line(line) ⇒ Object

– writer



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pwrake/iomux/handler.rb', line 85

def put_line(line)
  begin
    @iow.print line.to_str+"\n"
    @iow.flush
    #Log.debug "Handler#put_line #{line.inspect} @iow=#{@iow.inspect}"
  rescue Errno::EPIPE => e
    if Rake.application.options.debug
      $stderr.puts "Errno::EPIPE in #{self.class}.put_line '#{line.chomp}'"
      $stderr.puts e.backtrace.join("\n")
    end
    Log.error "Errno::EPIPE in #{self.class}.put_line '#{line.chomp}'\n"+
      e.backtrace.join("\n")
    #raise e
  end
end

#puts(line) ⇒ Object



101
102
103
# File 'lib/pwrake/iomux/handler.rb', line 101

def puts(line)
  @iow.puts(line)
end

#wait_message(end_msg) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pwrake/iomux/handler.rb', line 109

def wait_message(end_msg)
  if line = @ior.gets
    line.chomp!
    m = "Handler#wait_message: #{line} host=#{@host}"
    if line == end_msg
      Log.debug m
    else
      Log.error m
    end
  else
    Log.error "Handler#wait_message: fail to read @ior"
  end
end