class BotnetV2::Master

Public Class Methods

new(password, ssl_port = 8008, no_ssl_port = 8009, ssl = true, hybrid_mode = false) click to toggle source
# File lib/BotnetV2/Master.rb, line 10
def initialize(password, ssl_port = 8008, no_ssl_port = 8009, ssl = true, hybrid_mode = false)
  @password = password
  @verified_clients = Hash.new
  @ready_workers = Queue.new
  @ready_clients = Hash.new
  @work_bundles = Queue.new
  @result_bundles = Hash.new
  @processed_tasks = 0
  @started_tasks = 0
  @worker_corrector = 0
  @exit = false
  @t1 = nil
  @t2 = nil

  if ssl || hybrid_mode
    network = BotNetwork.new
    @t1 = network.listen(Proc.new do |connection| on_connect_handler(connection) end, ssl_port)
  end
  if !ssl || hybrid_mode
    network = BotNetwork.new
    @t2 = network.listen_no_ssl(Proc.new do |connection| on_connect_handler(connection) end, no_ssl_port)
  end

  @main_loop = Thread.start do main_loop end
end

Public Instance Methods

count_pending_results(hashed_queues) click to toggle source
# File lib/BotnetV2/Master.rb, line 102
def count_pending_results(hashed_queues)
  counter = 0
  hashed_queues.each_key do |key|
    counter += hashed_queues[key].length
  end
  counter
end
exit!() click to toggle source
# File lib/BotnetV2/Master.rb, line 110
def exit!
  @exit = true
  @main_loop.kill
  @t1.kill if @t1
  @t2.kill if @t2
  puts '[' + Time.new.strftime('%Y-%m-%d %H:%M:%S') + '] Killed listeners!'
end
main_loop() click to toggle source
# File lib/BotnetV2/Master.rb, line 36
def main_loop
  loop do
    connection = @ready_workers.pop
    @worker_corrector = 1
    unless connection.closed?
      msg = Hash.new
      msg['onWork'] = @work_bundles.pop
      @worker_corrector = 0
      @started_tasks += 1
      connection.send msg
    end
    @worker_corrector = 0
  end
end
on_connect_handler(connection) click to toggle source
# File lib/BotnetV2/Master.rb, line 51
def on_connect_handler(connection)
  connection.on_message_handler_loop(Proc.new do |msg| on_message(msg, connection) end)
end
on_message(message, connection) click to toggle source
# File lib/BotnetV2/Master.rb, line 55
def on_message (message, connection)
  return unless message
  @verified_clients[connection] = true if message['verify'] == @password
  return unless @verified_clients[connection]
  @ready_workers << connection if message['worker_onReady'] != nil
  @work_bundles << message['onWork'] if message['onWork'] != nil
  @ready_clients[message['client_onDisconnect']] = nil if message['client_onDisconnect'] != nil

  if message['status']
    msg = Hash.new
    report = Hash.new
    report['ready_clients'] = @ready_clients.count
    report['ready_workers'] = @ready_workers.length + @worker_corrector
    report['verified_clients'] = @verified_clients.count
    report['work_bundles'] = @work_bundles.length
    report['result_bundles'] = count_pending_results @result_bundles
    report['processed_tasks'] = @processed_tasks
    report['active_tasks'] = @started_tasks - @processed_tasks
    report['task_id'] = message['status']
    msg['status'] = report
    connection.send msg
  end

  if message['client_onReady'] != nil
    client_id = message['client_onReady']
    @ready_clients[client_id] = connection
    @result_bundles[client_id] = Queue.new if @result_bundles[client_id] == nil
    until @result_bundles[client_id].empty?
      msg = Hash.new
      msg['onResult'] = @result_bundles[client_id].pop
      connection.send msg
    end
  end
  if message['onResult'] != nil
    @processed_tasks += 1
    client_id = message['onResult']['clientId']
    if @ready_clients[client_id] == nil
      @result_bundles[client_id] = Queue.new if @result_bundles[client_id] == nil
      @result_bundles[client_id] << message['onResult']
    else
      msg = Hash.new
      msg['onResult'] = message['onResult']
      @ready_clients[client_id].send msg
    end
  end
end