Skip to content

Commit

Permalink
show active seeds and peers in downloads (#3978)
Browse files Browse the repository at this point in the history
Active seeds/peers vs Total seeds/peers
  • Loading branch information
xoriole committed Oct 24, 2018
1 parent 55a9b7e commit 1623380
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,20 @@ def get_peerlist(self):
peers.append(peer_dict)
return peers

def get_num_connected_seeds_peers(self):
""" Returns number of connected seeders and leechers """
num_seeds = num_peers = 0
if not self.handle or not self.handle.is_valid():
return 0, 0

for peer_info in self.handle.get_peer_info():
if peer_info.flags & peer_info.seed:
num_seeds += 1
else:
num_peers += 1

return num_seeds, num_peers

@checkHandleAndSynchronize(default={})
def get_tracker_status(self):
# Make sure all trackers are in the tracker_status dict
Expand Down
2 changes: 2 additions & 0 deletions Tribler/Core/Modules/restapi/downloads_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def render_GET(self, request):
tracker_info.append({"url": url, "peers": url_info[0], "status": url_info[1]})

num_seeds, num_peers = state.get_num_seeds_peers()
num_connected_seeds, num_connected_peers = download.get_num_connected_seeds_peers()

download_json = {"name": tdef.get_name_utf8(), "progress": state.get_progress(),
"infohash": tdef.get_infohash().encode('hex'),
Expand All @@ -220,6 +221,7 @@ def render_GET(self, request):
"status": dlstatus_strings[state.get_status()],
"size": tdef.get_length(), "eta": state.get_eta(),
"num_peers": num_peers, "num_seeds": num_seeds,
"num_connected_peers": num_connected_peers, "num_connected_seeds": num_connected_seeds,
"total_up": state.get_total_transferred(UPLOAD),
"total_down": state.get_total_transferred(DOWNLOAD), "ratio": state.get_seeding_ratio(),
"trackers": tracker_info, "hops": download.get_hops(),
Expand Down
26 changes: 26 additions & 0 deletions Tribler/Test/Core/Libtorrent/test_libtorrent_download_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,32 @@ def mocked_set_share_mode(val):
self.libtorrent_download_impl.set_share_mode(True)
self.assertTrue(mocked_set_share_mode.called)

def test_get_num_connected_seeds_peers(self):
"""
Test whether connected peers and seeds are correctly returned
"""
def get_peer_info(seeders, leechers):
peer_info = []
for _ in xrange(seeders):
seeder = MockObject()
seeder.flags = 140347 # some value where seed flag(1024) is true
seeder.seed = 1024
peer_info.append(seeder)
for _ in xrange(leechers):
leecher = MockObject()
leecher.flags = 131242 # some value where seed flag(1024) is false
leecher.seed = 1024
peer_info.append(leecher)
return peer_info

mock_seeders = 15
mock_leechers = 6
self.libtorrent_download_impl.handle.get_peer_info = lambda: get_peer_info(mock_seeders, mock_leechers)

num_seeds, num_peers = self.libtorrent_download_impl.get_num_connected_seeds_peers()
self.assertEqual(num_seeds, mock_seeders, "Expected seeders differ")
self.assertEqual(num_peers, mock_leechers, "Expected peers differ")

def test_set_priority(self):
"""
Test whether setting the priority calls the right methods in LibtorrentDownloadImpl
Expand Down
4 changes: 2 additions & 2 deletions TriblerGUI/widgets/downloadwidgetitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def update_item(self):
self.setText(3, "Streaming")
else:
self.setText(3, DLSTATUS_STRINGS[eval(self.download_info["status"])])
self.setText(4, str(self.download_info["num_seeds"]))
self.setText(5, str(self.download_info["num_peers"]))
self.setText(4, "%s (%s)" % (self.download_info["num_connected_seeds"], self.download_info["num_seeds"]))
self.setText(5, "%s (%s)" % (self.download_info["num_connected_peers"], self.download_info["num_peers"]))
self.setText(6, format_speed(self.download_info["speed_down"]))
self.setText(7, format_speed(self.download_info["speed_up"]))
self.setText(8, "%.3f" % float(self.download_info["ratio"]))
Expand Down

0 comments on commit 1623380

Please sign in to comment.