Class: Caldecott::Client::HttpTunnel::Reader

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

Instance Method Summary collapse

Constructor Details

#initialize(log, uri, conn, auth_token) ⇒ Reader

Returns a new instance of Reader.



137
138
139
140
141
# File 'lib/caldecott/client/http_tunnel.rb', line 137

def initialize(log, uri, conn, auth_token)
  @log, @base_uri, @conn, @auth_token = log, uri, conn, auth_token
  @closing = false
  start
end

Instance Method Details

#closeObject



143
144
145
# File 'lib/caldecott/client/http_tunnel.rb', line 143

def close
  @closing = true
end

#start(seq = 1, attempts = MAX_RETRIES) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/caldecott/client/http_tunnel.rb', line 147

def start(seq = 1, attempts = MAX_RETRIES)
  return if @closing

  if attempts <= 0
    @conn.trigger_on_close
    return
  end

  uri = "#{@base_uri}/#{seq}"
  @log.debug "get #{uri}"
  req = EM::HttpRequest.new(uri).get :timeout => 0, :head => { "Auth-Token" => @auth_token }

  req.errback do
    @log.debug "get #{uri} error"
    start(seq, attempts - 1)
  end

  req.callback do
    @log.debug "get #{uri} #{req.response_header.status}"
    case req.response_header.status
    when 200
      @conn.trigger_on_receive(req.response)
      start(seq + 1)
    when 404
      @conn.trigger_on_close
    else
      start(seq, attempts - 1)
    end
  end
end