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



511
512
513
514
515
516
517
518
519
# File 'lib/sprout/tasks/fdb_task.rb', line 511

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.



503
504
505
# File 'lib/sprout/tasks/fdb_task.rb', line 503

def kill_on_fault=(value)
  @kill_on_fault = value
end

#runtime_exception_encounteredObject (readonly)

Returns the value of attribute runtime_exception_encountered.



502
503
504
# File 'lib/sprout/tasks/fdb_task.rb', line 502

def runtime_exception_encountered
  @runtime_exception_encountered
end

#test_result_closingObject

Returns the value of attribute test_result_closing.



500
501
502
# File 'lib/sprout/tasks/fdb_task.rb', line 500

def test_result_closing
  @test_result_closing
end

#test_result_fileObject

:nodoc:



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

def test_result_file
  @test_result_file
end

#test_result_preludeObject

Returns the value of attribute test_result_prelude.



499
500
501
# File 'lib/sprout/tasks/fdb_task.rb', line 499

def test_result_prelude
  @test_result_prelude
end

Instance Method Details

#clean_test_result(result) ⇒ Object



636
637
638
# File 'lib/sprout/tasks/fdb_task.rb', line 636

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

#create_input(exe) ⇒ Object



529
530
531
# File 'lib/sprout/tasks/fdb_task.rb', line 529

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

#fault_found?(message) ⇒ Boolean

Returns:

  • (Boolean)


631
632
633
634
# File 'lib/sprout/tasks/fdb_task.rb', line 631

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

#joinObject

Block for the life of the input process



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'lib/sprout/tasks/fdb_task.rb', line 649

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

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

  @listener.join
end

#killObject

Kill the buffer



675
676
677
# File 'lib/sprout/tasks/fdb_task.rb', line 675

def kill
  @listener.kill
end

#kill_on_fault?Boolean

Returns:

  • (Boolean)


521
522
523
# File 'lib/sprout/tasks/fdb_task.rb', line 521

def kill_on_fault?
  @kill_on_fault
end

#listen(exe) ⇒ Object

Listen for messages from the input process



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
619
620
621
622
623
624
625
# File 'lib/sprout/tasks/fdb_task.rb', line 542

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
            @runtime_exception_encountered = true
            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)


627
628
629
# File 'lib/sprout/tasks/fdb_task.rb', line 627

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

#sleep_until(str) ⇒ Object



533
534
535
536
537
538
539
# File 'lib/sprout/tasks/fdb_task.rb', line 533

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

#user_inputObject



525
526
527
# File 'lib/sprout/tasks/fdb_task.rb', line 525

def user_input
  @user_input ||= $stdin
end

#wait_for_promptObject

Block until prompted returns true



668
669
670
671
672
# File 'lib/sprout/tasks/fdb_task.rb', line 668

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



680
681
682
683
684
685
686
687
688
# File 'lib/sprout/tasks/fdb_task.rb', line 680

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



640
641
642
643
644
645
646
# File 'lib/sprout/tasks/fdb_task.rb', line 640

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