[Xfce4-commits] <eatmonkey:aria2-xml-rpc> Cache the status information of each download within Aria2Listener

Mike Massonnet noreply at xfce.org
Sat Feb 27 00:44:07 CET 2010


Updating branch refs/heads/aria2-xml-rpc
         to cd69d41cf757a98de4b3b72afc0f7fca8e4417b0 (commit)
       from 3ee3e0487e6418cad7bc75a02f1c4c5615cd06f3 (commit)

commit cd69d41cf757a98de4b3b72afc0f7fca8e4417b0
Author: Mike Massonnet <mmassonnet at xfce.org>
Date:   Wed Feb 24 13:00:44 2010 +0100

    Cache the status information of each download within Aria2Listener
    
    The class Aria2Listener polls each second the XML-RPC server and caches
    the status of each download inside the public available property
    @downloads. The aria2 test program (./eataria2.rb) and the manager have
    been updated to use the cached data.
    
    The number of requests have been divided by two and so did the CPU usage
    of the client. The number of emission resquests have stalled to a fixed
    number.

 src/eataria2.rb   |   99 ++++++++++++++++++++++++----------------------------
 src/eatmanager.rb |    8 ++--
 2 files changed, 50 insertions(+), 57 deletions(-)

diff --git a/src/eataria2.rb b/src/eataria2.rb
index 1c09b3b..56a6f76 100755
--- a/src/eataria2.rb
+++ b/src/eataria2.rb
@@ -49,6 +49,9 @@ class Eat::Aria2 < GLib::Object
 	signal_new("download_status", GLib::Signal::RUN_FIRST, nil,
 			nil,					# Return type: void
 			String)					# Parameters: gid
+	signal_new("download_waiting", GLib::Signal::RUN_FIRST, nil,
+			nil,					# Return type: void
+			String)					# Parameters: gid
 	signal_new("download_completed", GLib::Signal::RUN_FIRST, nil,
 			nil,					# Return type: void
 			String)					# Parameters: gid
@@ -59,24 +62,19 @@ class Eat::Aria2 < GLib::Object
 			nil,					# Return type: void
 			String)					# Parameters: gid
 
-	def signal_do_connected()
-	end
-	def signal_do_disconnected()
-	end
-	def signal_do_shutdown()
-	end
-	def signal_do_download_status(gid)
-	end
-	def signal_do_download_completed(gid)
-	end
-	def signal_do_download_removed(gid)
-	end
-	def signal_do_download_stopped(gid)
-	end
+	def signal_do_connected(); end
+	def signal_do_disconnected(); end
+	def signal_do_shutdown(); end
+	def signal_do_download_status(gid); end
+	def signal_do_download_waiting(gid); end
+	def signal_do_download_completed(gid); end
+	def signal_do_download_removed(gid); end
+	def signal_do_download_stopped(gid); end
 
 	def initialize(hostname = DEFAULT_HOST, port = DEFAULT_PORT, user = nil, password = nil)
 		super(nil)
 
+		# Store new downloads added through add_<FOO>
 		@new_downloads = []
 
 		@confdir = ENV['XDG_CONFIG_HOME']
@@ -430,13 +428,20 @@ end
 
 
 
+=begin
+Eat::Aria2Listener:
+Polls each second the state of active downloads and caches the status for each
+active download inside the public (read-only) property @downloads.
+=end
 class Eat::Aria2Listener < Eat::Aria2
 
 	include Singleton
 
+	attr_reader :downloads
+
 	def initialize()
 		super
-		@downloads = []
+		@downloads = {}
 		start
 	end
 
@@ -448,40 +453,30 @@ class Eat::Aria2Listener < Eat::Aria2
 			if @is_connected
 
 				# Emit status signal for each active download
-				tell_active.each do |res|
-					gid = res["gid"]
-					@downloads << gid if !@downloads.find_index(gid)
+				tell_active.each do |status|
+					gid = status["gid"]
 					active_downloads << gid
+					@downloads[gid] = status
 					signal_emit("download_status", gid)
 				end
 
