Module: Hara::App

Includes:
ClientInteraction
Defined in:
lib/hara/app.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

Actions =
{}

Instance Attribute Summary

Attributes included from ClientInteraction

#client_ip, #client_port, #handshake, #socket

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClientInteraction

#close, #headers, #send_msg, #socket_setup

Class Method Details

.included(klass) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/hara/app.rb', line 13

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
end

Instance Method Details

#action_missing(action, args) ⇒ Object

like method_missing

Raises:

  • (NoMethodError)


50
51
52
53
# File 'lib/hara/app.rb', line 50

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

#after_action(action, args) ⇒ Object



41
42
# File 'lib/hara/app.rb', line 41

def after_action action, args
end

#after_connectObject

callback methods



35
36
# File 'lib/hara/app.rb', line 35

def after_connect
end

#app_finalizerObject



107
108
109
110
111
# File 'lib/hara/app.rb', line 107

def app_finalizer
  on_close @_close_info
ensure
  @socket.close if @socket
end

#before_action(action, args) ⇒ Object



38
39
# File 'lib/hara/app.rb', line 38

def before_action action, args
end

#call_action(action, *args) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/hara/app.rb', line 95

def call_action action, *args
  if Actions.has_key? action
    Actions[action].bind(self).call *args
  else
    action_missing action, *args
  end
end

#hara_setupObject



62
63
64
65
# File 'lib/hara/app.rb', line 62

def hara_setup
  info "#{client_ip} coming"
  after_connect
end

#initialize(handshake, socket) ⇒ Object

below are internal functions(should not been overriding)



57
58
59
60
# File 'lib/hara/app.rb', line 57

def initialize handshake, socket
  socket_setup handshake, socket
  async.hara_setup
end

#on_close(close_info = {}) ⇒ Object



44
45
# File 'lib/hara/app.rb', line 44

def on_close close_info = {}
end

#process_msg(message) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hara/app.rb', line 67

def process_msg message
  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

#response_msg(msg) ⇒ Object

respond to client



84
85
86
87
# File 'lib/hara/app.rb', line 84

def response_msg msg
  raise DuplicateResponseError if @_response_msg
  @_response_msg = msg
end

#send_response_msg(message_id) ⇒ Object



89
90
91
92
93
# File 'lib/hara/app.rb', line 89

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

#set_close_info(close_info) ⇒ Object



103
104
105
# File 'lib/hara/app.rb', line 103

def set_close_info close_info
  @_close_info = close_info
end