Class: Pwrake::NBIO::Handler

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

Overview


Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader, writer, hostname = nil) ⇒ Handler

Returns a new instance of Handler.



471
472
473
474
475
476
477
478
# File 'lib/pwrake/nbio.rb', line 471

def initialize(reader,writer,hostname=nil)
  @writer = writer
  @reader = reader
  @host = hostname
  @reader.host = @host
  @writer.host = @host
  @exited = false
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



479
480
481
# File 'lib/pwrake/nbio.rb', line 479

def host
  @host
end

#readerObject (readonly)

Returns the value of attribute reader.



479
480
481
# File 'lib/pwrake/nbio.rb', line 479

def reader
  @reader
end

#writerObject (readonly)

Returns the value of attribute writer.



479
480
481
# File 'lib/pwrake/nbio.rb', line 479

def writer
  @writer
end

Class Method Details

.exit(hdl_set) ⇒ Object



545
546
547
548
549
550
551
# File 'lib/pwrake/nbio.rb', line 545

def self.exit(hdl_set)
  hdl_set.each do |hdl|
    Fiber.new do
      hdl.exit
    end.resume
  end
end

.kill(hdl_set, sig) ⇒ Object



537
538
539
540
541
542
543
# File 'lib/pwrake/nbio.rb', line 537

def self.kill(hdl_set,sig)
  hdl_set.each do |hdl|
    Fiber.new do
      hdl.put_kill(sig)
    end.resume
  end
end

Instance Method Details

#exitObject



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/pwrake/nbio.rb', line 498

def exit
  iow = @writer.io
  Log.debug "Handler#exit iow=#{iow.inspect}" if defined? Log
  if @exited
    Log.debug "Handler<##{object_id}#exit multiple called" if defined? Log
    bt = caller.join("\n ")
    Log.debug "Handler#exit bt=\n #{bt}" if defined? Log
    return
  end
  @exited = true
  exit_msg = "exited"
  begin
    @writer.put_line "exit"
  rescue Errno::EPIPE => e
    m = "#{e} in #{self.class}.exit iow=#{iow.inspect}"
    if defined? Log
      Log.warn m
    else
      $stderr.puts m
    end
    return
  end
  Log.debug "Handler#exit: end: @writer.put_line \"exit\"" if defined? Log
  #
  if @reader.class == Reader # MultiReader not work
    while line = @reader.get_line
      # here might receive "retire:0" from branch...
      line.chomp!
      Log.debug "Handler#exit: #{line} host=#{@host}" if defined? Log
      return if line == exit_msg
    end
  end
end

#get_lineObject



481
482
483
# File 'lib/pwrake/nbio.rb', line 481

def get_line
  @reader.get_line
end

#haltObject



532
533
534
535
# File 'lib/pwrake/nbio.rb', line 532

def halt
  @writer.halt
  @reader.halt
end

#put_exitObject



494
495
496
# File 'lib/pwrake/nbio.rb', line 494

def put_exit
  @writer.put_line "exit"
end

#put_kill(sig = "INT") ⇒ Object



489
490
491
492
# File 'lib/pwrake/nbio.rb', line 489

def put_kill(sig="INT")
  @writer.io.write("kill:#{sig}\n")
  @writer.io.flush
end

#put_line(s) ⇒ Object



485
486
487
# File 'lib/pwrake/nbio.rb', line 485

def put_line(s)
  @writer.put_line(s)
end