lib/hara/app.rb in hara-0.3.0 vs lib/hara/app.rb in hara-0.4.0
- old
+ new
@@ -44,17 +44,17 @@
def on_close close_info = {}
end
##################
- #like method_missing
+ # like method_missing
def action_missing action, args
info "#{client_ip} request action: #{action} args: #{args.inspect}, action not defined"
raise NoMethodError, "undefined action '#{action}' for #{self}:#{self.class}"
end
- #below are internal functions(should not been overriding)
+ # below are internal functions(should not been overriding)
def initialize handshake, socket
socket_setup handshake, socket
async.hara_setup
end
@@ -63,17 +63,34 @@
info "#{client_ip} coming"
after_connect
end
def process_msg message
- action, args = Hara.decode_msg(message)
- info "#{client_ip} request action: #{action} args: #{args.inspect}"
- before_action action, *args
- call_action action, *args
- after_action action, *args
- rescue StandardError => e
- info "#{client_ip} processing error:\n#{e.inspect}"
- terminate
+ exclusive do
+ begin
+ id, action, args = Hara.decode_msg(message).values_at('_id', 'action', 'args')
+ info "#{client_ip} request action: #{action} args: #{args.inspect}"
+ before_action action, *args
+ call_action action, *args
+ after_action action, *args
+ send_response_msg id
+ rescue StandardError => e
+ info "#{client_ip} processing error:\n#{e.inspect}"
+ terminate
+ end
+ end
+ end
+
+ # respond to client
+ def response_msg msg
+ raise DuplicateResponseError if @_response_msg
+ @_response_msg = msg
+ end
+
+ def send_response_msg message_id
+ msg, @_response_msg = @_response_msg, nil
+ message = Hara.encode_msg(_id: message_id, type: :response, args: msg)
+ socket.send message
end
def call_action action, *args
if Actions.has_key? action
Actions[action].bind(self).call *args