Class: RHACK::ScoutSquad

Inherits:
Array show all
Defined in:
lib/rhack/scout_squad.rb

Instance Method Summary collapse

Methods inherited from Array

#grabprlist, #to_pl

Constructor Details

#initialize(*args) ⇒ ScoutSquad

Returns a new instance of ScoutSquad.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rhack/scout_squad.rb', line 11

def initialize(*args)
  raise ArgumentError, "can't create empty squad" if (num = args.pop) < 1
  proxies = nil
  super []
  if args[0].is Scout
    s = args[0]
  else
    unless args[0].is String
      if (opts = args[-1]).is Hash and (opts[:cp] || opts[:ck]).is Hash
        L.warn "it's useless to setup cookies for untargeted squad!"
      end
    end
    if !args[0]
      args[0] = ''
    elsif !args[0].is String
      args.unshift ''
    end
    if args[1] and args[1][0].is Array
      proxies = args[1]
      args[1] = proxies.shift
    end
    self[0] = s = Scout(*args)
    num -=1
  end
  num.times {|i| 
    self << Scout(s.root+s.path, (proxies ? proxies[i] : s.proxy), s.ua, s.refforge, :ck => s.main_cks, :raise => s.raise_err, :timeout => s.timeout, :retry => s.retry)
  }
end

Instance Method Details

#nextObject

Raises:



76
77
78
79
80
81
82
83
84
85
# File 'lib/rhack/scout_squad.rb', line 76

def next
  raise PickError if !b
  if scout = to_a.find_available?
    L.debug {"picked the next available scout##{scout.object_id}"}
    scout
  else
    wait_for_available
    self.next
  end 
end

#randObject

Raises:



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rhack/scout_squad.rb', line 64

def rand
  raise PickError if !b
  # to_a because Array#reject returns object of this class
  if scout = to_a.rand_by_available?
    L.debug {"randomly picked an available scout##{scout.object_id}"}
    scout
  else
    wait_for_available
    self.rand 
  end 
end

#to_sObject Also known as: inspect



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rhack/scout_squad.rb', line 87

def to_s
  str = '<#ScoutSquad @ '
  if b
    if first.webproxy
      str << "#{first.proxy} ~ "
    elsif first.proxy
      str << first.proxy*':'+" ~ " 
    end
    str << "#{untargeted ? "no target" : first.root} "
  end
  str << "x#{size}>"
end

#untargetedObject



45
46
47
# File 'lib/rhack/scout_squad.rb', line 45

def untargeted
  first.root == 'http://'
end

#update(uri, forced = nil) ⇒ Object



40
41
42
43
# File 'lib/rhack/scout_squad.rb', line 40

def update uri, forced=nil
  each {|s| return L.warn "failed to update loaded scout with url: #{s.http.url}" if s.loaded?} if !forced
  each {|s| s.update uri}
end

#wait_for_availableObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rhack/scout_squad.rb', line 49

def wait_for_available
  #L.debug {"Curl.carier_thread = #{Curl.carier_thread}; Thread.current = #{Thread.current}"}
  Curl.execute :unless_already
  #L.debug {"Curl.carier_thread = #{Curl.carier_thread}; Thread.current = #{Thread.current}"}
  # Carier.requests освобождаются ещё до колбека,
  # но колбеки выполняются последовательно,
  # поэтому здесь мы можем усыплять тред,
  # но только если это не тред самого Carier
  if Curl.carier_thread == Thread.current
    Curl.wait # runs Multi#perform
  else
    sleep 1
  end
end