Class: Caldecott::Client::HttpTunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/caldecott/client/http_tunnel.rb

Defined Under Namespace

Classes: Reader, Writer

Constant Summary collapse

MAX_RETRIES =
10

Instance Method Summary collapse

Constructor Details

#initialize(logger, url, dst_host, dst_port, auth_token) ⇒ HttpTunnel

Returns a new instance of HttpTunnel.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/caldecott/client/http_tunnel.rb', line 11

def initialize(logger, url, dst_host, dst_port, auth_token)
  @log, @auth_token = logger, auth_token
  @closing = false
  init_msg = ""

  # FIXME: why is this optional?
  if dst_host
    init_msg = { :host => dst_host, :port => dst_port }.to_json
  end

  start(url, init_msg)
end

Instance Method Details

#closeObject



42
43
44
45
46
47
48
# File 'lib/caldecott/client/http_tunnel.rb', line 42

def close
  return if @closing or @closed
  @closing = true
  @writer.close if @writer
  @reader.close if @reader
  stop
end

#onclose(&blk) ⇒ Object



29
30
31
32
# File 'lib/caldecott/client/http_tunnel.rb', line 29

def onclose(&blk)
  @onclose = blk
  @onclose.call if @closed
end

#onopen(&blk) ⇒ Object



24
25
26
27
# File 'lib/caldecott/client/http_tunnel.rb', line 24

def onopen(&blk)
  @onopen = blk
  @onopen.call if @opened
end

#onreceive(&blk) ⇒ Object



34
35
36
# File 'lib/caldecott/client/http_tunnel.rb', line 34

def onreceive(&blk)
  @onreceive = blk
end

#send_data(data) ⇒ Object



38
39
40
# File 'lib/caldecott/client/http_tunnel.rb', line 38

def send_data(data)
  @writer.send_data(data)
end

#start(base_uri, init_msg, attempts = MAX_RETRIES) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/caldecott/client/http_tunnel.rb', line 66

def start(base_uri, init_msg, attempts = MAX_RETRIES)
  if attempts <= 0
    trigger_on_close
    return
  end

  begin
    parsed_uri = Addressable::URI.parse(base_uri)
    parsed_uri.path = '/tunnels'

    @log.debug "post #{parsed_uri.to_s}"
    req = EM::HttpRequest.new(parsed_uri.to_s).post :body => init_msg, :head => { "Auth-Token" => @auth_token, "Content-Length" => init_msg.bytesize }

    req.callback do
      @log.debug "post #{parsed_uri.to_s} #{req.response_header.status}"
      unless [200, 201, 204].include?(req.response_header.status)
        start(base_uri, init_msg, attempts - 1)
      else
        resp = JSON.parse(req.response)

        parsed_uri.path = resp["path"]
        @tun_uri = parsed_uri.to_s

        parsed_uri.path = resp["path_out"]
        @reader = Reader.new(@log, parsed_uri.to_s, self, @auth_token)

        parsed_uri.path = resp["path_in"]
        @writer = Writer.new(@log, parsed_uri.to_s, self, @auth_token)
        trigger_on_open
      end
    end

    req.errback do
      @log.debug "post #{parsed_uri.to_s} error"
      start(base_uri, init_msg, attempts - 1)
    end

  rescue Exception => e
    @log.error e
    trigger_on_close
    raise e
  end
end

#stop(attempts = MAX_RETRIES) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/caldecott/client/http_tunnel.rb', line 110

def stop(attempts = MAX_RETRIES)
  if attempts <= 0
    trigger_on_close
    return
  end

  return if @tun_uri.nil?

  @log.debug "delete #{@tun_uri}"
  req = EM::HttpRequest.new("#{@tun_uri}").delete :head => { "Auth-Token" => @auth_token }

  req.errback do
    @log.debug "delete #{@tun_uri} error"
    stop(attempts - 1)
  end

  req.callback do
    @log.debug "delete #{@tun_uri} #{req.response_header.status}"
    if [200, 202, 204, 404].include?(req.response_header.status)
      trigger_on_close
    else
      stop(attempts - 1)
    end
  end
end

#trigger_on_closeObject



55
56
57
58
59
60
# File 'lib/caldecott/client/http_tunnel.rb', line 55

def trigger_on_close
  close
  @closed = true
  @onclose.call if @onclose
  @onclose = nil
end

#trigger_on_openObject



50
51
52
53
# File 'lib/caldecott/client/http_tunnel.rb', line 50

def trigger_on_open
  @opened = true
  @onopen.call if @onopen
end

#trigger_on_receive(data) ⇒ Object



62
63
64
# File 'lib/caldecott/client/http_tunnel.rb', line 62

def trigger_on_receive(data)
  @onreceive.call(data)
end