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,18 +12,16 @@ module RocketAMF
def fetch def fetch
uri = @service.gateway.uri uri = @service.gateway.uri
data = envelope.serialize data = envelope.serialize
if EventMachine.reactor_running?
req = EM::HttpRequest.new(uri).post :body => data
response_body = req.response
else
req = Net::HTTP::Post.new(uri.path) req = Net::HTTP::Post.new(uri.path)
req.body = data req.body = data
res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) } res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
case res case res
when Net::HTTPSuccess, Net::HTTPRedirection when Net::HTTPSuccess
result = RocketAMF::Envelope.new.populate_from_stream(res.body) response_body = 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
else else
error = nil error = nil
begin begin
@ -34,6 +32,17 @@ module RocketAMF
raise ConnectionError, "Error connecting to gateway: #{error}" raise ConnectionError, "Error connecting to gateway: #{error}"
end end
end 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 private