Class: ProcessMuncher

Inherits:
Object
  • Object
show all
Defined in:
lib/replicant/process_muncher.rb

Instance Method Summary collapse

Constructor Details

#initialize(repl) ⇒ ProcessMuncher

Returns a new instance of ProcessMuncher.



3
4
5
# File 'lib/replicant/process_muncher.rb', line 3

def initialize(repl)
  @repl = repl
end

Instance Method Details

#find_pidObject



23
24
25
# File 'lib/replicant/process_muncher.rb', line 23

def find_pid
  process_table.invert[@repl.default_package]
end

#process_tableObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/replicant/process_muncher.rb', line 7

def process_table
  result = AdbCommand.new(@repl, "shell ps", :silent => true).execute
  if result.code > 0
    @signal_exit = true
    {}
  else
    processes = result.output
    # Parses something like:
    # u0_a27    1333  123   517564 18668 ffffffff b75a59eb S com.android.musicfx
    processes.lines.map do |pid_line|
      columns = pid_line.split
      [columns[1], columns[-1]]
    end.to_h
  end
end

#scan_pidObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/replicant/process_muncher.rb', line 27

def scan_pid
  @last_pid = nil
  t = Thread.new do 
    begin
      while @repl.default_package
        pid = find_pid
        t.exit if @signal_exit
        pid_changed = (pid && pid != @last_pid) || (pid.nil? && @last_pid)
        yield pid if block_given? && pid_changed
        @last_pid = pid
        sleep 2
      end
    rescue Exception => e
      puts e.inspect
      puts e.backtrace.join("\n")
      raise e
    end
  end
end