-				# Check/Cleanup unhandled new downloads
-				(@new_downloads - active_downloads).each do |gid|
-					@downloads << gid
-				end
-				@new_downloads.clear
-
 				# Emit specific signal for each inactive download
-				(@downloads - active_downloads).each do |gid|
-					# Get status
+				(@new_downloads - active_downloads).each do |gid|
+					# Store status
 					status = tell_status(gid)
+					@downloads[gid] = status
 					case status["status"]
 						when "complete" then signal_emit("download_completed", gid)
+						when "waiting" then signal_emit("download_waiting", gid)
 						when "removed" then signal_emit("download_removed", gid)
-						when "error"
-							if status["errorCode"] == "5"
-								# Download was aborted due to "pause"
-								debug "pause lasted too long, download %s dropped" % gid
-							else
-								puts "Unhandled error (%s: %s)" % [gid, status["errorCode"]]
-							end
-						else puts "Unhandled status (%s: %s)" % [gid, status["status"]]
+						when "error" then puts "Error (%s: %s)" % [gid, status["errorCode"]]
+						else puts "Inconsistent status (%s: %s)" % [gid, status["status"]]
 					end
+					@new_downloads.delete(gid)
 				end
 
 			end
 
-			@downloads = active_downloads
 			true
 		end
 	end
@@ -680,25 +675,23 @@ end
 
 
 if __FILE__ == $0
-	aria2 = Eat::Aria2.new
+	aria2 = Eat::Aria2Listener.instance
 	aria2.connect
-	begin
-		if aria2.is_connected
-			aria2.tell_active.each do |res|
-				gid = res["gid"]
-				puts " *** Set download max rate to 16KB ***"
-				aria2.change_options(gid, "max-download-limit" => "16000")
-				puts " *** Get status ***"
-				pp aria2.tell_status(gid)
-				puts " *** Get uris ***"
-				pp aria2.get_uris(gid)
-				puts " *** Get files ***"
-				pp aria2.get_files(gid)
-				puts " *** Get peers ***"
-				pp aria2.get_peers(gid).length
+	Thread.new do
+		begin
+			if aria2.is_connected
+				puts ""
+				puts " ****** DOWNLOADS STATUS ****** "
+				puts ""
+				aria2.downloads.each do |gid, status|
+					puts "#{gid}: ", status
+					puts " *** Get peers ***"
+					pp aria2.get_peers(gid).length
+				end
 			end
-		end
-		sleep 5
-	end while true
+		end while sleep 5
+	end
+	mainloop = GLib::MainLoop.new
+	mainloop.run
 end
 
diff --git a/src/eatmanager.rb b/src/eatmanager.rb
index 048f1b5..cf0f399 100755
--- a/src/eatmanager.rb
+++ b/src/eatmanager.rb
@@ -172,8 +172,8 @@ class Eat::Manager
 		return if @removed_downloads.include? gid_i
 
 		# Retrieve status information on gid
-		status = @aria2.tell_status(gid)
-		return if status.empty?
+		status = @aria2.downloads[gid]
+		return if status == nil
 
 		# Find gid in model
 		@liststore.each do |model, path, iter|
@@ -401,7 +401,7 @@ class Eat::Manager
 		# Resume gid that have been removed
 		@treeview.selection.selected_each do |model, path, iter|
 			gid = iter[0].to_s
-			status = @aria2.tell_status(gid)
+			status = @aria2.downloads[gid]
 			case status["status"]
 			when "removed"
 				# Restart the download queued at position 0 and delete current row
@@ -410,7 +410,7 @@ class Eat::Manager
 				if !uri.empty?
 					percent = iter[8]
 					@liststore.remove(iter)
-					gid = @aria2.add_uri(uri, nil, 0)
+					gid = @aria2.add_uri(uri, [], 0)
 					if gid != nil
 						# TODO this is a little better than nothing, when
 						# the download is resumed it will reappear immediately



More information about the Xfce4-commits mailing list