Sha256: 8f47fdc68e4d1481c8cbff1c12d743f1509e963ca6802ba192e71c44ef351fbd

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'celluloid'
require 'hara/client_interaction'

module Hara
  module Filter
    include ClientInteraction

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

    module ClassMethods
      def pool_size= size
        Hara.filter_pool_size = size
      end

      def pool_size
        Hara.filter_pool_size
      end
    end

    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

    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

    # you should implement this method and return true or false
    def filter
      raise NotImplementedError, 'You must implement method filter, return value can be true or false.'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hara-0.4.0 lib/hara/filter.rb
hara-0.3.0 lib/hara/filter.rb