forked from OpenNeo/impress
have remote_gateway request use EM requests if available
This commit is contained in:
parent
e85c50bf62
commit
792d2ff1f2
1 changed files with 27 additions and 18 deletions
|
@ -12,27 +12,36 @@ 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 = Net::HTTP::Post.new(uri.path)
|
req = EM::HttpRequest.new(uri).post :body => data
|
||||||
req.body = data
|
response_body = req.response
|
||||||
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
|
|
||||||
else
|
else
|
||||||
error = nil
|
req = Net::HTTP::Post.new(uri.path)
|
||||||
begin
|
req.body = data
|
||||||
res.error!
|
res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
|
||||||
rescue Exception => scoped_error
|
case res
|
||||||
error = scoped_error
|
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
|
end
|
||||||
raise ConnectionError, "Error connecting to gateway: #{error}"
|
|
||||||
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
Loading…
Reference in a new issue