Class: Qcmd::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/qcmd/network.rb

Constant Summary collapse

BROWSE_TIMEOUT =
8

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.browse_threadObject

Returns the value of attribute browse_thread.



8
9
10
# File 'lib/qcmd/network.rb', line 8

def browse_thread
  @browse_thread
end

.machinesObject

Returns the value of attribute machines.



8
9
10
# File 'lib/qcmd/network.rb', line 8

def machines
  @machines
end

Class Method Details

.browseObject

browse can be used alone to populate the machines list



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/qcmd/network.rb', line 11

def browse
  self.machines = []
  self.browse_thread = Thread.start do
    DNSSD.browse! '_qlab._udp' do |b|
      DNSSD.resolve b.name, b.type, b.domain do |r|
        self.machines << Qcmd::Machine.new(b.name, r.target, r.port)
      end
    end
  end

  naps = 0
  changed = false
  previous = 0

  # sleep for BROWSE_TIMEOUT loops
  while naps < BROWSE_TIMEOUT
    sleep 0.2
    naps += 1

    if machines.size != previous
      previous = machines.size
    end
  end

  Thread.kill(browse_thread) if browse_thread.alive?
end

.browse_and_display(options = {}) ⇒ Object



58
59
60
61
62
63
# File 'lib/qcmd/network.rb', line 58

def browse_and_display options={}
  browse
  if !options[:machine_given] || (options[:machine_given] && !find(options[:machine_name]).nil?)
    display options
  end
end

.display(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/qcmd/network.rb', line 38

def display options={}
  longest = machines.map {|m| m.name.size}.max

  Qcmd.print
  Qcmd.print "Found #{ machines.size } QLab machine#{ machines.size == 1 ? '' : 's'}"
  Qcmd.print

  machines.each_with_index do |machine, n|
    if Qcmd.debug?
      Qcmd.print "#{ n + 1 }. %-#{ longest + 2 }s %s" % [machine.name, machine.client_string]
    else
      Qcmd.print "#{ n + 1 }. %-#{ longest + 2 }s" % [machine.name]
    end
  end

  Qcmd.print
  Qcmd.print 'type `connect MACHINE` to connect to a machine'
  Qcmd.print
end

.find(machine_name) ⇒ Object



65
66
67
# File 'lib/qcmd/network.rb', line 65

def find machine_name
  machines.find {|m| m.name == machine_name}
end

.namesObject



69
70
71
# File 'lib/qcmd/network.rb', line 69

def names
  machines.map(&:name)
end