break stuff up - micah

This commit is contained in:
Marcos Pinto 2007-08-03 21:23:12 +00:00
parent 43ae5566ee
commit ba3ccb7635
1 changed files with 204 additions and 182 deletions

View File

@ -1,4 +1,3 @@
from itertools import izip
import gtk import gtk
import math import math
@ -19,6 +18,13 @@ class PiecesManager(object):
self.piece_info = [] self.piece_info = []
self.first_indexes = [] self.first_indexes = []
self.last_indexes = [] self.last_indexes = []
self.all_files = None
self.file_priorities = None
self.index = 0
self.prev_file_index = -1
self.file_index = 0
self.next_file_index = 1
self.num_files = 0
def set_unique_id(self, unique_id): def set_unique_id(self, unique_id):
self.unique_id = unique_id self.unique_id = unique_id
@ -36,56 +42,95 @@ class PiecesManager(object):
self.tooltips = [] self.tooltips = []
self.first_indexes = [] self.first_indexes = []
self.last_indexes = [] self.last_indexes = []
self.all_files = None
self.file_priorities = None
self.index = 0
self.prev_file_index = -1
self.file_index = 0
self.next_file_index = 1
self.num_files = 0
def prepare_pieces_store(self): def prepare_pieces_store(self):
self.vbox = gtk.VBox() self.vbox = gtk.VBox()
self.viewport.add(self.vbox) self.viewport.add(self.vbox)
all_files = self.manager.get_torrent_file_info(self.unique_id) self.all_files = self.manager.get_torrent_file_info(self.unique_id)
file_priorities = self.manager.get_priorities(self.unique_id) self.file_priorities = self.manager.get_priorities(self.unique_id)
state = self.manager.get_torrent_state(self.unique_id) state = self.manager.get_torrent_state(self.unique_id)
num_pieces = state["num_pieces"] self.num_pieces = state["num_pieces"]
prev_file_index = -1 for priority in self.file_priorities:
file_index = 0 if self.file_index == 0:
next_file_index = 1
for file, priority in izip(all_files, file_priorities):
if file_index == 0:
file_piece_range = self.manager.get_file_piece_range(self.unique_id,\ file_piece_range = self.manager.get_file_piece_range(self.unique_id,\
file_index, file['size']) self.file_index, self.all_files[self.file_index]['size'])
self.first_indexes.append(file_piece_range['first_index']) self.first_indexes.append(file_piece_range['first_index'])
self.last_indexes.append(file_piece_range['last_index']) self.last_indexes.append(file_piece_range['last_index'])
if priority > 0: if priority > 0:
#if file is being downloaded build the file pieces information #if file is being downloaded build the file pieces information
temp_prev_priority = 1 self.build_file_pieces()
else:
#if file is not being downloaded skip the file pieces
self.skip_current_file()
self.file_index += 1
self.next_file_index += 1
self.prev_file_index += 1
self.get_current_pieces_info()
self.vbox.show_all()
def build_file_pieces(self):
label = gtk.Label() label = gtk.Label()
label.set_alignment(0,0) label.set_alignment(0,0)
label.set_text(file['path']) label.set_text(self.all_files[self.file_index]['path'])
self.vbox.pack_start(label, expand=False) self.vbox.pack_start(label, expand=False)
table = gtk.Table() table = gtk.Table()
self.rows = int(math.ceil((self.last_indexes[file_index]-self.first_indexes[file_index])/self.columns)+1) self.rows = int(math.ceil((self.last_indexes[self.file_index]-self.first_indexes[self.file_index])/self.columns)+1)
self.vbox.pack_start(table, expand=False) self.vbox.pack_start(table, expand=False)
table.resize(self.rows, self.columns) table.resize(self.rows, self.columns)
table.set_size_request((self.columns+1)*self.piece_width, (self.rows+1)*self.piece_height) table.set_size_request((self.columns+1)*self.piece_width, (self.rows+1)*self.piece_height)
if self.last_indexes[file_index] != self.first_indexes[file_index]: if self.last_indexes[self.file_index] != self.first_indexes[self.file_index]:
#if there is more than one piece #if there is more than one piece
if self.first_indexes[file_index] == 0\ self.build_pieces_table(table)
or self.first_indexes[file_index] != self.last_indexes[prev_file_index]: only_one_piece = False
else:
#if file only has one piece
self.index = 0
only_one_piece = True
main_index = self.last_indexes[self.file_index]
# do the following even if file has only one piece and that piece has already been created
if self.next_file_index < len(self.all_files):
#if there is another file
file_piece_range = self.manager.get_file_piece_range(self.unique_id,\
self.next_file_index, self.all_files[self.next_file_index]['size'])
self.first_indexes.append(file_piece_range['first_index'])
self.last_indexes.append(file_piece_range['last_index'])
if self.file_index > 0 and not self.piece_info[main_index] is None and only_one_piece:
#if file has only one piece and it is shared destroy the table
table.destroy()
if self.file_index == 0 or self.piece_info[main_index] is None or not only_one_piece:
# piece could be shared if file has only one piece and it's not the first file
# only create it if it does not exist
self.build_last_file_piece(table, main_index, only_one_piece)
def build_pieces_table(self, table):
temp_prev_priority = 1
if self.first_indexes[self.file_index] == 0\
or self.first_indexes[self.file_index] != self.last_indexes[self.prev_file_index]:
#if first piece is not a shared piece #if first piece is not a shared piece
temp_range = self.last_indexes[file_index]-self.first_indexes[file_index] temp_range = self.last_indexes[self.file_index]-self.first_indexes[self.file_index]
diff = 0 diff = 0
else: else:
#if first piece is shared #if first piece is shared
temp_prev_priority = file_priorities[prev_file_index] temp_prev_priority = self.file_priorities[self.prev_file_index]
if temp_prev_priority > 0: if temp_prev_priority > 0:
#if last file was not skipped, skip the first piece #if last file was not skipped, skip the first piece
diff = 1 diff = 1
temp_range = self.last_indexes[file_index]-(self.first_indexes[file_index]+1) temp_range = self.last_indexes[self.file_index]-(self.first_indexes[self.file_index]+1)
#otherwise keep the first piece #otherwise keep the first piece
else: else:
diff = 0 diff = 0
temp_range = self.last_indexes[file_index]-self.first_indexes[file_index] temp_range = self.last_indexes[self.file_index]-self.first_indexes[self.file_index]
#last piece handled outside of loop, skip it from range #last piece handled outside of loop, skip it from range
for index in xrange(temp_range): for index in xrange(temp_range):
main_index = diff+self.first_indexes[file_index]+index main_index = diff+self.first_indexes[self.file_index]+index
if temp_prev_priority > 0: if temp_prev_priority > 0:
#normal behavior #normal behavior
self.piece_info.append({'blocks_total':0, 'blocks_finished':0, 'blocks_requested':0}) self.piece_info.append({'blocks_total':0, 'blocks_finished':0, 'blocks_requested':0})
@ -95,11 +140,7 @@ class PiecesManager(object):
self.peer_speed.append("unknown") self.peer_speed.append("unknown")
else: else:
#if first piece is shared with a skipped file #if first piece is shared with a skipped file
self.piece_info[main_index] = {'blocks_total':0, 'blocks_finished':0, 'blocks_requested':0} self.share_skipped_piece(main_index)
self.progress[main_index] = gtk.ProgressBar()
self.tooltips[main_index] = gtk.Tooltips()
self.eventboxes[main_index] = gtk.EventBox()
self.peer_speed[main_index] = "unknown"
temp_prev_priority = 1 temp_prev_priority = 1
self.progress[main_index].set_size_request(self.piece_width, self.piece_height) self.progress[main_index].set_size_request(self.piece_width, self.piece_height)
row = index/self.columns row = index/self.columns
@ -119,47 +160,25 @@ class PiecesManager(object):
self.tooltips.append(gtk.Tooltips()) self.tooltips.append(gtk.Tooltips())
self.eventboxes.append(gtk.EventBox()) self.eventboxes.append(gtk.EventBox())
self.peer_speed.append("unknown") self.peer_speed.append("unknown")
index = index+1 self.index = index+1
only_one_piece = False
else: def build_last_file_piece(self, table, main_index, only_one_piece):
#if file only has one piece
index = 0
only_one_piece = True
main_index = self.last_indexes[file_index]
# do the following even if file has only one piece
# and the piece does not need created
if next_file_index < len(all_files):
#if there is another file
file_piece_range = self.manager.get_file_piece_range(self.unique_id,\
next_file_index, all_files[next_file_index]['size'])
self.first_indexes.append(file_piece_range['first_index'])
self.last_indexes.append(file_piece_range['last_index'])
if file_index > 0 and not self.piece_info[main_index] is None and only_one_piece:
#if file has only one piece and it is shared destroy the table
table.destroy()
if file_index == 0 or self.piece_info[main_index] is None or not only_one_piece:
# piece could be shared if file has only one piece and it's not the first file
# only create it if it does not exist
if only_one_piece: if only_one_piece:
#if piece is shared with a skipped file #if piece is shared with a skipped file
self.piece_info[main_index] = {'blocks_total':0, 'blocks_finished':0, 'blocks_requested':0} self.share_skipped_piece(main_index)
self.progress[main_index] = gtk.ProgressBar()
self.tooltips[main_index] = gtk.Tooltips()
self.eventboxes[main_index] = gtk.EventBox()
self.peer_speed[main_index] = "unknown"
self.progress[main_index].set_size_request(self.piece_width, self.piece_height) self.progress[main_index].set_size_request(self.piece_width, self.piece_height)
if next_file_index < len(all_files): if self.next_file_index < len(self.all_files):
# if there is another file # if there is another file
if file_priorities[next_file_index]==0\ if self.file_priorities[self.next_file_index]==0\
or self.last_indexes[file_index] != self.first_indexes[next_file_index]: or self.last_indexes[self.file_index] != self.first_indexes[self.next_file_index]:
#if next file is skipped or there is no shared piece, keep last piece #if next file is skipped or there is no shared piece, keep last piece
row=index/self.columns row=self.index/self.columns
column=index%self.columns column=self.index%self.columns
table.attach(self.eventboxes[main_index], column, column+1, row, row+1, table.attach(self.eventboxes[main_index], column, column+1, row, row+1,
xoptions=0, yoptions=0, xpadding=0, ypadding=0) xoptions=0, yoptions=0, xpadding=0, ypadding=0)
self.eventboxes[main_index].add(self.progress[main_index]) self.eventboxes[main_index].add(self.progress[main_index])
if file_priorities[next_file_index]>0\ if self.file_priorities[self.next_file_index]>0\
and self.last_indexes[file_index] == self.first_indexes[next_file_index]: and self.last_indexes[self.file_index] == self.first_indexes[self.next_file_index]:
#if next file is not skipped and there is a shared piece, do not keep last piece #if next file is not skipped and there is a shared piece, do not keep last piece
if only_one_piece: if only_one_piece:
#only piece in file is shared, destroy table for file #only piece in file is shared, destroy table for file
@ -177,8 +196,8 @@ class PiecesManager(object):
self.vbox.pack_start(temp_table, expand=False) self.vbox.pack_start(temp_table, expand=False)
else: else:
#if there is no other file #if there is no other file
row=index/self.columns row=self.index/self.columns
column=index%self.columns column=self.index%self.columns
table.attach(self.eventboxes[main_index], column, column+1, row, row+1, table.attach(self.eventboxes[main_index], column, column+1, row, row+1,
xoptions=0, yoptions=0, xpadding=0, ypadding=0) xoptions=0, yoptions=0, xpadding=0, ypadding=0)
self.eventboxes[main_index].add(self.progress[main_index]) self.eventboxes[main_index].add(self.progress[main_index])
@ -189,47 +208,35 @@ class PiecesManager(object):
else: else:
#if the last piece is not already finished #if the last piece is not already finished
self.tooltips[main_index].set_tip(self.eventboxes[main_index], _("Piece not started")) self.tooltips[main_index].set_tip(self.eventboxes[main_index], _("Piece not started"))
else:
#if file is not being downloaded skip the file pieces def share_skipped_piece(self, main_index):
if self.first_indexes[file_index] == 0 or self.first_indexes[file_index] != self.last_indexes[prev_file_index]: self.piece_info[main_index] = {'blocks_total':0, 'blocks_finished':0, 'blocks_requested':0}
self.progress[main_index] = gtk.ProgressBar()
self.tooltips[main_index] = gtk.Tooltips()
self.eventboxes[main_index] = gtk.EventBox()
self.peer_speed[main_index] = "unknown"
def skip_current_file(self):
if self.first_indexes[self.file_index] == 0 or self.first_indexes[self.file_index] != self.last_indexes[self.prev_file_index]:
#if first piece is not shared #if first piece is not shared
temp_range = 1+self.last_indexes[file_index]-self.first_indexes[file_index] temp_range = 1+self.last_indexes[self.file_index]-self.first_indexes[self.file_index]
else: else:
#if first piece is shared #if first piece is shared
temp_range = self.last_indexes[file_index]-self.first_indexes[file_index] temp_range = self.last_indexes[self.file_index]-self.first_indexes[self.file_index]
for index in xrange(temp_range): for index in xrange(temp_range):
self.piece_info.append(None) self.piece_info.append(None)
self.progress.append(None) self.progress.append(None)
self.eventboxes.append(None) self.eventboxes.append(None)
self.tooltips.append(None) self.tooltips.append(None)
self.peer_speed.append(None) self.peer_speed.append(None)
if next_file_index < len(all_files): if self.next_file_index < len(self.all_files):
#if there is another file #if there is another file
file_piece_range = self.manager.get_file_piece_range(self.unique_id,\ file_piece_range = self.manager.get_file_piece_range(self.unique_id,\
next_file_index, all_files[next_file_index]['size']) self.next_file_index, self.all_files[self.next_file_index]['size'])
self.first_indexes.append(file_piece_range['first_index']) self.first_indexes.append(file_piece_range['first_index'])
self.last_indexes.append(file_piece_range['last_index']) self.last_indexes.append(file_piece_range['last_index'])
if self.last_indexes[next_file_index] >= num_pieces: if self.last_indexes[self.next_file_index] >= self.num_pieces:
self.last_indexes[next_file_index] = num_pieces-1 self.last_indexes[self.next_file_index] = self.num_pieces-1
file_index += 1
next_file_index += 1
prev_file_index += 1
#get currently downloading piece information
all_piece_info = self.manager.get_all_piece_info(self.unique_id)
for piece_index in all_piece_info:
index = piece_index['piece_index']
if not self.piece_info[index] is None:
temp_piece_info = {'blocks_total':piece_index['blocks_total'], \
'blocks_finished':piece_index['blocks_finished']}
self.piece_info[index] = temp_piece_info
blocks_total = str(temp_piece_info['blocks_total'])
info_string = str(temp_piece_info['blocks_finished']) + "/" + blocks_total + " " + _("blocks finished") + "\n" \
+ _("peer speed: unknown")
if self.progress[index].get_fraction() == 0:
self.progress[index].set_fraction(0.5)
self.tooltips[index].set_tip(self.eventboxes[index], info_string)
self.vbox.show_all()
def handle_event(self, event): def handle_event(self, event):
#protect against pieces trying to display after file priority changed #protect against pieces trying to display after file priority changed
@ -261,6 +268,21 @@ class PiecesManager(object):
self.piece_info[index]['blocks_finished'] += 1 self.piece_info[index]['blocks_finished'] += 1
self.update_pieces_store(event['piece_index']) self.update_pieces_store(event['piece_index'])
def get_current_pieces_info(self):
all_piece_info = self.manager.get_all_piece_info(self.unique_id)
for piece_index in all_piece_info:
index = piece_index['piece_index']
if not self.piece_info[index] is None:
temp_piece_info = {'blocks_total':piece_index['blocks_total'], \
'blocks_finished':piece_index['blocks_finished']}
self.piece_info[index] = temp_piece_info
blocks_total = str(temp_piece_info['blocks_total'])
info_string = str(temp_piece_info['blocks_finished']) + "/" + blocks_total + " " + _("blocks finished") + "\n" \
+ _("peer speed: unknown")
if self.progress[index].get_fraction() == 0:
self.progress[index].set_fraction(0.5)
self.tooltips[index].set_tip(self.eventboxes[index], info_string)
def update_pieces_store(self, index, piece_finished=False): def update_pieces_store(self, index, piece_finished=False):
if piece_finished: if piece_finished:
self.progress[index].set_fraction(1) self.progress[index].set_fraction(1)