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 Method Summary collapse

Methods included from Timeouts

#wait_with_timeout

Constructor Details

#initializeController

Returns a new instance of Controller.



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

def initialize
  @connected    = false
  @browsers     = {}
  @host_aliases = {}
end

Instance Method Details

#browser(id = nil) ⇒ Object



12
13
14
15
# File 'lib/terminus/controller.rb', line 12

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

#browser=(params) ⇒ Object



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

def browser=(params)
  ensure_connection
  return @browser = params if Browser === params
  @browser = wait_with_timeout(:selected_browser) do
    @browsers.values.find { |b| b === params }
  end
end

#browsersObject



17
18
19
# File 'lib/terminus/controller.rb', line 17

def browsers
  @browsers.values
end

#drop_browser(browser) ⇒ Object



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

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

#ensure_browsers(n = 1) ⇒ Object



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

def ensure_browsers(n = 1)
  ensure_connection
  wait_with_timeout(:browsers) { @browsers.size >= n }
end

#messengerObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/terminus/controller.rb', line 39

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



50
51
52
# File 'lib/terminus/controller.rb', line 50

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

#rewrite_local(url, dock_host) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/terminus/controller.rb', line 54

def rewrite_local(url, dock_host)
  uri = URI.parse(url)
  uri.host = '127.0.0.1' if uri.host == dock_host
  uri.path = '' if uri.path == '/'
  
  # 1.8.7 does not have Hash#key, and 1.9.2 gives warnings for #index
  remote_host = @host_aliases.respond_to?(:key) ?
                @host_aliases.key(Host.new(uri)) :
                @host_aliases.index(Host.new(uri))

  if remote_host
    uri.host = remote_host.host
    uri.port = remote_host.port
  end
  uri.host = dock_host if dock_host and uri.host =~ LOCALHOST
  uri
end

#rewrite_remote(url, dock_host = nil) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/terminus/controller.rb', line 72

def rewrite_remote(url, dock_host = nil)
  uri = URI.parse(url)
  return uri unless URI::HTTP === uri and uri.host !~ LOCALHOST and uri.host != dock_host
  server = boot(uri)
  uri.host, uri.port = server.host, server.port
  uri
end