Class: Terminus::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/terminus/connector.rb

Constant Summary collapse

RECV_SIZE =
1024
BIND_TIMEOUT =
5

Instance Method Summary collapse

Constructor Details

#initialize(browser, timeout = BIND_TIMEOUT) ⇒ Connector

Returns a new instance of Connector.



32
33
34
35
36
37
38
# File 'lib/terminus/connector.rb', line 32

def initialize(browser, timeout = BIND_TIMEOUT)
  @browser = browser
  @skips   = 0
  @server  = start_server
  @timeout = timeout
  reset
end

Instance Method Details

#closeObject



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

def close
  [@server, @socket].compact.each do |s|
    s.close_read
    s.close_write
  end
end

#connected?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/terminus/connector.rb', line 46

def connected?
  not @socket.nil?
end

#drain_socketObject



68
69
70
# File 'lib/terminus/connector.rb', line 68

def drain_socket
  @closing = true if @socket
end

#portObject



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

def port
  @server.addr[1]
end

#request(message) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/terminus/connector.rb', line 54

def request(message)
  @browser.debug(:send, @browser.id, message)
  accept unless connected?
  @driver.text(message)
  true while @closing && receive
  result = receive
  @browser.debug(:recv, @browser.id, result)
  reset if result.nil?
  result
rescue Errno::EBADF, Errno::ECONNRESET, Errno::EPIPE, Errno::EWOULDBLOCK
  reset
  nil
end

#resetObject



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

def reset
  @closing = false
  @driver  = nil
  @socket  = nil
end

#write(data) ⇒ Object



79
80
81
# File 'lib/terminus/connector.rb', line 79

def write(data)
  @socket.write(data)
end