Class: Tnnl::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/tnnl/process.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid, name) ⇒ Process

Returns a new instance of Process.



36
37
38
39
# File 'lib/tnnl/process.rb', line 36

def initialize(pid, name)
  @pid = pid.to_i
  @name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/tnnl/process.rb', line 3

def name
  @name
end

#pidObject

Returns the value of attribute pid.



3
4
5
# File 'lib/tnnl/process.rb', line 3

def pid
  @pid
end

Class Method Details

.kill_allObject



30
31
32
# File 'lib/tnnl/process.rb', line 30

def kill_all
  list.each(&:kill)
end

.kill_several(*to_kill) ⇒ Object



24
25
26
27
28
# File 'lib/tnnl/process.rb', line 24

def kill_several(*to_kill)
  list.each_with_index do |process, i|
    process.kill if to_kill.include?(i+1)
  end
end

.listObject

Returns an array of instances of Tnnl::Process representing each of the currently open SSH tunnels created by Tnnl on this machine.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tnnl/process.rb', line 8

def list
  # Grep the process list to get processes with "tnnl" somewhere in the 
  # name. Format the output from ps for easier parsing.
  list = `ps -eo pid,comm | grep tnnl`.split(/\n/)

  # Transform each string into an instance of Tnnl::Process.
  processes = list.map do |line|
    pid, name = line.strip.split(' ')
    self.new(pid, name)
  end

  # Remove any processes we might have found that don't conform to our 
  # naming convention.
  processes.select { |p| p.name =~ /^tnnl\[.*\]$/ }
end

Instance Method Details

#killObject



41
42
43
# File 'lib/tnnl/process.rb', line 41

def kill
  ::Process.kill('INT', pid)
end

#to_sObject



45
46
47
48
49
# File 'lib/tnnl/process.rb', line 45

def to_s
   = name.scan(/\[(.*)\]/).last.first
  local_port, host, remote_port = .split(':')
  "localhost:#{local_port}  ==>  #{host}:#{remote_port}"
end