From 08f85138b392aabd933ad609381b264ea362039b Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 6 Jan 2014 22:40:02 +0100 Subject: [PATCH] tracker: Don't rely on event=started. The tracker now adds every peer it sees to the database even if it never sent the started event or if an update comes too late and the peer has already been removed. It will now be re-added. --- webapp/backend/tracker.py | 90 +++++++++++++++++++------------------- webapp/handlers_tracker.py | 3 +- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/webapp/backend/tracker.py b/webapp/backend/tracker.py index b76875f1..d5642c4a 100644 --- a/webapp/backend/tracker.py +++ b/webapp/backend/tracker.py @@ -89,13 +89,55 @@ class Tracker(Object): Remove all peers that have timed out. """ self.db.execute("DELETE FROM tracker \ - WHERE last_update < NOW() - INTERVAL '%s s'", int(self._interval * 1.1)) + WHERE last_update < NOW() - INTERVAL '%s s'", int(self._interval * 1.2)) - def update_peer(self, peer_id, info_hash, address6=None, port6=None, - address4=None, port4=None, downloaded=None, uploaded=None, left_data=None): + def update_peer(self, peer_id, info_hash, **kwargs): + # Translate the location IP address + address4 = kwargs.get("address4", None) if address4 and address4.startswith("172.28.1."): - address = "178.63.73.246" + kwargs.update({ + "address4" : "178.63.73.246", + }) + + if self.peer_exists(peer_id, info_hash): + self.__update_peer(peer_id, info_hash, **kwargs) + else: + self.__insert_peer(peer_id, info_hash, **kwargs) + + def complete(self, info_hash): + ret = self.db.get("SELECT COUNT(*) AS c FROM tracker \ + WHERE hash = %s AND left_data = 0", info_hash) + + if ret: + return ret.c + def incomplete(self, info_hash): + ret = self.db.get("SELECT COUNT(*) AS c FROM tracker \ + WHERE hash = %s AND left_data > 0", info_hash) + + if ret: + return ret.c + + def handle_event(self, event, peer_id, info_hash, **kwargs): + # stopped + if event == "stopped": + self.remove_peer(peer_id, info_hash) + + def peer_exists(self, peer_id, info_hash): + ret = self.db.get("SELECT COUNT(*) AS c FROM tracker \ + WHERE id = %s AND hash = %s", peer_id, info_hash) + + if ret and ret.c > 0: + return True + + return False + + def __insert_peer(self, peer_id, info_hash, address6=None, port6=None, address4=None, port4=None, **kwargs): + self.db.execute("INSERT INTO tracker(id, hash, address6, port6, address4, port4) \ + VALUES(%s, %s, %s, %s, %s, %s)", peer_id, info_hash, address6, port6, address4, port4) + + def __update_peer(self, peer_id, info_hash, address6=None, port6=None, + address4=None, port4=None, downloaded=None, uploaded=None, left_data=None): query = "UPDATE tracker SET last_update = NOW()" args = [] @@ -132,46 +174,6 @@ class Tracker(Object): self.db.execute(query, *args) - def complete(self, info_hash): - ret = self.db.get("SELECT COUNT(*) AS c FROM tracker \ - WHERE hash = %s AND left_data = 0", info_hash) - - if ret: - return ret.c - - def incomplete(self, info_hash): - ret = self.db.get("SELECT COUNT(*) AS c FROM tracker \ - WHERE hash = %s AND left_data > 0", info_hash) - - if ret: - return ret.c - - def handle_event(self, event, peer_id, info_hash, **kwargs): - # started - if event == "started": - self.insert_peer(peer_id, info_hash, **kwargs) - - # stopped - elif event == "stopped": - self.remove_peer(peer_id, info_hash) - - def peer_exists(self, peer_id, info_hash): - ret = self.db.get("SELECT COUNT(*) AS c FROM tracker \ - WHERE id = %s AND hash = %s", peer_id, info_hash) - - if ret and ret.c > 0: - return True - - return False - - def insert_peer(self, peer_id, info_hash, address6=None, port6=None, address4=None, port4=None): - exists = self.peer_exists(peer_id, info_hash) - if exists: - return - - self.db.execute("INSERT INTO tracker(id, hash, address6, port6, address4, port4) \ - VALUES(%s, %s, %s, %s, %s, %s)", peer_id, info_hash, address6, port6, address4, port4) - def remove_peer(self, peer_id, info_hash): self.db.execute("DELETE FROM tracker \ WHERE id = %s AND hash = %s", peer_id, info_hash) diff --git a/webapp/handlers_tracker.py b/webapp/handlers_tracker.py index bbcc64f2..b280b239 100644 --- a/webapp/handlers_tracker.py +++ b/webapp/handlers_tracker.py @@ -150,8 +150,7 @@ class TrackerAnnounceHandler(TrackerBaseHandler): self.send_tracker_error("Got unknown event") return - self.tracker.handle_event(event, peer_id, info_hash, - address6=addr_ipv6, port6=port_ipv6, address4=addr_ipv4, port4=port_ipv4) + self.tracker.handle_event(event, peer_id, info_hash) peer_info = { "address6" : addr_ipv6, -- 2.47.3