Module: Messed::Tasks::Console::KeyboardHandler

Includes:
EM::Protocols::LineText2
Defined in:
lib/messed/tasks/console.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



8
9
10
# File 'lib/messed/tasks/console.rb', line 8

def environment
  @environment
end

#rootObject

Returns the value of attribute root.



8
9
10
# File 'lib/messed/tasks/console.rb', line 8

def root
  @root
end

Instance Method Details

#log_outgoing(responses) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/messed/tasks/console.rb', line 10

def log_outgoing(responses)
  if responses.empty?
    puts "No responses"
  else
    puts "Responding ... #{responses.map{|r| "`#{r.body}'"}.join(', ')}"
  end
end

#post_initObject



23
24
25
26
# File 'lib/messed/tasks/console.rb', line 23

def post_init
  print_banner
  prompt
end


28
29
30
31
32
# File 'lib/messed/tasks/console.rb', line 28

def print_banner
  puts "?  help"
  puts "!q quit"
  puts "Eventhing else is a message"
end

#promptObject



18
19
20
21
# File 'lib/messed/tasks/console.rb', line 18

def prompt
  $stdout << ">> "
  $stdout.flush
end

#receive_line(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/messed/tasks/console.rb', line 34

def receive_line(data)
  case data.strip
  when '!q'
    EM.stop_event_loop
    exit
  when '?'
    print_banner
  else
    pid = EM.fork_reactor do
      Booter.new(root, :environment => environment, :supress_banner => true) do |booter|
        Messed::Logger.instance.setup_logger(::Logger.new(STDOUT), :debug)
        log_outgoing(booter.application.process(booter.application.message_class.new(data.strip)))
        EM.stop_event_loop
      end
    end
    Process.wait(pid)
  end
  prompt
end