class Kraken::Handler

This class handle a Socket.

Attributes

connection[RW]

Public Class Methods

new(socket) click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 13
def initialize(socket)
  return if socket.nil?

  @socket = socket
  @thread = Thread.new do
    begin
      start
    rescue StandardError => e
      Kraken::Log.error e
      close
    end
  end
end

Public Instance Methods

close() click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 27
def close
  @connection.delete unless @connection.nil?

  Kraken::Log.info 'bye bye socket'

  @socket.close
  @thread.kill
end
read_trigger() click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 36
def read_trigger
  trigger = read
  args = read_args
  [Kraken::Config.instance.triggers[trigger.downcase], args]
end

Private Instance Methods

absolute_value(obj) click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 146
def absolute_value(obj)
  "a\n#{obj}"
end
hash_protocol(obj) click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 134
def hash_protocol(obj)
  ret = ''
  obj.each_pair { |key, value| ret += "#{key}\n#{to_args value}\n" }
  ret.chomp
end
read() click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 84
def read
  r = @socket.gets
  raise 'connection lost' if r.nil?
  r.chomp
end
read_args() click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 90
def read_args
  type = read
  case type
  when 'h'
    return read_hash
  when 'v'
    return read_vector
  when 'n'
    return nil
  when 'a'
    return read
  end
end
read_authentication_info() click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 65
def read_authentication_info
  version = read
  user = read
  pass = read
  [version, user, pass]
end
read_hash() click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 104
def read_hash
  n = read.to_i
  awns = {}
  n.times { awns[read.to_sym] = read_args }
  awns
end
read_vector() click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 111
def read_vector
  n = read.to_i
  awns = []
  n.times { awns << read_args }
  awns
end
start() click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 44
def start
  version, user, pass = read_authentication_info

  raise 'invalid version' unless Kraken::Config.instance.server_version == version

  @connection = Connection.create(user: user, pass: pass)
  @connection.save

  raise 'only one connection by user' unless @connection.single_user?

  if @connection.authenticate
    write 'ok'
  else
    write 'nop'
    raise 'can not authenticate'
  end

  work
  close
end
to_args(obj) click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 122
def to_args(obj)
  case obj
  when Hash
    return "h\n#{obj.size}\n#{hash_protocol obj}"
  when Array
    return "v\n#{obj.size}\n#{vector_protocol obj}"
  when NilClass
    return 'n'
  end
  absolute_value(obj)
end
vector_protocol(obj) click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 140
def vector_protocol(obj)
  ret = ''
  obj.each { |value| ret += "#{to_args value}\n" }
  ret.chomp
end
work() click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 72
def work
  loop do
    trigger, args = read_trigger

    shot = trigger.new(self, args)
    shot.run
    callback = shot.callback

    write to_args(callback)
  end
end
write(txt) click to toggle source
# File lib/kraken/core/handlers/handler.rb, line 118
def write(txt)
  @socket.puts txt.chomp
end