Class: CFTunnel

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

Direct Known Subclasses

CFConsole

Constant Summary collapse

HELPER_NAME =
"caldecott"
HELPER_APP =
File.expand_path("../helper-app", __FILE__)
HELPER_VERSION =

bump this AND the version info reported by HELPER_APP/server.rb this is to keep the helper in sync with any updates here

"0.0.4"
PORT_RANGE =
10

Instance Method Summary collapse

Constructor Details

#initialize(client, service, port = 10000) ⇒ CFTunnel

Returns a new instance of CFTunnel.



15
16
17
18
19
# File 'lib/tunnel/tunnel.rb', line 15

def initialize(client, service, port = 10000)
  @client = client
  @service = service
  @port = port
end

Instance Method Details

#open!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tunnel/tunnel.rb', line 21

def open!
  if helper
    auth = helper_auth

    unless helper_healthy?(auth)
      delete_helper
      auth = create_helper
    end
  else
    auth = create_helper
  end

  bind_to_helper if @service && !helper_already_binds?

  info = get_connection_info(auth)

  start_tunnel(info, auth)

  info
end

#pick_port!(port = @port) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tunnel/tunnel.rb', line 64

def pick_port!(port = @port)
  original = port

  PORT_RANGE.times do |n|
    begin
      TCPSocket.open("localhost", port)
      port += 1
    rescue
      return @port = port
    end
  end

  @port = grab_ephemeral_port
end

#wait_for_endObject



55
56
57
58
59
60
61
# File 'lib/tunnel/tunnel.rb', line 55

def wait_for_end
  if @local_tunnel_thread
    @local_tunnel_thread.join
  else
    raise "Tunnel wasn't started!"
  end
end

#wait_for_startObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tunnel/tunnel.rb', line 42

def wait_for_start
  10.times do |n|
    begin
      TCPSocket.open("localhost", @port).close
      return true
    rescue => e
      sleep 1
    end
  end

  raise "Could not connect to local tunnel."
end