Source

event_shipper / lib / event_shipper / proxy.rb


require 'redis'
require 'yaml'
require 'clamp'

require_relative 'filters'
require_relative 'udp'

module EventShipper
  class Proxy < Clamp::Command

    option %w(-c --config), "FILE", "Location of the configuration file."

    def execute
      fail "Please specify a configuration file to use on the command line. (--config)" \
        unless config

      configuration = YAML.load_file(config)

      listen = configuration['listen']
      host, port = listen['host'], listen['port']
      transport = UDP.new(host, port)

      if configuration['encrypt']
        users = configuration['users']
        userdb = Hash[users.map { |str| str.split('/') }]

        transport.wrap Filter::Decrypt.new(userdb)
      end

      redis_config = configuration['redis']
      redis = Redis.new(
        host: redis_config['host'], 
        port: redis_config['port'])

      begin
        trap('SIGINFO') { puts transport.stats }
      rescue ArgumentError
        # unsupported signal SIGINFO
      end

      transport.dispatch { |queue, message| 
        # puts message }
        redis.lpush queue, message }
    end
  end
end