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, tth = nil, size = -1,, offset = 0) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fargo/supports/downloads.rb', line 30

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

  unless nicks.include? nick
    raise ConnectionException.new "User #{nick} does not exist!" 
  end

  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



67
68
69
70
71
# File 'lib/fargo/supports/downloads.rb', line 67

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

#remove_download(nick, file) ⇒ Object



60
61
62
63
64
65
# File 'lib/fargo/supports/downloads.rb', line 60

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fargo/supports/downloads.rb', line 48

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



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

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