class Zashoku::Net::Client

Attributes

callbacks[RW]

Public Class Methods

command(host, port, msg, args = {}) click to toggle source
# File lib/core/net/client.rb, line 53
def self.command(host, port, msg, args = {})
  sock = TCPSocket.new(host, port)
  sock.puts(JSON.generate({'msg' => msg}.merge(args)))
  result = sock.readline
  sock.close
  {
    payload: JSON.parse(result).map { |k, v| "#{k}: #{v}\n" }.join,
    status: true
  }
rescue Errno::ECONNREFUSED
  {
    payload: "error: could not connect connect to #{host}:#{port}\n",
    status: false
  }
rescue EOFError
  {
    payload: nil,
    status: false
  }
end
new(host, port) click to toggle source
# File lib/core/net/client.rb, line 12
def initialize(host, port)
  @host, @port = host, port
  @socket = connect(@host, @port)
  @alive = true
  @blocked = false

  @timeout = 2

  @callbacks = []

  @command_queue = Queue.new
  @result_queue = Queue.new
  @event_queue = Queue.new

  @semaphore = Mutex.new

  start_threads
  say_helo
end

Public Instance Methods

alive?() click to toggle source
# File lib/core/net/client.rb, line 85
def alive?
  @alive
end
command(msg, args = {}) click to toggle source
# File lib/core/net/client.rb, line 105
def command(msg, args = {})
  return unless alive?
  @semaphore.synchronize {
    payload = { 'msg' => msg }.merge(args)
    @command_queue << JSON.generate(payload)

    result = nil
    start = Time.now
    thread = Thread.new do
      result = @result_queue.pop
    end
    sleep 0.1 until result || (Time.now - start) > @timeout
    thread.exit

    result ||= {}

    result.keys.length == 1 ? result['response'] : result
  }
end
command_to_file(file, msg, args = {}) click to toggle source
# File lib/core/net/client.rb, line 125
def command_to_file(file, msg, args = {})
  return unless alive?
  Zashoku.logger.info('stopping client threads from command_to_file')
  stop_threads
  @semaphore.synchronize {
    payload = { 'msg' => msg, 'raw' => true }.merge(args)
    @socket.puts(JSON.generate(payload))
    f = File.open(file, 'w')
    Zashoku::Util.alert("opened file #{file}")
    f.sync = true
    Zashoku::Util.alert('waiting for server...')
    #l = @socket.gets(Zashoku::EOF).split(Zashoku::EOF).first.to_i
    #Zashoku::Util.alert("got length #{l}")
    #r = 0
    loop do
      d = @socket.gets(Zashoku::EOF, Zashoku::CConf[:core][:net][:bsize])
      dl = d.length
      f.print(d)
      #Zashoku::Util.alert( "file #{((r += dl) / Float(l) * 100).round}% buffered" )
      break if dl < Zashoku::CConf[:core][:net][:bsize]
    end
    f.close
  }
  Zashoku::Util.alert('file buffered')
  Zashoku.logger.info('re-starting client threads from command_to_file')
  start_threads
end
connect(host, port, loud_fail: true) click to toggle source
# File lib/core/net/client.rb, line 74
def connect(host, port, loud_fail: true)
  TCPSocket.new(host, port)
rescue Errno::ECONNREFUSED
  if loud_fail
    puts "error: could not connect to #{host}:#{port}"
    Thread.exit
  end
  @alive = false
  nil
end
disconnect() click to toggle source
# File lib/core/net/client.rb, line 97
def disconnect
  command('disconnect')
  @command_thread.exit
  @results_thread.exit
  @events_thread.exit
  @alive = false
end
dispatch_events!() click to toggle source
# File lib/core/net/client.rb, line 186
def dispatch_events!
  loop do
    event = @event_queue.pop

    callbacks.each do |callback|
      Thread.new do
        callback.call event
      end
    end
  end
end
distribute_results!() click to toggle source
# File lib/core/net/client.rb, line 169
def distribute_results!
  raw = @socket.readline
  response = JSON.parse(raw)
  if response['event']
    @event_queue << response
  else
    @result_queue << response
  end
rescue JSON::ParserError
  Zashoku.logger.error("discarding #{raw}, JSON::ParserError")
rescue EOFError
  Zashoku.logger.error('eof error')
  lost_connection
rescue IOError
  Util.alert("error: socket#{@socket}")
end
get_items(_mod) click to toggle source
# File lib/core/net/client.rb, line 198
def get_items(_mod)
  {}
  # Util.decode_object(send("items #{mod}"))
end
lost_connection() click to toggle source
# File lib/core/net/client.rb, line 89
def lost_connection
  @alive = false
  @socket.close
  Zashoku.logger.warn('lost connection')
  Util.alert('no connection')
  stop_threads
end
pump_commands!() click to toggle source
# File lib/core/net/client.rb, line 153
def pump_commands!
  loop do
    begin
      @socket.puts(@command_queue.pop)
    rescue
      lost_connection
    end
  end
end
pump_results!() click to toggle source
# File lib/core/net/client.rb, line 163
def pump_results!
  loop do
    distribute_results!
  end
end
say_helo() click to toggle source
# File lib/core/net/client.rb, line 32
def say_helo
  r = command(
    'helo',
    {zv: Zashoku::Version, app: Zashoku::CConf[:app][:name]}
  )
  Zashoku.logger.warn('Everything is not okay') unless r['response'] == 'ok'
end
start_threads() click to toggle source
# File lib/core/net/client.rb, line 40
def start_threads
  @command_thread = Thread.new { pump_commands! }
  @results_thread = Thread.new { pump_results! }
  @events_thread = Thread.new { dispatch_events! }
end
stop_threads() click to toggle source
# File lib/core/net/client.rb, line 46
def stop_threads
  @command_thread&.exit
  @results_thread&.exit
  @events_thread&.exit
end