def runtorrentwindow if ! @a_viewtorrent.to_s.empty? $torrenttodownload = TorrentRootDir + $rwdtorrentfilesdir + "/" + @a_viewtorrent + ".torrent" rwdtorrentwindowtwo end end def runtorrentwindowhttp $torrenttodownload = @a_viewtorrenthttp rwdtorrentwindowtwo end $temptorrentnumber = 0 def rwdtorrentwindowtwo require 'extras/rubytorrent' @rwd_call_after_back << "rwdtorrentwindowtwo" #if @rwd_tab== "rwdtorrentrefresh" @rwd_window = "torrentdisplaywindowtwo" # end if $temptorrentnumber == 0 display_cols = 0 @@dlratelim = nil @ulratelim = nil @need_update = true display_fn = "" dest = "" display_status = "" display_completed = 0 display_total = 0 @@display_dlamt = 0 @@display_dlrate = 0 @@display_ulamt = 0 @@display_ulrate = 0 display_rate = 0 @@display_conn_peers = 0 @@display_fail_peers = 0 display_untried_peers = 0 display_tracker = "not connected" @@display_errcount = 0 @dlblocks = 0 @ulblocks = 0 @got_blocks = 0 @sent_blocks = 0 @@last_got_block = nil @@last_sent_block = nil @start_time = nil @use_rate = false proxy = ENV["http_proxy"] dest= $rwdtorrentdownloads $torrenttimestart = Time.now #Dir.chdir($rwdtorrentdownloads) begin mi = RubyTorrent::MetaInfo.from_location("#{$torrenttodownload}", proxy) rescue RubyTorrent::MetaInfoFormatError, RubyTorrent::BEncodingError => e die %{Error: can\'t parse metainfo file "#{$torrenttodownload}"---maybe not a .torrent?} rescue RubyTorrent::TypedStructError => e $stderr.print " error parsing metainfo file, and it's likely something I should know about. please email the torrent file to wmorgan-rubytorrent-bug@masanjin.net, along with this backtrace: (this is RubyTorrent version #{RubyTorrent::VERSION})" raise e rescue IOError, SystemCallError => e $stderr.puts %{Error: can't read file "#{$torrenttodownload}": #{e.message}} #exit end unless dest.nil? or mi.nil? if FileTest.directory?(dest) && mi.info.single? dest = File.join(dest, mi.info.name) elsif FileTest.file?(dest) && mi.info.multiple? die %{Error: .torrent contains multiple files, but "#{dest}" is a single file (must be a directory)} end begin display_status = "checking file on disk..." # @display_det = File.expand_path(dest || mi.info.name) + (mi.single? ? "" : "/") if mi.single? display_fn = "#{mi.info.name} (#{mi.info.length} in one file)" else display_fn = "#{mi.info.name}/ (#{mi.info.total_length} in #{mi.info.files.length} files)" end display_total = mi.info.num_pieces * mi.info.piece_length display_completed = 0 @display_use_rate = true @display_start_timer num_pieces = 0 start = Time.now every = 10 package = RubyTorrent::Package.new(mi, dest) do |piece| num_pieces += 1 if (num_pieces % every) == 0 display_completed = (num_pieces * mi.info.piece_length) display_rate = display_completed.to_f / (Time.now - start) end end display_status = "starting peer..." @display_use_rate = false $bt = RubyTorrent::BitTorrent.new(mi, package, :http_proxy => proxy, :dlratelim => @@dlratelim, :ulratelim => @ulratelim) @torrent_connecting = true #runtorrentthread $temptorrentnumber =1 end Dir.chdir($progdir) @torrentdisplayvariable = $bt.percent_completed.to_s @torrentstarttime = $torrenttimestart @torrentcurrenttime = Time.now.to_s end # unless end # end of initial block - should only run once per download begin if $bt.nil? @rwd_window = "main" @rwd_tab = "rwdtorrentrefresh" else if display_tracker.nil? display_tracker = "not connected" end if @@display_errcount.nil? @@display_errcount = 0 end $bt.on_event(self, :received_block) do |s, b, peer| @display_got_block @torrent_connecting = false end $bt.on_event(self, :sent_block) do |s, b, peer| @display_sent_block @torrent_connecting = false end $bt.on_event(self, :discarded_piece) { |s, p| @@display_errcount += 1 } $bt.on_event(self, :tracker_connected) do |s, url| display_tracker = url display_untried_peers = $bt.num_possible_peers end if @@display_fail_peers.nil? @@display_fail_peers = 0 end if @@display_conn_peers.nil? @@display_conn_peers = 0 end $bt.on_event(self, :tracker_lost) { |s, url| display_tracker = "can't connect to #{url}" } $bt.on_event(self, :forgetting_peer) { |s, p| @@display_fail_peers += 1 } $bt.on_event(self, :removed_peer, :added_peer) do |s, p| if (@@display_conn_peers = $bt.num_active_peers) == 0 @torrent_connecting = true end end if display_untried_peers.to_s.empty? display_untried_peers = 0 end $bt.on_event(self, :added_peer) { |s, p| @@display_conn_peers += 1 } $bt.on_event(self, :trying_peer) { |s, p| display_untried_peers -= 1 unless display_untried_peers == 0 } display_total = $bt.total_bytes @display_start_timer if true display_status = if $bt.complete? "seeding (download complete)" elsif @torrent_connecting "connecting to peers" else "downloading" end if display_cols.nil? display_cols = 0 end if display_total.nil? display_total = 0 end if display_completed.nil? display_completed =0 end if @@last_got_block.nil? @@last_got_block = 0 end if @@last_sent_block.nil? @@last_sent_block = 0 end @@display_dlamt = $bt.dlamt @@display_dlrate = $bt.dlrate @@display_ulamt = $bt.ulamt @@display_ulrate = $bt.ulrate complete_width = [display_cols - 23, 0].max complete_ticks = ((display_completed.to_f / display_total) * complete_width) elapsed = (@torrentstarttime ? Time.now - @torrentstarttime : nil) rate = (@display_use_rate ? display_rate : @@display_dlrate) remaining = rate && (rate > 0 ? (display_total - display_completed).to_f / rate : nil) dlstall = @@last_got_block.to_i && ((Time.now - @@last_got_block).to_i > STALL_SECS) ulstall = @@last_sent_block.to_i && ((Time.now - @@last_sent_block).to_i > STALL_SECS) display_progress = $bt.percent_completed.to_s display_completed = $bt.bytes_completed.to_s dest= $rwdtorrentdownloads $bt.on_event(self, :complete) { display_status = "Download Complete..." } @display_Dest = " Destination Directory: #{dest}" @display_Status = " Status: #{display_status}" @display_Total = "Total File Size to Downloaded: #{display_total}" @display_Completed = "Total Size Downloaded this session: #{display_completed}" @display_Progress = "Percent Progress this session: #{display_progress}" @display_Time_elapsed = " Time: elapsed #{numeric_to_time(elapsed)}, remaining #{numeric_to_time(remaining)}" @display_Download = "Download: #{numeric_to_sz(@@display_dlamt)} at #{numeric_to_sz(@@display_dlrate)}" @display_Upload = " Upload: #{numeric_to_sz(@@display_ulamt)} at #{numeric_to_sz(@@display_ulrate)}" @display_Peers = " Peers: connected to #{@@display_conn_peers} (#{@@display_fail_peers} failed, #{display_untried_peers} untried)" #@display_Tracker = " Tracker: #{display_tracker}" @display_Errors = " Errors: #{@@display_errcount}" end # rescue SystemCallError, StandardError, Exception # $stderr.print "system call error: " + $! #end # exception rescue end # if bt not nil rescue NoMethodError, Exception $stderr.print "error: " + $! @a_torrentdata = $! end # exception end def torrent_die(x); $stderr << "#{x}\n" && exit(-1); end STALL_SECS = 15 def got_block @got_blocks += 1 @@last_got_block = Time.now end def sent_block @sent_blocks += 1 @@last_sent_block = Time.now end def sigwinch_handler(sig = nil) @need_update = true end def start_timer @start_time = Time.now end def numeric_to_sz(size_in) if size_in.nil? size_in = 0 end if size_in < 1024 "#{size_in.round}b" elsif size_in < 1024 ** 2 "#{(size_in / 1024 ).round}k" elsif size_in < 1024 ** 3 sprintf("%.1fm", size_in.to_f / (1024 ** 2)) else sprintf("%.2fg", size_in.to_f / (1024 ** 3)) end end MIN = 60 HOUR = 60 * MIN DAY = 24 * HOUR def numeric_to_time(time_in) if time_in.nil? time_in = 0 end if time_in < MIN sprintf("0:%02d", time_in) elsif time_in < HOUR sprintf("%d:%02d", time_in / MIN, time_in % MIN) elsif time_in < DAY sprintf("%d:%02d:%02d", time_in / HOUR, (time_in % HOUR) / MIN, (time_in % HOUR) % MIN) else sprintf("%dd %d:%02d:%02d", time_in / DAY, (time_in % DAY) / HOUR, ((time_in % DAY) % HOUR) / MIN, ((time_in % DAY) % HOUR) % MIN) end end