have remote_gateway request use EM requests if available

This commit is contained in:
Emi Matchu 2010-10-10 19:41:01 -04:00
parent e85c50bf62
commit 792d2ff1f2

View file

@ -12,27 +12,36 @@ module RocketAMF
def fetch
uri = @service.gateway.uri
data = envelope.serialize
req = Net::HTTP::Post.new(uri.path)
req.body = data
res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
result = RocketAMF::Envelope.new.populate_from_stream(res.body)
first_message_data = result.messages[0].data
if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE
raise AMFError.new(first_message_data)
end
return result
if EventMachine.reactor_running?
req = EM::HttpRequest.new(uri).post :body => data
response_body = req.response
else
error = nil
begin
res.error!
rescue Exception => scoped_error
error = scoped_error
req = Net::HTTP::Post.new(uri.path)
req.body = data
res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
case res
when Net::HTTPSuccess
response_body = res.body
else
error = nil
begin
res.error!
rescue Exception => scoped_error
error = scoped_error
end
raise ConnectionError, "Error connecting to gateway: #{error}"
end
raise ConnectionError, "Error connecting to gateway: #{error}"
end
begin
result = RocketAMF::Envelope.new.populate_from_stream(response_body)
rescue Exception => e
raise ConnectionError, "Error parsing gateway response: #{e.message}"
end
first_message_data = result.messages[0].data
if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE
raise AMFError.new(first_message_data)
end
result
end
private