lib/hara/app.rb in hara-0.2.2 vs lib/hara/app.rb in hara-0.3.0

- old
+ new

@@ -1,54 +1,34 @@ -require "hara/version" +require 'hara/client_interaction' require 'celluloid' require 'socket' require 'json' module Hara - class << self - def env - @_env ||= (ENV['APP_ENV'] || :development).to_sym - end - - def env= env - @_env = env - end - - #decode message, return action and args - def decode_msg msg - msg = JSON.parse(msg) - msg.values_at 'action', 'args' - end - - def encode_msg action, *args - {action: action, args: args}.to_json - end - end - module App - attr_reader :socket, :handshake, :client_ip, :client_port + include ClientInteraction Actions = {} class << self def included klass - klass.send :include, Celluloid - klass.send :include, Celluloid::Logger - klass.send :finalizer, :app_finalizer - klass.send :extend, ClassMethods - ::Hara.const_set :Application, klass + klass.send :include, Celluloid + klass.send :include, Celluloid::Logger + klass.send :finalizer, :app_finalizer + klass.send :extend, ClassMethods + ::Hara.const_set :Application, klass end end module ClassMethods 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 + 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 ##callback methods @@ -70,26 +50,14 @@ 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 - #get client headers - def headers - handshake.headers_downcased - end - - # close connection - def close code = nil, body = nil - @socket.close code, body - end - #below are internal functions(should not been overriding) def initialize handshake, socket - @handshake = handshake - @socket = socket - @client_port, @client_ip = Socket.unpack_sockaddr_in(socket.get_peername) #to get ip address of user + socket_setup handshake, socket async.hara_setup end def hara_setup info "#{client_ip} coming" @@ -107,12 +75,12 @@ terminate end def call_action action, *args if Actions.has_key? action - Actions[action].bind(self).call *args + Actions[action].bind(self).call *args else - action_missing action, *args + action_missing action, *args end end def set_close_info close_info @_close_info = close_info