2010-10-07 07:46:23 -07:00
|
|
|
module RocketAMF
|
|
|
|
class RemoteGateway
|
|
|
|
class Request
|
|
|
|
ERROR_CODE = 'AMFPHP_RUNTIME_ERROR'
|
|
|
|
|
|
|
|
def initialize(service, method, *params)
|
|
|
|
@service = service
|
|
|
|
@method = method
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
2012-08-11 15:47:25 -07:00
|
|
|
def fetch(options={})
|
2010-10-07 07:46:23 -07:00
|
|
|
uri = @service.gateway.uri
|
|
|
|
data = envelope.serialize
|
2012-08-11 15:47:25 -07:00
|
|
|
|
|
|
|
req = Net::HTTP::Post.new(uri.path)
|
|
|
|
req.body = data
|
|
|
|
|
|
|
|
begin
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
http.read_timeout = options[:timeout] if options[:timeout]
|
|
|
|
res = http.request(req)
|
|
|
|
rescue Exception => e
|
|
|
|
raise ConnectionError, e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
if res.is_a?(Net::HTTPSuccess)
|
|
|
|
response_body = res.body
|
2010-10-07 07:46:23 -07:00
|
|
|
else
|
2012-08-11 15:47:25 -07:00
|
|
|
error = nil
|
2011-02-19 19:49:13 -08:00
|
|
|
begin
|
2012-08-11 15:47:25 -07:00
|
|
|
res.error!
|
|
|
|
rescue Exception => scoped_error
|
|
|
|
error = scoped_error
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
2012-08-11 15:47:25 -07:00
|
|
|
raise ConnectionError, error.message
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
2012-08-11 15:47:25 -07:00
|
|
|
|
2010-10-10 16:41:01 -07:00
|
|
|
begin
|
|
|
|
result = RocketAMF::Envelope.new.populate_from_stream(response_body)
|
|
|
|
rescue Exception => e
|
2011-02-19 19:49:13 -08:00
|
|
|
raise ConnectionError, e.message
|
2010-10-10 16:41:01 -07:00
|
|
|
end
|
2012-08-11 15:47:25 -07:00
|
|
|
|
2010-10-10 16:41:01 -07:00
|
|
|
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
|
2012-08-11 15:47:25 -07:00
|
|
|
|
2010-10-10 16:41:01 -07:00
|
|
|
result
|
2010-10-07 07:46:23 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def envelope
|
|
|
|
output = Envelope.new
|
|
|
|
output.messages << wrapper_message
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
|
|
|
def wrapper_message
|
|
|
|
message = Message.new 'null', '/1', [remoting_message]
|
|
|
|
end
|
|
|
|
|
|
|
|
def remoting_message
|
|
|
|
message = Values::RemotingMessage.new
|
|
|
|
message.source = @service.name
|
|
|
|
message.operation = @method
|
|
|
|
message.body = @params
|
|
|
|
message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-19 19:49:13 -08:00
|
|
|
class ConnectionError < RuntimeError
|
|
|
|
def initialize(message)
|
|
|
|
@message = message
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
"Error connecting to gateway: #{@message}"
|
|
|
|
end
|
|
|
|
end
|
2010-10-07 07:46:23 -07:00
|
|
|
class AMFError < RuntimeError
|
|
|
|
DATA_KEYS = [:details, :line, :code]
|
|
|
|
attr_reader *DATA_KEYS
|
|
|
|
attr_reader :message
|
|
|
|
|
|
|
|
def initialize(data)
|
|
|
|
DATA_KEYS.each do |key|
|
|
|
|
instance_variable_set "@#{key}", data[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
@message = data[:description]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|