Module: Fargo::Supports::Downloads

Extended by:
ActiveSupport::Concern
Included in:
Client
Defined in:
lib/fargo/supports/downloads.rb

Defined Under Namespace

Classes: Download

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_downloadsObject (readonly)

Returns the value of attribute current_downloads.



14
15
16
# File 'lib/fargo/supports/downloads.rb', line 14

def current_downloads
  @current_downloads
end

#download_slotsObject (readonly)

Returns the value of attribute download_slots.



14
15
16
# File 'lib/fargo/supports/downloads.rb', line 14

def download_slots
  @download_slots
end

#failed_downloadsObject (readonly)

Returns the value of attribute failed_downloads.



14
15
16
# File 'lib/fargo/supports/downloads.rb', line 14

def failed_downloads
  @failed_downloads
end

#finished_downloadsObject (readonly)

Returns the value of attribute finished_downloads.



14
15
16
# File 'lib/fargo/supports/downloads.rb', line 14

def finished_downloads
  @finished_downloads
end

#open_download_slotsObject (readonly)

Returns the value of attribute open_download_slots.



14
15
16
# File 'lib/fargo/supports/downloads.rb', line 14

def open_download_slots
  @open_download_slots
end

#queued_downloadsObject (readonly)

Returns the value of attribute queued_downloads.



14
15
16
# File 'lib/fargo/supports/downloads.rb', line 14

def queued_downloads
  @queued_downloads
end

#timed_outObject (readonly)

Returns the value of attribute timed_out.



14
15
16
# File 'lib/fargo/supports/downloads.rb', line 14

def timed_out
  @timed_out
end

#tryingObject (readonly)

Returns the value of attribute trying.



14
15
16
# File 'lib/fargo/supports/downloads.rb', line 14

def trying
  @trying
end

Instance Method Details

#clear_failed_downloadsObject



22
23
24
# File 'lib/fargo/supports/downloads.rb', line 22

def clear_failed_downloads
  failed_downloads.clear
end

#clear_finished_downloadsObject



26
27
28
# File 'lib/fargo/supports/downloads.rb', line 26

def clear_finished_downloads
  finished_downloads.clear
end

#download(nick, file = nil, tth = nil, size = -1,, offset = 0) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fargo/supports/downloads.rb', line 30

def download nick, file=nil, tth=nil, size=-1, offset=0
  raise ConnectionException.new 'Not connected yet!' unless hub

  if nick.is_a?(Supports::FileList::Listing)
    listing = nick
    nick    = listing.nick
    file    = listing.name
    tth     = listing.tth
    size    = listing.size
  elsif nick.is_a?(Download)
    dl     = nick
    nick   = dl.nick
    file   = dl.file
    tth    = dl.tth
    size   = dl.size || -1
    offset = dl.offset || 0
  end

  raise 'File must not be nil!' if file.nil?
  unless nicks.include? nick
    raise ConnectionException.new "User #{nick} does not exist!"
  end

  tth = tth.gsub /^TTH:/, '' if tth

  download         = Download.new nick, file, tth, size, offset
  download.percent = 0
  download.status  = 'idle'

  # Append it to the queue of things to download. This will be processed
  # elsewhere
  @to_download << download
  true
end

#lock_next_download!(user, connection) ⇒ Object



84
85
86
87
88
# File 'lib/fargo/supports/downloads.rb', line 84

def lock_next_download! user, connection
  @downloading_lock.synchronize {
    return get_next_download_with_lock! user, connection
  }
end

#remove_download(nick, file) ⇒ Object



77
78
79
80
81
82
# File 'lib/fargo/supports/downloads.rb', line 77

def remove_download nick, file
  # We need to synchronize this access, so append these arguments to a
  # queue to be processed later
  @to_remove << [nick, file]
  true
end

#retry_download(nick, file) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fargo/supports/downloads.rb', line 65

def retry_download nick, file
  dl = (@failed_downloads[nick] ||= []).detect{ |h| h.file == file }

  if dl.nil?
    Fargo.logger.warn "#{file} isn't a failed download for: #{nick}!"
    return
  end

  @failed_downloads[nick].delete dl
  download dl.nick, dl.file, dl.tth, dl.size
end

#try_again(nick) ⇒ Object

If a connection timed out, retry all queued downloads for that user



91
92
93
94
95
96
97
98
99
100
# File 'lib/fargo/supports/downloads.rb', line 91

def try_again nick
  return false unless @timed_out.include? nick

  @timed_out.delete nick
  downloads = @failed_downloads[nick].dup
  @failed_downloads[nick].clear
  downloads.each{ |d| download nick, d.file, d.tth, d.size }

  true
end