Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging and multi threading #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import logging
import os
import message
import sys


class Run(object):
percentage_completed = -1
last_log_line = ""

def __init__(self):
self.torrent = torrent.Torrent().load_from_path("torrent.torrent")
self.torrent = torrent.Torrent().load_from_path(os.path.join('torrents', sys.argv[1]))
gallexis marked this conversation as resolved.
Show resolved Hide resolved
self.tracker = tracker.Tracker(self.torrent)

self.pieces_manager = pieces_manager.PiecesManager(self.torrent)
Expand Down Expand Up @@ -92,11 +93,13 @@ def display_progression(self):

def _exit_threads(self):
self.peers_manager.is_active = False
logging.info(f"Downloading finished in: {time.time() - start_time} seconds")
os._exit(0)


if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)

start_time = time.time()
run = Run()
run.start()
3 changes: 3 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice to have, but I dont think it is needed by everyone. Consider removing please.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, can you remove this line?

echo "Redirecting all logs into ./torrents/$1.log"
python3 main.py $1 2>&1 | tee "./torrents/$1.log"
38 changes: 29 additions & 9 deletions tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import peer
from message import UdpTrackerConnection, UdpTrackerAnnounce, UdpTrackerAnnounceOutput
from peers_manager import PeersManager
from threading import Thread

__author__ = 'alexisgallepe'

Expand All @@ -15,7 +16,6 @@
MAX_PEERS_TRY_CONNECT = 30
MAX_PEERS_CONNECTED = 8


class SockAddr:
def __init__(self, ip, port, allowed=True):
self.ip = ip
Expand All @@ -34,6 +34,10 @@ def __init__(self, torrent):
self.dict_sock_addr = {}

def get_peers_from_trackers(self):
logging.info("---------------------------------------")
logging.info(f"Trackers: {self.torrent.announce_list}")
logging.info("---------------------------------------")

for i, tracker in enumerate(self.torrent.announce_list):
if len(self.dict_sock_addr) >= MAX_PEERS_TRY_CONNECT:
break
Expand All @@ -51,7 +55,6 @@ def get_peers_from_trackers(self):
self.udp_scrapper(tracker_url)
except Exception as e:
logging.error("UDP scraping failed: %s " % e.__str__())

else:
logging.error("unknown scheme for: %s " % tracker_url)

Expand All @@ -62,17 +65,26 @@ def get_peers_from_trackers(self):
def try_peer_connect(self):
logging.info("Trying to connect to %d peer(s)" % len(self.dict_sock_addr))

def task(obj):
new_peer = peer.Peer(int(self.torrent.number_of_pieces), sock_addr.ip, sock_addr.port)
if not new_peer.connect():
return
obj.connected_peers[new_peer.__hash__()] = new_peer
print('Connected to %d/%d peers' % (len(obj.connected_peers), MAX_PEERS_CONNECTED))

threads = []

for _, sock_addr in self.dict_sock_addr.items():
if len(self.connected_peers) >= MAX_PEERS_CONNECTED:
break
t = Thread(target=task, args=(self,))
threads.append(t)
t.start()

new_peer = peer.Peer(int(self.torrent.number_of_pieces), sock_addr.ip, sock_addr.port)
if not new_peer.connect():
continue
for t in threads:
t.join()

print('Connected to %d/%d peers' % (len(self.connected_peers), MAX_PEERS_CONNECTED))

self.connected_peers[new_peer.__hash__()] = new_peer
logging.info("Finished connecting to peers")

def http_scraper(self, torrent, tracker):
params = {
Expand Down Expand Up @@ -112,6 +124,11 @@ def http_scraper(self, torrent, tracker):
s = SockAddr(p['ip'], p['port'])
self.dict_sock_addr[s.__hash__()] = s

logging.info("---------------------------------------")
logging.info(f"Got {len(list_peers.values())} peers from tracker: {tracker}")
logging.info("All peers: %d" % len(self.dict_sock_addr))
logging.info("---------------------------------------")

except Exception as e:
logging.exception("HTTP scraping failed: %s" % e.__str__())

Expand Down Expand Up @@ -152,7 +169,10 @@ def udp_scrapper(self, announce):
if sock_addr.__hash__() not in self.dict_sock_addr:
self.dict_sock_addr[sock_addr.__hash__()] = sock_addr

print("Got %d peers" % len(self.dict_sock_addr))
logging.info("---------------------------------------")
logging.info(f"Got {len(tracker_announce_output.list_sock_addr)} peers from tracker: {announce}")
logging.info("All peers: %d" % len(self.dict_sock_addr))
logging.info("---------------------------------------")

def send_message(self, conn, sock, tracker_message):
message = tracker_message.to_bytes()
Expand Down