lib/ruby_fs/stream.rb in ruby_fs-1.0.4 vs lib/ruby_fs/stream.rb in ruby_fs-1.0.5
- old
+ new
@@ -15,10 +15,12 @@
Connected = Class.new ConnectionStatus
Disconnected = Class.new ConnectionStatus
include Celluloid::IO
+ finalizer :finalize
+
def initialize(host, port, secret, event_callback, events = true)
super()
@host, @port, @secret, @event_callback, @events = host, port, secret, event_callback, events
@command_callbacks = []
@lexer = Lexer.new method(:receive_request)
@@ -58,13 +60,15 @@
# @return [RubyFS::Response] response the command's response object
def command(command, options = {}, &callback)
uuid = SecureRandom.uuid
@command_callbacks << (callback || lambda { |reply| signal uuid, reply })
string = "#{command}\n"
+ body_value = options.delete :command_body_value
options.each_pair do |key, value|
string << "#{key.to_s.gsub '_', '-'}: #{value}\n" if value
end
+ string << "\n" << body_value << "\n" if body_value
string << "\n"
send_data string
wait uuid unless callback
end
@@ -106,25 +110,23 @@
# @param [#to_s] appname the app to execute
# @param [optional, String] options the application options
#
# @return [RubyFS::Response] response the application's response object
def application(call, appname, options = nil)
- sendmsg call, :call_command => 'execute', :execute_app_name => appname, :execute_app_arg => options
+ opts = {call_command: 'execute', execute_app_name: appname}
+ if options
+ opts[:content_type] = 'text/plain'
+ opts[:content_length] = options.bytesize
+ opts[:command_body_value] = options
+ end
+ sendmsg call, opts
end
#
# Shutdown the stream and disconnect from the socket
alias :shutdown :terminate
- # @private
- def finalize
- logger.debug "Finalizing stream"
- @socket.close if @socket
- @state = :stopped
- fire_event Disconnected.new
- end
-
#
# Fire an event to the specified callback
#
# @param [Object] event the event to fire
def fire_event(event)
@@ -143,10 +145,17 @@
end
end
private
+ def finalize
+ logger.debug "Finalizing stream"
+ @socket.close if @socket
+ @state = :stopped
+ fire_event Disconnected.new
+ end
+
def receive_data(data)
logger.trace "[RECV] #{data}"
@lexer << data
end
@@ -157,10 +166,10 @@
fire_event Event.new(headers, json_content_2_hash(content))
when 'command/reply', 'api/response'
@command_callbacks.pop.call CommandReply.new(headers, (content == '' ? nil : content))
when 'auth/request'
command "auth #{@secret}" do
- command! "event json ALL" if @events
+ async.command "event json ALL" if @events
end
when 'text/disconnect-notice'
terminate
else
raise "Unknown request type received (#{headers.inspect})"