Class: Zold::Remotes

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/remotes.rb

Overview

All remotes

Defined Under Namespace

Classes: Empty, Remote

Constant Summary collapse

PORT =

The default TCP port all nodes are supposed to use.

4096

Instance Method Summary collapse

Constructor Details

#initialize(file, farm = Farm::Empty.new) ⇒ Remotes

Returns a new instance of Remotes.



66
67
68
69
# File 'lib/zold/remotes.rb', line 66

def initialize(file, farm = Farm::Empty.new)
  @file = file
  @farm = farm
end

Instance Method Details

#add(host, port = Remotes::PORT) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/zold/remotes.rb', line 99

def add(host, port = Remotes::PORT)
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  raise 'Port can\'t be negative' if port < 0
  raise 'Port can\'t be over 65536' if port > 0xffff
  raise "#{host}:#{port} alread exists" if exists?(host, port)
  list = load
  list << { host: host.downcase, port: port, score: 0 }
  list.uniq! { |r| "#{r[:host]}:#{r[:port]}" }
  save(list)
end

#allObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/zold/remotes.rb', line 71

def all
  list = load
  max_score = list.map { |r| r[:score] }.max || 0
  max_score = 1 if max_score.zero?
  max_errors = list.map { |r| r[:errors] }.max || 0
  max_errors = 1 if max_errors.zero?
  list.sort_by do |r|
    (1 - r[:errors] / max_errors) * 5 + (r[:score] / max_score)
  end.reverse
end

#cleanObject



82
83
84
# File 'lib/zold/remotes.rb', line 82

def clean
  save([])
end

#error(host, port = Remotes::PORT) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/zold/remotes.rb', line 133

def error(host, port = Remotes::PORT)
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  raise "#{host}:#{port} is absent" unless exists?(host, port)
  list = load
  list.find do |r|
    r[:host] == host.downcase && r[:port] == port
  end[:errors] += 1
  save(list)
end

#exists?(host, port = Remotes::PORT) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/zold/remotes.rb', line 94

def exists?(host, port = Remotes::PORT)
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  !load.find { |r| r[:host] == host.downcase && r[:port] == port }.nil?
end

#iterate(log) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/zold/remotes.rb', line 118

def iterate(log)
  best = @farm.best[0]
  require_relative 'score'
  score = best.nil? ? Score::ZERO : best
  all.each do |r|
    begin
      yield Remotes::Remote.new(r[:host], r[:port], score)
    rescue StandardError => e
      error(r[:host], r[:port])
      log.info("#{Rainbow("#{r[:host]}:#{r[:port]}").red}: #{e.message}")
      log.debug(e.backtrace[0..5].join("\n\t"))
    end
  end
end

#remove(host, port = Remotes::PORT) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/zold/remotes.rb', line 110

def remove(host, port = Remotes::PORT)
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  raise "#{host}:#{port} is absent" unless exists?(host, port)
  list = load
  list.reject! { |r| r[:host] == host.downcase && r[:port] == port }
  save(list)
end

#rescore(host, port, score) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/zold/remotes.rb', line 143

def rescore(host, port, score)
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  raise "#{host}:#{port} is absent" unless exists?(host, port)
  list = load
  list.find do |r|
    r[:host] == host.downcase && r[:port] == port
  end[:score] = score
  save(list)
end

#resetObject



86
87
88
89
90
91
92
# File 'lib/zold/remotes.rb', line 86

def reset
  FileUtils.mkdir_p(File.dirname(@file))
  FileUtils.copy(
    File.join(File.dirname(__FILE__), '../../resources/remotes'),
    @file
  )
end