Module: Hara::Filter

Includes:
ClientInteraction
Included in:
DefaultFilter
Defined in:
lib/hara/filter.rb

Defined Under Namespace

Modules: ClassMethods

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, #response_msg, #send_msg, #socket_setup

Class Method Details

.included(klass) ⇒ Object



9
10
11
12
13
14
# File 'lib/hara/filter.rb', line 9

def included klass
  klass.send :include, Celluloid
  klass.send :include, Celluloid::Logger
  Hara.filter_class = klass
  klass.send :extend, ClassMethods
end

Instance Method Details

#filterObject

you should implement this method and return true or false

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/hara/filter.rb', line 56

def filter
  raise NotImplementedError, 'You must implement method filter, return value can be true or false.'
end

#run_filter(handshake, socket) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hara/filter.rb', line 43

def run_filter handshake, socket
  exclusive do
    socket_setup handshake, socket
    pass = self.filter
    if pass
      setup_socket_callbacks handshake, socket
    else
      close
    end
  end
end

#setup_socket_callbacks(handshake, ws) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hara/filter.rb', line 27

def setup_socket_callbacks handshake, ws
  actor = Hara::Application.new handshake, ws

  ws.onclose { |close_info = {}|
    begin
      actor.async.set_close_info close_info
      actor.terminate! if actor.alive?
    rescue Celluloid::DeadActorError => e
    end
  }

  ws.onmessage { |msg|
    actor.async.process_msg msg
  }
end