Class: Sprout::FDBBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/sprout/tasks/fdb_task.rb

Overview

A buffer that provides clean blocking support for the fdb command shell

Constant Summary collapse

PLAYER_TERMINATED =
'Player session terminated'
EXIT_PROMPT =
'The program is running.  Exit anyway? (y or n)'
PROMPT =
'(fdb) '
QUIT =
'quit'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exe, output, user_input = nil) ⇒ FDBBuffer

The constructor expects a buffered input and output



505
506
507
508
509
510
511
512
513
# File 'lib/sprout/tasks/fdb_task.rb', line 505

def initialize(exe, output, user_input=nil)
  @output = output
  @prompted = false
  @faulted = false
  @user_input = user_input
  @found_search = false
  @pending_expression = nil
  listen exe
end

Instance Attribute Details

#kill_on_fault=(value) ⇒ Object (writeonly)

Sets the attribute kill_on_fault

Parameters:

  • value

    the value to set the attribute kill_on_fault to.



497
498
499
# File 'lib/sprout/tasks/fdb_task.rb', line 497

def kill_on_fault=(value)
  @kill_on_fault = value
end

#test_result_closingObject

Returns the value of attribute test_result_closing.



496
497
498
# File 'lib/sprout/tasks/fdb_task.rb', line 496

def test_result_closing
  @test_result_closing
end

#test_result_fileObject

:nodoc:



494
495
496
# File 'lib/sprout/tasks/fdb_task.rb', line 494

def test_result_file
  @test_result_file
end

#test_result_preludeObject

Returns the value of attribute test_result_prelude.



495
496
497
# File 'lib/sprout/tasks/fdb_task.rb', line 495

def test_result_prelude
  @test_result_prelude
end

Instance Method Details

#clean_test_result(result) ⇒ Object



629
630
631
# File 'lib/sprout/tasks/fdb_task.rb', line 629

def clean_test_result(result)
  return result.gsub(/^\[trace\]\s/m, '')
end

#create_input(exe) ⇒ Object



523
524
525
# File 'lib/sprout/tasks/fdb_task.rb', line 523

def create_input(exe)
  ProcessRunner.new("#{exe}")
end

#fault_found?(message) ⇒ Boolean

Returns:

  • (Boolean)


624
625
626
627
# File 'lib/sprout/tasks/fdb_task.rb', line 624

def fault_found?(message)
  match = message.match(/\[Fault\]\s.*,.*$/) 
  return !match.nil?
end

#joinObject

Block for the life of the input process



642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/sprout/tasks/fdb_task.rb', line 642

def join
  puts ">> Entering FDB interactive mode, type 'help' for more info."
  print PROMPT
  $stdout.flush

  t = Thread.new {
    while true do
      msg = user_input.gets.chomp!
      @input.puts msg
      wait_for_prompt
    end
  }

  @listener.join
end

#killObject

Kill the buffer



666
667
668
# File 'lib/sprout/tasks/fdb_task.rb', line 666

def kill
  @listener.kill
end

#kill_on_fault?Boolean

Returns:

  • (Boolean)


515
516
517
# File 'lib/sprout/tasks/fdb_task.rb', line 515

def kill_on_fault?
  @kill_on_fault
end

#listen(exe) ⇒ Object

Listen for messages from the input process



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/sprout/tasks/fdb_task.rb', line 536

def listen(exe)
  @input = nil
  @listener = Thread.new do
    @input = create_input(exe)
    def puts(msg)
      $stdout.puts msg
    end
    
    @inside_test_result = false
    full_output = ''
    test_result = ''
    char = ''
    line = ''
    while true do
      begin
        char = @input.readpartial 1
      rescue EOFError => e
        puts "End of File - Exiting Now"
        @prompted = true
        break
      end

      if(char == "\n")
        if(@inside_test_result && !line.index(test_result_prelude))
          test_result << line + char
        end
        line = ''
      else
        line << char
        full_output << char
      end

      if(!@inside_test_result)
        @output.print char
        @output.flush
      end
      
      if(!test_result_prelude.nil? && line.index(test_result_prelude))
        test_result = ''
        @inside_test_result = true
      end
      
      if(@inside_test_result && line.index(test_result_closing))
        write_test_result(test_result)
        @inside_test_result = false
        Thread.new {
          write("\n")
          write('y')
          write('kill')
          write('y')
          write('quit')
        }
      end

      if(line == PROMPT || line.match(/\(y or n\) $/))
        full_output_cache = full_output
        line = ''
        full_output = ''
        @prompted = true
        if(should_kill?(full_output_cache))
          Thread.new {
            wait_for_prompt
            write('info stack') # Output the full stack trace
            write('info locals') # Output local variables
            write('kill') # Kill the running SWF file
            write('y') # Confirm killing SWF
            write('quit') # Quit FDB safely
          }
        end

      elsif(@pending_expression && line.match(/#{@pending_expression}/))
        @found_search = true
        @pending_expression = nil
      elsif(line == PLAYER_TERMINATED)
        puts ""
        puts "Closed SWF Connection - Exiting Now"
        @prompted = true
        break
      end
    end
  end
  
end

#should_kill?(message) ⇒ Boolean

Returns:

  • (Boolean)


620
621
622
# File 'lib/sprout/tasks/fdb_task.rb', line 620

def should_kill?(message)
  return (@kill_on_fault && fault_found?(message))
end

#sleep_until(str) ⇒ Object



527
528
529
530
531
532
533
# File 'lib/sprout/tasks/fdb_task.rb', line 527

def sleep_until(str)
  @found_search = false
  @pending_expression = str
  while !@found_search do
    sleep(0.2)
  end
end

#user_inputObject



519
520
521
# File 'lib/sprout/tasks/fdb_task.rb', line 519

def user_input
  @user_input ||= $stdin
end

#wait_for_promptObject

Block until prompted returns true



659
660
661
662
663
# File 'lib/sprout/tasks/fdb_task.rb', line 659

def wait_for_prompt
  while !@prompted do
    sleep(0.2)
  end
end

#write(msg) ⇒ Object

Send a message to the buffer input and reset the prompted flag to false



671
672
673
674
675
676
677
678
679
# File 'lib/sprout/tasks/fdb_task.rb', line 671

def write(msg)
  @prompted = false
  @input.puts msg
  print msg + "\n"
  $stdout.flush
  if(msg != "c" && msg != "continue")
    wait_for_prompt
  end
end

#write_test_result(result) ⇒ Object



633
634
635
636
637
638
639
# File 'lib/sprout/tasks/fdb_task.rb', line 633

def write_test_result(result)
  result = clean_test_result result
  FileUtils.makedirs(File.dirname(test_result_file))
  File.open(test_result_file, File::CREAT|File::TRUNC|File::RDWR) do |f|
    f.puts(result)
  end
end