lib/hara/app.rb in hara-0.0.1 vs lib/hara/app.rb in hara-0.0.2
- old
+ new
@@ -8,22 +8,31 @@
attr_reader :socket
alias halt terminate
+ finalizer :app_finalizer
+
Actions = {}
- def self.inherited klass
- ::Hara.const_set :Application, klass
- end
+ class << self
+ def inherited klass
+ ::Hara.const_set :Application, klass
+ end
- def initialize socket
- @socket = socket
- async.setup
+ def define_action action, &block
+ action = action.to_s
+ warn "Action #{action} duplication defined" if Actions.has_key? action
+ Hara::Application.send :define_method, action, &block
+ method = Hara::Application.send :instance_method, action
+ Hara::Application.send :remove_method, action
+ Actions[action] = method
+ end
end
- def after_connect
+
+ def init
end
#return true or false
def authentication
true
@@ -31,41 +40,14 @@
def authentication_failed
halt
end
+ #after_authentication only called when authentication succeeded
def after_authentication
end
- class << self
- def define_action action, &block
- action = action.to_s
- warn "Action #{action} duplication defined" if Actions.has_key? action
- Hara::Application.send :define_method, action, &block
- method = Hara::Application.send :instance_method, action
- Hara::Application.send :remove_method, action
- Actions[action] = method
- end
- end
-
- def process message
- @command = JSON.parse(message)
- action = @command["action"]
- args = @command["args"]
- info "#{Time.now} : #{@socket.remote_ip} request action: #{action}"
- before_process action, args
- call_action action, *args
- rescue JSON::ParserError
- warn "#{Time.now} : #{@socket.remote_ip} message can't parse"
- terminate
- rescue StandardError => e
- warn "#{Time.now} : #{@socket.remote_ip} want action: #{action} args: #{args} but process error:\n#{e.inspect}"
- terminate
- ensure
- after_process action, args
- end
-
def before_process action, args
end
def after_process action, args
end
@@ -79,10 +61,27 @@
end
def on_close
end
+ private
+ def initialize socket
+ @socket = socket
+ async.setup
+ end
+
+ def setup
+ init
+ info "#{Time.now} : #{socket.remote_ip} coming"
+ unless authentication
+ authentication_failed
+ else
+ after_authentication
+ async.run
+ end
+ end
+
def run
while msg = @socket.read
process msg
if @closed
terminate
@@ -97,32 +96,37 @@
ensure
terminate
end
end
- def finalizer
- on_close unless @closed
+ def process message
+ @command = JSON.parse(message)
+ action = @command["action"]
+ args = @command["args"]
+ info "#{Time.now} : #{@socket.remote_ip} request action: #{action}"
+ before_process action, *args
+ call_action action, *args
+ rescue JSON::ParserError
+ warn "#{Time.now} : #{@socket.remote_ip} message can't parse"
+ terminate
+ rescue StandardError => e
+ warn "#{Time.now} : #{@socket.remote_ip} want action: #{action} args: #{args} but process error:\n#{e.inspect}"
+ terminate
ensure
- @socket.close if @socket
+ after_process action, *args
end
- private
- def setup
- info "#{Time.now} : #{socket.remote_ip} coming"
- after_connect
- unless authentication
- authentication_failed
- else
- after_authentication
- async.run
- end
- end
-
def call_action action, *args
if Actions.has_key? action
Actions[action].bind(self).call *args
else
- action_missing action, args
+ action_missing action, *args
end
+ end
+
+ def app_finalizer
+ on_close unless @closed
+ ensure
+ @socket.close if @socket
end
end
end