From 792d2ff1f2bcf00241fcae5e346cc1aa04b11156 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sun, 10 Oct 2010 19:41:01 -0400 Subject: [PATCH] have remote_gateway request use EM requests if available --- lib/rocketamf/remote_gateway/request.rb | 45 +++++++++++++++---------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/rocketamf/remote_gateway/request.rb b/lib/rocketamf/remote_gateway/request.rb index 508a3aa4..97073038 100644 --- a/lib/rocketamf/remote_gateway/request.rb +++ b/lib/rocketamf/remote_gateway/request.rb @@ -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