Class: Terminus::Controller

Inherits:
Object
  • Object
show all
Includes:
Timeouts
Defined in:
lib/terminus/controller.rb

Constant Summary

Constants included from Timeouts

Timeouts::TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Timeouts

#wait_with_timeout

Constructor Details

#initializeController

Returns a new instance of Controller.



7
8
9
10
11
12
# File 'lib/terminus/controller.rb', line 7

def initialize
  @connected = false
  @browsers = {}
  @ping_callbacks = []
  trap('INT') { exit }
end

Instance Attribute Details

#dock_hostObject

Returns the value of attribute dock_host.



5
6
7
# File 'lib/terminus/controller.rb', line 5

def dock_host
  @dock_host
end

Instance Method Details

#browser(id = nil) ⇒ Object



14
15
16
17
# File 'lib/terminus/controller.rb', line 14

def browser(id = nil)
  return @browser if id.nil?
  @browsers[id] ||= Browser.new(self)
end

#browser=(params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/terminus/controller.rb', line 19

def browser=(params)
  @browser = case params
    when :docked then @browsers.values.find { |b| b.docked? }
    when Browser then params
    when Hash then
      wait_with_timeout(:named_window) do
        @browsers.values.find { |b| b.name == params[:name] }
      end
  end
end

#drop_browser(browser) ⇒ Object



30
31
32
33
# File 'lib/terminus/controller.rb', line 30

def drop_browser(browser)
  @browsers.delete(browser.id)
  @browser = nil if @browser == browser
end

#ensure_browserObject



35
36
37
38
# File 'lib/terminus/controller.rb', line 35

def ensure_browser
  ensure_connection
  wait_with_timeout(:browser) { @browser }
end

#ensure_docked_browserObject



40
41
42
43
# File 'lib/terminus/controller.rb', line 40

def ensure_docked_browser
  ensure_connection
  wait_with_timeout(:docked_browser) { @browsers.any? { |id,b| b.docked? } }
end

#messengerObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/terminus/controller.rb', line 45

def messenger
  Terminus.ensure_reactor_running
  return @messenger if defined?(@messenger)
  
  @messenger = Faye::Client.new(Terminus.endpoint)
  
  @messenger.subscribe('/terminus/ping',    &method(:accept_ping))
  @messenger.subscribe('/terminus/results', &method(:accept_result))
  @messenger
end

#return_to_dockObject



56
57
58
# File 'lib/terminus/controller.rb', line 56

def return_to_dock
  @browsers.each { |id, b| b.return_to_dock }
end