Module: Fargo::Supports::Downloads

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_downloadsObject (readonly)

Returns the value of attribute current_downloads.



18
19
20
# File 'lib/fargo/supports/downloads.rb', line 18

def current_downloads
  @current_downloads
end

#failed_downloadsObject (readonly)

Returns the value of attribute failed_downloads.



18
19
20
# File 'lib/fargo/supports/downloads.rb', line 18

def failed_downloads
  @failed_downloads
end

#finished_downloadsObject (readonly)

Returns the value of attribute finished_downloads.



18
19
20
# File 'lib/fargo/supports/downloads.rb', line 18

def finished_downloads
  @finished_downloads
end

#queued_downloadsObject (readonly)

Returns the value of attribute queued_downloads.



18
19
20
# File 'lib/fargo/supports/downloads.rb', line 18

def queued_downloads
  @queued_downloads
end

#timed_outObject (readonly)

Returns the value of attribute timed_out.



18
19
20
# File 'lib/fargo/supports/downloads.rb', line 18

def timed_out
  @timed_out
end

#tryingObject (readonly)

Returns the value of attribute trying.



18
19
20
# File 'lib/fargo/supports/downloads.rb', line 18

def trying
  @trying
end

Instance Method Details

#clear_failed_downloadsObject



29
30
31
# File 'lib/fargo/supports/downloads.rb', line 29

def clear_failed_downloads
  failed_downloads.clear
end

#clear_finished_downloadsObject



33
34
35
# File 'lib/fargo/supports/downloads.rb', line 33

def clear_finished_downloads
  finished_downloads.clear
end

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



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fargo/supports/downloads.rb', line 37

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

  if nick.is_a?(Download)
    dl     = nick
    nick   = dl.nick
    file   = dl.file
    tth    = dl.tth
    size   = dl.size || -1
    offset = dl.offset || 0
  elsif nick.is_a?(Listing) # i.e. a listing
    listing = nick
    nick    = listing.nick
    file    = listing.name
    tth     = listing.tth
    size    = listing.size
  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'

  # Using the mutex can be expensive in start_download, defer this
  if @timed_out.include? download.nick
    download.status = 'timeout'
    @failed_downloads[download.nick] << download
  else
    unless @queued_downloads[download.nick].include?(download) ||
        @current_downloads[download.nick] == download
      @queued_downloads[download.nick] << download

      # This uses the lock and could be expensive, defer this for later
      EventMachine.defer{ start_download }
    end
  end

  true
end

#has_download_slot?Boolean

Returns:

  • (Boolean)


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

def has_download_slot?
  open_download_slots > 0
end

#lock_next_download!(user, connection) ⇒ Object



102
103
104
105
106
# File 'lib/fargo/supports/downloads.rb', line 102

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

#open_download_slotsObject



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

def open_download_slots
  config.download_slots - @trying.size - @current_downloads.size
end

#remove_download(nick, file) ⇒ Object



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

def remove_download nick, file
  # We need to synchronize this access, so append these arguments to a
  # queue to be processed later
  EventMachine.defer do
    @downloading_lock.synchronize {
      @queued_downloads[nick].delete_if{ |h| h.file == file }
    }
  end
end

#retry_download(nick, file) ⇒ Object



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

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

  return if dl.nil?

  @failed_downloads[nick].delete dl
  download dl
end

#try_again(nick) ⇒ Object

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



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fargo/supports/downloads.rb', line 109

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

  @timed_out.delete nick
  downloads = @failed_downloads[nick].dup
  @failed_downloads[nick].clear
  # Reschedule all the failed downloads again
  downloads.each{ |d| download nick, d.file, d.tth, d.size }

  true
end